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