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