drop mem_alloc and mem_free, use my own hand crafted optmized macros that
[apps/madmutt.git] / imap / browse.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4  * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 /* Mutt browser support routines */
12
13 #if HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #include <stdlib.h>
18 #include <ctype.h>
19
20 #include <lib-lib/mem.h>
21
22 #include "lib/mem.h"
23 #include "lib/str.h"
24 #include "lib/intl.h"
25 #include "lib/debug.h"
26
27 #include "mutt.h"
28 #include "ascii.h"
29 #include "enter.h"
30 #include "imap_private.h"
31
32 /* -- forward declarations -- */
33 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
34                                    struct browser_state *state,
35                                    short isparent);
36 static void imap_add_folder (char delim, char *folder, int noselect,
37                              int noinferiors, struct browser_state *state,
38                              short isparent);
39 static int compare_names (struct folder_file *a, struct folder_file *b);
40 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
41                                  IMAP_NAMESPACE_INFO * nsi, int nsilen,
42                                  int *nns);
43 static int browse_verify_namespace (IMAP_DATA * idata,
44                                     IMAP_NAMESPACE_INFO * nsi, int nns);
45
46 /* imap_browse: IMAP hook into the folder browser, fills out browser_state,
47  *   given a current folder to browse */
48 int imap_browse (char *path, struct browser_state *state)
49 {
50   IMAP_DATA *idata;
51   char buf[LONG_STRING];
52   char buf2[LONG_STRING];
53   char nsbuf[LONG_STRING];
54   char mbox[LONG_STRING];
55   char list_cmd[5];
56   IMAP_NAMESPACE_INFO nsi[16];
57   int home_namespace = 0;
58   int n;
59   int i;
60   int nsup;
61   char ctmp;
62   int nns = 0;
63   char *cur_folder;
64   short showparents = 0;
65   int noselect;
66   int noinferiors;
67   int save_lsub;
68   IMAP_MBOX mx;
69
70   if (imap_parse_path (path, &mx)) {
71     mutt_error (_("%s is an invalid IMAP path"), path);
72     return -1;
73   }
74
75   save_lsub = option (OPTIMAPCHECKSUBSCRIBED);
76   unset_option (OPTIMAPCHECKSUBSCRIBED);
77   strfcpy (list_cmd, option (OPTIMAPLSUB) ? "LSUB" : "LIST",
78            sizeof (list_cmd));
79
80   if (!(idata = imap_conn_find (&(mx.account), 0)))
81     goto fail;
82
83   if (!mx.mbox) {
84     home_namespace = 1;
85     mbox[0] = '\0';             /* Do not replace "" with "INBOX" here */
86     mx.mbox = str_dup (ImapHomeNamespace);
87     if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
88       mutt_message _("Getting namespaces...");
89
90       if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
91                                 nsi, sizeof (nsi), &nns) != 0)
92         goto fail;
93       if (browse_verify_namespace (idata, nsi, nns) != 0)
94         goto fail;
95     }
96   }
97
98   mutt_message _("Getting folder list...");
99
100   /* skip check for parents when at the root */
101   if (mx.mbox && mx.mbox[0] != '\0') {
102     imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
103     imap_munge_mbox_name (buf, sizeof (buf), mbox);
104     imap_unquote_string (buf);  /* As kludgy as it gets */
105     mbox[sizeof (mbox) - 1] = '\0';
106     strncpy (mbox, buf, sizeof (mbox) - 1);
107     n = str_len (mbox);
108
109     debug_print (3, ("mbox: %s\n", mbox));
110
111     /* if our target exists and has inferiors, enter it if we
112      * aren't already going to */
113     if (mbox[n - 1] != idata->delim) {
114       snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
115       imap_cmd_start (idata, buf);
116       do {
117         if (imap_parse_list_response (idata, &cur_folder, &noselect,
118                                       &noinferiors, &idata->delim) != 0)
119           goto fail;
120
121         if (cur_folder) {
122           imap_unmunge_mbox_name (cur_folder);
123
124           if (!noinferiors && cur_folder[0] &&
125               (n = str_len (mbox)) < LONG_STRING - 1) {
126             mbox[n++] = idata->delim;
127             mbox[n] = '\0';
128           }
129         }
130       }
131       while (ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN));
132     }
133
134     /* if we're descending a folder, mark it as current in browser_state */
135     if (mbox[n - 1] == idata->delim) {
136       /* don't show parents in the home namespace */
137       if (!home_namespace)
138         showparents = 1;
139       imap_qualify_path (buf, sizeof (buf), &mx, mbox);
140       state->folder = str_dup (buf);
141       n--;
142     }
143
144     /* Find superiors to list
145      * Note: UW-IMAP servers return folder + delimiter when asked to list
146      *  folder + delimiter. Cyrus servers don't. So we ask for folder,
147      *  and tack on delimiter ourselves.
148      * Further note: UW-IMAP servers return nothing when asked for 
149      *  NAMESPACES without delimiters at the end. Argh! */
150     for (n--; n >= 0 && mbox[n] != idata->delim; n--);
151     if (n > 0) {                /* "aaaa/bbbb/" -> "aaaa" */
152       /* forget the check, it is too delicate (see above). Have we ever
153        * had the parent not exist? */
154       ctmp = mbox[n];
155       mbox[n] = '\0';
156
157       if (showparents) {
158         debug_print (3, ("adding parent %s\n", mbox));
159         imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
160       }
161
162       /* if our target isn't a folder, we are in our superior */
163       if (!state->folder) {
164         /* store folder with delimiter */
165         mbox[n++] = ctmp;
166         ctmp = mbox[n];
167         mbox[n] = '\0';
168         imap_qualify_path (buf, sizeof (buf), &mx, mbox);
169         state->folder = str_dup (buf);
170       }
171       mbox[n] = ctmp;
172     }
173     /* "/bbbb/" -> add  "/", "aaaa/" -> add "" */
174     else {
175       char relpath[2];
176
177       /* folder may be "/" */
178       snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
179       if (showparents)
180         imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
181       if (!state->folder) {
182         imap_qualify_path (buf, sizeof (buf), &mx, relpath);
183         state->folder = str_dup (buf);
184       }
185     }
186   }
187
188   /* no namespace, no folder: set folder to host only */
189   if (!state->folder) {
190     imap_qualify_path (buf, sizeof (buf), &mx, NULL);
191     state->folder = str_dup (buf);
192   }
193
194   if (home_namespace && mbox[0] != '\0') {
195     /* Listing the home namespace, so INBOX should be included. Home 
196      * namespace is not "", so we have to list it explicitly. We ask the 
197      * server to see if it has descendants. */
198     debug_print (3, ("adding INBOX\n"));
199     if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
200       goto fail;
201   }
202
203   nsup = state->entrylen;
204
205   debug_print (3, ("Quoting mailbox scan: %s:\n", mbox));
206   snprintf (buf, sizeof (buf), "%s%%", mbox);
207   imap_quote_string (buf2, sizeof (buf2), buf);
208   debug_print (3, ("%s\n", buf2));
209   snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
210   if (browse_add_list_result (idata, buf, state, 0))
211     goto fail;
212
213   if (!state->entrylen) {
214     mutt_error _("No such folder");
215
216     goto fail;
217   }
218
219   mutt_clear_error ();
220
221   qsort (&(state->entry[nsup]), state->entrylen - nsup,
222          sizeof (state->entry[0]),
223          (int (*)(const void *, const void *)) compare_names);
224   if (home_namespace) {         /* List additional namespaces */
225     for (i = 0; i < nns; i++)
226       if (nsi[i].listable && !nsi[i].home_namespace) {
227         imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
228                          nsi[i].noinferiors, state, 0);
229         debug_print (3, ("adding namespace: %s\n", nsi[i].prefix));
230       }
231   }
232
233   if (save_lsub)
234     set_option (OPTIMAPCHECKSUBSCRIBED);
235
236   p_delete(&mx.mbox);
237   return 0;
238
239 fail:
240   if (save_lsub)
241     set_option (OPTIMAPCHECKSUBSCRIBED);
242   p_delete(&mx.mbox);
243   return -1;
244 }
245
246 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
247 int imap_mailbox_create (const char *folder)
248 {
249   IMAP_DATA *idata;
250   IMAP_MBOX mx;
251   char buf[LONG_STRING];
252   short n;
253
254   if (imap_parse_path (folder, &mx) < 0) {
255     debug_print (1, ("Bad starting path %s\n", folder));
256     return -1;
257   }
258
259   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
260     debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
261     goto fail;
262   }
263
264   strfcpy (buf, NONULL (mx.mbox), sizeof (buf));
265
266   /* append a delimiter if necessary */
267   n = str_len (buf);
268   if (n && (n < sizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
269     buf[n++] = idata->delim;
270     buf[n] = '\0';
271   }
272
273   if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
274     goto fail;
275
276   if (!str_len (buf)) {
277     mutt_error (_("Mailbox must have a name."));
278     mutt_sleep (1);
279     goto fail;
280   }
281
282   if (imap_create_mailbox (idata, buf) < 0)
283     goto fail;
284
285   mutt_message _("Mailbox created.");
286
287   mutt_sleep (0);
288
289   p_delete(&mx.mbox);
290   return 0;
291
292 fail:
293   p_delete(&mx.mbox);
294   return -1;
295 }
296
297 int imap_mailbox_rename (const char *mailbox)
298 {
299   IMAP_DATA *idata;
300   IMAP_MBOX mx;
301   char buf[LONG_STRING];
302   char newname[SHORT_STRING];
303
304   if (imap_parse_path (mailbox, &mx) < 0) {
305     debug_print (1, ("Bad source mailbox %s\n", mailbox));
306     return -1;
307   }
308
309   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
310     debug_print (1, ("Couldn't find open connection to %s\n", mx.account.host));
311     goto fail;
312   }
313
314   snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
315
316   if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
317     goto fail;
318
319   if (!str_len (newname)) {
320     mutt_error (_("Mailbox must have a name."));
321     mutt_sleep (1);
322     goto fail;
323   }
324
325   if (imap_rename_mailbox (idata, &mx, newname) < 0) {
326     mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
327     mutt_sleep (1);
328     goto fail;
329   }
330
331   mutt_message (_("Mailbox renamed."));
332   mutt_sleep (0);
333
334   p_delete(&mx.mbox);
335   return 0;
336
337 fail:
338   p_delete(&mx.mbox);
339   return -1;
340 }
341
342 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
343                                    struct browser_state *state,
344                                    short isparent)
345 {
346   char *name;
347   int noselect;
348   int noinferiors;
349   IMAP_MBOX mx;
350
351   if (imap_parse_path (state->folder, &mx)) {
352     debug_print (2, ("current folder %s makes no sense\n", state->folder));
353     return -1;
354   }
355
356   imap_cmd_start (idata, cmd);
357
358   do {
359     if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
360                                   &idata->delim) != 0) {
361       p_delete(&mx.mbox);
362       return -1;
363     }
364
365     if (name) {
366       /* Let a parent folder never be selectable for navigation */
367       if (isparent)
368         noselect = 1;
369       /* prune current folder from output */
370       if (isparent || str_ncmp (name, mx.mbox, str_len (name)))
371         imap_add_folder (idata->delim, name, noselect, noinferiors, state,
372                          isparent);
373     }
374   }
375   while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
376
377   p_delete(&mx.mbox);
378   return 0;
379 }
380
381 /* imap_add_folder: add a folder name to the browser list, formatting it as
382  *   necessary. */
383 static void imap_add_folder (char delim, char *folder, int noselect,
384                              int noinferiors, struct browser_state *state,
385                              short isparent)
386 {
387   char tmp[LONG_STRING];
388   char relpath[LONG_STRING];
389   IMAP_MBOX mx;
390
391   if (imap_parse_path (state->folder, &mx))
392     return;
393
394   imap_unmunge_mbox_name (folder);
395
396   if (state->entrylen + 1 == state->entrymax) {
397     mem_realloc (&state->entry,
398                   sizeof (struct folder_file) * (state->entrymax += 256));
399     memset (state->entry + state->entrylen, 0,
400             (sizeof (struct folder_file) *
401              (state->entrymax - state->entrylen)));
402   }
403
404   /* render superiors as unix-standard ".." */
405   if (isparent)
406     strfcpy (relpath, "../", sizeof (relpath));
407   /* strip current folder from target, to render a relative path */
408   else if (!str_ncmp (mx.mbox, folder, str_len (mx.mbox)))
409     strfcpy (relpath, folder + str_len (mx.mbox), sizeof (relpath));
410   else
411     strfcpy (relpath, folder, sizeof (relpath));
412
413   /* apply filemask filter. This should really be done at menu setup rather
414    * than at scan, since it's so expensive to scan. But that's big changes
415    * to browser.c */
416   if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
417     p_delete(&mx.mbox);
418     return;
419   }
420
421   imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
422   (state->entry)[state->entrylen].name = str_dup (tmp);
423
424   /* mark desc with delim in browser if it can have subfolders */
425   if (!isparent && !noinferiors && str_len (relpath) < sizeof (relpath) - 1) {
426     relpath[str_len (relpath) + 1] = '\0';
427     relpath[str_len (relpath)] = delim;
428   }
429
430   (state->entry)[state->entrylen].desc = str_dup (relpath);
431
432   (state->entry)[state->entrylen].imap = 1;
433   /* delimiter at the root is useless. */
434   if (folder[0] == '\0')
435     delim = '\0';
436   (state->entry)[state->entrylen].delim = delim;
437   (state->entry)[state->entrylen].selectable = !noselect;
438   (state->entry)[state->entrylen].inferiors = !noinferiors;
439   (state->entrylen)++;
440
441   p_delete(&mx.mbox);
442 }
443
444 static int compare_names (struct folder_file *a, struct folder_file *b)
445 {
446   return str_cmp (a->name, b->name);
447 }
448
449 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
450                                  IMAP_NAMESPACE_INFO * nsi, int nsilen,
451                                  int *nns)
452 {
453   char *s;
454   int n;
455   char ns[LONG_STRING];
456   char delim = '/';
457   int type;
458   int nsbused = 0;
459   int rc;
460
461   *nns = 0;
462   nsbuf[nsblen - 1] = '\0';
463
464   imap_cmd_start (idata, "NAMESPACE");
465
466   do {
467     if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
468       break;
469
470     s = imap_next_word (idata->cmd.buf);
471     if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
472       /* There are three sections to the response, User, Other, Shared,
473        * and maybe more by extension */
474       for (type = IMAP_NS_PERSONAL; *s; type++) {
475         s = imap_next_word (s);
476         if (*s && ascii_strncasecmp (s, "NIL", 3)) {
477           s++;
478           while (*s && *s != ')') {
479             s++;                /* skip ( */
480             /* copy namespace */
481             n = 0;
482             delim = '\0';
483
484             if (*s == '\"') {
485               s++;
486               while (*s && *s != '\"') {
487                 if (*s == '\\')
488                   s++;
489                 ns[n++] = *s;
490                 s++;
491               }
492               if (*s)
493                 s++;
494             }
495             else
496               while (*s && !ISSPACE (*s)) {
497                 ns[n++] = *s;
498                 s++;
499               }
500             ns[n] = '\0';
501             /* delim? */
502             s = imap_next_word (s);
503             /* delimiter is meaningless if namespace is "". Why does
504              * Cyrus provide one?! */
505             if (n && *s && *s == '\"') {
506               if (s[1] && s[2] == '\"')
507                 delim = s[1];
508               else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
509                 delim = s[2];
510             }
511             /* skip "" namespaces, they are already listed at the root */
512             if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
513               debug_print (3, ("adding %s\n", ns));
514               nsi->type = type;
515               /* Cyrus doesn't append the delimiter to the namespace,
516                * but UW-IMAP does. We'll strip it here and add it back
517                * as if it were a normal directory, from the browser */
518               if (n && (ns[n - 1] == delim))
519                 ns[--n] = '\0';
520               strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
521               nsi->prefix = nsbuf + nsbused;
522               nsbused += n + 1;
523               nsi->delim = delim;
524               nsi++;
525               (*nns)++;
526             }
527             while (*s && *s != ')')
528               s++;
529             if (*s)
530               s++;
531           }
532         }
533       }
534     }
535   }
536   while (rc == IMAP_CMD_CONTINUE);
537
538   if (rc != IMAP_CMD_OK)
539     return -1;
540
541   return 0;
542 }
543
544 /* Check which namespaces have contents */
545 static int browse_verify_namespace (IMAP_DATA * idata,
546                                     IMAP_NAMESPACE_INFO * nsi, int nns)
547 {
548   char buf[LONG_STRING];
549   int i = 0;
550   char *name;
551   char delim;
552
553   for (i = 0; i < nns; i++, nsi++) {
554     /* Cyrus gives back nothing if the % isn't added. This may return lots
555      * of data in some cases, I guess, but I currently feel that's better
556      * than invisible namespaces */
557     if (nsi->delim)
558       snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
559                 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix,
560                 nsi->delim);
561     else
562       snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
563                 option (OPTIMAPLSUB) ? "LSUB" : "LIST", nsi->prefix);
564
565     imap_cmd_start (idata, buf);
566
567     nsi->listable = 0;
568     nsi->home_namespace = 0;
569     do {
570       if (imap_parse_list_response (idata, &name, &nsi->noselect,
571                                     &nsi->noinferiors, &delim) != 0)
572         return -1;
573       nsi->listable |= (name != NULL);
574     }
575     while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
576   }
577
578   return 0;
579 }