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