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