2 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* general IMAP utility functions */
26 #include "mx.h" /* for M_IMAP */
28 #include "imap_private.h"
37 #include <sys/types.h>
41 #include <netinet/in.h>
45 /* -- public functions -- */
47 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
48 * an IMAP path in canonical and absolute form.
49 * Inputs: a buffer containing an IMAP path, and the number of bytes in
51 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
52 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
53 * fails, which it might if there isn't enough room in the buffer. */
54 int imap_expand_path (char* path, size_t len)
59 char fixedpath[LONG_STRING];
62 if (imap_parse_path (path, &mx) < 0)
65 idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW);
66 mutt_account_tourl (&mx.account, &url);
67 imap_fix_path (idata, mx.mbox, fixedpath, sizeof (fixedpath));
70 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
77 static int imap_hcache_namer (const char* path, char* dest, size_t dlen)
79 return snprintf (dest, dlen, "%s.hcache", path);
82 header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path)
86 char cachepath[LONG_STRING];
87 char mbox[LONG_STRING];
90 imap_cachepath (idata, path, mbox, sizeof (mbox));
93 if (!idata->ctx || imap_parse_path (idata->ctx->path, &mx) < 0)
96 imap_cachepath (idata, mx.mbox, mbox, sizeof (mbox));
100 mutt_account_tourl (&idata->conn->account, &url);
102 url_ciss_tostring (&url, cachepath, sizeof (cachepath), U_PATH);
104 return mutt_hcache_open (HeaderCache, cachepath, imap_hcache_namer);
107 void imap_hcache_close (IMAP_DATA* idata)
112 mutt_hcache_close (idata->hcache);
113 idata->hcache = NULL;
116 HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned int uid)
125 sprintf (key, "/%u", uid);
126 uv = (unsigned int*)mutt_hcache_fetch (idata->hcache, key,
130 if (*uv == idata->uid_validity)
131 h = mutt_hcache_restore ((unsigned char*)uv, NULL);
138 int imap_hcache_put (IMAP_DATA* idata, HEADER* h)
145 sprintf (key, "/%u", HEADER_DATA (h)->uid);
146 return mutt_hcache_store (idata->hcache, key, h, idata->uid_validity,
150 int imap_hcache_del (IMAP_DATA* idata, unsigned int uid)
157 sprintf (key, "/%u", uid);
158 return mutt_hcache_delete (idata->hcache, key, imap_hcache_keylen);
162 /* imap_parse_path: given an IMAP mailbox name, return host, port
163 * and a path IMAP servers will recognise.
164 * mx.mbox is malloc'd, caller must free it */
165 int imap_parse_path (const char* path, IMAP_MBOX* mx)
167 static unsigned short ImapPort = 0;
168 static unsigned short ImapsPort = 0;
169 struct servent* service;
177 service = getservbyname ("imap", "tcp");
179 ImapPort = ntohs (service->s_port);
181 ImapPort = IMAP_PORT;
182 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
186 service = getservbyname ("imaps", "tcp");
188 ImapsPort = ntohs (service->s_port);
190 ImapsPort = IMAP_SSL_PORT;
191 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
195 memset(&mx->account, 0, sizeof(mx->account));
196 mx->account.port = ImapPort;
197 mx->account.type = M_ACCT_TYPE_IMAP;
199 c = safe_strdup (path);
200 url_parse_ciss (&url, c);
201 if (url.scheme == U_IMAP || url.scheme == U_IMAPS)
203 if (mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host)
209 mx->mbox = safe_strdup (url.path);
211 if (url.scheme == U_IMAPS)
212 mx->account.flags |= M_ACCT_SSL;
216 /* old PINE-compatibility code */
220 if (sscanf (path, "{%127[^}]}", tmp) != 1)
223 c = strchr (path, '}');
227 /* walk past closing '}' */
228 mx->mbox = safe_strdup (c+1);
230 if ((c = strrchr (tmp, '@')))
233 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
234 strfcpy (tmp, c+1, sizeof (tmp));
235 mx->account.flags |= M_ACCT_USER;
238 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
240 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
246 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
247 mx->account.flags |= M_ACCT_PORT;
248 if (sscanf (tmp, "/%s", tmp) == 1)
250 if (!ascii_strncmp (tmp, "ssl", 3))
251 mx->account.flags |= M_ACCT_SSL;
254 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
262 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
263 mx->account.port = ImapsPort;
268 /* silly helper for mailbox name string comparisons, because of INBOX */
269 int imap_mxcmp (const char* mx1, const char* mx2)
279 if (!ascii_strcasecmp (mx1, "INBOX") && !ascii_strcasecmp (mx2, "INBOX"))
282 b1 = safe_malloc (strlen (mx1) + 1);
283 b2 = safe_malloc (strlen (mx2) + 1);
285 imap_fix_path (NULL, mx1, b1, strlen (mx1) + 1);
286 imap_fix_path (NULL, mx2, b2, strlen (mx2) + 1);
288 rc = mutt_strcmp (b1, b2);
295 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
297 void imap_pretty_mailbox (char* path)
299 IMAP_MBOX home, target;
306 if (imap_parse_path (path, &target) < 0)
309 tlen = mutt_strlen (target.mbox);
310 /* check whether we can do '=' substitution */
311 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
313 hlen = mutt_strlen (home.mbox);
314 if (tlen && mutt_account_match (&home.account, &target.account) &&
315 !mutt_strncmp (home.mbox, target.mbox, hlen))
320 for (delim = ImapDelimChars; *delim != '\0'; delim++)
321 if (target.mbox[hlen] == *delim)
327 /* do the '=' substitution */
330 /* copy remaining path, skipping delimiter */
333 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
334 path[tlen - hlen - 1] = '\0';
338 mutt_account_tourl (&target.account, &url);
339 url.path = target.mbox;
340 /* FIXME: That hard-coded constant is bogus. But we need the actual
341 * size of the buffer from mutt_pretty_mailbox. And these pretty
342 * operations usually shrink the result. Still... */
343 url_ciss_tostring (&url, path, 1024, 0);
349 /* -- library functions -- */
351 /* imap_continue: display a message and ask the user if she wants to
353 int imap_continue (const char* msg, const char* resp)
355 imap_error (msg, resp);
356 return mutt_yesorno (_("Continue?"), 0);
359 /* imap_error: show an error and abort */
360 void imap_error (const char *where, const char *msg)
362 mutt_error ("%s [%s]\n", where, msg);
366 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
367 * Returns NULL on failure (no mem) */
368 IMAP_DATA* imap_new_idata (void)
370 IMAP_DATA* idata = safe_calloc (1, sizeof (IMAP_DATA));
375 if (!(idata->cmdbuf = mutt_buffer_init (NULL)))
378 idata->cmdslots = ImapPipelineDepth + 2;
379 if (!(idata->cmds = safe_calloc(idata->cmdslots, sizeof(*idata->cmds))))
381 mutt_buffer_free(&idata->cmdbuf);
388 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
389 void imap_free_idata (IMAP_DATA** idata)
394 FREE (&(*idata)->capstr);
395 mutt_free_list (&(*idata)->flags);
396 imap_mboxcache_free (*idata);
397 mutt_buffer_free(&(*idata)->cmdbuf);
398 FREE (&(*idata)->buf);
399 mutt_bcache_close (&(*idata)->bcache);
400 FREE (&(*idata)->cmds);
401 FREE (idata); /* __FREE_CHECKED__ */
405 * Fix up the imap path. This is necessary because the rest of mutt
406 * assumes a hierarchy delimiter of '/', which is not necessarily true
407 * in IMAP. Additionally, the filesystem converts multiple hierarchy
408 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
409 * are not required to do this.
410 * Moreover, IMAP servers may dislike the path ending with the delimiter.
412 char *imap_fix_path (IMAP_DATA *idata, const char *mailbox, char *path,
419 delim = idata->delim;
421 while (mailbox && *mailbox && i < plen - 1)
423 if ((ImapDelimChars && strchr(ImapDelimChars, *mailbox))
424 || *mailbox == delim)
426 /* use connection delimiter if known. Otherwise use user delimiter */
431 (strchr(ImapDelimChars, *mailbox) || *mailbox == delim))
442 if (i && path[--i] != delim)
449 void imap_cachepath(IMAP_DATA* idata, const char* mailbox, char* dest,
453 const char* p = mailbox;
455 for (s = dest; p && *p && dlen; dlen--)
457 if (*p == idata->delim)
460 /* simple way to avoid collisions with UIDs */
461 if (*(p + 1) >= '0' && *(p + 1) <= '9')
475 /* imap_get_literal_count: write number of bytes in an IMAP literal into
476 * bytes, return 0 on success, -1 on failure. */
477 int imap_get_literal_count(const char *buf, long *bytes)
482 if (!buf || !(pc = strchr (buf, '{')))
487 while (isdigit ((unsigned char) *pc))
495 /* imap_get_qualifier: in a tagged response, skip tag and status for
496 * the qualifier message. Used by imap_copy_message for TRYCREATE */
497 char* imap_get_qualifier (char* buf)
502 s = imap_next_word (s);
503 /* skip OK/NO/BAD response */
504 s = imap_next_word (s);
509 /* imap_next_word: return index into string where next IMAP word begins */
510 char *imap_next_word (char *s)
522 quoted = quoted ? 0 : 1;
523 if (!quoted && ISSPACE (*s))
532 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
533 time_t imap_parse_date (char *s)
538 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
543 t.tm_mon = mutt_check_month (s);
548 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
555 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
560 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
565 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
572 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
573 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
577 return (mutt_mktime (&t, 0) + tz);
580 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
581 * and relative path. */
582 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
586 mutt_account_tourl (&mx->account, &url);
589 url_ciss_tostring (&url, dest, len, 0);
593 /* imap_quote_string: quote string according to IMAP rules:
594 * surround string with quotes, escape " and \ with \ */
595 void imap_quote_string (char *dest, size_t dlen, const char *src)
597 char quote[] = "\"\\", *pt;
604 /* save room for trailing quote-char */
607 for (; *s && dlen; s++)
609 if (strchr (quote, *s))
627 /* imap_unquote_string: equally stupid unquoting routine */
628 void imap_unquote_string (char *s)
659 * Quoting and UTF-7 conversion
662 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
666 buf = safe_strdup (src);
667 imap_utf7_encode (&buf);
669 imap_quote_string (dest, dlen, buf);
674 void imap_unmunge_mbox_name (char *s)
678 imap_unquote_string(s);
680 buf = safe_strdup (s);
683 imap_utf7_decode (&buf);
684 strncpy (s, buf, strlen (s));
690 /* imap_wordcasecmp: find word a in word list b */
691 int imap_wordcasecmp(const char *a, const char *b)
693 char tmp[SHORT_STRING];
697 tmp[SHORT_STRING-1] = 0;
698 for(i=0;i < SHORT_STRING-2;i++,s++)
700 if (!*s || ISSPACE(*s))
709 return ascii_strcasecmp(a, tmp);
713 * Imap keepalive: poll the current folder to keep the
718 static RETSIGTYPE alrm_handler (int sig)
723 void imap_keepalive (void)
729 conn = mutt_socket_head ();
732 if (conn->account.type == M_ACCT_TYPE_IMAP)
734 idata = (IMAP_DATA*) conn->data;
736 if (idata->state >= IMAP_AUTHENTICATED
737 && time(NULL) >= idata->lastread + ImapKeepalive)
743 ctx = safe_calloc (1, sizeof (CONTEXT));
746 imap_check_mailbox (ctx, NULL, 1);
756 int imap_wait_keepalive (pid_t pid)
758 struct sigaction oldalrm;
759 struct sigaction act;
763 short imap_passive = option (OPTIMAPPASSIVE);
765 set_option (OPTIMAPPASSIVE);
766 set_option (OPTKEEPQUIET);
768 sigprocmask (SIG_SETMASK, NULL, &oldmask);
770 sigemptyset (&act.sa_mask);
771 act.sa_handler = alrm_handler;
773 act.sa_flags = SA_INTERRUPT;
778 sigaction (SIGALRM, &act, &oldalrm);
780 alarm (ImapKeepalive);
781 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
783 alarm (0); /* cancel a possibly pending alarm */
785 alarm (ImapKeepalive);
788 alarm (0); /* cancel a possibly pending alarm */
790 sigaction (SIGALRM, &oldalrm, NULL);
791 sigprocmask (SIG_SETMASK, &oldmask, NULL);
793 unset_option (OPTKEEPQUIET);
795 unset_option (OPTIMAPPASSIVE);
800 /* Allow/disallow re-opening a folder upon expunge. */
802 void imap_allow_reopen (CONTEXT *ctx)
804 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
805 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
808 void imap_disallow_reopen (CONTEXT *ctx)
810 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
811 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;
814 int imap_account_match (const ACCOUNT* a1, const ACCOUNT* a2)
816 IMAP_DATA* a1_idata = imap_conn_find (a1, M_IMAP_CONN_NONEW);
817 IMAP_DATA* a2_idata = imap_conn_find (a2, M_IMAP_CONN_NONEW);
818 const ACCOUNT* a1_canon = a1_idata == NULL ? a1 : &a1_idata->conn->account;
819 const ACCOUNT* a2_canon = a2_idata == NULL ? a2 : &a2_idata->conn->account;
821 return mutt_account_match (a1_canon, a2_canon);