d7837e190a6033fc7f4cd082ce4485be2fbb1de2
[apps/madmutt.git] / imap / util.c
1 /*
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>
6  *
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.
10  */
11
12 /* general IMAP utility functions */
13
14 #include <lib-lib/lib-lib.h>
15 #include <signal.h>
16 #include <netdb.h>
17
18 #include "mutt.h"
19 #include "mx.h"                 /* for M_IMAP */
20 #include "imap_private.h"
21 #include <lib-sys/mutt_ssl.h>
22
23 /* -- public functions -- */
24
25 /* imap_expand_path: IMAP implementation of mutt_expand_path. Rewrite
26  *   an IMAP path in canonical and absolute form.
27  * Inputs: a buffer containing an IMAP path, and the number of bytes in
28  *   that buffer.
29  * Outputs: The buffer is rewritten in place with the canonical IMAP path.
30  * Returns 0 on success, or -1 if imap_parse_path chokes or url_ciss_tostring
31  *   fails, which it might if there isn't enough room in the buffer. */
32 int imap_expand_path (char *path, size_t len)
33 {
34   IMAP_MBOX mx;
35   ciss_url_t url;
36   int rc;
37
38   if (imap_parse_path (path, &mx) < 0)
39     return -1;
40
41   mutt_account_tourl (&mx.account, &url);
42   url.path = mx.mbox;
43
44   rc = url_ciss_tostring (&url, path, len, U_DECODE_PASSWD);
45   p_delete(&mx.mbox);
46
47   return rc;
48 }
49
50 /* imap_parse_path: given an IMAP mailbox name, return host, port
51  *   and a path IMAP servers will recognise.
52  * mx.mbox is malloc'd, caller must free it */
53 int imap_parse_path (const char *path, IMAP_MBOX * mx)
54 {
55   static unsigned short ImapPort = 0;
56   static unsigned short ImapsPort = 0;
57   struct servent *service;
58   ciss_url_t url;
59   char *c;
60
61   if (!ImapPort) {
62     service = getservbyname ("imap", "tcp");
63     if (service)
64       ImapPort = ntohs (service->s_port);
65     else
66       ImapPort = IMAP_PORT;
67   }
68   if (!ImapsPort) {
69     service = getservbyname ("imaps", "tcp");
70     if (service)
71       ImapsPort = ntohs (service->s_port);
72     else
73       ImapsPort = IMAP_SSL_PORT;
74   }
75
76   /* Defaults */
77   mx->account.flags = 0;
78   mx->account.port = ImapPort;
79   mx->account.type = M_ACCT_TYPE_IMAP;
80
81   c = m_strdup(path);
82   url_parse_ciss (&url, c);
83
84   if (!(url.scheme == U_IMAP || url.scheme == U_IMAPS) ||
85       mutt_account_fromurl (&mx->account, &url) < 0 || !*mx->account.host) {
86     p_delete(&c);
87     return -1;
88   }
89
90   mx->mbox = m_strdup(url.path);
91
92   if (url.scheme == U_IMAPS)
93     mx->account.flags |= M_ACCT_SSL;
94
95   p_delete(&c);
96
97   if ((mx->account.flags & M_ACCT_SSL) && !(mx->account.flags & M_ACCT_PORT))
98     mx->account.port = ImapsPort;
99
100   return 0;
101 }
102
103 /* imap_pretty_mailbox: called by mutt_pretty_mailbox to make IMAP paths
104  *   look nice. */
105 void imap_pretty_mailbox (char *path)
106 {
107   IMAP_MBOX home, target;
108   ciss_url_t url;
109   char *delim;
110   int tlen;
111   int hlen = 0;
112   char home_match = 0;
113
114   if (imap_parse_path (path, &target) < 0)
115     return;
116
117   tlen = m_strlen(target.mbox);
118   /* check whether we can do '=' substitution */
119   if (mx_get_magic (Maildir) == M_IMAP && !imap_parse_path (Maildir, &home)) {
120     hlen = m_strlen(home.mbox);
121     if (tlen && mutt_account_match (&home.account, &target.account) &&
122         !m_strncmp(home.mbox, target.mbox, hlen)) {
123       if (!hlen)
124         home_match = 1;
125       else
126         for (delim = ImapDelimChars; *delim != '\0'; delim++)
127           if (target.mbox[hlen] == *delim)
128             home_match = 1;
129     }
130     p_delete(&home.mbox);
131   }
132
133   /* do the '=' substitution */
134   if (home_match) {
135     *path++ = '=';
136     /* copy remaining path, skipping delimiter */
137     if (!hlen)
138       hlen = -1;
139     memcpy (path, target.mbox + hlen + 1, tlen - hlen - 1);
140     path[tlen - hlen - 1] = '\0';
141   }
142   else {
143     mutt_account_tourl (&target.account, &url);
144     url.path = target.mbox;
145     /* FIXME: That hard-coded constant is bogus. But we need the actual
146      *   size of the buffer from mutt_pretty_mailbox. And these pretty
147      *   operations usually shrink the result. Still... */
148     url_ciss_tostring (&url, path, 1024, 0);
149   }
150
151   p_delete(&target.mbox);
152 }
153
154 /* -- library functions -- */
155
156 /* imap_continue: display a message and ask the user if she wants to
157  *   go on. */
158 int imap_continue (const char *msg, const char *resp)
159 {
160   imap_error (msg, resp);
161   return mutt_yesorno (_("Continue?"), 0);
162 }
163
164 /* imap_error: show an error and abort */
165 void imap_error (const char *where, const char *msg)
166 {
167   mutt_error ("%s [%s]\n", where, msg);
168   mutt_sleep (2);
169 }
170
171 /* imap_new_idata: Allocate and initialise a new IMAP_DATA structure.
172  *   Returns NULL on failure (no mem) */
173 IMAP_DATA *imap_new_idata (void)
174 {
175   return p_new(IMAP_DATA, 1);
176 }
177
178 /* imap_free_idata: Release and clear storage in an IMAP_DATA structure. */
179 void imap_free_idata (IMAP_DATA ** idata)
180 {
181   if (!idata)
182     return;
183
184   p_delete(&(*idata)->capstr);
185   string_list_wipe(&(*idata)->flags);
186   p_delete(&((*idata)->cmd.buf));
187   p_delete(idata);
188 }
189
190 /*
191  * Fix up the imap path.  This is necessary because the rest of mutt
192  * assumes a hierarchy delimiter of '/', which is not necessarily true
193  * in IMAP.  Additionally, the filesystem converts multiple hierarchy
194  * delimiters into a single one, ie "///" is equal to "/".  IMAP servers
195  * are not required to do this.
196  * Moreover, IMAP servers may dislike the path ending with the delimiter.
197  */
198 char *imap_fix_path (IMAP_DATA * idata, char *mailbox, char *path,
199                      ssize_t plen)
200 {
201   int x = 0;
202
203   if (!mailbox || !*mailbox) {
204     m_strcpy(path, plen, "INBOX");
205     return path;
206   }
207
208   while (mailbox && *mailbox && (x < (plen - 1))) {
209     if ((*mailbox == '/') || (*mailbox == idata->delim)) {
210       while ((*mailbox == '/') || (*mailbox == idata->delim))
211         mailbox++;
212       path[x] = idata->delim;
213     }
214     else {
215       path[x] = *mailbox;
216       mailbox++;
217     }
218     x++;
219   }
220   if (x && path[--x] != idata->delim)
221     x++;
222   path[x] = '\0';
223   return path;
224 }
225
226 /* imap_get_literal_count: write number of bytes in an IMAP literal into
227  *   bytes, return 0 on success, -1 on failure. */
228 int imap_get_literal_count (const char *buf, long *bytes)
229 {
230   char *pc;
231   char *pn;
232
233   if (!(pc = strchr (buf, '{')))
234     return (-1);
235   pc++;
236   pn = pc;
237   while (isdigit ((unsigned char) *pc))
238     pc++;
239   *pc = 0;
240   *bytes = atoi (pn);
241   return (0);
242 }
243
244 /* imap_get_qualifier: in a tagged response, skip tag and status for
245  *   the qualifier message. Used by imap_copy_message for TRYCREATE */
246 char *imap_get_qualifier (char *buf)
247 {
248   char *s = buf;
249
250   /* skip tag */
251   s = imap_next_word (s);
252   /* skip OK/NO/BAD response */
253   s = imap_next_word (s);
254
255   return s;
256 }
257
258 /* imap_next_word: return index into string where next IMAP word begins */
259 char *imap_next_word (char *s)
260 {
261   int quoted = 0;
262
263   while (*s) {
264     if (*s == '\\') {
265       s++;
266       if (*s)
267         s++;
268       continue;
269     }
270     if (*s == '\"')
271       quoted = quoted ? 0 : 1;
272     if (!quoted && ISSPACE (*s))
273       break;
274     s++;
275   }
276
277   return vskipspaces(s);
278 }
279
280 /* imap_parse_date: date is of the form: DD-MMM-YYYY HH:MM:SS +ZZzz */
281 time_t imap_parse_date (char *s)
282 {
283   struct tm t;
284   time_t tz;
285
286   t.tm_mday = (s[0] == ' ' ? s[1] - '0' : (s[0] - '0') * 10 + (s[1] - '0'));
287   s += 2;
288   if (*s != '-')
289     return 0;
290   s++;
291   t.tm_mon = mutt_check_month (s);
292   s += 3;
293   if (*s != '-')
294     return 0;
295   s++;
296   t.tm_year =
297     (s[0] - '0') * 1000 + (s[1] - '0') * 100 + (s[2] - '0') * 10 + (s[3] -
298                                                                     '0') -
299     1900;
300   s += 4;
301   if (*s != ' ')
302     return 0;
303   s++;
304
305   /* time */
306   t.tm_hour = (s[0] - '0') * 10 + (s[1] - '0');
307   s += 2;
308   if (*s != ':')
309     return 0;
310   s++;
311   t.tm_min = (s[0] - '0') * 10 + (s[1] - '0');
312   s += 2;
313   if (*s != ':')
314     return 0;
315   s++;
316   t.tm_sec = (s[0] - '0') * 10 + (s[1] - '0');
317   s += 2;
318   if (*s != ' ')
319     return 0;
320   s++;
321
322   /* timezone */
323   tz = ((s[1] - '0') * 10 + (s[2] - '0')) * 3600 +
324     ((s[3] - '0') * 10 + (s[4] - '0')) * 60;
325   if (s[0] == '+')
326     tz = -tz;
327
328   return (mutt_mktime (&t, 0) + tz);
329 }
330
331 /* imap_qualify_path: make an absolute IMAP folder target, given IMAP_MBOX
332  *   and relative path. */
333 void imap_qualify_path (char *dest, size_t len, IMAP_MBOX * mx, char *path)
334 {
335   ciss_url_t url;
336
337   mutt_account_tourl (&mx->account, &url);
338   url.path = path;
339
340   url_ciss_tostring (&url, dest, len, 0);
341 }
342
343
344 /* imap_quote_string: quote string according to IMAP rules:
345  *   surround string with quotes, escape " and \ with \ */
346 void imap_quote_string (char *dest, size_t dlen, const char *src)
347 {
348   char quote[] = "\"\\", *pt;
349   const char *s;
350
351   pt = dest;
352   s = src;
353
354   *pt++ = '"';
355   /* save room for trailing quote-char */
356   dlen -= 2;
357
358   for (; *s && dlen; s++) {
359     if (strchr (quote, *s)) {
360       dlen -= 2;
361       if (!dlen)
362         break;
363       *pt++ = '\\';
364       *pt++ = *s;
365     }
366     else {
367       *pt++ = *s;
368       dlen--;
369     }
370   }
371   *pt++ = '"';
372   *pt = 0;
373 }
374
375 /* imap_unquote_string: equally stupid unquoting routine */
376 void imap_unquote_string (char *s)
377 {
378   char *d = s;
379
380   if (*s == '\"')
381     s++;
382   else
383     return;
384
385   while (*s) {
386     if (*s == '\"') {
387       *d = '\0';
388       return;
389     }
390     if (*s == '\\') {
391       s++;
392     }
393     if (*s) {
394       *d = *s;
395       d++;
396       s++;
397     }
398   }
399   *d = '\0';
400 }
401
402 /*
403  * Quoting and UTF-7 conversion
404  */
405
406 void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
407 {
408   char *buf;
409
410   buf = m_strdup(src);
411   imap_utf7_encode (&buf);
412
413   imap_quote_string (dest, dlen, buf);
414
415   p_delete(&buf);
416 }
417
418 void imap_unmunge_mbox_name (char *s)
419 {
420   char *buf;
421
422   imap_unquote_string (s);
423
424   buf = m_strdup(s);
425   if (buf) {
426     imap_utf7_decode (&buf);
427     m_strcpy(s, m_strlen(s) + 1, buf);
428   }
429
430   p_delete(&buf);
431 }
432
433 /* imap_wordcasecmp: find word a in word list b */
434 int imap_wordcasecmp (const char *a, const char *b)
435 {
436   char tmp[SHORT_STRING];
437   char *s = (char *) b;
438   int i;
439
440   tmp[SHORT_STRING - 1] = 0;
441   for (i = 0; i < SHORT_STRING - 2; i++, s++) {
442     if (!*s || ISSPACE (*s)) {
443       tmp[i] = 0;
444       break;
445     }
446     tmp[i] = *s;
447   }
448   tmp[i + 1] = 0;
449
450   return ascii_strcasecmp (a, tmp);
451 }
452
453 /* 
454  * Imap keepalive: poll the current folder to keep the
455  * connection alive.
456  * 
457  */
458
459 static RETSIGTYPE alrm_handler (int sig __attribute__((unused)))
460 {
461   /* empty */
462 }
463
464 void imap_keepalive (void)
465 {
466   CONNECTION *conn;
467   CONTEXT *ctx = NULL;
468   IMAP_DATA *idata;
469
470   conn = mutt_socket_head ();
471   while (conn) {
472     if (conn->account.type == M_ACCT_TYPE_IMAP) {
473       idata = (IMAP_DATA *) conn->data;
474
475       if (idata->state >= IMAP_AUTHENTICATED
476           && time (NULL) >= idata->lastread + ImapKeepalive) {
477         if (idata->ctx)
478           ctx = idata->ctx;
479         else {
480           ctx = p_new(CONTEXT, 1);
481           ctx->data = idata;
482         }
483         imap_check_mailbox (ctx, NULL, 1);
484         if (!idata->ctx)
485           p_delete(&ctx);
486       }
487     }
488
489     conn = conn->next;
490   }
491 }
492
493 int imap_wait_keepalive (pid_t pid)
494 {
495   struct sigaction oldalrm;
496   struct sigaction act;
497   sigset_t oldmask;
498   int rc;
499
500   short imap_passive = option (OPTIMAPPASSIVE);
501   int imap_askreconnect = quadoption (OPT_IMAPRECONNECT);
502
503   set_option (OPTIMAPPASSIVE);
504   set_option (OPTKEEPQUIET);
505   set_quadoption (OPT_IMAPRECONNECT, M_NO);
506
507   sigprocmask (SIG_SETMASK, NULL, &oldmask);
508
509   sigemptyset (&act.sa_mask);
510   act.sa_handler = alrm_handler;
511 #ifdef SA_INTERRUPT
512   act.sa_flags = SA_INTERRUPT;
513 #else
514   act.sa_flags = 0;
515 #endif
516
517   sigaction (SIGALRM, &act, &oldalrm);
518
519   alarm (ImapKeepalive);
520   while (waitpid (pid, &rc, 0) < 0 && errno == EINTR) {
521     alarm (0);                  /* cancel a possibly pending alarm */
522     imap_keepalive ();
523     alarm (ImapKeepalive);
524   }
525
526   alarm (0);                    /* cancel a possibly pending alarm */
527
528   sigaction (SIGALRM, &oldalrm, NULL);
529   sigprocmask (SIG_SETMASK, &oldmask, NULL);
530
531   unset_option (OPTKEEPQUIET);
532   if (!imap_passive)
533     unset_option (OPTIMAPPASSIVE);
534   set_quadoption (OPT_IMAPRECONNECT, imap_askreconnect);
535
536   return rc;
537 }
538
539 /* Allow/disallow re-opening a folder upon expunge. */
540
541 void imap_allow_reopen (CONTEXT * ctx)
542 {
543   if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
544     CTX_DATA->reopen |= IMAP_REOPEN_ALLOW;
545 }
546
547 void imap_disallow_reopen (CONTEXT * ctx)
548 {
549   if (ctx && ctx->magic == M_IMAP && CTX_DATA->ctx == ctx)
550     CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW;
551 }