more useless and cluttered things.
[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     if (mutt_bit_isset (idata->capabilities, NAMESPACE)) {
75       mutt_message _("Getting namespaces...");
76
77       if (browse_get_namespace (idata, nsbuf, sizeof (nsbuf),
78                                 nsi, sizeof (nsi), &nns) != 0)
79         goto fail;
80       if (browse_verify_namespace (idata, nsi, nns) != 0)
81         goto fail;
82     }
83   }
84
85   mutt_message _("Getting folder list...");
86
87   /* skip check for parents when at the root */
88   if (mx.mbox && mx.mbox[0] != '\0') {
89     imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
90     imap_munge_mbox_name (buf, sizeof (buf), mbox);
91     imap_unquote_string (buf);  /* As kludgy as it gets */
92     m_strcpy(mbox, sizeof(mbox), buf);
93     n = m_strlen(mbox);
94
95     /* if our target exists and has inferiors, enter it if we
96      * aren't already going to */
97     if (mbox[n - 1] != idata->delim) {
98       snprintf (buf, sizeof (buf), "%s \"\" \"%s\"", list_cmd, mbox);
99       imap_cmd_start (idata, buf);
100       do {
101         if (imap_parse_list_response (idata, &cur_folder, &noselect,
102                                       &noinferiors, &idata->delim) != 0)
103           goto fail;
104
105         if (cur_folder) {
106           imap_unmunge_mbox_name (cur_folder);
107
108           if (!noinferiors && cur_folder[0] &&
109               (n = m_strlen(mbox)) < LONG_STRING - 1) {
110             mbox[n++] = idata->delim;
111             mbox[n] = '\0';
112           }
113         }
114       } while (m_strncmp(idata->cmd.buf.data, idata->cmd.seq, SEQLEN));
115     }
116
117     /* if we're descending a folder, mark it as current in browser_state */
118     if (mbox[n - 1] == idata->delim) {
119       /* don't show parents in the home namespace */
120       if (!home_namespace)
121         showparents = 1;
122       imap_qualify_path (buf, sizeof (buf), &mx, mbox);
123       state->folder = m_strdup(buf);
124       n--;
125     }
126
127     /* Find superiors to list
128      * Note: UW-IMAP servers return folder + delimiter when asked to list
129      *  folder + delimiter. Cyrus servers don't. So we ask for folder,
130      *  and tack on delimiter ourselves.
131      * Further note: UW-IMAP servers return nothing when asked for 
132      *  NAMESPACES without delimiters at the end. Argh! */
133     for (n--; n >= 0 && mbox[n] != idata->delim; n--);
134     if (n > 0) {                /* "aaaa/bbbb/" -> "aaaa" */
135       /* forget the check, it is too delicate (see above). Have we ever
136        * had the parent not exist? */
137       ctmp = mbox[n];
138       mbox[n] = '\0';
139
140       if (showparents) {
141         imap_add_folder (idata->delim, mbox, 1, 0, state, 1);
142       }
143
144       /* if our target isn't a folder, we are in our superior */
145       if (!state->folder) {
146         /* store folder with delimiter */
147         mbox[n++] = ctmp;
148         ctmp = mbox[n];
149         mbox[n] = '\0';
150         imap_qualify_path (buf, sizeof (buf), &mx, mbox);
151         state->folder = m_strdup(buf);
152       }
153       mbox[n] = ctmp;
154     }
155     /* "/bbbb/" -> add  "/", "aaaa/" -> add "" */
156     else {
157       char relpath[2];
158
159       /* folder may be "/" */
160       snprintf (relpath, sizeof (relpath), "%c", n < 0 ? '\0' : idata->delim);
161       if (showparents)
162         imap_add_folder (idata->delim, relpath, 1, 0, state, 1);
163       if (!state->folder) {
164         imap_qualify_path (buf, sizeof (buf), &mx, relpath);
165         state->folder = m_strdup(buf);
166       }
167     }
168   }
169
170   /* no namespace, no folder: set folder to host only */
171   if (!state->folder) {
172     imap_qualify_path (buf, sizeof (buf), &mx, NULL);
173     state->folder = m_strdup(buf);
174   }
175
176   if (home_namespace && mbox[0] != '\0') {
177     /* Listing the home namespace, so INBOX should be included. Home 
178      * namespace is not "", so we have to list it explicitly. We ask the 
179      * server to see if it has descendants. */
180     if (browse_add_list_result (idata, "LIST \"\" \"INBOX\"", state, 0))
181       goto fail;
182   }
183
184   nsup = state->entrylen;
185
186   snprintf (buf, sizeof (buf), "%s%%", mbox);
187   imap_quote_string (buf2, sizeof (buf2), buf);
188   snprintf (buf, sizeof (buf), "%s \"\" %s", list_cmd, buf2);
189   if (browse_add_list_result (idata, buf, state, 0))
190     goto fail;
191
192   if (!state->entrylen) {
193     mutt_error _("No such folder");
194
195     goto fail;
196   }
197
198   mutt_clear_error ();
199
200   qsort (&(state->entry[nsup]), state->entrylen - nsup,
201          sizeof (state->entry[0]),
202          (int (*)(const void *, const void *)) compare_names);
203   if (home_namespace) {         /* List additional namespaces */
204     for (i = 0; i < nns; i++)
205       if (nsi[i].listable && !nsi[i].home_namespace) {
206         imap_add_folder (nsi[i].delim, nsi[i].prefix, nsi[i].noselect,
207                          nsi[i].noinferiors, state, 0);
208       }
209   }
210
211   if (save_lsub)
212     set_option (OPTIMAPCHECKSUBSCRIBED);
213
214   p_delete(&mx.mbox);
215   return 0;
216
217 fail:
218   if (save_lsub)
219     set_option (OPTIMAPCHECKSUBSCRIBED);
220   p_delete(&mx.mbox);
221   return -1;
222 }
223
224 /* imap_mailbox_create: Prompt for a new mailbox name, and try to create it */
225 int imap_mailbox_create (const char *folder)
226 {
227   IMAP_DATA *idata;
228   IMAP_MBOX mx;
229   char buf[LONG_STRING];
230   short n;
231
232   if (imap_parse_path (folder, &mx) < 0) {
233     return -1;
234   }
235
236   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
237     goto fail;
238   }
239
240   m_strcpy(buf, sizeof(buf), NONULL(mx.mbox));
241
242   /* append a delimiter if necessary */
243   n = m_strlen(buf);
244   if (n && (n < ssizeof (buf) - 1) && (buf[n - 1] != idata->delim)) {
245     buf[n++] = idata->delim;
246     buf[n] = '\0';
247   }
248
249   if (mutt_get_field (_("Create mailbox: "), buf, sizeof (buf), M_FILE) < 0)
250     goto fail;
251
252   if (!m_strlen(buf)) {
253     mutt_error (_("Mailbox must have a name."));
254     mutt_sleep (1);
255     goto fail;
256   }
257
258   if (imap_create_mailbox (idata, buf) < 0)
259     goto fail;
260
261   mutt_message _("Mailbox created.");
262
263   mutt_sleep (0);
264
265   p_delete(&mx.mbox);
266   return 0;
267
268 fail:
269   p_delete(&mx.mbox);
270   return -1;
271 }
272
273 int imap_mailbox_rename (const char *mailbox)
274 {
275   IMAP_DATA *idata;
276   IMAP_MBOX mx;
277   char buf[LONG_STRING];
278   char newname[STRING];
279
280   if (imap_parse_path (mailbox, &mx) < 0) {
281     return -1;
282   }
283
284   if (!(idata = imap_conn_find (&mx.account, M_IMAP_CONN_NONEW))) {
285     goto fail;
286   }
287
288   snprintf (buf, sizeof (buf), _("Rename mailbox %s to: "), mx.mbox);
289
290   if (mutt_get_field (buf, newname, sizeof (newname), M_FILE) < 0)
291     goto fail;
292
293   if (!m_strlen(newname)) {
294     mutt_error (_("Mailbox must have a name."));
295     mutt_sleep (1);
296     goto fail;
297   }
298
299   if (imap_rename_mailbox (idata, &mx, newname) < 0) {
300     mutt_error (_("Rename failed: %s"),
301                 imap_get_qualifier(idata->cmd.buf.data));
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.data, 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.data);
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.data, idata->cmd.seq, SEQLEN) != 0));
545   }
546
547   return 0;
548 }