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