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