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