2 * Copyright (C) 1996-8 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 /* general IMAP utility functions */
26 #include "mx.h" /* for M_IMAP */
28 #include "imap_private.h"
34 #include <sys/types.h>
38 #include <netinet/in.h>
42 /* -- public functions -- */
44 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
45 * an IMAP path in canonical and absolute form.
46 * Inputs: a buffer containing an IMAP path, and the number of bytes in
48 * Outputs: The buffer is rewritten in place with the canonical IMAP path.
49 * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
50 * fails, which it might if there isn't enough room in the buffer. */
51 int imap_expand_path (char* path, size_t len)
57 if (imap_parse_path (path, &mx) < 0)
60 mutt_account_tourl (&mx.account, &url);
63 rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
69 /* imap_parse_path: given an IMAP mailbox name, return host, port
70 * and a path IMAP servers will recognise.
71 * mx.mbox is malloc'd, caller must free it */
72 int imap_parse_path (const char* path, IMAP_MBOX* mx)
74 static unsigned short ImapPort = 0;
75 static unsigned short ImapsPort = 0;
76 struct servent* service;
84 service = getservbyname ("imap", "tcp");
86 ImapPort = ntohs (service->s_port);
89 dprint (3, (debugfile, "Using default IMAP port %d\n", ImapPort));
93 service = getservbyname ("imaps", "tcp");
95 ImapsPort = ntohs (service->s_port);
97 ImapsPort = IMAP_SSL_PORT;
98 dprint (3, (debugfile, "Using default IMAPS port %d\n", ImapsPort));
102 mx->account.flags = 0;
103 mx->account.port = ImapPort;
104 mx->account.type = M_ACCT_TYPE_IMAP;
106 c = safe_strdup (path);
107 url_parse_ciss (&url, c);
108 if (url.scheme == U_IMAP || url.scheme == U_IMAPS)
110 if (mutt_account_fromurl (&mx->account, &url) < 0)
116 mx->mbox = safe_strdup (url.path);
118 if (url.scheme == U_IMAPS)
119 mx->account.flags |= M_ACCT_SSL;
123 /* old PINE-compatibility code */
127 if (sscanf (path, "{%127[^}]}", tmp) != 1)
130 c = strchr (path, '}');
134 /* walk past closing '}' */
135 mx->mbox = safe_strdup (c+1);
137 if ((c = strrchr (tmp, '@')))
140 strfcpy (mx->account.user, tmp, sizeof (mx->account.user));
141 strfcpy (tmp, c+1, sizeof (tmp));
142 mx->account.flags |= M_ACCT_USER;
145 if ((n = sscanf (tmp, "%127[^:/]%127s", mx->account.host, tmp)) < 1)
147 dprint (1, (debugfile, "imap_parse_path: NULL host in %s\n", path));
153 if (sscanf (tmp, ":%hu%127s", &(mx->account.port), tmp) >= 1)
154 mx->account.flags |= M_ACCT_PORT;
155 if (sscanf (tmp, "/%s", tmp) == 1)
157 if (!ascii_strncmp (tmp, "ssl", 3))
158 mx->account.flags |= M_ACCT_SSL;
161 dprint (1, (debugfile, "imap_parse_path: Unknown connection type in %s\n", path));
169 #if defined(USE_SSL) || defined(USE_GNUTLS)
170 if (option (OPTIMAPFORCESSL))
171 mx->account.flags |= M_ACCT_SSL;
174 if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
175 mx->account.port = ImapsPort;
180 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
182 void imap_pretty_mailbox (char* path)
184 IMAP_MBOX home, target;
191 if (imap_parse_path (path, &target) < 0)
194 tlen = mutt_strlen (target.mbox);
195 /* check whether we can do '=' substitution */
196 if (mx_is_imap(Maildir) && !imap_parse_path (Maildir, &home))
198 hlen = mutt_strlen (home.mbox);
199 if (tlen && mutt_account_match (&home.account, &target.account) &&
200 !mutt_strncmp (home.mbox, target.mbox, hlen))
205 for (delim = ImapDelimChars; *delim != '\0'; delim++)
206 if (target.mbox[hlen] == *delim)
212 /* do the '=' substitution */
215 /* copy remaining path, skipping delimiter */
218 memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
219 path[tlen - hlen - 1] = '\0';
223 mutt_account_tourl (&target.account, &url);
224 url.path = target.mbox;
225 /* FIXME: That hard-coded constant is bogus. But we need the actual
226 * size of the buffer from mutt_pretty_mailbox. And these pretty
227 * operations usually shrink the result. Still... */
228 url_ciss_tostring (&url, path, 1024, 0);
234 /* -- library functions -- */
236 /* imap_continue: display a message and ask the user if she wants to
238 int imap_continue (const char* msg, const char* resp)
240 imap_error (msg, resp);
241 return mutt_yesorno (_("Continue?"), 0);
244 /* imap_error: show an error and abort */
245 void imap_error (const char *where, const char *msg)
247 mutt_error ("%s [%s]\n", where, msg);
251 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
252 * Returns NULL on failure (no mem) */
253 IMAP_DATA* imap_new_idata (void) {
254 return safe_calloc (1, sizeof (IMAP_DATA));
257 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
258 void imap_free_idata (IMAP_DATA** idata) {
262 FREE (&(*idata)->capstr);
263 mutt_free_list (&(*idata)->flags);
264 FREE (&((*idata)->cmd.buf));
269 * Fix up the imap path. This is necessary because the rest of mutt
270 * assumes a hierarchy delimiter of '/', which is not necessarily true
271 * in IMAP. Additionally, the filesystem converts multiple hierarchy
272 * delimiters into a single one, ie "///" is equal to "/". IMAP servers
273 * are not required to do this.
274 * Moreover, IMAP servers may dislike the path ending with the delimiter.
276 char *imap_fix_path (IMAP_DATA *idata, char *mailbox, char *path,
281 if (!mailbox || !*mailbox)
283 strfcpy (path, "INBOX", plen);
287 while (mailbox && *mailbox && (x < (plen - 1)))
289 if ((*mailbox == '/') || (*mailbox == idata->delim))
291 while ((*mailbox == '/') || (*mailbox == idata->delim)) mailbox++;
292 path[x] = idata->delim;
301 if (x && path[--x] != idata->delim)
307 /* imap_get_literal_count: write number of bytes in an IMAP literal into
308 * bytes, return 0 on success, -1 on failure. */
309 int imap_get_literal_count(const char *buf, long *bytes)
314 if (!(pc = strchr (buf, '{')))
318 while (isdigit ((unsigned char) *pc))
325 /* imap_get_qualifier: in a tagged response, skip tag and status for
326 * the qualifier message. Used by imap_copy_message for TRYCREATE */
327 char* imap_get_qualifier (char* buf)
332 s = imap_next_word (s);
333 /* skip OK/NO/BAD response */
334 s = imap_next_word (s);
339 /* imap_next_word: return index into string where next IMAP word begins */
340 char *imap_next_word (char *s)
352 quoted = quoted ? 0 : 1;
353 if (!quoted && ISSPACE (*s))
362 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
363 time_t imap_parse_date (char *s)
368 t.tm_mday = (s[0] == ' '? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
373 t.tm_mon = mutt_check_month (s);
378 t.tm_year = (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] - '0') - 1900;
385 t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
390 t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
395 t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
402 tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
403 ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
407 return (mutt_mktime (&t, 0) + tz);
410 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
411 * and relative path. */
412 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX *mx, char* path)
416 mutt_account_tourl (&mx->account, &url);
419 url_ciss_tostring (&url, dest, len, 0);
423 /* imap_quote_string: quote string according to IMAP rules:
424 * surround string with quotes, escape " and \ with \ */
425 void imap_quote_string (char *dest, size_t dlen, const char *src)
427 char quote[] = "\"\\", *pt;
434 /* save room for trailing quote-char */
437 for (; *s && dlen; s++)
439 if (strchr (quote, *s))
457 /* imap_unquote_string: equally stupid unquoting routine */
458 void imap_unquote_string (char *s)
489 * Quoting and UTF-7 conversion
492 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
496 buf = safe_strdup (src);
497 imap_utf7_encode (&buf);
499 imap_quote_string (dest, dlen, buf);
504 void imap_unmunge_mbox_name (char *s)
508 imap_unquote_string(s);
510 buf = safe_strdup (s);
513 imap_utf7_decode (&buf);
514 strncpy (s, buf, strlen (s));
520 /* imap_wordcasecmp: find word a in word list b */
521 int imap_wordcasecmp(const char *a, const char *b)
523 char tmp[SHORT_STRING];
527 tmp[SHORT_STRING-1] = 0;
528 for(i=0;i < SHORT_STRING-2;i++,s++)
530 if (!*s || ISSPACE(*s))
539 return ascii_strcasecmp(a, tmp);
543 * Imap keepalive: poll the current folder to keep the
548 static RETSIGTYPE alrm_handler (int sig)
553 void imap_keepalive (void)
559 conn = mutt_socket_head ();
562 if (conn->account.type == M_ACCT_TYPE_IMAP)
564 idata = (IMAP_DATA*) conn->data;
566 if (idata->state >= IMAP_AUTHENTICATED
567 && time(NULL) >= idata->lastread + ImapKeepalive)
573 ctx = safe_calloc (1, sizeof (CONTEXT));
576 imap_check_mailbox (ctx, NULL, 1);
586 int imap_wait_keepalive (pid_t pid)
588 struct sigaction oldalrm;
589 struct sigaction act;
593 short imap_passive = option (OPTIMAPPASSIVE);
595 set_option (OPTIMAPPASSIVE);
596 set_option (OPTKEEPQUIET);
598 sigprocmask (SIG_SETMASK, NULL, &oldmask);
600 sigemptyset (&act.sa_mask);
601 act.sa_handler = alrm_handler;
603 act.sa_flags = SA_INTERRUPT;
608 sigaction (SIGALRM, &act, &oldalrm);
610 alarm (ImapKeepalive);
611 while (waitpid (pid, &rc, 0) < 0 && errno == EINTR)
613 alarm (0); /* cancel a possibly pending alarm */
615 alarm (ImapKeepalive);
618 alarm (0); /* cancel a possibly pending alarm */
620 sigaction (SIGALRM, &oldalrm, NULL);
621 sigprocmask (SIG_SETMASK, &oldmask, NULL);
623 unset_option (OPTKEEPQUIET);
625 unset_option (OPTIMAPPASSIVE);
630 /* Allow/disallow re-opening a folder upon expunge. */
632 void imap_allow_reopen (CONTEXT *ctx)
634 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
635 CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
638 void imap_disallow_reopen (CONTEXT *ctx)
640 if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
641 CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;