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"
22 /* -- public functions -- */
24 /* imap_parse_path: given an IMAP mailbox name, return host, port
25 * and a path IMAP servers will recognise.
26 * mx.mbox is malloc'd, caller must free it */
27 int imap_parse_path (const char *path, IMAP_MBOX * mx)
29 static unsigned short ImapPort = 0;
30 static unsigned short ImapsPort = 0;
31 struct servent *service;
36 service = getservbyname ("imap", "tcp");
38 ImapPort = ntohs (service->s_port);
43 service = getservbyname ("imaps", "tcp");
45 ImapsPort = ntohs (service->s_port);
47 ImapsPort = IMAP_SSL_PORT;
51 mx->account.flags = 0;
52 mx->account.port = ImapPort;
53 mx->account.type = M_ACCT_TYPE_IMAP;
56 url_parse_ciss (&url, c);
58 if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
59 mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host) {
64 mx->mbox = m_strdup(url.path);
66 if (url.scheme == U_IMAPS)
67 mx->account.flags |= M_ACCT_SSL;
71 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
72 mx->account.port = ImapsPort;
77 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
79 void imap_pretty_mailbox (char *path)
81 IMAP_MBOX home, target;
88 if (imap_parse_path (path, &target) < 0)
91 tlen = m_strlen(target.mbox);
92 /* check whether we can do '=' substitution */
93 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
94 hlen = m_strlen(home.mbox);
95 if (tlen && mutt_account_match (&home.account, &target.account) &&
96 !m_strncmp(home.mbox, target.mbox, hlen)) {
100 for (delim = ImapDelimChars; *delim != '\0'; delim++)
101 if (target.mbox[hlen] == *delim)
104 p_delete(&home.mbox);
107 /* do the '=' substitution */
110 /* copy remaining path, skipping delimiter */
113 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
114 path[tlen - hlen - 1] = '\0';
117 mutt_account_tourl (&target.account, &url);
118 url.path = target.mbox;
119 /* FIXME: That hard-coded constant is bogus. But we need the actual
120 * size of the buffer from mutt_pretty_mailbox. And these pretty
121 * operations usually shrink the result. Still... */
122 url_ciss_tostring (&url, path, 1024, 0);
125 p_delete(&target.mbox);
128 /* -- library functions -- */
130 /* imap_continue: display a message and ask the user if she wants to
132 int imap_continue (const char *msg, const char *resp)
134 imap_error (msg, resp);
135 return mutt_yesorno (_("Continue?"), 0);
138 /* imap_error: show an error and abort */
139 void imap_error (const char *where, const char *msg)
141 mutt_error ("%s [%s]\n", where, msg);
145 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
146 * Returns NULL on failure (no mem) */
147 IMAP_DATA *imap_new_idata (void)
149 IMAP_DATA *res = p_new(IMAP_DATA, 1);
150 buffer_init(&res->cmd.buf);
154 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
155 void imap_free_idata (IMAP_DATA ** idata)
158 p_delete(&(*idata)->capstr);
159 string_list_wipe(&(*idata)->flags);
160 buffer_wipe(&((*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 = strtol(s, &s, 10);
264 t.tm_mon = mutt_check_month(s);
268 t.tm_year = strtol(s, &s, 10) - 1900;
273 t.tm_hour = strtol(s, &s, 10);
276 t.tm_min = strtol(s, &s, 10);
279 t.tm_sec = strtol(s, &s, 10);
284 tz = strtol(s + 1, NULL, 10);
285 tz = (tz / 100) * 3600 + (tz % 100) * 60;
289 return (mutt_mktime (&t, 0) + tz);
292 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
293 * and relative path. */
294 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
298 mutt_account_tourl (&mx->account, &url);
301 url_ciss_tostring (&url, dest, len, 0);
305 /* imap_quote_string: quote string according to IMAP rules:
306 * surround string with quotes, escape " and \ with \ */
307 void imap_quote_string (char *dest, size_t dlen, const char *src)
309 char quote[] = "\"\\", *pt;
316 /* save room for trailing quote-char */
319 for (; *s && dlen; s++) {
320 if (strchr (quote, *s)) {
336 /* imap_unquote_string: equally stupid unquoting routine */
337 void imap_unquote_string (char *s)
364 * Quoting and UTF-7 conversion
367 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
372 imap_utf7_encode (&buf);
374 imap_quote_string (dest, dlen, buf);
379 void imap_unmunge_mbox_name (char *s)
383 imap_unquote_string (s);
387 imap_utf7_decode (&buf);
388 m_strcpy(s, m_strlen(s) + 1, buf);
394 /* imap_wordcasecmp: find word a in word list b */
395 int imap_wordcasecmp (const char *a, const char *b)
398 char *s = (char *) b;
402 for (i = 0; i < STRING - 2; i++, s++) {
403 if (!*s || ISSPACE (*s)) {
411 return ascii_strcasecmp (a, tmp);
415 * Imap keepalive: poll the current folder to keep the
420 static void alrm_handler (int sig __attribute__((unused)))
425 void imap_keepalive (void)
431 conn = mutt_socket_head ();
433 if (conn->account.type == M_ACCT_TYPE_IMAP) {
434 idata = (IMAP_DATA *) conn->data;
436 if (idata->state >= IMAP_AUTHENTICATED
437 && time (NULL) >= idata->lastread + ImapKeepalive) {
441 ctx = p_new(CONTEXT, 1);
444 imap_check_mailbox (ctx, NULL, 1);
454 int imap_wait_keepalive (pid_t pid)
456 struct sigaction oldalrm;
457 struct sigaction act;
461 short imap_passive = option (OPTIMAPPASSIVE);
462 int imap_askreconnect = quadoption (OPT_IMAPRECONNECT);
464 set_option (OPTIMAPPASSIVE);
465 set_option (OPTKEEPQUIET);
466 set_quadoption (OPT_IMAPRECONNECT, M_NO);
468 sigprocmask (SIG_SETMASK, NULL, &oldmask);
470 sigemptyset (&act.sa_mask);
471 act.sa_handler = alrm_handler;
473 act.sa_flags = SA_INTERRUPT;
478 sigaction (SIGALRM, &act, &oldalrm);
480 alarm (ImapKeepalive);
481 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
482 alarm (0); /* cancel a possibly pending alarm */
484 alarm (ImapKeepalive);
487 alarm (0); /* cancel a possibly pending alarm */
489 sigaction (SIGALRM, &oldalrm, NULL);
490 sigprocmask (SIG_SETMASK, &oldmask, NULL);
492 unset_option (OPTKEEPQUIET);
494 unset_option (OPTIMAPPASSIVE);
495 set_quadoption (OPT_IMAPRECONNECT, imap_askreconnect);
500 /* Allow/disallow re-opening a folder upon expunge. */
502 void imap_allow_reopen (CONTEXT * ctx)
504 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
505 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
508 void imap_disallow_reopen (CONTEXT * ctx)
510 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
511 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;