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-2008 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)
58 char fixedpath[LONG_STRING];
61 if (imap_parse_path (path, &mx) < 0)
64 mutt_account_tourl (&mx.account, &url);
65 imap_fix_path(NULL, mx.mbox, fixedpath, sizeof (fixedpath));
68 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
75 static int imap_hcache_namer (const char* path, char* dest, size_t dlen)
77 return snprintf (dest, dlen, "%s.hcache", path);
80 header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path)
84 char cachepath[LONG_STRING];
85 char mbox[LONG_STRING];
88 imap_cachepath (idata, path, mbox, sizeof (mbox));
91 if (!idata->ctx || imap_parse_path (idata->ctx->path, &mx) < 0)
94 imap_cachepath (idata, mx.mbox, mbox, sizeof (mbox));
98 mutt_account_tourl (&idata->conn->account, &url);
100 url_ciss_tostring (&url, cachepath, sizeof (cachepath), U_PATH);
102 return mutt_hcache_open (HeaderCache, cachepath, imap_hcache_namer);
105 void imap_hcache_close (IMAP_DATA* idata)
110 mutt_hcache_close (idata->hcache);
111 idata->hcache = NULL;
114 HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned int uid)
123 sprintf (key, "/%u", uid);
124 uv = (unsigned int*)mutt_hcache_fetch (idata->hcache, key,
128 if (*uv == idata->uid_validity)
129 h = mutt_hcache_restore ((unsigned char*)uv, NULL);
136 int imap_hcache_put (IMAP_DATA* idata, HEADER* h)
143 sprintf (key, "/%u", HEADER_DATA (h)->uid);
144 return mutt_hcache_store (idata->hcache, key, h, idata->uid_validity,
148 int imap_hcache_del (IMAP_DATA* idata, unsigned int uid)
155 sprintf (key, "/%u", uid);
156 return mutt_hcache_delete (idata->hcache, key, imap_hcache_keylen);
160 /* imap_parse_path: given an IMAP mailbox name, return host, port
161 * and a path IMAP servers will recognise.
162 * mx.mbox is malloc'd, caller must free it */
163 int imap_parse_path (const char* path, IMAP_MBOX* mx)
165 static unsigned short ImapPort = 0;
166 static unsigned short ImapsPort = 0;
167 struct servent* service;
175 service = getservbyname ("imap", "tcp");
177 ImapPort = ntohs (service->s_port);
179 ImapPort = IMAP_PORT;
180 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
184 service = getservbyname ("imaps", "tcp");
186 ImapsPort = ntohs (service->s_port);
188 ImapsPort = IMAP_SSL_PORT;
189 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
193 memset(&mx->account, 0, sizeof(mx->account));
194 mx->account.port = ImapPort;
195 mx->account.type = M_ACCT_TYPE_IMAP;
197 c = safe_strdup (path);
198 url_parse_ciss (&url, c);
199 if (url.scheme == U_IMAP || url.scheme == U_IMAPS)
201 if (mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host)
207 mx->mbox = safe_strdup (url.path);
209 if (url.scheme == U_IMAPS)
210 mx->account.flags |= M_ACCT_SSL;
214 /* old PINE-compatibility code */
218 if (sscanf (path, "{%127[^}]}", tmp) != 1)
221 c = strchr (path, '}');
225 /* walk past closing '}' */
226 mx->mbox = safe_strdup (c+1);
228 if ((c = strrchr (tmp, '@')))
231 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
232 strfcpy (tmp, c+1, sizeof (tmp));
233 mx->account.flags |= M_ACCT_USER;
236 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
238 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
244 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
245 mx->account.flags |= M_ACCT_PORT;
246 if (sscanf (tmp, "/%s", tmp) == 1)
248 if (!ascii_strncmp (tmp, "ssl", 3))
249 mx->account.flags |= M_ACCT_SSL;
252 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
260 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
261 mx->account.port = ImapsPort;
266 /* silly helper for mailbox name string comparisons, because of INBOX */
267 int imap_mxcmp (const char* mx1, const char* mx2)
269 if (!ascii_strcasecmp (mx1, "INBOX") && !ascii_strcasecmp (mx2, "INBOX"))
272 return mutt_strcmp (mx1, mx2);
275 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
277 void imap_pretty_mailbox (char* path)
279 IMAP_MBOX home, target;
286 if (imap_parse_path (path, &target) < 0)
289 tlen = mutt_strlen (target.mbox);
290 /* check whether we can do '=' substitution */
291 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
293 hlen = mutt_strlen (home.mbox);
294 if (tlen && mutt_account_match (&home.account, &target.account) &&
295 !mutt_strncmp (home.mbox, target.mbox, hlen))
300 for (delim = ImapDelimChars; *delim != '\0'; delim++)
301 if (target.mbox[hlen] == *delim)
307 /* do the '=' substitution */
310 /* copy remaining path, skipping delimiter */
313 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
314 path[tlen - hlen - 1] = '\0';
318 mutt_account_tourl (&target.account, &url);
319 url.path = target.mbox;
320 /* FIXME: That hard-coded constant is bogus. But we need the actual
321 * size of the buffer from mutt_pretty_mailbox. And these pretty
322 * operations usually shrink the result. Still... */
323 url_ciss_tostring (&url, path, 1024, 0);
329 /* -- library functions -- */
331 /* imap_continue: display a message and ask the user if she wants to
333 int imap_continue (const char* msg, const char* resp)
335 imap_error (msg, resp);
336 return mutt_yesorno (_("Continue?"), 0);
339 /* imap_error: show an error and abort */
340 void imap_error (const char *where, const char *msg)
342 mutt_error ("%s [%s]\n", where, msg);
346 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
347 * Returns NULL on failure (no mem) */
348 IMAP_DATA* imap_new_idata (void)
350 IMAP_DATA* idata = safe_calloc (1, sizeof (IMAP_DATA));
355 if (!(idata->cmdbuf = mutt_buffer_init (NULL)))
358 idata->cmdslots = ImapPipelineDepth + 2;
359 if (!(idata->cmds = safe_calloc(idata->cmdslots, sizeof(*idata->cmds))))
361 mutt_buffer_free(&idata->cmdbuf);
368 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
369 void imap_free_idata (IMAP_DATA** idata)
374 FREE (&(*idata)->capstr);
375 mutt_free_list (&(*idata)->flags);
376 imap_mboxcache_free (*idata);
377 mutt_buffer_free(&(*idata)->cmdbuf);
378 FREE (&(*idata)->buf);
379 mutt_bcache_close (&(*idata)->bcache);
380 FREE (&(*idata)->cmds);
381 FREE (idata); /* __FREE_CHECKED__ */
385 * Fix up the imap path. This is necessary because the rest of mutt
386 * assumes a hierarchy delimiter of '/', which is not necessarily true
387 * in IMAP. Additionally, the filesystem converts multiple hierarchy
388 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
389 * are not required to do this.
390 * Moreover, IMAP servers may dislike the path ending with the delimiter.
392 char *imap_fix_path (IMAP_DATA *idata, char *mailbox, char *path,
399 delim = idata->delim;
400 else if (ImapDelimChars && ImapDelimChars[0])
401 delim = ImapDelimChars[0];
405 while (mailbox && *mailbox && i < plen - 1)
407 if (strchr(ImapDelimChars, *mailbox) || *mailbox == delim)
410 (strchr(ImapDelimChars, *mailbox) || *mailbox == delim))
421 if (i && path[--i] != delim)
428 void imap_cachepath(IMAP_DATA* idata, const char* mailbox, char* dest,
432 const char* p = mailbox;
434 for (s = dest; p && *p && dlen; dlen--)
436 if (*p == idata->delim)
439 /* simple way to avoid collisions with UIDs */
440 if (*(p + 1) >= '0' && *(p + 1) <= '9')
454 /* imap_get_literal_count: write number of bytes in an IMAP literal into
455 * bytes, return 0 on success, -1 on failure. */
456 int imap_get_literal_count(const char *buf, long *bytes)
461 if (!buf || !(pc = strchr (buf, '{')))
466 while (isdigit ((unsigned char) *pc))
474 /* imap_get_qualifier: in a tagged response, skip tag and status for
475 * the qualifier message. Used by imap_copy_message for TRYCREATE */
476 char* imap_get_qualifier (char* buf)
481 s = imap_next_word (s);
482 /* skip OK/NO/BAD response */
483 s = imap_next_word (s);
488 /* imap_next_word: return index into string where next IMAP word begins */
489 char *imap_next_word (char *s)
501 quoted = quoted ? 0 : 1;
502 if (!quoted && ISSPACE (*s))
511 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
512 time_t imap_parse_date (char *s)
517 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
522 t.tm_mon = mutt_check_month (s);
527 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
534 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
539 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
544 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
551 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
552 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
556 return (mutt_mktime (&t, 0) + tz);
559 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
560 * and relative path. */
561 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
565 mutt_account_tourl (&mx->account, &url);
568 url_ciss_tostring (&url, dest, len, 0);
572 /* imap_quote_string: quote string according to IMAP rules:
573 * surround string with quotes, escape " and \ with \ */
574 void imap_quote_string (char *dest, size_t dlen, const char *src)
576 char quote[] = "\"\\", *pt;
583 /* save room for trailing quote-char */
586 for (; *s && dlen; s++)
588 if (strchr (quote, *s))
606 /* imap_unquote_string: equally stupid unquoting routine */
607 void imap_unquote_string (char *s)
638 * Quoting and UTF-7 conversion
641 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
645 buf = safe_strdup (src);
646 imap_utf7_encode (&buf);
648 imap_quote_string (dest, dlen, buf);
653 void imap_unmunge_mbox_name (char *s)
657 imap_unquote_string(s);
659 buf = safe_strdup (s);
662 imap_utf7_decode (&buf);
663 strncpy (s, buf, strlen (s));
669 /* imap_wordcasecmp: find word a in word list b */
670 int imap_wordcasecmp(const char *a, const char *b)
672 char tmp[SHORT_STRING];
676 tmp[SHORT_STRING-1] = 0;
677 for(i=0;i < SHORT_STRING-2;i++,s++)
679 if (!*s || ISSPACE(*s))
688 return ascii_strcasecmp(a, tmp);
692 * Imap keepalive: poll the current folder to keep the
697 static RETSIGTYPE alrm_handler (int sig)
702 void imap_keepalive (void)
708 conn = mutt_socket_head ();
711 if (conn->account.type == M_ACCT_TYPE_IMAP)
713 idata = (IMAP_DATA*) conn->data;
715 if (idata->state >= IMAP_AUTHENTICATED
716 && time(NULL) >= idata->lastread + ImapKeepalive)
722 ctx = safe_calloc (1, sizeof (CONTEXT));
725 imap_check_mailbox (ctx, NULL, 1);
735 int imap_wait_keepalive (pid_t pid)
737 struct sigaction oldalrm;
738 struct sigaction act;
742 short imap_passive = option (OPTIMAPPASSIVE);
744 set_option (OPTIMAPPASSIVE);
745 set_option (OPTKEEPQUIET);
747 sigprocmask (SIG_SETMASK, NULL, &oldmask);
749 sigemptyset (&act.sa_mask);
750 act.sa_handler = alrm_handler;
752 act.sa_flags = SA_INTERRUPT;
757 sigaction (SIGALRM, &act, &oldalrm);
759 alarm (ImapKeepalive);
760 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
762 alarm (0); /* cancel a possibly pending alarm */
764 alarm (ImapKeepalive);
767 alarm (0); /* cancel a possibly pending alarm */
769 sigaction (SIGALRM, &oldalrm, NULL);
770 sigprocmask (SIG_SETMASK, &oldmask, NULL);
772 unset_option (OPTKEEPQUIET);
774 unset_option (OPTIMAPPASSIVE);
779 /* Allow/disallow re-opening a folder upon expunge. */
781 void imap_allow_reopen (CONTEXT *ctx)
783 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
784 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
787 void imap_disallow_reopen (CONTEXT *ctx)
789 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
790 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;
793 int imap_account_match (const ACCOUNT* a1, const ACCOUNT* a2)
795 IMAP_DATA* a1_idata = imap_conn_find (a1, M_IMAP_CONN_NONEW);
796 IMAP_DATA* a2_idata = imap_conn_find (a2, M_IMAP_CONN_NONEW);
797 const ACCOUNT* a1_canon = a1_idata == NULL ? a1 : &a1_idata->conn->account;
798 const ACCOUNT* a2_canon = a2_idata == NULL ? a2 : &a2_idata->conn->account;
800 return mutt_account_match (a1_canon, a2_canon);