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