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