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