sort out some prototypes, put them where they belong.
[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-lib/lib-lib.h>
21
22 #include <lib-ui/enter.h>
23
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   m_strcpy(list_cmd, sizeof(list_cmd),
74            option(OPTIMAPLSUB) ? "LSUB" : "string_list_t");
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 = m_strdup(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     m_strcpy(mbox, sizeof(mbox), buf);
102     n = m_strlen(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 = m_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 = m_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         imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
152       }
153
154       /* if our target isn't a folder, we are in our superior */
155       if (!state->folder) {
156         /* store folder with delimiter */
157         mbox[n++] = ctmp;
158         ctmp = mbox[n];
159         mbox[n] = '\0';
160         imap_qualify_path (buf, sizeof (buf), &mx, mbox);
161         state->folder = m_strdup(buf);
162       }
163       mbox[n] = ctmp;
164     }
165     /* "/bbbb/" -> add  "/", "aaaa/" -> add "" */
166     else {
167       char relpath[2];
168
169       /* folder may be "/" */
170       snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
171       if (showparents)
172         imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
173       if (!state->folder) {
174         imap_qualify_path (buf, sizeof (buf), &mx, relpath);
175         state->folder = m_strdup(buf);
176       }
177     }
178   }
179
180   /* no namespace, no folder: set folder to host only */
181   if (!state->folder) {
182     imap_qualify_path (buf, sizeof (buf), &mx, NULL);
183     state->folder = m_strdup(buf);
184   }
185
186   if (home_namespace && mbox[0] != '\0') {
187     /* Listing the home namespace, so INBOX should be included. Home 
188      * namespace is not "", so we have to list it explicitly. We ask the 
189      * server to see if it has descendants. */
190     if (browse_add_list_result (idata, "string_list_t \"\" \"INBOX\"", state, 0))
191       goto fail;
192   }
193
194   nsup = state->entrylen;
195
196   snprintf (buf, sizeof (buf), "%s%%", mbox);
197   imap_quote_string (buf2, sizeof (buf2), buf);
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       }
219   }
220
221   if (save_lsub)
222     set_option (OPTIMAPCHECKSUBSCRIBED);
223
224   p_delete(&mx.mbox);
225   return 0;
226
227 fail:
228   if (save_lsub)
229     set_option (OPTIMAPCHECKSUBSCRIBED);
230   p_delete(&mx.mbox);
231   return -1;
232 }
233
234 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
235 int imap_mailbox_create (const char *folder)
236 {
237   IMAP_DATA *idata;
238   IMAP_MBOX mx;
239   char buf[LONG_STRING];
240   short n;
241
242   if (imap_parse_path (folder, &mx) < 0) {
243     return -1;
244   }
245
246   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
247     goto fail;
248   }
249
250   m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
251
252   /* append a delimiter if necessary */
253   n = m_strlen(buf);
254   if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
255     buf[n++] = idata->delim;
256     buf[n] = '\0';
257   }
258
259   if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
260     goto fail;
261
262   if (!m_strlen(buf)) {
263     mutt_error (_("Mailbox must have a name."));
264     mutt_sleep (1);
265     goto fail;
266   }
267
268   if (imap_create_mailbox (idata, buf) < 0)
269     goto fail;
270
271   mutt_message _("Mailbox created.");
272
273   mutt_sleep (0);
274
275   p_delete(&mx.mbox);
276   return 0;
277
278 fail:
279   p_delete(&mx.mbox);
280   return -1;
281 }
282
283 int imap_mailbox_rename (const char *mailbox)
284 {
285   IMAP_DATA *idata;
286   IMAP_MBOX mx;
287   char buf[LONG_STRING];
288   char newname[SHORT_STRING];
289
290   if (imap_parse_path (mailbox, &mx) < 0) {
291     return -1;
292   }
293
294   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
295     goto fail;
296   }
297
298   snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
299
300   if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
301     goto fail;
302
303   if (!m_strlen(newname)) {
304     mutt_error (_("Mailbox must have a name."));
305     mutt_sleep (1);
306     goto fail;
307   }
308
309   if (imap_rename_mailbox (idata, &mx, newname) < 0) {
310     mutt_error (_("Rename failed: %s"), imap_get_qualifier (idata->cmd.buf));
311     mutt_sleep (1);
312     goto fail;
313   }
314
315   mutt_message (_("Mailbox renamed."));
316   mutt_sleep (0);
317
318   p_delete(&mx.mbox);
319   return 0;
320
321 fail:
322   p_delete(&mx.mbox);
323   return -1;
324 }
325
326 static int browse_add_list_result (IMAP_DATA * idata, const char *cmd,
327                                    struct browser_state *state,
328                                    short isparent)
329 {
330   char *name;
331   int noselect;
332   int noinferiors;
333   IMAP_MBOX mx;
334
335   if (imap_parse_path (state->folder, &mx)) {
336     return -1;
337   }
338
339   imap_cmd_start (idata, cmd);
340
341   do {
342     if (imap_parse_list_response (idata, &name, &noselect, &noinferiors,
343                                   &idata->delim) != 0) {
344       p_delete(&mx.mbox);
345       return -1;
346     }
347
348     if (name) {
349       /* Let a parent folder never be selectable for navigation */
350       if (isparent)
351         noselect = 1;
352       /* prune current folder from output */
353       if (isparent || m_strncmp(name, mx.mbox, m_strlen(name)))
354         imap_add_folder (idata->delim, name, noselect, noinferiors, state,
355                          isparent);
356     }
357   }
358   while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
359
360   p_delete(&mx.mbox);
361   return 0;
362 }
363
364 /* imap_add_folder: add a folder name to the browser list, formatting it as
365  *   necessary. */
366 static void imap_add_folder (char delim, char *folder, int noselect,
367                              int noinferiors, struct browser_state *state,
368                              short isparent)
369 {
370   char tmp[LONG_STRING];
371   char relpath[LONG_STRING];
372   IMAP_MBOX mx;
373
374   if (imap_parse_path (state->folder, &mx))
375     return;
376
377   imap_unmunge_mbox_name (folder);
378
379   if (state->entrylen + 1 == state->entrymax) {
380     p_realloc(&state->entry, state->entrymax += 256);
381     p_clear(state->entry + state->entrylen,
382             state->entrymax - state->entrylen);
383   }
384
385   /* render superiors as unix-standard ".." */
386   if (isparent)
387     m_strcpy(relpath, sizeof(relpath), "../");
388   /* strip current folder from target, to render a relative path */
389   else if (!m_strncmp(mx.mbox, folder, m_strlen(mx.mbox)))
390     m_strcpy(relpath, sizeof(relpath), folder + m_strlen(mx.mbox));
391   else
392     m_strcpy(relpath, sizeof(relpath), folder);
393
394   /* apply filemask filter. This should really be done at menu setup rather
395    * than at scan, since it's so expensive to scan. But that's big changes
396    * to browser.c */
397   if (!((regexec (Mask.rx, relpath, 0, NULL, 0) == 0) ^ Mask.not)) {
398     p_delete(&mx.mbox);
399     return;
400   }
401
402   imap_qualify_path (tmp, sizeof (tmp), &mx, folder);
403   (state->entry)[state->entrylen].name = m_strdup(tmp);
404
405   /* mark desc with delim in browser if it can have subfolders */
406   if (!isparent && !noinferiors && m_strlen(relpath) < ssizeof (relpath) - 1) {
407     relpath[m_strlen(relpath) + 1] = '\0';
408     relpath[m_strlen(relpath)] = delim;
409   }
410
411   (state->entry)[state->entrylen].desc = m_strdup(relpath);
412
413   (state->entry)[state->entrylen].imap = 1;
414   /* delimiter at the root is useless. */
415   if (folder[0] == '\0')
416     delim = '\0';
417   (state->entry)[state->entrylen].delim = delim;
418   (state->entry)[state->entrylen].selectable = !noselect;
419   (state->entry)[state->entrylen].inferiors = !noinferiors;
420   (state->entrylen)++;
421
422   p_delete(&mx.mbox);
423 }
424
425 static int compare_names (struct folder_file *a, struct folder_file *b)
426 {
427   return m_strcmp(a->name, b->name);
428 }
429
430 static int browse_get_namespace (IMAP_DATA * idata, char *nsbuf, int nsblen,
431                                  IMAP_NAMESPACE_INFO * nsi, int nsilen,
432                                  int *nns)
433 {
434   char *s;
435   int n;
436   char ns[LONG_STRING];
437   char delim = '/';
438   int type;
439   int nsbused = 0;
440   int rc;
441
442   *nns = 0;
443   nsbuf[nsblen - 1] = '\0';
444
445   imap_cmd_start (idata, "NAMESPACE");
446
447   do {
448     if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
449       break;
450
451     s = imap_next_word (idata->cmd.buf);
452     if (ascii_strncasecmp ("NAMESPACE", s, 9) == 0) {
453       /* There are three sections to the response, User, Other, Shared,
454        * and maybe more by extension */
455       for (type = IMAP_NS_PERSONAL; *s; type++) {
456         s = imap_next_word (s);
457         if (*s && ascii_strncasecmp (s, "NIL", 3)) {
458           s++;
459           while (*s && *s != ')') {
460             s++;                /* skip ( */
461             /* copy namespace */
462             n = 0;
463             delim = '\0';
464
465             if (*s == '\"') {
466               s++;
467               while (*s && *s != '\"') {
468                 if (*s == '\\')
469                   s++;
470                 ns[n++] = *s;
471                 s++;
472               }
473               if (*s)
474                 s++;
475             }
476             else
477               while (*s && !ISSPACE (*s)) {
478                 ns[n++] = *s;
479                 s++;
480               }
481             ns[n] = '\0';
482             /* delim? */
483             s = imap_next_word (s);
484             /* delimiter is meaningless if namespace is "". Why does
485              * Cyrus provide one?! */
486             if (n && *s && *s == '\"') {
487               if (s[1] && s[2] == '\"')
488                 delim = s[1];
489               else if (s[1] && s[1] == '\\' && s[2] && s[3] == '\"')
490                 delim = s[2];
491             }
492             /* skip "" namespaces, they are already listed at the root */
493             if ((ns[0] != '\0') && (nsbused < nsblen) && (*nns < nsilen)) {
494               nsi->type = type;
495               /* Cyrus doesn't append the delimiter to the namespace,
496                * but UW-IMAP does. We'll strip it here and add it back
497                * as if it were a normal directory, from the browser */
498               if (n && (ns[n - 1] == delim))
499                 ns[--n] = '\0';
500               strncpy (nsbuf + nsbused, ns, nsblen - nsbused - 1);
501               nsi->prefix = nsbuf + nsbused;
502               nsbused += n + 1;
503               nsi->delim = delim;
504               nsi++;
505               (*nns)++;
506             }
507             while (*s && *s != ')')
508               s++;
509             if (*s)
510               s++;
511           }
512         }
513       }
514     }
515   }
516   while (rc == IMAP_CMD_CONTINUE);
517
518   if (rc != IMAP_CMD_OK)
519     return -1;
520
521   return 0;
522 }
523
524 /* Check which namespaces have contents */
525 static int browse_verify_namespace (IMAP_DATA * idata,
526                                     IMAP_NAMESPACE_INFO * nsi, int nns)
527 {
528   char buf[LONG_STRING];
529   int i = 0;
530   char *name;
531   char delim;
532
533   for (i = 0; i < nns; i++, nsi++) {
534     /* Cyrus gives back nothing if the % isn't added. This may return lots
535      * of data in some cases, I guess, but I currently feel that's better
536      * than invisible namespaces */
537     if (nsi->delim)
538       snprintf (buf, sizeof (buf), "%s \"\" \"%s%c%%\"",
539                 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix,
540                 nsi->delim);
541     else
542       snprintf (buf, sizeof (buf), "%s \"\" \"%s%%\"",
543                 option (OPTIMAPLSUB) ? "LSUB" : "string_list_t", nsi->prefix);
544
545     imap_cmd_start (idata, buf);
546
547     nsi->listable = 0;
548     nsi->home_namespace = 0;
549     do {
550       if (imap_parse_list_response (idata, &name, &nsi->noselect,
551                                     &nsi->noinferiors, &delim) != 0)
552         return -1;
553       nsi->listable |= (name != NULL);
554     }
555     while ((ascii_strncmp (idata->cmd.buf, idata->cmd.seq, SEQLEN) != 0));
556   }
557
558   return 0;
559 }