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-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 /* general IMAP utility functions */
26 #include "mx.h" /* for M_IMAP */
28 #include "imap_private.h"
34 #include <sys/types.h>
38 #include <netinet/in.h>
42 /* -- public functions -- */
44 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
45 * an IMAP path in canonical and absolute form.
46 * Inputs: a buffer containing an IMAP path, and the number of bytes in
48 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
49 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
50 * fails, which it might if there isn't enough room in the buffer. */
51 int imap_expand_path (char *path, size_t len)
57 if (imap_parse_path (path, &mx) < 0)
60 mutt_account_tourl (&mx.account, &url);
63 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
69 /* imap_parse_path: given an IMAP mailbox name, return host, port
70 * and a path IMAP servers will recognise.
71 * mx.mbox is malloc'd, caller must free it */
72 int imap_parse_path (const char *path, IMAP_MBOX * mx)
74 static unsigned short ImapPort = 0;
75 static unsigned short ImapsPort = 0;
76 struct servent *service;
83 service = getservbyname ("imap", "tcp");
85 ImapPort = ntohs (service->s_port);
88 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
91 service = getservbyname ("imaps", "tcp");
93 ImapsPort = ntohs (service->s_port);
95 ImapsPort = IMAP_SSL_PORT;
96 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
100 mx->account.flags = 0;
101 mx->account.port = ImapPort;
102 mx->account.type = M_ACCT_TYPE_IMAP;
104 c = safe_strdup (path);
105 url_parse_ciss (&url, c);
106 if (url.scheme == U_IMAP || url.scheme == U_IMAPS) {
107 if (mutt_account_fromurl (&mx->account, &url) < 0) {
112 mx->mbox = safe_strdup (url.path);
114 if (url.scheme == U_IMAPS)
115 mx->account.flags |= M_ACCT_SSL;
119 /* old PINE-compatibility code */
122 if (sscanf (path, "{%127[^}]}", tmp) != 1)
125 c = strchr (path, '}');
129 /* walk past closing '}' */
130 mx->mbox = safe_strdup (c + 1);
132 if ((c = strrchr (tmp, '@'))) {
134 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
135 strfcpy (tmp, c + 1, sizeof (tmp));
136 mx->account.flags |= M_ACCT_USER;
139 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1) {
140 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
146 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
147 mx->account.flags |= M_ACCT_PORT;
148 if (sscanf (tmp, "/%s", tmp) == 1) {
149 if (!ascii_strncmp (tmp, "ssl", 3))
150 mx->account.flags |= M_ACCT_SSL;
154 "imap_parse_path: Unknown connection type in %s\n", path));
162 #if defined(USE_SSL) || defined(USE_GNUTLS)
163 if (option (OPTIMAPFORCESSL))
164 mx->account.flags |= M_ACCT_SSL;
167 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
168 mx->account.port = ImapsPort;
173 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
175 void imap_pretty_mailbox (char *path)
177 IMAP_MBOX home, target;
184 if (imap_parse_path (path, &target) < 0)
187 tlen = mutt_strlen (target.mbox);
188 /* check whether we can do '=' substitution */
189 if (mx_is_imap (Maildir) && !imap_parse_path (Maildir, &home)) {
190 hlen = mutt_strlen (home.mbox);
191 if (tlen && mutt_account_match (&home.account, &target.account) &&
192 !mutt_strncmp (home.mbox, target.mbox, hlen)) {
196 for (delim = ImapDelimChars; *delim != '\0'; delim++)
197 if (target.mbox[hlen] == *delim)
203 /* do the '=' substitution */
206 /* copy remaining path, skipping delimiter */
209 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
210 path[tlen - hlen - 1] = '\0';
213 mutt_account_tourl (&target.account, &url);
214 url.path = target.mbox;
215 /* FIXME: That hard-coded constant is bogus. But we need the actual
216 * size of the buffer from mutt_pretty_mailbox. And these pretty
217 * operations usually shrink the result. Still... */
218 url_ciss_tostring (&url, path, 1024, 0);
224 /* -- library functions -- */
226 /* imap_continue: display a message and ask the user if she wants to
228 int imap_continue (const char *msg, const char *resp)
230 imap_error (msg, resp);
231 return mutt_yesorno (_("Continue?"), 0);
234 /* imap_error: show an error and abort */
235 void imap_error (const char *where, const char *msg)
237 mutt_error ("%s [%s]\n", where, msg);
241 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
242 * Returns NULL on failure (no mem) */
243 IMAP_DATA *imap_new_idata (void)
245 return safe_calloc (1, sizeof (IMAP_DATA));
248 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
249 void imap_free_idata (IMAP_DATA ** idata)
254 FREE (&(*idata)->capstr);
255 mutt_free_list (&(*idata)->flags);
256 FREE (&((*idata)->cmd.buf));
261 * Fix up the imap path. This is necessary because the rest of mutt
262 * assumes a hierarchy delimiter of '/', which is not necessarily true
263 * in IMAP. Additionally, the filesystem converts multiple hierarchy
264 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
265 * are not required to do this.
266 * Moreover, IMAP servers may dislike the path ending with the delimiter.
268 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
273 if (!mailbox || !*mailbox) {
274 strfcpy (path, "INBOX", plen);
278 while (mailbox && *mailbox && (x < (plen - 1))) {
279 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
280 while ((*mailbox == '/') || (*mailbox == idata->delim))
282 path[x] = idata->delim;
290 if (x && path[--x] != idata->delim)
296 /* imap_get_literal_count: write number of bytes in an IMAP literal into
297 * bytes, return 0 on success, -1 on failure. */
298 int imap_get_literal_count (const char *buf, long *bytes)
303 if (!(pc = strchr (buf, '{')))
307 while (isdigit ((unsigned char) *pc))
314 /* imap_get_qualifier: in a tagged response, skip tag and status for
315 * the qualifier message. Used by imap_copy_message for TRYCREATE */
316 char *imap_get_qualifier (char *buf)
321 s = imap_next_word (s);
322 /* skip OK/NO/BAD response */
323 s = imap_next_word (s);
328 /* imap_next_word: return index into string where next IMAP word begins */
329 char *imap_next_word (char *s)
341 quoted = quoted ? 0 : 1;
342 if (!quoted && ISSPACE (*s))
351 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
352 time_t imap_parse_date (char *s)
357 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
362 t.tm_mon = mutt_check_month (s);
368 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
377 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
382 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
387 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
394 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
395 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
399 return (mutt_mktime (&t, 0) + tz);
402 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
403 * and relative path. */
404 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
408 mutt_account_tourl (&mx->account, &url);
411 url_ciss_tostring (&url, dest, len, 0);
415 /* imap_quote_string: quote string according to IMAP rules:
416 * surround string with quotes, escape " and \ with \ */
417 void imap_quote_string (char *dest, size_t dlen, const char *src)
419 char quote[] = "\"\\", *pt;
426 /* save room for trailing quote-char */
429 for (; *s && dlen; s++) {
430 if (strchr (quote, *s)) {
446 /* imap_unquote_string: equally stupid unquoting routine */
447 void imap_unquote_string (char *s)
474 * Quoting and UTF-7 conversion
477 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
481 buf = safe_strdup (src);
482 imap_utf7_encode (&buf);
484 imap_quote_string (dest, dlen, buf);
489 void imap_unmunge_mbox_name (char *s)
493 imap_unquote_string (s);
495 buf = safe_strdup (s);
497 imap_utf7_decode (&buf);
498 strncpy (s, buf, strlen (s));
504 /* imap_wordcasecmp: find word a in word list b */
505 int imap_wordcasecmp (const char *a, const char *b)
507 char tmp[SHORT_STRING];
508 char *s = (char *) b;
511 tmp[SHORT_STRING - 1] = 0;
512 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
513 if (!*s || ISSPACE (*s)) {
521 return ascii_strcasecmp (a, tmp);
525 * Imap keepalive: poll the current folder to keep the
530 static RETSIGTYPE alrm_handler (int sig)
535 void imap_keepalive (void)
541 conn = mutt_socket_head ();
543 if (conn->account.type == M_ACCT_TYPE_IMAP) {
544 idata = (IMAP_DATA *) conn->data;
546 if (idata->state >= IMAP_AUTHENTICATED
547 && time (NULL) >= idata->lastread + ImapKeepalive) {
551 ctx = safe_calloc (1, sizeof (CONTEXT));
554 imap_check_mailbox (ctx, NULL, 1);
564 int imap_wait_keepalive (pid_t pid)
566 struct sigaction oldalrm;
567 struct sigaction act;
571 short imap_passive = option (OPTIMAPPASSIVE);
573 set_option (OPTIMAPPASSIVE);
574 set_option (OPTKEEPQUIET);
576 sigprocmask (SIG_SETMASK, NULL, &oldmask);
578 sigemptyset (&act.sa_mask);
579 act.sa_handler = alrm_handler;
581 act.sa_flags = SA_INTERRUPT;
586 sigaction (SIGALRM, &act, &oldalrm);
588 alarm (ImapKeepalive);
589 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
590 alarm (0); /* cancel a possibly pending alarm */
592 alarm (ImapKeepalive);
595 alarm (0); /* cancel a possibly pending alarm */
597 sigaction (SIGALRM, &oldalrm, NULL);
598 sigprocmask (SIG_SETMASK, &oldmask, NULL);
600 unset_option (OPTKEEPQUIET);
602 unset_option (OPTIMAPPASSIVE);
607 /* Allow/disallow re-opening a folder upon expunge. */
609 void imap_allow_reopen (CONTEXT * ctx)
611 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
612 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
615 void imap_disallow_reopen (CONTEXT * ctx)
617 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
618 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;