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