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