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