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