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