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 */
14 #include <lib-lib/lib-lib.h>
17 #include <lib-mx/mx.h>
20 #include "imap_private.h"
21 #include <lib-sys/mutt_ssl.h>
23 /* -- public functions -- */
25 /* imap_parse_path: given an IMAP mailbox name, return host, port
26 * and a path IMAP servers will recognise.
27 * mx.mbox is malloc'd, caller must free it */
28 int imap_parse_path (const char *path, IMAP_MBOX * mx)
30 static unsigned short ImapPort = 0;
31 static unsigned short ImapsPort = 0;
32 struct servent *service;
37 service = getservbyname ("imap", "tcp");
39 ImapPort = ntohs (service->s_port);
44 service = getservbyname ("imaps", "tcp");
46 ImapsPort = ntohs (service->s_port);
48 ImapsPort = IMAP_SSL_PORT;
52 mx->account.flags = 0;
53 mx->account.port = ImapPort;
54 mx->account.type = M_ACCT_TYPE_IMAP;
57 url_parse_ciss (&url, c);
59 if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
60 mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host) {
65 mx->mbox = m_strdup(url.path);
67 if (url.scheme == U_IMAPS)
68 mx->account.flags |= M_ACCT_SSL;
72 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
73 mx->account.port = ImapsPort;
78 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
80 void imap_pretty_mailbox (char *path)
82 IMAP_MBOX home, target;
89 if (imap_parse_path (path, &target) < 0)
92 tlen = m_strlen(target.mbox);
93 /* check whether we can do '=' substitution */
94 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
95 hlen = m_strlen(home.mbox);
96 if (tlen && mutt_account_match (&home.account, &target.account) &&
97 !m_strncmp(home.mbox, target.mbox, hlen)) {
101 for (delim = ImapDelimChars; *delim != '\0'; delim++)
102 if (target.mbox[hlen] == *delim)
105 p_delete(&home.mbox);
108 /* do the '=' substitution */
111 /* copy remaining path, skipping delimiter */
114 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
115 path[tlen - hlen - 1] = '\0';
118 mutt_account_tourl (&target.account, &url);
119 url.path = target.mbox;
120 /* FIXME: That hard-coded constant is bogus. But we need the actual
121 * size of the buffer from mutt_pretty_mailbox. And these pretty
122 * operations usually shrink the result. Still... */
123 url_ciss_tostring (&url, path, 1024, 0);
126 p_delete(&target.mbox);
129 /* -- library functions -- */
131 /* imap_continue: display a message and ask the user if she wants to
133 int imap_continue (const char *msg, const char *resp)
135 imap_error (msg, resp);
136 return mutt_yesorno (_("Continue?"), 0);
139 /* imap_error: show an error and abort */
140 void imap_error (const char *where, const char *msg)
142 mutt_error ("%s [%s]\n", where, msg);
146 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
147 * Returns NULL on failure (no mem) */
148 IMAP_DATA *imap_new_idata (void)
150 return p_new(IMAP_DATA, 1);
153 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
154 void imap_free_idata (IMAP_DATA ** idata)
159 p_delete(&(*idata)->capstr);
160 string_list_wipe(&(*idata)->flags);
161 p_delete(&((*idata)->cmd.buf));
166 * Fix up the imap path. This is necessary because the rest of mutt
167 * assumes a hierarchy delimiter of '/', which is not necessarily true
168 * in IMAP. Additionally, the filesystem converts multiple hierarchy
169 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
170 * are not required to do this.
171 * Moreover, IMAP servers may dislike the path ending with the delimiter.
173 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
178 if (!mailbox || !*mailbox) {
179 m_strcpy(path, plen, "INBOX");
183 while (mailbox && *mailbox && (x < (plen - 1))) {
184 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
185 while ((*mailbox == '/') || (*mailbox == idata->delim))
187 path[x] = idata->delim;
195 if (x && path[--x] != idata->delim)
201 /* imap_get_literal_count: write number of bytes in an IMAP literal into
202 * bytes, return 0 on success, -1 on failure. */
203 int imap_get_literal_count (const char *buf, long *bytes)
208 if (!(pc = strchr (buf, '{')))
212 while (isdigit ((unsigned char) *pc))
219 /* imap_get_qualifier: in a tagged response, skip tag and status for
220 * the qualifier message. Used by imap_copy_message for TRYCREATE */
221 char *imap_get_qualifier (char *buf)
226 s = imap_next_word (s);
227 /* skip OK/NO/BAD response */
228 s = imap_next_word (s);
233 /* imap_next_word: return index into string where next IMAP word begins */
234 char *imap_next_word (char *s)
246 quoted = quoted ? 0 : 1;
247 if (!quoted && ISSPACE (*s))
252 return vskipspaces(s);
255 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
256 time_t imap_parse_date (char *s)
261 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
266 t.tm_mon = mutt_check_month (s);
272 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
281 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
286 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
291 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
298 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
299 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
303 return (mutt_mktime (&t, 0) + tz);
306 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
307 * and relative path. */
308 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
312 mutt_account_tourl (&mx->account, &url);
315 url_ciss_tostring (&url, dest, len, 0);
319 /* imap_quote_string: quote string according to IMAP rules:
320 * surround string with quotes, escape " and \ with \ */
321 void imap_quote_string (char *dest, size_t dlen, const char *src)
323 char quote[] = "\"\\", *pt;
330 /* save room for trailing quote-char */
333 for (; *s && dlen; s++) {
334 if (strchr (quote, *s)) {
350 /* imap_unquote_string: equally stupid unquoting routine */
351 void imap_unquote_string (char *s)
378 * Quoting and UTF-7 conversion
381 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
386 imap_utf7_encode (&buf);
388 imap_quote_string (dest, dlen, buf);
393 void imap_unmunge_mbox_name (char *s)
397 imap_unquote_string (s);
401 imap_utf7_decode (&buf);
402 m_strcpy(s, m_strlen(s) + 1, buf);
408 /* imap_wordcasecmp: find word a in word list b */
409 int imap_wordcasecmp (const char *a, const char *b)
411 char tmp[SHORT_STRING];
412 char *s = (char *) b;
415 tmp[SHORT_STRING - 1] = 0;
416 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
417 if (!*s || ISSPACE (*s)) {
425 return ascii_strcasecmp (a, tmp);
429 * Imap keepalive: poll the current folder to keep the
434 static RETSIGTYPE alrm_handler (int sig __attribute__((unused)))
439 void imap_keepalive (void)
445 conn = mutt_socket_head ();
447 if (conn->account.type == M_ACCT_TYPE_IMAP) {
448 idata = (IMAP_DATA *) conn->data;
450 if (idata->state >= IMAP_AUTHENTICATED
451 && time (NULL) >= idata->lastread + ImapKeepalive) {
455 ctx = p_new(CONTEXT, 1);
458 imap_check_mailbox (ctx, NULL, 1);
468 int imap_wait_keepalive (pid_t pid)
470 struct sigaction oldalrm;
471 struct sigaction act;
475 short imap_passive = option (OPTIMAPPASSIVE);
476 int imap_askreconnect = quadoption (OPT_IMAPRECONNECT);
478 set_option (OPTIMAPPASSIVE);
479 set_option (OPTKEEPQUIET);
480 set_quadoption (OPT_IMAPRECONNECT, M_NO);
482 sigprocmask (SIG_SETMASK, NULL, &oldmask);
484 sigemptyset (&act.sa_mask);
485 act.sa_handler = alrm_handler;
487 act.sa_flags = SA_INTERRUPT;
492 sigaction (SIGALRM, &act, &oldalrm);
494 alarm (ImapKeepalive);
495 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
496 alarm (0); /* cancel a possibly pending alarm */
498 alarm (ImapKeepalive);
501 alarm (0); /* cancel a possibly pending alarm */
503 sigaction (SIGALRM, &oldalrm, NULL);
504 sigprocmask (SIG_SETMASK, &oldmask, NULL);
506 unset_option (OPTKEEPQUIET);
508 unset_option (OPTIMAPPASSIVE);
509 set_quadoption (OPT_IMAPRECONNECT, imap_askreconnect);
514 /* Allow/disallow re-opening a folder upon expunge. */
516 void imap_allow_reopen (CONTEXT * ctx)
518 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
519 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
522 void imap_disallow_reopen (CONTEXT * ctx)
524 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
525 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;