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