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"
25 #include <sys/types.h>
29 #include <netinet/in.h>
33 /* -- public functions -- */
35 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
36 * an IMAP path in canonical and absolute form.
37 * Inputs: a buffer containing an IMAP path, and the number of bytes in
39 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
40 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
41 * fails, which it might if there isn't enough room in the buffer. */
42 int imap_expand_path (char *path, size_t len)
48 if (imap_parse_path (path, &mx) < 0)
51 mutt_account_tourl (&mx.account, &url);
54 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
60 /* imap_parse_path: given an IMAP mailbox name, return host, port
61 * and a path IMAP servers will recognise.
62 * mx.mbox is malloc'd, caller must free it */
63 int imap_parse_path (const char *path, IMAP_MBOX * mx)
65 static unsigned short ImapPort = 0;
66 static unsigned short ImapsPort = 0;
67 struct servent *service;
74 service = getservbyname ("imap", "tcp");
76 ImapPort = ntohs (service->s_port);
79 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
82 service = getservbyname ("imaps", "tcp");
84 ImapsPort = ntohs (service->s_port);
86 ImapsPort = IMAP_SSL_PORT;
87 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
91 mx->account.flags = 0;
92 mx->account.port = ImapPort;
93 mx->account.type = M_ACCT_TYPE_IMAP;
95 c = safe_strdup (path);
96 url_parse_ciss (&url, c);
97 if (url.scheme == U_IMAP || url.scheme == U_IMAPS) {
98 if (mutt_account_fromurl (&mx->account, &url) < 0) {
103 mx->mbox = safe_strdup (url.path);
105 if (url.scheme == U_IMAPS)
106 mx->account.flags |= M_ACCT_SSL;
110 /* old PINE-compatibility code */
113 if (sscanf (path, "{%127[^}]}", tmp) != 1)
116 c = strchr (path, '}');
120 /* walk past closing '}' */
121 mx->mbox = safe_strdup (c + 1);
123 if ((c = strrchr (tmp, '@'))) {
125 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
126 strfcpy (tmp, c + 1, sizeof (tmp));
127 mx->account.flags |= M_ACCT_USER;
130 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1) {
131 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
137 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
138 mx->account.flags |= M_ACCT_PORT;
139 if (sscanf (tmp, "/%s", tmp) == 1) {
140 if (!ascii_strncmp (tmp, "ssl", 3))
141 mx->account.flags |= M_ACCT_SSL;
145 "imap_parse_path: Unknown connection type in %s\n", path));
153 #if defined(USE_SSL) || defined(USE_GNUTLS)
154 if (option (OPTIMAPFORCESSL))
155 mx->account.flags |= M_ACCT_SSL;
158 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
159 mx->account.port = ImapsPort;
164 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
166 void imap_pretty_mailbox (char *path)
168 IMAP_MBOX home, target;
175 if (imap_parse_path (path, &target) < 0)
178 tlen = mutt_strlen (target.mbox);
179 /* check whether we can do '=' substitution */
180 if (mx_is_imap (Maildir) && !imap_parse_path (Maildir, &home)) {
181 hlen = mutt_strlen (home.mbox);
182 if (tlen && mutt_account_match (&home.account, &target.account) &&
183 !mutt_strncmp (home.mbox, target.mbox, hlen)) {
187 for (delim = ImapDelimChars; *delim != '\0'; delim++)
188 if (target.mbox[hlen] == *delim)
194 /* do the '=' substitution */
197 /* copy remaining path, skipping delimiter */
200 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
201 path[tlen - hlen - 1] = '\0';
204 mutt_account_tourl (&target.account, &url);
205 url.path = target.mbox;
206 /* FIXME: That hard-coded constant is bogus. But we need the actual
207 * size of the buffer from mutt_pretty_mailbox. And these pretty
208 * operations usually shrink the result. Still... */
209 url_ciss_tostring (&url, path, 1024, 0);
215 /* -- library functions -- */
217 /* imap_continue: display a message and ask the user if she wants to
219 int imap_continue (const char *msg, const char *resp)
221 imap_error (msg, resp);
222 return mutt_yesorno (_("Continue?"), 0);
225 /* imap_error: show an error and abort */
226 void imap_error (const char *where, const char *msg)
228 mutt_error ("%s [%s]\n", where, msg);
232 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
233 * Returns NULL on failure (no mem) */
234 IMAP_DATA *imap_new_idata (void)
236 return safe_calloc (1, sizeof (IMAP_DATA));
239 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
240 void imap_free_idata (IMAP_DATA ** idata)
245 FREE (&(*idata)->capstr);
246 mutt_free_list (&(*idata)->flags);
247 FREE (&((*idata)->cmd.buf));
252 * Fix up the imap path. This is necessary because the rest of mutt
253 * assumes a hierarchy delimiter of '/', which is not necessarily true
254 * in IMAP. Additionally, the filesystem converts multiple hierarchy
255 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
256 * are not required to do this.
257 * Moreover, IMAP servers may dislike the path ending with the delimiter.
259 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
264 if (!mailbox || !*mailbox) {
265 strfcpy (path, "INBOX", plen);
269 while (mailbox && *mailbox && (x < (plen - 1))) {
270 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
271 while ((*mailbox == '/') || (*mailbox == idata->delim))
273 path[x] = idata->delim;
281 if (x && path[--x] != idata->delim)
287 /* imap_get_literal_count: write number of bytes in an IMAP literal into
288 * bytes, return 0 on success, -1 on failure. */
289 int imap_get_literal_count (const char *buf, long *bytes)
294 if (!(pc = strchr (buf, '{')))
298 while (isdigit ((unsigned char) *pc))
305 /* imap_get_qualifier: in a tagged response, skip tag and status for
306 * the qualifier message. Used by imap_copy_message for TRYCREATE */
307 char *imap_get_qualifier (char *buf)
312 s = imap_next_word (s);
313 /* skip OK/NO/BAD response */
314 s = imap_next_word (s);
319 /* imap_next_word: return index into string where next IMAP word begins */
320 char *imap_next_word (char *s)
332 quoted = quoted ? 0 : 1;
333 if (!quoted && ISSPACE (*s))
342 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
343 time_t imap_parse_date (char *s)
348 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
353 t.tm_mon = mutt_check_month (s);
359 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
368 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
373 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
378 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
385 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
386 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
390 return (mutt_mktime (&t, 0) + tz);
393 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
394 * and relative path. */
395 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
399 mutt_account_tourl (&mx->account, &url);
402 url_ciss_tostring (&url, dest, len, 0);
406 /* imap_quote_string: quote string according to IMAP rules:
407 * surround string with quotes, escape " and \ with \ */
408 void imap_quote_string (char *dest, size_t dlen, const char *src)
410 char quote[] = "\"\\", *pt;
417 /* save room for trailing quote-char */
420 for (; *s && dlen; s++) {
421 if (strchr (quote, *s)) {
437 /* imap_unquote_string: equally stupid unquoting routine */
438 void imap_unquote_string (char *s)
465 * Quoting and UTF-7 conversion
468 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
472 buf = safe_strdup (src);
473 imap_utf7_encode (&buf);
475 imap_quote_string (dest, dlen, buf);
480 void imap_unmunge_mbox_name (char *s)
484 imap_unquote_string (s);
486 buf = safe_strdup (s);
488 imap_utf7_decode (&buf);
489 strncpy (s, buf, mutt_strlen (s));
495 /* imap_wordcasecmp: find word a in word list b */
496 int imap_wordcasecmp (const char *a, const char *b)
498 char tmp[SHORT_STRING];
499 char *s = (char *) b;
502 tmp[SHORT_STRING - 1] = 0;
503 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
504 if (!*s || ISSPACE (*s)) {
512 return ascii_strcasecmp (a, tmp);
516 * Imap keepalive: poll the current folder to keep the
521 static RETSIGTYPE alrm_handler (int sig)
526 void imap_keepalive (void)
532 conn = mutt_socket_head ();
534 if (conn->account.type == M_ACCT_TYPE_IMAP) {
535 idata = (IMAP_DATA *) conn->data;
537 if (idata->state >= IMAP_AUTHENTICATED
538 && time (NULL) >= idata->lastread + ImapKeepalive) {
542 ctx = safe_calloc (1, sizeof (CONTEXT));
545 imap_check_mailbox (ctx, NULL, 1);
555 int imap_wait_keepalive (pid_t pid)
557 struct sigaction oldalrm;
558 struct sigaction act;
562 short imap_passive = option (OPTIMAPPASSIVE);
564 set_option (OPTIMAPPASSIVE);
565 set_option (OPTKEEPQUIET);
567 sigprocmask (SIG_SETMASK, NULL, &oldmask);
569 sigemptyset (&act.sa_mask);
570 act.sa_handler = alrm_handler;
572 act.sa_flags = SA_INTERRUPT;
577 sigaction (SIGALRM, &act, &oldalrm);
579 alarm (ImapKeepalive);
580 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
581 alarm (0); /* cancel a possibly pending alarm */
583 alarm (ImapKeepalive);
586 alarm (0); /* cancel a possibly pending alarm */
588 sigaction (SIGALRM, &oldalrm, NULL);
589 sigprocmask (SIG_SETMASK, &oldmask, NULL);
591 unset_option (OPTKEEPQUIET);
593 unset_option (OPTIMAPPASSIVE);
598 /* Allow/disallow re-opening a folder upon expunge. */
600 void imap_allow_reopen (CONTEXT * ctx)
602 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
603 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
606 void imap_disallow_reopen (CONTEXT * ctx)
608 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
609 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;