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);
133 dprint (3, (debugfile, "hcache uidvalidity mismatch: %u", *uv));
140 int imap_hcache_put (IMAP_DATA* idata, HEADER* h)
147 sprintf (key, "/%u", HEADER_DATA (h)->uid);
148 return mutt_hcache_store (idata->hcache, key, h, idata->uid_validity,
152 int imap_hcache_del (IMAP_DATA* idata, unsigned int uid)
159 sprintf (key, "/%u", uid);
160 return mutt_hcache_delete (idata->hcache, key, imap_hcache_keylen);
164 /* imap_parse_path: given an IMAP mailbox name, return host, port
165 * and a path IMAP servers will recognise.
166 * mx.mbox is malloc'd, caller must free it */
167 int imap_parse_path (const char* path, IMAP_MBOX* mx)
169 static unsigned short ImapPort = 0;
170 static unsigned short ImapsPort = 0;
171 struct servent* service;
179 service = getservbyname ("imap", "tcp");
181 ImapPort = ntohs (service->s_port);
183 ImapPort = IMAP_PORT;
184 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
188 service = getservbyname ("imaps", "tcp");
190 ImapsPort = ntohs (service->s_port);
192 ImapsPort = IMAP_SSL_PORT;
193 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
197 memset(&mx->account, 0, sizeof(mx->account));
198 mx->account.port = ImapPort;
199 mx->account.type = M_ACCT_TYPE_IMAP;
201 c = safe_strdup (path);
202 url_parse_ciss (&url, c);
203 if (url.scheme == U_IMAP || url.scheme == U_IMAPS)
205 if (mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host)
211 mx->mbox = safe_strdup (url.path);
213 if (url.scheme == U_IMAPS)
214 mx->account.flags |= M_ACCT_SSL;
218 /* old PINE-compatibility code */
222 if (sscanf (path, "{%127[^}]}", tmp) != 1)
225 c = strchr (path, '}');
229 /* walk past closing '}' */
230 mx->mbox = safe_strdup (c+1);
232 if ((c = strrchr (tmp, '@')))
235 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
236 strfcpy (tmp, c+1, sizeof (tmp));
237 mx->account.flags |= M_ACCT_USER;
240 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
242 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
248 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
249 mx->account.flags |= M_ACCT_PORT;
250 if (sscanf (tmp, "/%s", tmp) == 1)
252 if (!ascii_strncmp (tmp, "ssl", 3))
253 mx->account.flags |= M_ACCT_SSL;
256 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
264 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
265 mx->account.port = ImapsPort;
270 /* silly helper for mailbox name string comparisons, because of INBOX */
271 int imap_mxcmp (const char* mx1, const char* mx2)
281 if (!ascii_strcasecmp (mx1, "INBOX") && !ascii_strcasecmp (mx2, "INBOX"))
284 b1 = safe_malloc (strlen (mx1) + 1);
285 b2 = safe_malloc (strlen (mx2) + 1);
287 imap_fix_path (NULL, mx1, b1, strlen (mx1) + 1);
288 imap_fix_path (NULL, mx2, b2, strlen (mx2) + 1);
290 rc = mutt_strcmp (b1, b2);
297 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
299 void imap_pretty_mailbox (char* path)
301 IMAP_MBOX home, target;
308 if (imap_parse_path (path, &target) < 0)
311 tlen = mutt_strlen (target.mbox);
312 /* check whether we can do '=' substitution */
313 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
315 hlen = mutt_strlen (home.mbox);
316 if (tlen && mutt_account_match (&home.account, &target.account) &&
317 !mutt_strncmp (home.mbox, target.mbox, hlen))
321 else if (ImapDelimChars)
322 for (delim = ImapDelimChars; *delim != '\0'; delim++)
323 if (target.mbox[hlen] == *delim)
329 /* do the '=' substitution */
332 /* copy remaining path, skipping delimiter */
335 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
336 path[tlen - hlen - 1] = '\0';
340 mutt_account_tourl (&target.account, &url);
341 url.path = target.mbox;
342 /* FIXME: That hard-coded constant is bogus. But we need the actual
343 * size of the buffer from mutt_pretty_mailbox. And these pretty
344 * operations usually shrink the result. Still... */
345 url_ciss_tostring (&url, path, 1024, 0);
351 /* -- library functions -- */
353 /* imap_continue: display a message and ask the user if she wants to
355 int imap_continue (const char* msg, const char* resp)
357 imap_error (msg, resp);
358 return mutt_yesorno (_("Continue?"), 0);
361 /* imap_error: show an error and abort */
362 void imap_error (const char *where, const char *msg)
364 mutt_error ("%s [%s]\n", where, msg);
368 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
369 * Returns NULL on failure (no mem) */
370 IMAP_DATA* imap_new_idata (void)
372 IMAP_DATA* idata = safe_calloc (1, sizeof (IMAP_DATA));
377 if (!(idata->cmdbuf = mutt_buffer_init (NULL)))
380 idata->cmdslots = ImapPipelineDepth + 2;
381 if (!(idata->cmds = safe_calloc(idata->cmdslots, sizeof(*idata->cmds))))
383 mutt_buffer_free(&idata->cmdbuf);
390 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
391 void imap_free_idata (IMAP_DATA** idata)
396 FREE (&(*idata)->capstr);
397 mutt_free_list (&(*idata)->flags);
398 imap_mboxcache_free (*idata);
399 mutt_buffer_free(&(*idata)->cmdbuf);
400 FREE (&(*idata)->buf);
401 mutt_bcache_close (&(*idata)->bcache);
402 FREE (&(*idata)->cmds);
403 FREE (idata); /* __FREE_CHECKED__ */
407 * Fix up the imap path. This is necessary because the rest of mutt
408 * assumes a hierarchy delimiter of '/', which is not necessarily true
409 * in IMAP. Additionally, the filesystem converts multiple hierarchy
410 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
411 * are not required to do this.
412 * Moreover, IMAP servers may dislike the path ending with the delimiter.
414 char *imap_fix_path (IMAP_DATA *idata, const char *mailbox, char *path,
421 delim = idata->delim;
423 while (mailbox && *mailbox && i < plen - 1)
425 if ((ImapDelimChars && strchr(ImapDelimChars, *mailbox))
426 || (delim && *mailbox == delim))
428 /* use connection delimiter if known. Otherwise use user delimiter */
433 && ((ImapDelimChars && strchr(ImapDelimChars, *mailbox))
434 || (delim && *mailbox == delim)))
445 if (i && path[--i] != delim)
452 void imap_cachepath(IMAP_DATA* idata, const char* mailbox, char* dest,
456 const char* p = mailbox;
458 for (s = dest; p && *p && dlen; dlen--)
460 if (*p == idata->delim)
463 /* simple way to avoid collisions with UIDs */
464 if (*(p + 1) >= '0' && *(p + 1) <= '9')
478 /* imap_get_literal_count: write number of bytes in an IMAP literal into
479 * bytes, return 0 on success, -1 on failure. */
480 int imap_get_literal_count(const char *buf, long *bytes)
485 if (!buf || !(pc = strchr (buf, '{')))
490 while (isdigit ((unsigned char) *pc))
498 /* imap_get_qualifier: in a tagged response, skip tag and status for
499 * the qualifier message. Used by imap_copy_message for TRYCREATE */
500 char* imap_get_qualifier (char* buf)
505 s = imap_next_word (s);
506 /* skip OK/NO/BAD response */
507 s = imap_next_word (s);
512 /* imap_next_word: return index into string where next IMAP word begins */
513 char *imap_next_word (char *s)
525 quoted = quoted ? 0 : 1;
526 if (!quoted && ISSPACE (*s))
535 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
536 time_t imap_parse_date (char *s)
541 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
546 t.tm_mon = mutt_check_month (s);
551 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
558 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
563 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
568 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
575 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
576 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
580 return (mutt_mktime (&t, 0) + tz);
583 /* format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz.
584 * Caller should provide a buffer of IMAP_DATELEN bytes */
585 void imap_make_date (char *buf, time_t timestamp)
587 struct tm* tm = localtime (×tamp);
588 time_t tz = mutt_local_tz (timestamp);
592 snprintf (buf, IMAP_DATELEN, "%02d-%s-%d %02d:%02d:%02d %+03d%02d",
593 tm->tm_mday, Months[tm->tm_mon], tm->tm_year + 1900,
594 tm->tm_hour, tm->tm_min, tm->tm_sec,
595 (int) tz / 60, (int) abs (tz) % 60);
598 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
599 * and relative path. */
600 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
604 mutt_account_tourl (&mx->account, &url);
607 url_ciss_tostring (&url, dest, len, 0);
611 /* imap_quote_string: quote string according to IMAP rules:
612 * surround string with quotes, escape " and \ with \ */
613 void imap_quote_string (char *dest, size_t dlen, const char *src)
615 char quote[] = "\"\\", *pt;
622 /* save room for trailing quote-char */
625 for (; *s && dlen; s++)
627 if (strchr (quote, *s))
645 /* imap_unquote_string: equally stupid unquoting routine */
646 void imap_unquote_string (char *s)
677 * Quoting and UTF-7 conversion
680 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
684 buf = safe_strdup (src);
685 imap_utf7_encode (&buf);
687 imap_quote_string (dest, dlen, buf);
692 void imap_unmunge_mbox_name (char *s)
696 imap_unquote_string(s);
698 buf = safe_strdup (s);
701 imap_utf7_decode (&buf);
702 strncpy (s, buf, strlen (s));
708 /* imap_wordcasecmp: find word a in word list b */
709 int imap_wordcasecmp(const char *a, const char *b)
711 char tmp[SHORT_STRING];
715 tmp[SHORT_STRING-1] = 0;
716 for(i=0;i < SHORT_STRING-2;i++,s++)
718 if (!*s || ISSPACE(*s))
727 return ascii_strcasecmp(a, tmp);
731 * Imap keepalive: poll the current folder to keep the
736 static RETSIGTYPE alrm_handler (int sig)
741 void imap_keepalive (void)
747 conn = mutt_socket_head ();
750 if (conn->account.type == M_ACCT_TYPE_IMAP)
754 idata = (IMAP_DATA*) conn->data;
756 if (idata->state >= IMAP_AUTHENTICATED
757 && time(NULL) >= idata->lastread + ImapKeepalive)
763 ctx = safe_calloc (1, sizeof (CONTEXT));
765 /* imap_close_mailbox will set ctx->iadata->ctx to NULL, so we can't
766 * rely on the value of iadata->ctx to determine if this placeholder
767 * context needs to be freed.
771 /* if the imap connection closes during this call, ctx may be invalid
772 * after this point, and thus should not be read.
774 imap_check_mailbox (ctx, NULL, 1);
784 int imap_wait_keepalive (pid_t pid)
786 struct sigaction oldalrm;
787 struct sigaction act;
791 short imap_passive = option (OPTIMAPPASSIVE);
793 set_option (OPTIMAPPASSIVE);
794 set_option (OPTKEEPQUIET);
796 sigprocmask (SIG_SETMASK, NULL, &oldmask);
798 sigemptyset (&act.sa_mask);
799 act.sa_handler = alrm_handler;
801 act.sa_flags = SA_INTERRUPT;
806 sigaction (SIGALRM, &act, &oldalrm);
808 alarm (ImapKeepalive);
809 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
811 alarm (0); /* cancel a possibly pending alarm */
813 alarm (ImapKeepalive);
816 alarm (0); /* cancel a possibly pending alarm */
818 sigaction (SIGALRM, &oldalrm, NULL);
819 sigprocmask (SIG_SETMASK, &oldmask, NULL);
821 unset_option (OPTKEEPQUIET);
823 unset_option (OPTIMAPPASSIVE);
828 /* Allow/disallow re-opening a folder upon expunge. */
830 void imap_allow_reopen (CONTEXT *ctx)
832 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
833 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
836 void imap_disallow_reopen (CONTEXT *ctx)
838 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
839 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;
842 int imap_account_match (const ACCOUNT* a1, const ACCOUNT* a2)
844 IMAP_DATA* a1_idata = imap_conn_find (a1, M_IMAP_CONN_NONEW);
845 IMAP_DATA* a2_idata = imap_conn_find (a2, M_IMAP_CONN_NONEW);
846 const ACCOUNT* a1_canon = a1_idata == NULL ? a1 : &a1_idata->conn->account;
847 const ACCOUNT* a2_canon = a2_idata == NULL ? a2 : &a2_idata->conn->account;
849 return mutt_account_match (a1_canon, a2_canon);