2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 /* general IMAP utility functions */
16 #include <lib-lib/mem.h>
17 #include <lib-lib/ascii.h>
18 #include <lib-lib/url.h>
21 #include "mx.h" /* for M_IMAP */
22 #include "imap_private.h"
23 #include <lib-sys/mutt_ssl.h>
25 #include <lib-lib/macros.h>
26 #include <lib-lib/debug.h>
31 #include <sys/types.h>
35 #include <netinet/in.h>
39 /* -- public functions -- */
41 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
42 * an IMAP path in canonical and absolute form.
43 * Inputs: a buffer containing an IMAP path, and the number of bytes in
45 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
46 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
47 * fails, which it might if there isn't enough room in the buffer. */
48 int imap_expand_path (char *path, size_t len)
54 if (imap_parse_path (path, &mx) < 0)
57 mutt_account_tourl (&mx.account, &url);
60 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
66 /* imap_parse_path: given an IMAP mailbox name, return host, port
67 * and a path IMAP servers will recognise.
68 * mx.mbox is malloc'd, caller must free it */
69 int imap_parse_path (const char *path, IMAP_MBOX * mx)
71 static unsigned short ImapPort = 0;
72 static unsigned short ImapsPort = 0;
73 struct servent *service;
78 service = getservbyname ("imap", "tcp");
80 ImapPort = ntohs (service->s_port);
83 debug_print (3, ("Using default IMAP port %d\n", ImapPort));
86 service = getservbyname ("imaps", "tcp");
88 ImapsPort = ntohs (service->s_port);
90 ImapsPort = IMAP_SSL_PORT;
91 debug_print (3, ("Using default IMAPS port %d\n", ImapsPort));
95 mx->account.flags = 0;
96 mx->account.port = ImapPort;
97 mx->account.type = M_ACCT_TYPE_IMAP;
100 url_parse_ciss (&url, c);
102 if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
103 mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host) {
108 mx->mbox = m_strdup(url.path);
110 if (url.scheme == U_IMAPS)
111 mx->account.flags |= M_ACCT_SSL;
115 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
116 mx->account.port = ImapsPort;
121 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
123 void imap_pretty_mailbox (char *path)
125 IMAP_MBOX home, target;
132 if (imap_parse_path (path, &target) < 0)
135 tlen = m_strlen(target.mbox);
136 /* check whether we can do '=' substitution */
137 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
138 hlen = m_strlen(home.mbox);
139 if (tlen && mutt_account_match (&home.account, &target.account) &&
140 !m_strncmp(home.mbox, target.mbox, hlen)) {
144 for (delim = ImapDelimChars; *delim != '\0'; delim++)
145 if (target.mbox[hlen] == *delim)
148 p_delete(&home.mbox);
151 /* do the '=' substitution */
154 /* copy remaining path, skipping delimiter */
157 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
158 path[tlen - hlen - 1] = '\0';
161 mutt_account_tourl (&target.account, &url);
162 url.path = target.mbox;
163 /* FIXME: That hard-coded constant is bogus. But we need the actual
164 * size of the buffer from mutt_pretty_mailbox. And these pretty
165 * operations usually shrink the result. Still... */
166 url_ciss_tostring (&url, path, 1024, 0);
169 p_delete(&target.mbox);
172 /* -- library functions -- */
174 /* imap_continue: display a message and ask the user if she wants to
176 int imap_continue (const char *msg, const char *resp)
178 imap_error (msg, resp);
179 return mutt_yesorno (_("Continue?"), 0);
182 /* imap_error: show an error and abort */
183 void imap_error (const char *where, const char *msg)
185 mutt_error ("%s [%s]\n", where, msg);
189 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
190 * Returns NULL on failure (no mem) */
191 IMAP_DATA *imap_new_idata (void)
193 return p_new(IMAP_DATA, 1);
196 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
197 void imap_free_idata (IMAP_DATA ** idata)
202 p_delete(&(*idata)->capstr);
203 string_list_wipe(&(*idata)->flags);
204 p_delete(&((*idata)->cmd.buf));
209 * Fix up the imap path. This is necessary because the rest of mutt
210 * assumes a hierarchy delimiter of '/', which is not necessarily true
211 * in IMAP. Additionally, the filesystem converts multiple hierarchy
212 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
213 * are not required to do this.
214 * Moreover, IMAP servers may dislike the path ending with the delimiter.
216 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
221 if (!mailbox || !*mailbox) {
222 m_strcpy(path, plen, "INBOX");
226 while (mailbox && *mailbox && (x < (plen - 1))) {
227 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
228 while ((*mailbox == '/') || (*mailbox == idata->delim))
230 path[x] = idata->delim;
238 if (x && path[--x] != idata->delim)
244 /* imap_get_literal_count: write number of bytes in an IMAP literal into
245 * bytes, return 0 on success, -1 on failure. */
246 int imap_get_literal_count (const char *buf, long *bytes)
251 if (!(pc = strchr (buf, '{')))
255 while (isdigit ((unsigned char) *pc))
262 /* imap_get_qualifier: in a tagged response, skip tag and status for
263 * the qualifier message. Used by imap_copy_message for TRYCREATE */
264 char *imap_get_qualifier (char *buf)
269 s = imap_next_word (s);
270 /* skip OK/NO/BAD response */
271 s = imap_next_word (s);
276 /* imap_next_word: return index into string where next IMAP word begins */
277 char *imap_next_word (char *s)
289 quoted = quoted ? 0 : 1;
290 if (!quoted && ISSPACE (*s))
295 return vskipspaces(s);
298 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
299 time_t imap_parse_date (char *s)
304 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
309 t.tm_mon = mutt_check_month (s);
315 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
324 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
329 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
334 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
341 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
342 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
346 return (mutt_mktime (&t, 0) + tz);
349 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
350 * and relative path. */
351 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
355 mutt_account_tourl (&mx->account, &url);
358 url_ciss_tostring (&url, dest, len, 0);
362 /* imap_quote_string: quote string according to IMAP rules:
363 * surround string with quotes, escape " and \ with \ */
364 void imap_quote_string (char *dest, size_t dlen, const char *src)
366 char quote[] = "\"\\", *pt;
373 /* save room for trailing quote-char */
376 for (; *s && dlen; s++) {
377 if (strchr (quote, *s)) {
393 /* imap_unquote_string: equally stupid unquoting routine */
394 void imap_unquote_string (char *s)
421 * Quoting and UTF-7 conversion
424 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
429 imap_utf7_encode (&buf);
431 imap_quote_string (dest, dlen, buf);
436 void imap_unmunge_mbox_name (char *s)
440 imap_unquote_string (s);
444 imap_utf7_decode (&buf);
445 m_strcpy(s, m_strlen(s) + 1, buf);
451 /* imap_wordcasecmp: find word a in word list b */
452 int imap_wordcasecmp (const char *a, const char *b)
454 char tmp[SHORT_STRING];
455 char *s = (char *) b;
458 tmp[SHORT_STRING - 1] = 0;
459 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
460 if (!*s || ISSPACE (*s)) {
468 return ascii_strcasecmp (a, tmp);
472 * Imap keepalive: poll the current folder to keep the
477 static RETSIGTYPE alrm_handler (int sig)
482 void imap_keepalive (void)
488 conn = mutt_socket_head ();
490 if (conn->account.type == M_ACCT_TYPE_IMAP) {
491 idata = (IMAP_DATA *) conn->data;
493 if (idata->state >= IMAP_AUTHENTICATED
494 && time (NULL) >= idata->lastread + ImapKeepalive) {
498 ctx = p_new(CONTEXT, 1);
501 imap_check_mailbox (ctx, NULL, 1);
511 int imap_wait_keepalive (pid_t pid)
513 struct sigaction oldalrm;
514 struct sigaction act;
518 short imap_passive = option (OPTIMAPPASSIVE);
519 int imap_askreconnect = quadoption (OPT_IMAPRECONNECT);
521 set_option (OPTIMAPPASSIVE);
522 set_option (OPTKEEPQUIET);
523 set_quadoption (OPT_IMAPRECONNECT, M_NO);
525 sigprocmask (SIG_SETMASK, NULL, &oldmask);
527 sigemptyset (&act.sa_mask);
528 act.sa_handler = alrm_handler;
530 act.sa_flags = SA_INTERRUPT;
535 sigaction (SIGALRM, &act, &oldalrm);
537 alarm (ImapKeepalive);
538 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
539 alarm (0); /* cancel a possibly pending alarm */
541 alarm (ImapKeepalive);
544 alarm (0); /* cancel a possibly pending alarm */
546 sigaction (SIGALRM, &oldalrm, NULL);
547 sigprocmask (SIG_SETMASK, &oldmask, NULL);
549 unset_option (OPTKEEPQUIET);
551 unset_option (OPTIMAPPASSIVE);
552 set_quadoption (OPT_IMAPRECONNECT, imap_askreconnect);
557 /* Allow/disallow re-opening a folder upon expunge. */
559 void imap_allow_reopen (CONTEXT * ctx)
561 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
562 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
565 void imap_disallow_reopen (CONTEXT * ctx)
567 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
568 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;