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 */
17 #include "mx.h" /* for M_IMAP */
19 #include "imap_private.h"
24 #include "lib/debug.h"
29 #include <sys/types.h>
33 #include <netinet/in.h>
37 /* -- public functions -- */
39 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
40 * an IMAP path in canonical and absolute form.
41 * Inputs: a buffer containing an IMAP path, and the number of bytes in
43 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
44 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
45 * fails, which it might if there isn't enough room in the buffer. */
46 int imap_expand_path (char *path, size_t len)
52 if (imap_parse_path (path, &mx) < 0)
55 mutt_account_tourl (&mx.account, &url);
58 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
64 /* imap_parse_path: given an IMAP mailbox name, return host, port
65 * and a path IMAP servers will recognise.
66 * mx.mbox is malloc'd, caller must free it */
67 int imap_parse_path (const char *path, IMAP_MBOX * mx)
69 static unsigned short ImapPort = 0;
70 static unsigned short ImapsPort = 0;
71 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;
99 c = safe_strdup (path);
100 url_parse_ciss (&url, c);
101 if (url.scheme == U_IMAP || url.scheme == U_IMAPS) {
102 if (mutt_account_fromurl (&mx->account, &url) < 0) {
107 mx->mbox = safe_strdup (url.path);
109 if (url.scheme == U_IMAPS)
110 mx->account.flags |= M_ACCT_SSL;
114 /* old PINE-compatibility code */
117 if (sscanf (path, "{%127[^}]}", tmp) != 1)
120 c = strchr (path, '}');
124 /* walk past closing '}' */
125 mx->mbox = safe_strdup (c + 1);
127 if ((c = strrchr (tmp, '@'))) {
129 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
130 strfcpy (tmp, c + 1, sizeof (tmp));
131 mx->account.flags |= M_ACCT_USER;
134 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1) {
135 debug_print (1, ("NULL host in %s\n", path));
141 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
142 mx->account.flags |= M_ACCT_PORT;
143 if (sscanf (tmp, "/%s", tmp) == 1) {
144 if (!ascii_strncmp (tmp, "ssl", 3))
145 mx->account.flags |= M_ACCT_SSL;
147 debug_print (1, ("Unknown connection type in %s\n", path));
155 #if defined(USE_SSL) || defined(USE_GNUTLS)
156 if (option (OPTIMAPFORCESSL))
157 mx->account.flags |= M_ACCT_SSL;
160 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
161 mx->account.port = ImapsPort;
166 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
168 void imap_pretty_mailbox (char *path)
170 IMAP_MBOX home, target;
177 if (imap_parse_path (path, &target) < 0)
180 tlen = safe_strlen (target.mbox);
181 /* check whether we can do '=' substitution */
182 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
183 hlen = safe_strlen (home.mbox);
184 if (tlen && mutt_account_match (&home.account, &target.account) &&
185 !safe_strncmp (home.mbox, target.mbox, hlen)) {
189 for (delim = ImapDelimChars; *delim != '\0'; delim++)
190 if (target.mbox[hlen] == *delim)
196 /* do the '=' substitution */
199 /* copy remaining path, skipping delimiter */
202 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
203 path[tlen - hlen - 1] = '\0';
206 mutt_account_tourl (&target.account, &url);
207 url.path = target.mbox;
208 /* FIXME: That hard-coded constant is bogus. But we need the actual
209 * size of the buffer from mutt_pretty_mailbox. And these pretty
210 * operations usually shrink the result. Still... */
211 url_ciss_tostring (&url, path, 1024, 0);
217 /* -- library functions -- */
219 /* imap_continue: display a message and ask the user if she wants to
221 int imap_continue (const char *msg, const char *resp)
223 imap_error (msg, resp);
224 return mutt_yesorno (_("Continue?"), 0);
227 /* imap_error: show an error and abort */
228 void imap_error (const char *where, const char *msg)
230 mutt_error ("%s [%s]\n", where, msg);
234 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
235 * Returns NULL on failure (no mem) */
236 IMAP_DATA *imap_new_idata (void)
238 return safe_calloc (1, sizeof (IMAP_DATA));
241 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
242 void imap_free_idata (IMAP_DATA ** idata)
247 FREE (&(*idata)->capstr);
248 mutt_free_list (&(*idata)->flags);
249 FREE (&((*idata)->cmd.buf));
254 * Fix up the imap path. This is necessary because the rest of mutt
255 * assumes a hierarchy delimiter of '/', which is not necessarily true
256 * in IMAP. Additionally, the filesystem converts multiple hierarchy
257 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
258 * are not required to do this.
259 * Moreover, IMAP servers may dislike the path ending with the delimiter.
261 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
266 if (!mailbox || !*mailbox) {
267 strfcpy (path, "INBOX", plen);
271 while (mailbox && *mailbox && (x < (plen - 1))) {
272 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
273 while ((*mailbox == '/') || (*mailbox == idata->delim))
275 path[x] = idata->delim;
283 if (x && path[--x] != idata->delim)
289 /* imap_get_literal_count: write number of bytes in an IMAP literal into
290 * bytes, return 0 on success, -1 on failure. */
291 int imap_get_literal_count (const char *buf, long *bytes)
296 if (!(pc = strchr (buf, '{')))
300 while (isdigit ((unsigned char) *pc))
307 /* imap_get_qualifier: in a tagged response, skip tag and status for
308 * the qualifier message. Used by imap_copy_message for TRYCREATE */
309 char *imap_get_qualifier (char *buf)
314 s = imap_next_word (s);
315 /* skip OK/NO/BAD response */
316 s = imap_next_word (s);
321 /* imap_next_word: return index into string where next IMAP word begins */
322 char *imap_next_word (char *s)
334 quoted = quoted ? 0 : 1;
335 if (!quoted && ISSPACE (*s))
344 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
345 time_t imap_parse_date (char *s)
350 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
355 t.tm_mon = mutt_check_month (s);
361 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
370 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
375 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
380 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
387 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
388 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
392 return (mutt_mktime (&t, 0) + tz);
395 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
396 * and relative path. */
397 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
401 mutt_account_tourl (&mx->account, &url);
404 url_ciss_tostring (&url, dest, len, 0);
408 /* imap_quote_string: quote string according to IMAP rules:
409 * surround string with quotes, escape " and \ with \ */
410 void imap_quote_string (char *dest, size_t dlen, const char *src)
412 char quote[] = "\"\\", *pt;
419 /* save room for trailing quote-char */
422 for (; *s && dlen; s++) {
423 if (strchr (quote, *s)) {
439 /* imap_unquote_string: equally stupid unquoting routine */
440 void imap_unquote_string (char *s)
467 * Quoting and UTF-7 conversion
470 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
474 buf = safe_strdup (src);
475 imap_utf7_encode (&buf);
477 imap_quote_string (dest, dlen, buf);
482 void imap_unmunge_mbox_name (char *s)
486 imap_unquote_string (s);
488 buf = safe_strdup (s);
490 imap_utf7_decode (&buf);
491 strncpy (s, buf, safe_strlen (s));
497 /* imap_wordcasecmp: find word a in word list b */
498 int imap_wordcasecmp (const char *a, const char *b)
500 char tmp[SHORT_STRING];
501 char *s = (char *) b;
504 tmp[SHORT_STRING - 1] = 0;
505 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
506 if (!*s || ISSPACE (*s)) {
514 return ascii_strcasecmp (a, tmp);
518 * Imap keepalive: poll the current folder to keep the
523 static RETSIGTYPE alrm_handler (int sig)
528 void imap_keepalive (void)
534 conn = mutt_socket_head ();
536 if (conn->account.type == M_ACCT_TYPE_IMAP) {
537 idata = (IMAP_DATA *) conn->data;
539 if (idata->state >= IMAP_AUTHENTICATED
540 && time (NULL) >= idata->lastread + ImapKeepalive) {
544 ctx = safe_calloc (1, sizeof (CONTEXT));
547 imap_check_mailbox (ctx, NULL, 1);
557 int imap_wait_keepalive (pid_t pid)
559 struct sigaction oldalrm;
560 struct sigaction act;
564 short imap_passive = option (OPTIMAPPASSIVE);
566 set_option (OPTIMAPPASSIVE);
567 set_option (OPTKEEPQUIET);
569 sigprocmask (SIG_SETMASK, NULL, &oldmask);
571 sigemptyset (&act.sa_mask);
572 act.sa_handler = alrm_handler;
574 act.sa_flags = SA_INTERRUPT;
579 sigaction (SIGALRM, &act, &oldalrm);
581 alarm (ImapKeepalive);
582 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
583 alarm (0); /* cancel a possibly pending alarm */
585 alarm (ImapKeepalive);
588 alarm (0); /* cancel a possibly pending alarm */
590 sigaction (SIGALRM, &oldalrm, NULL);
591 sigprocmask (SIG_SETMASK, &oldmask, NULL);
593 unset_option (OPTKEEPQUIET);
595 unset_option (OPTIMAPPASSIVE);
600 /* Allow/disallow re-opening a folder upon expunge. */
602 void imap_allow_reopen (CONTEXT * ctx)
604 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
605 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
608 void imap_disallow_reopen (CONTEXT * ctx)
610 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
611 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;