2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
5 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
7 * This file is part of mutt-ng, see http://www.muttng.org/.
8 * It's licensed under the GNU General Public License,
9 * please see the file GPL in the top level source directory.
12 /* general IMAP utility functions */
17 #include "mx.h" /* for M_IMAP */
19 #include "imap_private.h"
28 #include <sys/types.h>
32 #include <netinet/in.h>
36 /* -- public functions -- */
38 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
39 * an IMAP path in canonical and absolute form.
40 * Inputs: a buffer containing an IMAP path, and the number of bytes in
42 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
43 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
44 * fails, which it might if there isn't enough room in the buffer. */
45 int imap_expand_path (char *path, size_t len)
51 if (imap_parse_path (path, &mx) < 0)
54 mutt_account_tourl (&mx.account, &url);
57 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
63 /* imap_parse_path: given an IMAP mailbox name, return host, port
64 * and a path IMAP servers will recognise.
65 * mx.mbox is malloc'd, caller must free it */
66 int imap_parse_path (const char *path, IMAP_MBOX * mx)
68 static unsigned short ImapPort = 0;
69 static unsigned short ImapsPort = 0;
70 struct servent *service;
77 service = getservbyname ("imap", "tcp");
79 ImapPort = ntohs (service->s_port);
82 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
85 service = getservbyname ("imaps", "tcp");
87 ImapsPort = ntohs (service->s_port);
89 ImapsPort = IMAP_SSL_PORT;
90 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
94 mx->account.flags = 0;
95 mx->account.port = ImapPort;
96 mx->account.type = M_ACCT_TYPE_IMAP;
98 c = safe_strdup (path);
99 url_parse_ciss (&url, c);
100 if (url.scheme == U_IMAP || url.scheme == U_IMAPS) {
101 if (mutt_account_fromurl (&mx->account, &url) < 0) {
106 mx->mbox = safe_strdup (url.path);
108 if (url.scheme == U_IMAPS)
109 mx->account.flags |= M_ACCT_SSL;
113 /* old PINE-compatibility code */
116 if (sscanf (path, "{%127[^}]}", tmp) != 1)
119 c = strchr (path, '}');
123 /* walk past closing '}' */
124 mx->mbox = safe_strdup (c + 1);
126 if ((c = strrchr (tmp, '@'))) {
128 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
129 strfcpy (tmp, c + 1, sizeof (tmp));
130 mx->account.flags |= M_ACCT_USER;
133 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1) {
134 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
140 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
141 mx->account.flags |= M_ACCT_PORT;
142 if (sscanf (tmp, "/%s", tmp) == 1) {
143 if (!ascii_strncmp (tmp, "ssl", 3))
144 mx->account.flags |= M_ACCT_SSL;
148 "imap_parse_path: Unknown connection type in %s\n", path));
156 #if defined(USE_SSL) || defined(USE_GNUTLS)
157 if (option (OPTIMAPFORCESSL))
158 mx->account.flags |= M_ACCT_SSL;
161 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
162 mx->account.port = ImapsPort;
167 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
169 void imap_pretty_mailbox (char *path)
171 IMAP_MBOX home, target;
178 if (imap_parse_path (path, &target) < 0)
181 tlen = safe_strlen (target.mbox);
182 /* check whether we can do '=' substitution */
183 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
184 hlen = safe_strlen (home.mbox);
185 if (tlen && mutt_account_match (&home.account, &target.account) &&
186 !safe_strncmp (home.mbox, target.mbox, hlen)) {
190 for (delim = ImapDelimChars; *delim != '\0'; delim++)
191 if (target.mbox[hlen] == *delim)
197 /* do the '=' substitution */
200 /* copy remaining path, skipping delimiter */
203 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
204 path[tlen - hlen - 1] = '\0';
207 mutt_account_tourl (&target.account, &url);
208 url.path = target.mbox;
209 /* FIXME: That hard-coded constant is bogus. But we need the actual
210 * size of the buffer from mutt_pretty_mailbox. And these pretty
211 * operations usually shrink the result. Still... */
212 url_ciss_tostring (&url, path, 1024, 0);
218 /* -- library functions -- */
220 /* imap_continue: display a message and ask the user if she wants to
222 int imap_continue (const char *msg, const char *resp)
224 imap_error (msg, resp);
225 return mutt_yesorno (_("Continue?"), 0);
228 /* imap_error: show an error and abort */
229 void imap_error (const char *where, const char *msg)
231 mutt_error ("%s [%s]\n", where, msg);
235 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
236 * Returns NULL on failure (no mem) */
237 IMAP_DATA *imap_new_idata (void)
239 return safe_calloc (1, sizeof (IMAP_DATA));
242 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
243 void imap_free_idata (IMAP_DATA ** idata)
248 FREE (&(*idata)->capstr);
249 mutt_free_list (&(*idata)->flags);
250 FREE (&((*idata)->cmd.buf));
255 * Fix up the imap path. This is necessary because the rest of mutt
256 * assumes a hierarchy delimiter of '/', which is not necessarily true
257 * in IMAP. Additionally, the filesystem converts multiple hierarchy
258 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
259 * are not required to do this.
260 * Moreover, IMAP servers may dislike the path ending with the delimiter.
262 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
267 if (!mailbox || !*mailbox) {
268 strfcpy (path, "INBOX", plen);
272 while (mailbox && *mailbox && (x < (plen - 1))) {
273 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
274 while ((*mailbox == '/') || (*mailbox == idata->delim))
276 path[x] = idata->delim;
284 if (x && path[--x] != idata->delim)
290 /* imap_get_literal_count: write number of bytes in an IMAP literal into
291 * bytes, return 0 on success, -1 on failure. */
292 int imap_get_literal_count (const char *buf, long *bytes)
297 if (!(pc = strchr (buf, '{')))
301 while (isdigit ((unsigned char) *pc))
308 /* imap_get_qualifier: in a tagged response, skip tag and status for
309 * the qualifier message. Used by imap_copy_message for TRYCREATE */
310 char *imap_get_qualifier (char *buf)
315 s = imap_next_word (s);
316 /* skip OK/NO/BAD response */
317 s = imap_next_word (s);
322 /* imap_next_word: return index into string where next IMAP word begins */
323 char *imap_next_word (char *s)
335 quoted = quoted ? 0 : 1;
336 if (!quoted && ISSPACE (*s))
345 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
346 time_t imap_parse_date (char *s)
351 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
356 t.tm_mon = mutt_check_month (s);
362 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
371 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
376 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
381 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
388 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
389 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
393 return (mutt_mktime (&t, 0) + tz);
396 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
397 * and relative path. */
398 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
402 mutt_account_tourl (&mx->account, &url);
405 url_ciss_tostring (&url, dest, len, 0);
409 /* imap_quote_string: quote string according to IMAP rules:
410 * surround string with quotes, escape " and \ with \ */
411 void imap_quote_string (char *dest, size_t dlen, const char *src)
413 char quote[] = "\"\\", *pt;
420 /* save room for trailing quote-char */
423 for (; *s && dlen; s++) {
424 if (strchr (quote, *s)) {
440 /* imap_unquote_string: equally stupid unquoting routine */
441 void imap_unquote_string (char *s)
468 * Quoting and UTF-7 conversion
471 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
475 buf = safe_strdup (src);
476 imap_utf7_encode (&buf);
478 imap_quote_string (dest, dlen, buf);
483 void imap_unmunge_mbox_name (char *s)
487 imap_unquote_string (s);
489 buf = safe_strdup (s);
491 imap_utf7_decode (&buf);
492 strncpy (s, buf, safe_strlen (s));
498 /* imap_wordcasecmp: find word a in word list b */
499 int imap_wordcasecmp (const char *a, const char *b)
501 char tmp[SHORT_STRING];
502 char *s = (char *) b;
505 tmp[SHORT_STRING - 1] = 0;
506 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
507 if (!*s || ISSPACE (*s)) {
515 return ascii_strcasecmp (a, tmp);
519 * Imap keepalive: poll the current folder to keep the
524 static RETSIGTYPE alrm_handler (int sig)
529 void imap_keepalive (void)
535 conn = mutt_socket_head ();
537 if (conn->account.type == M_ACCT_TYPE_IMAP) {
538 idata = (IMAP_DATA *) conn->data;
540 if (idata->state >= IMAP_AUTHENTICATED
541 && time (NULL) >= idata->lastread + ImapKeepalive) {
545 ctx = safe_calloc (1, sizeof (CONTEXT));
548 imap_check_mailbox (ctx, NULL, 1);
558 int imap_wait_keepalive (pid_t pid)
560 struct sigaction oldalrm;
561 struct sigaction act;
565 short imap_passive = option (OPTIMAPPASSIVE);
567 set_option (OPTIMAPPASSIVE);
568 set_option (OPTKEEPQUIET);
570 sigprocmask (SIG_SETMASK, NULL, &oldmask);
572 sigemptyset (&act.sa_mask);
573 act.sa_handler = alrm_handler;
575 act.sa_flags = SA_INTERRUPT;
580 sigaction (SIGALRM, &act, &oldalrm);
582 alarm (ImapKeepalive);
583 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
584 alarm (0); /* cancel a possibly pending alarm */
586 alarm (ImapKeepalive);
589 alarm (0); /* cancel a possibly pending alarm */
591 sigaction (SIGALRM, &oldalrm, NULL);
592 sigprocmask (SIG_SETMASK, &oldmask, NULL);
594 unset_option (OPTKEEPQUIET);
596 unset_option (OPTIMAPPASSIVE);
601 /* Allow/disallow re-opening a folder upon expunge. */
603 void imap_allow_reopen (CONTEXT * ctx)
605 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
606 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
609 void imap_disallow_reopen (CONTEXT * ctx)
611 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
612 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;