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 */
16 #include <lib-lib/mem.h>
17 #include <lib-lib/ascii.h>
18 #include <lib-lib/url.h>
21 #include "mx.h" /* for M_IMAP */
22 #include "imap_private.h"
23 #include <lib-sys/mutt_ssl.h>
25 #include <lib-lib/macros.h>
30 #include <sys/types.h>
34 #include <netinet/in.h>
38 /* -- public functions -- */
40 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
41 * an IMAP path in canonical and absolute form.
42 * Inputs: a buffer containing an IMAP path, and the number of bytes in
44 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
45 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
46 * fails, which it might if there isn't enough room in the buffer. */
47 int imap_expand_path (char *path, size_t len)
53 if (imap_parse_path (path, &mx) < 0)
56 mutt_account_tourl (&mx.account, &url);
59 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
65 /* imap_parse_path: given an IMAP mailbox name, return host, port
66 * and a path IMAP servers will recognise.
67 * mx.mbox is malloc'd, caller must free it */
68 int imap_parse_path (const char *path, IMAP_MBOX * mx)
70 static unsigned short ImapPort = 0;
71 static unsigned short ImapsPort = 0;
72 struct servent *service;
77 service = getservbyname ("imap", "tcp");
79 ImapPort = ntohs (service->s_port);
84 service = getservbyname ("imaps", "tcp");
86 ImapsPort = ntohs (service->s_port);
88 ImapsPort = IMAP_SSL_PORT;
92 mx->account.flags = 0;
93 mx->account.port = ImapPort;
94 mx->account.type = M_ACCT_TYPE_IMAP;
97 url_parse_ciss (&url, c);
99 if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
100 mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host) {
105 mx->mbox = m_strdup(url.path);
107 if (url.scheme == U_IMAPS)
108 mx->account.flags |= M_ACCT_SSL;
112 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
113 mx->account.port = ImapsPort;
118 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
120 void imap_pretty_mailbox (char *path)
122 IMAP_MBOX home, target;
129 if (imap_parse_path (path, &target) < 0)
132 tlen = m_strlen(target.mbox);
133 /* check whether we can do '=' substitution */
134 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
135 hlen = m_strlen(home.mbox);
136 if (tlen && mutt_account_match (&home.account, &target.account) &&
137 !m_strncmp(home.mbox, target.mbox, hlen)) {
141 for (delim = ImapDelimChars; *delim != '\0'; delim++)
142 if (target.mbox[hlen] == *delim)
145 p_delete(&home.mbox);
148 /* do the '=' substitution */
151 /* copy remaining path, skipping delimiter */
154 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
155 path[tlen - hlen - 1] = '\0';
158 mutt_account_tourl (&target.account, &url);
159 url.path = target.mbox;
160 /* FIXME: That hard-coded constant is bogus. But we need the actual
161 * size of the buffer from mutt_pretty_mailbox. And these pretty
162 * operations usually shrink the result. Still... */
163 url_ciss_tostring (&url, path, 1024, 0);
166 p_delete(&target.mbox);
169 /* -- library functions -- */
171 /* imap_continue: display a message and ask the user if she wants to
173 int imap_continue (const char *msg, const char *resp)
175 imap_error (msg, resp);
176 return mutt_yesorno (_("Continue?"), 0);
179 /* imap_error: show an error and abort */
180 void imap_error (const char *where, const char *msg)
182 mutt_error ("%s [%s]\n", where, msg);
186 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
187 * Returns NULL on failure (no mem) */
188 IMAP_DATA *imap_new_idata (void)
190 return p_new(IMAP_DATA, 1);
193 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
194 void imap_free_idata (IMAP_DATA ** idata)
199 p_delete(&(*idata)->capstr);
200 string_list_wipe(&(*idata)->flags);
201 p_delete(&((*idata)->cmd.buf));
206 * Fix up the imap path. This is necessary because the rest of mutt
207 * assumes a hierarchy delimiter of '/', which is not necessarily true
208 * in IMAP. Additionally, the filesystem converts multiple hierarchy
209 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
210 * are not required to do this.
211 * Moreover, IMAP servers may dislike the path ending with the delimiter.
213 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
218 if (!mailbox || !*mailbox) {
219 m_strcpy(path, plen, "INBOX");
223 while (mailbox && *mailbox && (x < (plen - 1))) {
224 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
225 while ((*mailbox == '/') || (*mailbox == idata->delim))
227 path[x] = idata->delim;
235 if (x && path[--x] != idata->delim)
241 /* imap_get_literal_count: write number of bytes in an IMAP literal into
242 * bytes, return 0 on success, -1 on failure. */
243 int imap_get_literal_count (const char *buf, long *bytes)
248 if (!(pc = strchr (buf, '{')))
252 while (isdigit ((unsigned char) *pc))
259 /* imap_get_qualifier: in a tagged response, skip tag and status for
260 * the qualifier message. Used by imap_copy_message for TRYCREATE */
261 char *imap_get_qualifier (char *buf)
266 s = imap_next_word (s);
267 /* skip OK/NO/BAD response */
268 s = imap_next_word (s);
273 /* imap_next_word: return index into string where next IMAP word begins */
274 char *imap_next_word (char *s)
286 quoted = quoted ? 0 : 1;
287 if (!quoted && ISSPACE (*s))
292 return vskipspaces(s);
295 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
296 time_t imap_parse_date (char *s)
301 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
306 t.tm_mon = mutt_check_month (s);
312 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
321 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
326 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
331 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
338 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
339 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
343 return (mutt_mktime (&t, 0) + tz);
346 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
347 * and relative path. */
348 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
352 mutt_account_tourl (&mx->account, &url);
355 url_ciss_tostring (&url, dest, len, 0);
359 /* imap_quote_string: quote string according to IMAP rules:
360 * surround string with quotes, escape " and \ with \ */
361 void imap_quote_string (char *dest, size_t dlen, const char *src)
363 char quote[] = "\"\\", *pt;
370 /* save room for trailing quote-char */
373 for (; *s && dlen; s++) {
374 if (strchr (quote, *s)) {
390 /* imap_unquote_string: equally stupid unquoting routine */
391 void imap_unquote_string (char *s)
418 * Quoting and UTF-7 conversion
421 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
426 imap_utf7_encode (&buf);
428 imap_quote_string (dest, dlen, buf);
433 void imap_unmunge_mbox_name (char *s)
437 imap_unquote_string (s);
441 imap_utf7_decode (&buf);
442 m_strcpy(s, m_strlen(s) + 1, buf);
448 /* imap_wordcasecmp: find word a in word list b */
449 int imap_wordcasecmp (const char *a, const char *b)
451 char tmp[SHORT_STRING];
452 char *s = (char *) b;
455 tmp[SHORT_STRING - 1] = 0;
456 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
457 if (!*s || ISSPACE (*s)) {
465 return ascii_strcasecmp (a, tmp);
469 * Imap keepalive: poll the current folder to keep the
474 static RETSIGTYPE alrm_handler (int sig __attribute__((unused)))
479 void imap_keepalive (void)
485 conn = mutt_socket_head ();
487 if (conn->account.type == M_ACCT_TYPE_IMAP) {
488 idata = (IMAP_DATA *) conn->data;
490 if (idata->state >= IMAP_AUTHENTICATED
491 && time (NULL) >= idata->lastread + ImapKeepalive) {
495 ctx = p_new(CONTEXT, 1);
498 imap_check_mailbox (ctx, NULL, 1);
508 int imap_wait_keepalive (pid_t pid)
510 struct sigaction oldalrm;
511 struct sigaction act;
515 short imap_passive = option (OPTIMAPPASSIVE);
516 int imap_askreconnect = quadoption (OPT_IMAPRECONNECT);
518 set_option (OPTIMAPPASSIVE);
519 set_option (OPTKEEPQUIET);
520 set_quadoption (OPT_IMAPRECONNECT, M_NO);
522 sigprocmask (SIG_SETMASK, NULL, &oldmask);
524 sigemptyset (&act.sa_mask);
525 act.sa_handler = alrm_handler;
527 act.sa_flags = SA_INTERRUPT;
532 sigaction (SIGALRM, &act, &oldalrm);
534 alarm (ImapKeepalive);
535 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
536 alarm (0); /* cancel a possibly pending alarm */
538 alarm (ImapKeepalive);
541 alarm (0); /* cancel a possibly pending alarm */
543 sigaction (SIGALRM, &oldalrm, NULL);
544 sigprocmask (SIG_SETMASK, &oldmask, NULL);
546 unset_option (OPTKEEPQUIET);
548 unset_option (OPTIMAPPASSIVE);
549 set_quadoption (OPT_IMAPRECONNECT, imap_askreconnect);
554 /* Allow/disallow re-opening a folder upon expunge. */
556 void imap_allow_reopen (CONTEXT * ctx)
558 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
559 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
562 void imap_disallow_reopen (CONTEXT * ctx)
564 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
565 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;