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 */
24 #include "mx.h" /* for M_IMAP */
26 #include "imap_private.h"
32 #include <sys/types.h>
36 #include <netinet/in.h>
40 /* -- public functions -- */
42 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
43 * an IMAP path in canonical and absolute form.
44 * Inputs: a buffer containing an IMAP path, and the number of bytes in
46 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
47 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
48 * fails, which it might if there isn't enough room in the buffer. */
49 int imap_expand_path (char* path, size_t len)
55 if (imap_parse_path (path, &mx) < 0)
58 mutt_account_tourl (&mx.account, &url);
61 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
67 /* imap_parse_path: given an IMAP mailbox name, return host, port
68 * and a path IMAP servers will recognise.
69 * mx.mbox is malloc'd, caller must free it */
70 int imap_parse_path (const char* path, IMAP_MBOX* mx)
72 static unsigned short ImapPort = 0;
73 static unsigned short ImapsPort = 0;
74 struct servent* service;
82 service = getservbyname ("imap", "tcp");
84 ImapPort = ntohs (service->s_port);
87 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)
108 if (mutt_account_fromurl (&mx->account, &url) < 0)
114 mx->mbox = safe_strdup (url.path);
116 if (url.scheme == U_IMAPS)
117 mx->account.flags |= M_ACCT_SSL;
121 /* old PINE-compatibility code */
125 if (sscanf (path, "{%127[^}]}", tmp) != 1)
128 c = strchr (path, '}');
132 /* walk past closing '}' */
133 mx->mbox = safe_strdup (c+1);
135 if ((c = strrchr (tmp, '@')))
138 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
139 strfcpy (tmp, c+1, sizeof (tmp));
140 mx->account.flags |= M_ACCT_USER;
143 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
145 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
151 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
152 mx->account.flags |= M_ACCT_PORT;
153 if (sscanf (tmp, "/%s", tmp) == 1)
155 if (!ascii_strncmp (tmp, "ssl", 3))
156 mx->account.flags |= M_ACCT_SSL;
159 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
167 #if defined(USE_SSL) || defined(USE_GNUTLS)
168 if (option (OPTIMAPFORCESSL))
169 mx->account.flags |= M_ACCT_SSL;
172 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
173 mx->account.port = ImapsPort;
178 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
180 void imap_pretty_mailbox (char* path)
182 IMAP_MBOX home, target;
189 if (imap_parse_path (path, &target) < 0)
192 tlen = mutt_strlen (target.mbox);
193 /* check whether we can do '=' substitution */
194 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
196 hlen = mutt_strlen (home.mbox);
197 if (tlen && mutt_account_match (&home.account, &target.account) &&
198 !mutt_strncmp (home.mbox, target.mbox, hlen))
203 for (delim = ImapDelimChars; *delim != '\0'; delim++)
204 if (target.mbox[hlen] == *delim)
210 /* do the '=' substitution */
213 /* copy remaining path, skipping delimiter */
216 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
217 path[tlen - hlen - 1] = '\0';
221 mutt_account_tourl (&target.account, &url);
222 url.path = target.mbox;
223 /* FIXME: That hard-coded constant is bogus. But we need the actual
224 * size of the buffer from mutt_pretty_mailbox. And these pretty
225 * operations usually shrink the result. Still... */
226 url_ciss_tostring (&url, path, 1024, 0);
232 /* -- library functions -- */
234 /* imap_continue: display a message and ask the user if she wants to
236 int imap_continue (const char* msg, const char* resp)
238 imap_error (msg, resp);
239 return mutt_yesorno (_("Continue?"), 0);
242 /* imap_error: show an error and abort */
243 void imap_error (const char *where, const char *msg)
245 mutt_error ("%s [%s]\n", where, msg);
249 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
250 * Returns NULL on failure (no mem) */
251 IMAP_DATA* imap_new_idata (void) {
252 return safe_calloc (1, sizeof (IMAP_DATA));
255 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
256 void imap_free_idata (IMAP_DATA** idata) {
260 FREE (&(*idata)->capstr);
261 mutt_free_list (&(*idata)->flags);
262 FREE (&((*idata)->cmd.buf));
267 * Fix up the imap path. This is necessary because the rest of mutt
268 * assumes a hierarchy delimiter of '/', which is not necessarily true
269 * in IMAP. Additionally, the filesystem converts multiple hierarchy
270 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
271 * are not required to do this.
273 char *imap_fix_path (IMAP_DATA *idata, char *mailbox, char *path,
278 if (!mailbox || !*mailbox)
280 strfcpy (path, "INBOX", plen);
284 while (mailbox && *mailbox && (x < (plen - 1)))
286 if ((*mailbox == '/') || (*mailbox == idata->delim))
288 while ((*mailbox == '/') || (*mailbox == idata->delim)) mailbox++;
289 path[x] = idata->delim;
302 /* imap_get_literal_count: write number of bytes in an IMAP literal into
303 * bytes, return 0 on success, -1 on failure. */
304 int imap_get_literal_count(const char *buf, long *bytes)
309 if (!(pc = strchr (buf, '{')))
313 while (isdigit ((unsigned char) *pc))
320 /* imap_get_qualifier: in a tagged response, skip tag and status for
321 * the qualifier message. Used by imap_copy_message for TRYCREATE */
322 char* imap_get_qualifier (char* buf)
327 s = imap_next_word (s);
328 /* skip OK/NO/BAD response */
329 s = imap_next_word (s);
334 /* imap_next_word: return index into string where next IMAP word begins */
335 char *imap_next_word (char *s)
347 quoted = quoted ? 0 : 1;
348 if (!quoted && ISSPACE (*s))
357 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
358 time_t imap_parse_date (char *s)
363 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
368 t.tm_mon = mutt_check_month (s);
373 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
380 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
385 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
390 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
397 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
398 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
402 return (mutt_mktime (&t, 0) + tz);
405 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
406 * and relative path. */
407 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
411 mutt_account_tourl (&mx->account, &url);
414 url_ciss_tostring (&url, dest, len, 0);
418 /* imap_quote_string: quote string according to IMAP rules:
419 * surround string with quotes, escape " and \ with \ */
420 void imap_quote_string (char *dest, size_t dlen, const char *src)
422 char quote[] = "\"\\", *pt;
429 /* save room for trailing quote-char */
432 for (; *s && dlen; s++)
434 if (strchr (quote, *s))
452 /* imap_unquote_string: equally stupid unquoting routine */
453 void imap_unquote_string (char *s)
484 * Quoting and UTF-7 conversion
487 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
491 buf = safe_strdup (src);
492 imap_utf7_encode (&buf);
494 imap_quote_string (dest, dlen, buf);
499 void imap_unmunge_mbox_name (char *s)
503 imap_unquote_string(s);
505 buf = safe_strdup (s);
508 imap_utf7_decode (&buf);
509 strncpy (s, buf, strlen (s));
515 /* imap_wordcasecmp: find word a in word list b */
516 int imap_wordcasecmp(const char *a, const char *b)
518 char tmp[SHORT_STRING];
522 tmp[SHORT_STRING-1] = 0;
523 for(i=0;i < SHORT_STRING-2;i++,s++)
525 if (!*s || ISSPACE(*s))
534 return ascii_strcasecmp(a, tmp);
538 * Imap keepalive: poll the current folder to keep the
543 static RETSIGTYPE alrm_handler (int sig)
548 void imap_keepalive (void)
554 conn = mutt_socket_head ();
557 if (conn->account.type == M_ACCT_TYPE_IMAP)
559 idata = (IMAP_DATA*) conn->data;
561 if (idata->state >= IMAP_AUTHENTICATED
562 && time(NULL) >= idata->lastread + ImapKeepalive)
568 ctx = safe_calloc (1, sizeof (CONTEXT));
571 imap_check_mailbox (ctx, NULL, 1);
581 int imap_wait_keepalive (pid_t pid)
583 struct sigaction oldalrm;
584 struct sigaction act;
588 short imap_passive = option (OPTIMAPPASSIVE);
590 set_option (OPTIMAPPASSIVE);
591 set_option (OPTKEEPQUIET);
593 sigprocmask (SIG_SETMASK, NULL, &oldmask);
595 sigemptyset (&act.sa_mask);
596 act.sa_handler = alrm_handler;
598 act.sa_flags = SA_INTERRUPT;
603 sigaction (SIGALRM, &act, &oldalrm);
605 alarm (ImapKeepalive);
606 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
608 alarm (0); /* cancel a possibly pending alarm */
610 alarm (ImapKeepalive);
613 alarm (0); /* cancel a possibly pending alarm */
615 sigaction (SIGALRM, &oldalrm, NULL);
616 sigprocmask (SIG_SETMASK, &oldmask, NULL);
618 unset_option (OPTKEEPQUIET);
620 unset_option (OPTIMAPPASSIVE);
625 /* Allow/disallow re-opening a folder upon expunge. */
627 void imap_allow_reopen (CONTEXT *ctx)
629 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
630 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
633 void imap_disallow_reopen (CONTEXT *ctx)
635 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
636 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;