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