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