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