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-2005 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"
38 #include <sys/types.h>
42 #include <netinet/in.h>
46 /* -- public functions -- */
48 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
49 * an IMAP path in canonical and absolute form.
50 * Inputs: a buffer containing an IMAP path, and the number of bytes in
52 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
53 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
54 * fails, which it might if there isn't enough room in the buffer. */
55 int imap_expand_path (char* path, size_t len)
61 if (imap_parse_path (path, &mx) < 0)
64 mutt_account_tourl (&mx.account, &url);
67 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
74 static int imap_hcache_namer (const char* path, char* dest, size_t dlen)
76 return snprintf (dest, dlen, "%s.hcache", path);
79 header_cache_t* imap_hcache_open (IMAP_DATA* idata, const char* path)
83 char cachepath[LONG_STRING];
84 char mbox[LONG_STRING];
87 imap_cachepath (idata, path, mbox, sizeof (mbox));
90 if (!idata->ctx || imap_parse_path (idata->ctx->path, &mx) < 0)
93 imap_cachepath (idata, mx.mbox, mbox, sizeof (mbox));
97 mutt_account_tourl (&idata->conn->account, &url);
99 url_ciss_tostring (&url, cachepath, sizeof (cachepath), U_PATH);
101 return mutt_hcache_open (HeaderCache, cachepath, imap_hcache_namer);
104 void imap_hcache_close (IMAP_DATA* idata)
109 mutt_hcache_close (idata->hcache);
110 idata->hcache = NULL;
113 HEADER* imap_hcache_get (IMAP_DATA* idata, unsigned int uid)
122 sprintf (key, "/%u", uid);
123 uv = (unsigned int*)mutt_hcache_fetch (idata->hcache, key,
127 if (*uv == idata->uid_validity)
128 h = mutt_hcache_restore ((unsigned char*)uv, NULL);
135 int imap_hcache_put (IMAP_DATA* idata, HEADER* h)
142 sprintf (key, "/%u", HEADER_DATA (h)->uid);
143 return mutt_hcache_store (idata->hcache, key, h, idata->uid_validity,
147 int imap_hcache_del (IMAP_DATA* idata, unsigned int uid)
154 sprintf (key, "/%u", uid);
155 return mutt_hcache_delete (idata->hcache, key, imap_hcache_keylen);
159 /* imap_parse_path: given an IMAP mailbox name, return host, port
160 * and a path IMAP servers will recognise.
161 * mx.mbox is malloc'd, caller must free it */
162 int imap_parse_path (const char* path, IMAP_MBOX* mx)
164 static unsigned short ImapPort = 0;
165 static unsigned short ImapsPort = 0;
166 struct servent* service;
174 service = getservbyname ("imap", "tcp");
176 ImapPort = ntohs (service->s_port);
178 ImapPort = IMAP_PORT;
179 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
183 service = getservbyname ("imaps", "tcp");
185 ImapsPort = ntohs (service->s_port);
187 ImapsPort = IMAP_SSL_PORT;
188 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
192 mx->account.flags = 0;
193 mx->account.port = ImapPort;
194 mx->account.type = M_ACCT_TYPE_IMAP;
196 c = safe_strdup (path);
197 url_parse_ciss (&url, c);
198 if (url.scheme == U_IMAP || url.scheme == U_IMAPS)
200 if (mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host)
206 mx->mbox = safe_strdup (url.path);
208 if (url.scheme == U_IMAPS)
209 mx->account.flags |= M_ACCT_SSL;
213 /* old PINE-compatibility code */
217 if (sscanf (path, "{%127[^}]}", tmp) != 1)
220 c = strchr (path, '}');
224 /* walk past closing '}' */
225 mx->mbox = safe_strdup (c+1);
227 if ((c = strrchr (tmp, '@')))
230 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
231 strfcpy (tmp, c+1, sizeof (tmp));
232 mx->account.flags |= M_ACCT_USER;
235 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
237 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
243 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
244 mx->account.flags |= M_ACCT_PORT;
245 if (sscanf (tmp, "/%s", tmp) == 1)
247 if (!ascii_strncmp (tmp, "ssl", 3))
248 mx->account.flags |= M_ACCT_SSL;
251 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
259 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
260 mx->account.port = ImapsPort;
265 /* silly helper for mailbox name string comparisons, because of INBOX */
266 int imap_mxcmp (const char* mx1, const char* mx2)
268 if (!ascii_strcasecmp (mx1, "INBOX") && !ascii_strcasecmp (mx2, "INBOX"))
271 return mutt_strcmp (mx1, mx2);
274 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
276 void imap_pretty_mailbox (char* path)
278 IMAP_MBOX home, target;
285 if (imap_parse_path (path, &target) < 0)
288 tlen = mutt_strlen (target.mbox);
289 /* check whether we can do '=' substitution */
290 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
292 hlen = mutt_strlen (home.mbox);
293 if (tlen && mutt_account_match (&home.account, &target.account) &&
294 !mutt_strncmp (home.mbox, target.mbox, hlen))
299 for (delim = ImapDelimChars; *delim != '\0'; delim++)
300 if (target.mbox[hlen] == *delim)
306 /* do the '=' substitution */
309 /* copy remaining path, skipping delimiter */
312 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
313 path[tlen - hlen - 1] = '\0';
317 mutt_account_tourl (&target.account, &url);
318 url.path = target.mbox;
319 /* FIXME: That hard-coded constant is bogus. But we need the actual
320 * size of the buffer from mutt_pretty_mailbox. And these pretty
321 * operations usually shrink the result. Still... */
322 url_ciss_tostring (&url, path, 1024, 0);
328 /* -- library functions -- */
330 /* imap_continue: display a message and ask the user if she wants to
332 int imap_continue (const char* msg, const char* resp)
334 imap_error (msg, resp);
335 return mutt_yesorno (_("Continue?"), 0);
338 /* imap_error: show an error and abort */
339 void imap_error (const char *where, const char *msg)
341 mutt_error ("%s [%s]\n", where, msg);
345 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
346 * Returns NULL on failure (no mem) */
347 IMAP_DATA* imap_new_idata (void)
349 IMAP_DATA* idata = safe_calloc (1, sizeof (IMAP_DATA));
354 if (!(idata->cmdbuf = mutt_buffer_init (NULL)))
360 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
361 void imap_free_idata (IMAP_DATA** idata)
366 FREE (&(*idata)->capstr);
367 mutt_free_list (&(*idata)->flags);
368 imap_mboxcache_free (*idata);
369 mutt_buffer_free(&(*idata)->cmdbuf);
370 FREE (&(*idata)->buf);
371 mutt_bcache_close (&(*idata)->bcache);
372 FREE (idata); /* __FREE_CHECKED__ */
376 * Fix up the imap path. This is necessary because the rest of mutt
377 * assumes a hierarchy delimiter of '/', which is not necessarily true
378 * in IMAP. Additionally, the filesystem converts multiple hierarchy
379 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
380 * are not required to do this.
381 * Moreover, IMAP servers may dislike the path ending with the delimiter.
383 char *imap_fix_path (IMAP_DATA *idata, char *mailbox, char *path,
388 while (mailbox && *mailbox && (x < (plen - 1)))
390 if ((*mailbox == '/') || (*mailbox == idata->delim))
392 while ((*mailbox == '/') || (*mailbox == idata->delim)) mailbox++;
393 path[x] = idata->delim;
402 if (x && path[--x] != idata->delim)
407 strfcpy (path, "INBOX", plen);
412 void imap_cachepath(IMAP_DATA* idata, const char* mailbox, char* dest,
416 const char* p = mailbox;
418 for (s = dest; p && *p && dlen; dlen--)
420 if (*p == idata->delim)
423 /* simple way to avoid collisions with UIDs */
424 if (*(p + 1) >= '0' && *(p + 1) <= '9')
438 /* imap_get_literal_count: write number of bytes in an IMAP literal into
439 * bytes, return 0 on success, -1 on failure. */
440 int imap_get_literal_count(const char *buf, long *bytes)
445 if (!buf || !(pc = strchr (buf, '{')))
450 while (isdigit ((unsigned char) *pc))
458 /* imap_get_qualifier: in a tagged response, skip tag and status for
459 * the qualifier message. Used by imap_copy_message for TRYCREATE */
460 char* imap_get_qualifier (char* buf)
465 s = imap_next_word (s);
466 /* skip OK/NO/BAD response */
467 s = imap_next_word (s);
472 /* imap_next_word: return index into string where next IMAP word begins */
473 char *imap_next_word (char *s)
485 quoted = quoted ? 0 : 1;
486 if (!quoted && ISSPACE (*s))
495 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
496 time_t imap_parse_date (char *s)
501 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
506 t.tm_mon = mutt_check_month (s);
511 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
518 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
523 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
528 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
535 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
536 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
540 return (mutt_mktime (&t, 0) + tz);
543 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
544 * and relative path. */
545 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
549 mutt_account_tourl (&mx->account, &url);
552 url_ciss_tostring (&url, dest, len, 0);
556 /* imap_quote_string: quote string according to IMAP rules:
557 * surround string with quotes, escape " and \ with \ */
558 void imap_quote_string (char *dest, size_t dlen, const char *src)
560 char quote[] = "\"\\", *pt;
567 /* save room for trailing quote-char */
570 for (; *s && dlen; s++)
572 if (strchr (quote, *s))
590 /* imap_unquote_string: equally stupid unquoting routine */
591 void imap_unquote_string (char *s)
622 * Quoting and UTF-7 conversion
625 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
629 buf = safe_strdup (src);
630 imap_utf7_encode (&buf);
632 imap_quote_string (dest, dlen, buf);
637 void imap_unmunge_mbox_name (char *s)
641 imap_unquote_string(s);
643 buf = safe_strdup (s);
646 imap_utf7_decode (&buf);
647 strncpy (s, buf, strlen (s));
653 /* imap_wordcasecmp: find word a in word list b */
654 int imap_wordcasecmp(const char *a, const char *b)
656 char tmp[SHORT_STRING];
660 tmp[SHORT_STRING-1] = 0;
661 for(i=0;i < SHORT_STRING-2;i++,s++)
663 if (!*s || ISSPACE(*s))
672 return ascii_strcasecmp(a, tmp);
676 * Imap keepalive: poll the current folder to keep the
681 static RETSIGTYPE alrm_handler (int sig)
686 void imap_keepalive (void)
692 conn = mutt_socket_head ();
695 if (conn->account.type == M_ACCT_TYPE_IMAP)
697 idata = (IMAP_DATA*) conn->data;
699 if (idata->state >= IMAP_AUTHENTICATED
700 && time(NULL) >= idata->lastread + ImapKeepalive)
706 ctx = safe_calloc (1, sizeof (CONTEXT));
709 imap_check_mailbox (ctx, NULL, 1);
719 int imap_wait_keepalive (pid_t pid)
721 struct sigaction oldalrm;
722 struct sigaction act;
726 short imap_passive = option (OPTIMAPPASSIVE);
728 set_option (OPTIMAPPASSIVE);
729 set_option (OPTKEEPQUIET);
731 sigprocmask (SIG_SETMASK, NULL, &oldmask);
733 sigemptyset (&act.sa_mask);
734 act.sa_handler = alrm_handler;
736 act.sa_flags = SA_INTERRUPT;
741 sigaction (SIGALRM, &act, &oldalrm);
743 alarm (ImapKeepalive);
744 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
746 alarm (0); /* cancel a possibly pending alarm */
748 alarm (ImapKeepalive);
751 alarm (0); /* cancel a possibly pending alarm */
753 sigaction (SIGALRM, &oldalrm, NULL);
754 sigprocmask (SIG_SETMASK, &oldmask, NULL);
756 unset_option (OPTKEEPQUIET);
758 unset_option (OPTIMAPPASSIVE);
763 /* Allow/disallow re-opening a folder upon expunge. */
765 void imap_allow_reopen (CONTEXT *ctx)
767 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
768 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
771 void imap_disallow_reopen (CONTEXT *ctx)
773 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
774 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;