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 */
20 #include "imap_private.h"
25 #include "lib/debug.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);
82 debug_print (3, ("Using default IMAP port %d\n", ImapPort));
85 service = getservbyname ("imaps", "tcp");
87 ImapsPort = ntohs (service->s_port);
89 ImapsPort = IMAP_SSL_PORT;
90 debug_print (3, ("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;
99 url_parse_ciss (&url, c);
101 if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
102 mutt_account_fromurl (&mx->account, &url) < 0) {
107 mx->mbox = str_dup (url.path);
109 if (url.scheme == U_IMAPS)
110 mx->account.flags |= M_ACCT_SSL;
114 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
115 mx->account.port = ImapsPort;
120 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
122 void imap_pretty_mailbox (char *path)
124 IMAP_MBOX home, target;
131 if (imap_parse_path (path, &target) < 0)
134 tlen = str_len (target.mbox);
135 /* check whether we can do '=' substitution */
136 if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
137 hlen = str_len (home.mbox);
138 if (tlen && mutt_account_match (&home.account, &target.account) &&
139 !str_ncmp (home.mbox, target.mbox, hlen)) {
143 for (delim = ImapDelimChars; *delim != '\0'; delim++)
144 if (target.mbox[hlen] == *delim)
147 mem_free (&home.mbox);
150 /* do the '=' substitution */
153 /* copy remaining path, skipping delimiter */
156 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
157 path[tlen - hlen - 1] = '\0';
160 mutt_account_tourl (&target.account, &url);
161 url.path = target.mbox;
162 /* FIXME: That hard-coded constant is bogus. But we need the actual
163 * size of the buffer from mutt_pretty_mailbox. And these pretty
164 * operations usually shrink the result. Still... */
165 url_ciss_tostring (&url, path, 1024, 0);
168 mem_free (&target.mbox);
171 /* -- library functions -- */
173 /* imap_continue: display a message and ask the user if she wants to
175 int imap_continue (const char *msg, const char *resp)
177 imap_error (msg, resp);
178 return mutt_yesorno (_("Continue?"), 0);
181 /* imap_error: show an error and abort */
182 void imap_error (const char *where, const char *msg)
184 mutt_error ("%s [%s]\n", where, msg);
188 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
189 * Returns NULL on failure (no mem) */
190 IMAP_DATA *imap_new_idata (void)
192 return mem_calloc (1, sizeof (IMAP_DATA));
195 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
196 void imap_free_idata (IMAP_DATA ** idata)
201 mem_free (&(*idata)->capstr);
202 mutt_free_list (&(*idata)->flags);
203 mem_free (&((*idata)->cmd.buf));
208 * Fix up the imap path. This is necessary because the rest of mutt
209 * assumes a hierarchy delimiter of '/', which is not necessarily true
210 * in IMAP. Additionally, the filesystem converts multiple hierarchy
211 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
212 * are not required to do this.
213 * Moreover, IMAP servers may dislike the path ending with the delimiter.
215 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
220 if (!mailbox || !*mailbox) {
221 strfcpy (path, "INBOX", plen);
225 while (mailbox && *mailbox && (x < (plen - 1))) {
226 if ((*mailbox == '/') || (*mailbox == idata->delim)) {
227 while ((*mailbox == '/') || (*mailbox == idata->delim))
229 path[x] = idata->delim;
237 if (x && path[--x] != idata->delim)
243 /* imap_get_literal_count: write number of bytes in an IMAP literal into
244 * bytes, return 0 on success, -1 on failure. */
245 int imap_get_literal_count (const char *buf, long *bytes)
250 if (!(pc = strchr (buf, '{')))
254 while (isdigit ((unsigned char) *pc))
261 /* imap_get_qualifier: in a tagged response, skip tag and status for
262 * the qualifier message. Used by imap_copy_message for TRYCREATE */
263 char *imap_get_qualifier (char *buf)
268 s = imap_next_word (s);
269 /* skip OK/NO/BAD response */
270 s = imap_next_word (s);
275 /* imap_next_word: return index into string where next IMAP word begins */
276 char *imap_next_word (char *s)
288 quoted = quoted ? 0 : 1;
289 if (!quoted && ISSPACE (*s))
298 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
299 time_t imap_parse_date (char *s)
304 t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
309 t.tm_mon = mutt_check_month (s);
315 (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
324 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
329 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
334 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
341 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
342 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
346 return (mutt_mktime (&t, 0) + tz);
349 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
350 * and relative path. */
351 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
355 mutt_account_tourl (&mx->account, &url);
358 url_ciss_tostring (&url, dest, len, 0);
362 /* imap_quote_string: quote string according to IMAP rules:
363 * surround string with quotes, escape " and \ with \ */
364 void imap_quote_string (char *dest, size_t dlen, const char *src)
366 char quote[] = "\"\\", *pt;
373 /* save room for trailing quote-char */
376 for (; *s && dlen; s++) {
377 if (strchr (quote, *s)) {
393 /* imap_unquote_string: equally stupid unquoting routine */
394 void imap_unquote_string (char *s)
421 * Quoting and UTF-7 conversion
424 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
429 imap_utf7_encode (&buf);
431 imap_quote_string (dest, dlen, buf);
436 void imap_unmunge_mbox_name (char *s)
440 imap_unquote_string (s);
444 imap_utf7_decode (&buf);
445 strncpy (s, buf, str_len (s));
451 /* imap_wordcasecmp: find word a in word list b */
452 int imap_wordcasecmp (const char *a, const char *b)
454 char tmp[SHORT_STRING];
455 char *s = (char *) b;
458 tmp[SHORT_STRING - 1] = 0;
459 for (i = 0; i < SHORT_STRING - 2; i++, s++) {
460 if (!*s || ISSPACE (*s)) {
468 return ascii_strcasecmp (a, tmp);
472 * Imap keepalive: poll the current folder to keep the
477 static RETSIGTYPE alrm_handler (int sig)
482 void imap_keepalive (void)
488 conn = mutt_socket_head ();
490 if (conn->account.type == M_ACCT_TYPE_IMAP) {
491 idata = (IMAP_DATA *) conn->data;
493 if (idata->state >= IMAP_AUTHENTICATED
494 && time (NULL) >= idata->lastread + ImapKeepalive) {
498 ctx = mem_calloc (1, sizeof (CONTEXT));
501 imap_check_mailbox (ctx, NULL, 1);
511 int imap_wait_keepalive (pid_t pid)
513 struct sigaction oldalrm;
514 struct sigaction act;
518 short imap_passive = option (OPTIMAPPASSIVE);
520 set_option (OPTIMAPPASSIVE);
521 set_option (OPTKEEPQUIET);
523 sigprocmask (SIG_SETMASK, NULL, &oldmask);
525 sigemptyset (&act.sa_mask);
526 act.sa_handler = alrm_handler;
528 act.sa_flags = SA_INTERRUPT;
533 sigaction (SIGALRM, &act, &oldalrm);
535 alarm (ImapKeepalive);
536 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
537 alarm (0); /* cancel a possibly pending alarm */
539 alarm (ImapKeepalive);
542 alarm (0); /* cancel a possibly pending alarm */
544 sigaction (SIGALRM, &oldalrm, NULL);
545 sigprocmask (SIG_SETMASK, &oldmask, NULL);
547 unset_option (OPTKEEPQUIET);
549 unset_option (OPTIMAPPASSIVE);
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;