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