93a5d76521494c097d7aaad776bd920a4fe04fa3
[apps/madmutt.git] / browser.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <lib-lib/lib-lib.h>
11
12 #include <dirent.h>
13 #include <pwd.h>
14 #include <grp.h>
15
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
20
21 #include "mutt.h"
22 #include "mx.h"
23 #include "buffy.h"
24 #include "sort.h"
25 #include "browser.h"
26 #include "attach.h"
27
28 #include <imap/imap.h>
29 #include <imap/mx_imap.h>
30 #ifdef USE_NNTP
31 #include "nntp.h"
32 #endif
33
34 static struct mapping_t FolderHelp[] = {
35   {N_("Exit"), OP_EXIT},
36   {N_("Chdir"), OP_CHANGE_DIRECTORY},
37   {N_("Mask"), OP_ENTER_MASK},
38   {N_("Help"), OP_HELP},
39   {NULL, OP_NULL}
40 };
41
42 #ifdef USE_NNTP
43 static struct mapping_t FolderNewsHelp[] = {
44   {N_("Exit"), OP_EXIT},
45   {N_("List"), OP_TOGGLE_MAILBOXES},
46   {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
47   {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
48   {N_("Catchup"), OP_CATCHUP},
49   {N_("Mask"), OP_ENTER_MASK},
50   {N_("Help"), OP_HELP},
51   {NULL, OP_NULL}
52 };
53 #endif
54
55 typedef struct folder_t {
56   struct folder_file *ff;
57   int num;
58 } FOLDER;
59
60 static char LastDir[_POSIX_PATH_MAX] = "";
61 static char LastDirBackup[_POSIX_PATH_MAX] = "";
62
63 /* Frees up the memory allocated for the local-global variables.  */
64 static void destroy_state (struct browser_state *state)
65 {
66   int c;
67
68   for (c = 0; c < state->entrylen; c++) {
69     p_delete(&((state->entry)[c].name));
70     p_delete(&((state->entry)[c].desc));
71     p_delete(&((state->entry)[c].st));
72   }
73   p_delete(&state->folder);
74   p_delete(&state->entry);
75 }
76
77 static int browser_compare_subject (const void *a, const void *b)
78 {
79   struct folder_file *pa = (struct folder_file *) a;
80   struct folder_file *pb = (struct folder_file *) b;
81
82   int r = strcoll(NONULL(pa->name), NONULL(pb->name));
83
84   return ((BrowserSort & SORT_REVERSE) ? -r : r);
85 }
86
87 static int browser_compare_date (const void *a, const void *b)
88 {
89   struct folder_file *pa = (struct folder_file *) a;
90   struct folder_file *pb = (struct folder_file *) b;
91
92   int r = pa->mtime - pb->mtime;
93
94   return ((BrowserSort & SORT_REVERSE) ? -r : r);
95 }
96
97 static int browser_compare_size (const void *a, const void *b)
98 {
99   struct folder_file *pa = (struct folder_file *) a;
100   struct folder_file *pb = (struct folder_file *) b;
101
102   int r = pa->size - pb->size;
103
104   return ((BrowserSort & SORT_REVERSE) ? -r : r);
105 }
106
107 static void browser_sort (struct browser_state *state)
108 {
109   int (*f) (const void *, const void *);
110
111   switch (BrowserSort & SORT_MASK) {
112   case SORT_ORDER:
113     return;
114   case SORT_DATE:
115 #ifdef USE_NNTP
116     if (option (OPTNEWS))
117       return;
118 #endif
119     f = browser_compare_date;
120     break;
121   case SORT_SIZE:
122 #ifdef USE_NNTP
123     if (option (OPTNEWS))
124       return;
125 #endif
126     f = browser_compare_size;
127     break;
128   case SORT_SUBJECT:
129   default:
130     f = browser_compare_subject;
131     break;
132   }
133   qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
134 }
135
136 static int link_is_dir (const char *folder, const char *path)
137 {
138   struct stat st;
139   char fullpath[_POSIX_PATH_MAX];
140
141   mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
142
143   if (stat (fullpath, &st) == 0)
144     return (S_ISDIR (st.st_mode));
145   else
146     return 0;
147 }
148
149 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
150                                       const char *src, const char *fmt,
151                                       const char *ifstring,
152                                       const char *elsestring,
153                                       unsigned long data, format_flag flags)
154 {
155   char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
156   const char *t_fmt;
157   time_t tnow;
158   FOLDER *folder = (FOLDER *) data;
159   struct passwd *pw;
160   struct group *gr;
161   int optional = (flags & M_FORMAT_OPTIONAL);
162
163   switch (op) {
164   case 'C':
165     snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
166     snprintf (dest, destlen, tmp, folder->num + 1);
167     break;
168
169   case 'd':
170     if (folder->ff->st != NULL) {
171       tnow = time (NULL);
172       t_fmt =
173         tnow - folder->ff->st->st_mtime <
174         31536000 ? "%b %d %H:%M" : "%b %d  %Y";
175       strftime (date, sizeof (date), t_fmt,
176                 localtime (&folder->ff->st->st_mtime));
177       mutt_format_s (dest, destlen, fmt, date);
178     }
179     else
180       mutt_format_s (dest, destlen, fmt, "");
181     break;
182
183   case 'f':
184     {
185       const char *s;
186
187       if (folder->ff->imap)
188         s = NONULL (folder->ff->desc);
189       else
190         s = NONULL (folder->ff->name);
191
192       snprintf (fn, sizeof (fn), "%s%s", s,
193                 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
194                                   (S_ISDIR (folder->ff->st->st_mode) ? "/" :
195                                    ((folder->ff->st->st_mode & S_IXUSR) !=
196                                     0 ? "*" : ""))) : "");
197
198       mutt_format_s (dest, destlen, fmt, fn);
199       break;
200     }
201   case 'F':
202     if (folder->ff->st != NULL) {
203       snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
204                 S_ISDIR (folder->ff->st->
205                          st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
206                                                     st_mode) ? 'l' : '-'),
207                 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
208                 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
209                 (folder->ff->st->st_mode & S_ISUID) !=
210                 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
211                 0 ? 'x' : '-',
212                 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
213                 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
214                 (folder->ff->st->st_mode & S_ISGID) !=
215                 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
216                 0 ? 'x' : '-',
217                 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
218                 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
219                 (folder->ff->st->st_mode & S_ISVTX) !=
220                 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
221                 0 ? 'x' : '-');
222       mutt_format_s (dest, destlen, fmt, permission);
223     }
224     else if (folder->ff->imap) {
225       /* mark folders with subfolders AND mail */
226       snprintf (permission, sizeof (permission), "IMAP %c",
227                 (folder->ff->inferiors
228                  && folder->ff->selectable) ? '+' : ' ');
229       mutt_format_s (dest, destlen, fmt, permission);
230     }
231     else
232       mutt_format_s (dest, destlen, fmt, "");
233     break;
234
235   case 'g':
236     if (folder->ff->st != NULL) {
237       if ((gr = getgrgid (folder->ff->st->st_gid)))
238         mutt_format_s (dest, destlen, fmt, gr->gr_name);
239       else {
240         snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
241         snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
242       }
243     }
244     else
245       mutt_format_s (dest, destlen, fmt, "");
246     break;
247
248   case 'l':
249     if (folder->ff->st != NULL) {
250       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
251       snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
252     }
253     else
254       mutt_format_s (dest, destlen, fmt, "");
255     break;
256
257   case 'N':
258     if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
259       if (!optional) {
260         snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
261         snprintf (dest, destlen, tmp, folder->ff->new);
262       }
263       else if (!folder->ff->new)
264         optional = 0;
265       break;
266     }
267     snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
268     snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
269     break;
270
271   case 's':
272     if (folder->ff->st != NULL) {
273       snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
274       snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
275     }
276     else
277       mutt_format_s (dest, destlen, fmt, "");
278     break;
279
280   case 't':
281     snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
282     snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
283     break;
284
285   case 'u':
286     if (folder->ff->st != NULL) {
287       if ((pw = getpwuid (folder->ff->st->st_uid)))
288         mutt_format_s (dest, destlen, fmt, pw->pw_name);
289       else {
290         snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
291         snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
292       }
293     }
294     else
295       mutt_format_s (dest, destlen, fmt, "");
296     break;
297
298   default:
299     snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
300     snprintf (dest, destlen, tmp, op);
301     break;
302   }
303
304   if (optional)
305     mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
306   else if (flags & M_FORMAT_OPTIONAL)
307     mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
308
309   return (src);
310 }
311
312 #ifdef USE_NNTP
313 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
314                                          const char *src, const char *fmt,
315                                          const char *ifstring,
316                                          const char *elsestring,
317                                          unsigned long data,
318                                          format_flag flags)
319 {
320   char fn[SHORT_STRING], tmp[SHORT_STRING];
321   FOLDER *folder = (FOLDER *) data;
322
323   switch (op) {
324   case 'C':
325     snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
326     snprintf (dest, destlen, tmp, folder->num + 1);
327     break;
328
329   case 'f':
330     m_strcpy(fn, sizeof(fn), folder->ff->name);
331     snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
332     snprintf (dest, destlen, tmp, fn);
333     break;
334
335   case 'N':
336     snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
337     if (folder->ff->nd->subscribed)
338       snprintf (dest, destlen, tmp, ' ');
339     else
340       snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
341     break;
342
343   case 'M':
344     snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
345     if (folder->ff->nd->deleted)
346       snprintf (dest, destlen, tmp, 'D');
347     else
348       snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
349     break;
350
351   case 's':
352     if (flags & M_FORMAT_OPTIONAL) {
353       if (folder->ff->nd->unread != 0)
354         mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
355                            data, flags);
356       else
357         mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
358                            data, flags);
359     }
360     else if (Context && Context->data == folder->ff->nd) {
361       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
362       snprintf (dest, destlen, tmp, Context->unread);
363     }
364     else {
365       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
366       snprintf (dest, destlen, tmp, folder->ff->nd->unread);
367     }
368     break;
369
370   case 'n':
371     if (Context && Context->data == folder->ff->nd) {
372       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
373       snprintf (dest, destlen, tmp, Context->new);
374     }
375     else if (option (OPTMARKOLD) &&
376              folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
377              folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
378       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379       snprintf (dest, destlen, tmp,
380                 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
381     }
382     else {
383       snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384       snprintf (dest, destlen, tmp, folder->ff->nd->unread);
385     }
386     break;
387
388   case 'd':
389     if (folder->ff->nd->desc != NULL) {
390       snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
391       snprintf (dest, destlen, tmp, folder->ff->nd->desc);
392     }
393     else {
394       snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
395       snprintf (dest, destlen, tmp, "");
396     }
397     break;
398   }
399   return (src);
400 }
401 #endif /* USE_NNTP */
402
403 #ifdef USE_NNTP
404 static void add_folder (MUTTMENU * m, struct browser_state *state,
405                         const char *name, const struct stat *s,
406                         void *data, int new)
407 #else
408 static void add_folder (MUTTMENU * m, struct browser_state *state,
409                         const char *name, const struct stat *s,
410                         int new)
411 #endif
412 {
413   if (state->entrylen == state->entrymax) {
414     /* need to allocate more space */
415     p_realloc(&state->entry, state->entrymax += 256);
416     p_clear(&state->entry[state->entrylen], 256);
417     if (m)
418       m->data = state->entry;
419   }
420
421   if (s != NULL) {
422     (state->entry)[state->entrylen].mode  = s->st_mode;
423     (state->entry)[state->entrylen].mtime = s->st_mtime;
424     (state->entry)[state->entrylen].size  = s->st_size;
425     (state->entry)[state->entrylen].st    = p_dup(s, 1);
426   }
427
428   (state->entry)[state->entrylen].new = new;
429   (state->entry)[state->entrylen].name = m_strdup(name);
430   (state->entry)[state->entrylen].desc = m_strdup(name);
431   (state->entry)[state->entrylen].imap = 0;
432 #ifdef USE_NNTP
433   if (option (OPTNEWS))
434     (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
435 #endif
436   (state->entrylen)++;
437 }
438
439 static void init_state (struct browser_state *state, MUTTMENU * menu)
440 {
441   state->entrylen = 0;
442   state->entrymax = 256;
443   state->entry = p_new(struct folder_file, state->entrymax);
444   state->imap_browse = 0;
445   if (menu)
446     menu->data = state->entry;
447 }
448
449 /* get list of all files/newsgroups with mask */
450 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
451                               char *d, const char *prefix)
452 {
453 #ifdef USE_NNTP
454   if (option (OPTNEWS)) {
455     string_list_t *tmp;
456     NNTP_DATA *data;
457     NNTP_SERVER *news = CurrentNewsSrv;
458
459 /*  buffy_check (0); */
460     init_state (state, menu);
461
462     for (tmp = news->list; tmp; tmp = tmp->next) {
463       if (!(data = (NNTP_DATA *) tmp->data))
464         continue;
465       nntp_sync_sidebar (data);
466       if (prefix && *prefix && strncmp (prefix, data->group,
467                                         m_strlen(prefix)) != 0)
468         continue;
469       if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
470         continue;
471 #ifdef USE_NNTP
472       add_folder (menu, state, data->group, NULL, data, data->new);
473 #else
474       add_folder (menu, state, data->group, NULL, data->new);
475 #endif
476     }
477     sidebar_draw ();
478   }
479   else
480 #endif /* USE_NNTP */
481   {
482     struct stat s;
483     DIR *dp;
484     struct dirent *de;
485     char buffer[_POSIX_PATH_MAX + SHORT_STRING];
486     int i = -1;
487
488     while (stat (d, &s) == -1) {
489       if (errno == ENOENT) {
490         /* The last used directory is deleted, try to use the parent dir. */
491         char *c = strrchr (d, '/');
492
493         if (c && (c > d)) {
494           *c = 0;
495           continue;
496         }
497       }
498       mutt_perror (d);
499       return (-1);
500     }
501
502     if (!S_ISDIR (s.st_mode)) {
503       mutt_error (_("%s is not a directory."), d);
504       return (-1);
505     }
506
507     buffy_check (0);
508
509     if ((dp = opendir (d)) == NULL) {
510       mutt_perror (d);
511       return (-1);
512     }
513
514     init_state (state, menu);
515
516     while ((de = readdir (dp)) != NULL) {
517       if (m_strcmp(de->d_name, ".") == 0)
518         continue;               /* we don't need . */
519
520       if (prefix && *prefix
521           && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
522         continue;
523       if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
524         continue;
525
526       mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
527       if (lstat (buffer, &s) == -1)
528         continue;
529
530       if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
531           (!S_ISLNK (s.st_mode)))
532         continue;
533
534       i = buffy_lookup (buffer);
535 #ifdef USE_NNTP
536       add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
537 #else
538       add_folder (menu, state, de->d_name, &s, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
539 #endif
540     }
541     closedir (dp);
542   }
543   sidebar_draw ();
544   browser_sort (state);
545   return 0;
546 }
547
548 /* get list of mailboxes/subscribed newsgroups */
549 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
550 {
551   struct stat s;
552   char buffer[LONG_STRING];
553
554 #ifdef USE_NNTP
555   if (option (OPTNEWS)) {
556     string_list_t *tmp;
557     NNTP_DATA *data;
558     NNTP_SERVER *news = CurrentNewsSrv;
559
560 /*  buffy_check (0); */
561     init_state (state, menu);
562
563     for (tmp = news->list; tmp; tmp = tmp->next) {
564       if ((data = (NNTP_DATA*) tmp->data) == NULL)
565         continue;
566       nntp_sync_sidebar (data);
567       if ((data->new || (data->subscribed && 
568                          (!option (OPTSHOWONLYUNREAD)|| data->unread))))
569         add_folder (menu, state, data->group, NULL, data, data->new);
570     }
571     sidebar_draw ();
572   }
573   else
574 #endif
575   {
576     int i = 0;
577     BUFFY* tmp;
578
579     if (!Incoming)
580       return (-1);
581     buffy_check (0);
582
583     init_state (state, menu);
584
585     for (i = 0; i < Incoming->length; i++) {
586       tmp = (BUFFY*) Incoming->data[i];
587       tmp->magic = mx_get_magic (tmp->path);
588       if (tmp->magic == M_IMAP) {
589 #ifdef USE_NNTP
590         add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
591 #else
592         add_folder (menu, state, tmp->path, NULL, tmp->new);
593 #endif
594         continue;
595       }
596       if (tmp->magic == M_POP) {
597 #ifdef USE_NNTP
598         add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
599 #else
600         add_folder (menu, state, tmp->path, NULL, tmp->new);
601 #endif
602         continue;
603       }
604 #ifdef USE_NNTP
605       if (tmp->magic == M_NNTP) {
606         add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
607         continue;
608       }
609 #endif
610       if (lstat (tmp->path, &s) == -1)
611         continue;
612
613       if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
614           (!S_ISLNK (s.st_mode)))
615         continue;
616
617       m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
618       mutt_pretty_mailbox (buffer);
619
620 #ifdef USE_NNTP
621       add_folder (menu, state, buffer, &s, NULL, tmp->new);
622 #else
623       add_folder (menu, state, buffer, &s, tmp->new);
624 #endif
625     }
626   }
627   browser_sort (state);
628   return 0;
629 }
630
631 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
632 {
633 #ifdef USE_NNTP
634   if (option (OPTNEWS))
635     return (regexec
636             (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
637 #endif
638   return (regexec
639           (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
640 }
641
642 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
643 {
644   FOLDER folder;
645
646   folder.ff = &((struct folder_file *) menu->data)[num];
647   folder.num = num;
648
649 #ifdef USE_NNTP
650   if (option (OPTNEWS))
651     mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
652                        (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
653   else
654 #endif
655     mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
656                        (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
657 }
658
659 static void init_menu (struct browser_state *state, MUTTMENU * menu,
660                        char *title, ssize_t titlelen, int buffy)
661 {
662   char path[_POSIX_PATH_MAX];
663
664   menu->max = state->entrylen;
665
666   if (menu->current >= menu->max)
667     menu->current = menu->max - 1;
668   if (menu->current < 0)
669     menu->current = 0;
670   if (menu->top > menu->current)
671     menu->top = 0;
672
673   menu->tagged = 0;
674
675 #ifdef USE_NNTP
676   if (option (OPTNEWS)) {
677     if (buffy)
678       snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
679     else
680       snprintf (title, titlelen, _("Newsgroups on server [%s]"),
681                 CurrentNewsSrv->conn->account.host);
682   }
683   else
684 #endif
685   if (buffy)
686     snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
687   else {
688     m_strcpy(path, sizeof(path), LastDir);
689     mutt_pretty_mailbox (path);
690     if (state->imap_browse && option (OPTIMAPLSUB))
691       snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
692                 path, NONULL (Mask.pattern));
693     else
694       snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
695                 path, NONULL (Mask.pattern));
696   }
697   menu->redraw = REDRAW_FULL;
698 }
699
700 static int file_tag (MUTTMENU * menu, int n, int m)
701 {
702   struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
703   int ot;
704
705   if (S_ISDIR (ff->mode)
706       || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
707     mutt_error _("Can't attach a directory!");
708
709     return 0;
710   }
711
712   ot = ff->tagged;
713   ff->tagged = (m >= 0 ? m : !ff->tagged);
714
715   return ff->tagged - ot;
716 }
717
718 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
719                         int *numfiles)
720 {
721   char buf[_POSIX_PATH_MAX];
722   char prefix[_POSIX_PATH_MAX] = "";
723   char helpstr[SHORT_STRING];
724   char title[STRING];
725   struct browser_state state;
726   MUTTMENU *menu;
727   struct stat st;
728   int i, killPrefix = 0;
729   int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
730   int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
731   int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
732
733   buffy = buffy && folder;
734
735   p_clear(&state, 1);
736
737   if (!folder)
738     m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
739
740 #ifdef USE_NNTP
741   if (option (OPTNEWS)) {
742     if (*f)
743       m_strcpy(prefix, sizeof(prefix), f);
744     else {
745       string_list_t *list;
746
747       /* default state for news reader mode is browse subscribed newsgroups */
748       buffy = 0;
749       for (list = CurrentNewsSrv->list; list; list = list->next) {
750         NNTP_DATA *data = (NNTP_DATA *) list->data;
751
752         if (data && data->subscribed) {
753           buffy = 1;
754           break;
755         }
756       }
757     }
758   }
759   else
760 #endif
761   if (*f) {
762     mutt_expand_path (f, flen);
763     if (imap_is_magic (f, NULL) == M_IMAP) {
764       init_state (&state, NULL);
765       state.imap_browse = 1;
766       if (!imap_browse (f, &state))
767         m_strcpy(LastDir, sizeof(LastDir), state.folder);
768     }
769     else {
770       for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
771       if (i > 0) {
772         if (f[0] == '/') {
773           i = MIN(ssizeof(LastDir) - 1, i);
774           m_strcpy(LastDir, sizeof(LastDir), f);
775         }
776         else {
777           getcwd(LastDir, sizeof(LastDir));
778           m_strcat(LastDir, sizeof(LastDir), "/");
779           m_strncat(LastDir, sizeof(LastDir), f, i);
780         }
781       }
782       else {
783         if (f[0] == '/')
784           strcpy (LastDir, "/");        /* __STRCPY_CHECKED__ */
785         else
786           getcwd (LastDir, sizeof (LastDir));
787       }
788
789       if (i <= 0 && f[0] != '/')
790         m_strcpy(prefix, sizeof(prefix), f);
791       else
792         m_strcpy(prefix, sizeof(prefix), f + i + 1);
793       killPrefix = 1;
794     }
795   }
796   else {
797     if (!folder)
798       getcwd (LastDir, sizeof (LastDir));
799     else if (!LastDir[0])
800       m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
801
802     if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
803       init_state (&state, NULL);
804       state.imap_browse = 1;
805       imap_browse (LastDir, &state);
806       browser_sort (&state);
807     }
808   }
809
810   *f = 0;
811
812   if (buffy) {
813     if (examine_mailboxes (NULL, &state) == -1)
814       goto bail;
815   }
816   else
817   if (!state.imap_browse)
818     if (examine_directory (NULL, &state, LastDir, prefix) == -1)
819       goto bail;
820
821   menu = mutt_new_menu ();
822   menu->menu = MENU_FOLDER;
823   menu->make_entry = folder_entry;
824   menu->search = select_file_search;
825   menu->title = title;
826   menu->data = state.entry;
827   if (multiple)
828     menu->tag = file_tag;
829
830   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
831 #ifdef USE_NNTP
832                                   (option (OPTNEWS)) ? FolderNewsHelp :
833 #endif
834                                   FolderHelp);
835
836   init_menu (&state, menu, title, sizeof (title), buffy);
837
838   for (;;) {
839     switch (i = mutt_menuLoop (menu)) {
840     case OP_GENERIC_SELECT_ENTRY:
841
842       if (!state.entrylen) {
843         mutt_error _("No files match the file mask");
844
845         break;
846       }
847
848       if (S_ISDIR (state.entry[menu->current].mode) ||
849           (S_ISLNK (state.entry[menu->current].mode) &&
850            link_is_dir (LastDir, state.entry[menu->current].name))
851           || state.entry[menu->current].inferiors
852         ) {
853         /* make sure this isn't a MH or maildir mailbox */
854         if (buffy) {
855           m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
856           mutt_expand_path (buf, sizeof (buf));
857         }
858         else if (state.imap_browse) {
859           m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
860         }
861         else
862           mutt_concat_path(buf, sizeof(buf), LastDir,
863                            state.entry[menu->current].name);
864
865         if ((mx_get_magic (buf) <= 0)
866             || state.entry[menu->current].inferiors)
867         {
868           char OldLastDir[_POSIX_PATH_MAX];
869
870           /* save the old directory */
871           m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
872
873           if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
874             if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
875               strcat (LastDir, "/..");  /* __STRCAT_CHECKED__ */
876             else {
877               char *p = strrchr (LastDir + 1, '/');
878
879               if (p)
880                 *p = 0;
881               else {
882                 if (LastDir[0] == '/')
883                   LastDir[1] = 0;
884                 else
885                   strcat (LastDir, "/..");      /* __STRCAT_CHECKED__ */
886               }
887             }
888           }
889           else if (buffy) {
890             m_strcpy(LastDir, sizeof(LastDir),
891                      state.entry[menu->current].name);
892             mutt_expand_path (LastDir, sizeof (LastDir));
893           }
894           else if (state.imap_browse) {
895             int n;
896             ciss_url_t url;
897
898             m_strcpy(LastDir, sizeof(LastDir),
899                      state.entry[menu->current].name);
900             /* tack on delimiter here */
901             n = m_strlen(LastDir) + 1;
902
903             /* special case "" needs no delimiter */
904             url_parse_ciss (&url, state.entry[menu->current].name);
905             if (url.path &&
906                 (state.entry[menu->current].delim != '\0') &&
907                 (n < ssizeof (LastDir))) {
908               LastDir[n] = '\0';
909               LastDir[n - 1] = state.entry[menu->current].delim;
910             }
911           }
912           else {
913             char tmp[_POSIX_PATH_MAX];
914
915             mutt_concat_path(tmp, sizeof(tmp), LastDir,
916                              state.entry[menu->current].name);
917             m_strcpy(LastDir, sizeof(LastDir), tmp);
918           }
919
920           destroy_state (&state);
921           if (killPrefix) {
922             prefix[0] = 0;
923             killPrefix = 0;
924           }
925           buffy = 0;
926           if (state.imap_browse) {
927             init_state (&state, NULL);
928             state.imap_browse = 1;
929             imap_browse (LastDir, &state);
930             browser_sort (&state);
931             menu->data = state.entry;
932           }
933           else
934           if (examine_directory (menu, &state, LastDir, prefix) == -1) {
935             /* try to restore the old values */
936             m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
937             if (examine_directory (menu, &state, LastDir, prefix) == -1) {
938               m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
939               goto bail;
940             }
941           }
942           menu->current = 0;
943           menu->top = 0;
944           init_menu (&state, menu, title, sizeof (title), buffy);
945           break;
946         }
947       }
948
949 #ifdef USE_NNTP
950       if (buffy || option (OPTNEWS))    /* news have not path */
951 #else
952       if (buffy)
953 #endif
954       {
955         m_strcpy(f, flen, state.entry[menu->current].name);
956         mutt_expand_path (f, flen);
957       }
958       else if (state.imap_browse)
959         m_strcpy(f, flen, state.entry[menu->current].name);
960       else
961         mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
962
963       /* Fall through to OP_EXIT */
964
965     case OP_EXIT:
966
967       if (multiple) {
968         char **tfiles;
969         int j;
970         int h;
971
972         if (menu->tagged) {
973           *numfiles = menu->tagged;
974           tfiles = p_new(char *, *numfiles);
975           for (h = 0, j = 0; h < state.entrylen; i++) {
976             struct folder_file ff = state.entry[i];
977             char full[_POSIX_PATH_MAX];
978
979             if (ff.tagged) {
980               mutt_concat_path(full, sizeof(full), LastDir, ff.name);
981               mutt_expand_path (full, sizeof (full));
982               tfiles[j++] = m_strdup(full);
983             }
984           }
985           *files = tfiles;
986         }
987         else if (f[0]) {        /* no tagged entries. return selected entry */
988           *numfiles = 1;
989           tfiles = p_new(char *, *numfiles);
990           mutt_expand_path (f, flen);
991           tfiles[0] = m_strdup(f);
992           *files = tfiles;
993         }
994       }
995
996       destroy_state (&state);
997       mutt_menuDestroy (&menu);
998       goto bail;
999
1000     case OP_BROWSER_TELL:
1001       if (state.entrylen)
1002         mutt_message ("%s", state.entry[menu->current].name);
1003       break;
1004
1005     case OP_BROWSER_TOGGLE_LSUB:
1006       if (option (OPTIMAPLSUB)) {
1007         unset_option (OPTIMAPLSUB);
1008       }
1009       else {
1010         set_option (OPTIMAPLSUB);
1011       }
1012       mutt_ungetch (0, OP_CHECK_NEW);
1013       break;
1014
1015     case OP_CREATE_MAILBOX:
1016       if (!state.imap_browse)
1017         mutt_error (_("Create is only supported for IMAP mailboxes"));
1018       else {
1019         imap_mailbox_create (LastDir);
1020         /* TODO: find a way to detect if the new folder would appear in
1021          *   this window, and insert it without starting over. */
1022         destroy_state (&state);
1023         init_state (&state, NULL);
1024         state.imap_browse = 1;
1025         imap_browse (LastDir, &state);
1026         browser_sort (&state);
1027         menu->data = state.entry;
1028         menu->current = 0;
1029         menu->top = 0;
1030         init_menu (&state, menu, title, sizeof (title), buffy);
1031         MAYBE_REDRAW (menu->redraw);
1032       }
1033       break;
1034
1035     case OP_RENAME_MAILBOX:
1036       if (!state.entry[menu->current].imap)
1037         mutt_error (_("Rename is only supported for IMAP mailboxes"));
1038       else {
1039         int nentry = menu->current;
1040
1041         if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1042           destroy_state (&state);
1043           init_state (&state, NULL);
1044           state.imap_browse = 1;
1045           imap_browse (LastDir, &state);
1046           browser_sort (&state);
1047           menu->data = state.entry;
1048           menu->current = 0;
1049           menu->top = 0;
1050           init_menu (&state, menu, title, sizeof (title), buffy);
1051           MAYBE_REDRAW (menu->redraw);
1052         }
1053       }
1054       break;
1055
1056     case OP_DELETE_MAILBOX:
1057       if (!state.entry[menu->current].imap)
1058         mutt_error (_("Delete is only supported for IMAP mailboxes"));
1059       else {
1060         char msg[SHORT_STRING];
1061         IMAP_MBOX mx;
1062         int nentry = menu->current;
1063
1064         imap_parse_path (state.entry[nentry].name, &mx);
1065         snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1066                   mx.mbox);
1067         if (mutt_yesorno (msg, M_NO) == M_YES) {
1068           if (!imap_delete_mailbox (Context, mx)) {
1069             /* free the mailbox from the browser */
1070             p_delete(&((state.entry)[nentry].name));
1071             p_delete(&((state.entry)[nentry].desc));
1072             /* and move all other entries up */
1073             if (nentry + 1 < state.entrylen)
1074               memmove (state.entry + nentry, state.entry + nentry + 1,
1075                        sizeof (struct folder_file) * (state.entrylen -
1076                                                       (nentry + 1)));
1077             state.entrylen--;
1078             mutt_message _("Mailbox deleted.");
1079
1080             init_menu (&state, menu, title, sizeof (title), buffy);
1081             MAYBE_REDRAW (menu->redraw);
1082           }
1083         }
1084         else
1085           mutt_message _("Mailbox not deleted.");
1086         p_delete(&mx.mbox);
1087       }
1088       break;
1089
1090     case OP_CHANGE_DIRECTORY:
1091
1092 #ifdef USE_NNTP
1093       if (option (OPTNEWS))
1094         break;
1095 #endif
1096
1097       m_strcpy(buf, sizeof(buf), LastDir);
1098       if (!state.imap_browse)
1099       {
1100         /* add '/' at the end of the directory name if not already there */
1101         ssize_t len = m_strlen(LastDir);
1102
1103         if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1104           buf[len] = '/';
1105       }
1106
1107       if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1108           buf[0]) {
1109         buffy = 0;
1110         mutt_expand_path (buf, sizeof (buf));
1111         if (imap_is_magic (buf, NULL) == M_IMAP) {
1112           m_strcpy(LastDir, sizeof(LastDir), buf);
1113           destroy_state (&state);
1114           init_state (&state, NULL);
1115           state.imap_browse = 1;
1116           imap_browse (LastDir, &state);
1117           browser_sort (&state);
1118           menu->data = state.entry;
1119           menu->current = 0;
1120           menu->top = 0;
1121           init_menu (&state, menu, title, sizeof (title), buffy);
1122         }
1123         else
1124         if (stat (buf, &st) == 0) {
1125           if (S_ISDIR (st.st_mode)) {
1126             destroy_state (&state);
1127             if (examine_directory (menu, &state, buf, prefix) == 0)
1128               m_strcpy(LastDir, sizeof(LastDir), buf);
1129             else {
1130               mutt_error _("Error scanning directory.");
1131
1132               if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1133                 mutt_menuDestroy (&menu);
1134                 goto bail;
1135               }
1136             }
1137             menu->current = 0;
1138             menu->top = 0;
1139             init_menu (&state, menu, title, sizeof (title), buffy);
1140           }
1141           else
1142             mutt_error (_("%s is not a directory."), buf);
1143         }
1144         else
1145           mutt_perror (buf);
1146       }
1147       MAYBE_REDRAW (menu->redraw);
1148       break;
1149
1150     case OP_ENTER_MASK:
1151
1152       m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1153       if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1154         regex_t *rx = p_new(regex_t, 1);
1155         char *s = buf;
1156         int not = 0, err;
1157
1158         buffy = 0;
1159         /* assume that the user wants to see everything */
1160         if (!buf[0])
1161           m_strcpy(buf, sizeof(buf), ".");
1162         s = vskipspaces(s);
1163         if (*s == '!') {
1164           s = vskipspaces(s + 1);
1165           not = 1;
1166         }
1167
1168         if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1169           regerror (err, rx, buf, sizeof (buf));
1170           regfree (rx);
1171           p_delete(&rx);
1172           mutt_error ("%s", buf);
1173         }
1174         else {
1175           m_strreplace(&Mask.pattern, buf);
1176           regfree (Mask.rx);
1177           p_delete(&Mask.rx);
1178           Mask.rx = rx;
1179           Mask.not = not;
1180
1181           destroy_state (&state);
1182           if (state.imap_browse) {
1183             init_state (&state, NULL);
1184             state.imap_browse = 1;
1185             imap_browse (LastDir, &state);
1186             browser_sort (&state);
1187             menu->data = state.entry;
1188             init_menu (&state, menu, title, sizeof (title), buffy);
1189           }
1190           else
1191           if (examine_directory (menu, &state, LastDir, NULL) == 0)
1192             init_menu (&state, menu, title, sizeof (title), buffy);
1193           else {
1194             mutt_error _("Error scanning directory.");
1195
1196             mutt_menuDestroy (&menu);
1197             goto bail;
1198           }
1199           killPrefix = 0;
1200           if (!state.entrylen) {
1201             mutt_error _("No files match the file mask");
1202
1203             break;
1204           }
1205         }
1206       }
1207       MAYBE_REDRAW (menu->redraw);
1208       break;
1209
1210     case OP_SORT:
1211     case OP_SORT_REVERSE:
1212
1213       {
1214         int resort = 1;
1215         int reverse = (i == OP_SORT_REVERSE);
1216
1217         switch (mutt_multi_choice ((reverse) ?
1218                                    _
1219                                    ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1220                                    :
1221                                    _
1222                                    ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1223                                    _("dazn"))) {
1224         case -1:               /* abort */
1225           resort = 0;
1226           break;
1227
1228         case 1:                /* (d)ate */
1229           BrowserSort = SORT_DATE;
1230           break;
1231
1232         case 2:                /* (a)lpha */
1233           BrowserSort = SORT_SUBJECT;
1234           break;
1235
1236         case 3:                /* si(z)e */
1237           BrowserSort = SORT_SIZE;
1238           break;
1239
1240         case 4:                /* do(n)'t sort */
1241           BrowserSort = SORT_ORDER;
1242           resort = 0;
1243           break;
1244         }
1245         if (resort) {
1246           BrowserSort |= reverse ? SORT_REVERSE : 0;
1247           browser_sort (&state);
1248           menu->redraw = REDRAW_FULL;
1249         }
1250         break;
1251       }
1252
1253     case OP_TOGGLE_MAILBOXES:
1254       buffy = 1 - buffy;
1255
1256     case OP_CHECK_NEW:
1257       destroy_state (&state);
1258       prefix[0] = 0;
1259       killPrefix = 0;
1260
1261       if (buffy) {
1262         if (examine_mailboxes (menu, &state) == -1)
1263           goto bail;
1264       }
1265       else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1266         init_state (&state, NULL);
1267         state.imap_browse = 1;
1268         imap_browse (LastDir, &state);
1269         browser_sort (&state);
1270         menu->data = state.entry;
1271       }
1272       else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1273         goto bail;
1274       init_menu (&state, menu, title, sizeof (title), buffy);
1275       break;
1276
1277     case OP_BUFFY_LIST:
1278       if (option (OPTFORCEBUFFYCHECK))
1279         buffy_check (1);
1280       buffy_list ();
1281       break;
1282
1283     case OP_BROWSER_NEW_FILE:
1284
1285       snprintf (buf, sizeof (buf), "%s/", LastDir);
1286       if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1287           0) {
1288         m_strcpy(f, flen, buf);
1289         destroy_state (&state);
1290         mutt_menuDestroy (&menu);
1291         goto bail;
1292       }
1293       MAYBE_REDRAW (menu->redraw);
1294       break;
1295
1296     case OP_BROWSER_VIEW_FILE:
1297       if (!state.entrylen) {
1298         mutt_error _("No files match the file mask");
1299
1300         break;
1301       }
1302
1303       if (state.entry[menu->current].selectable) {
1304         m_strcpy(f, flen, state.entry[menu->current].name);
1305         destroy_state (&state);
1306         mutt_menuDestroy (&menu);
1307         goto bail;
1308       }
1309       else
1310       if (S_ISDIR (state.entry[menu->current].mode) ||
1311             (S_ISLNK (state.entry[menu->current].mode) &&
1312                link_is_dir (LastDir, state.entry[menu->current].name))) {
1313         mutt_error _("Can't view a directory");
1314
1315         break;
1316       }
1317       else {
1318         BODY *b;
1319         char nbuf[_POSIX_PATH_MAX];
1320
1321         mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1322                          state.entry[menu->current].name);
1323         b = mutt_make_file_attach (nbuf);
1324         if (b != NULL) {
1325           mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1326           body_list_wipe(&b);
1327           menu->redraw = REDRAW_FULL;
1328         }
1329         else
1330           mutt_error _("Error trying to view file");
1331       }
1332       break;
1333
1334 #ifdef USE_NNTP
1335     case OP_CATCHUP:
1336     case OP_UNCATCHUP:
1337       if (option (OPTNEWS)) {
1338         struct folder_file *folder_f = &state.entry[menu->current];
1339         NNTP_DATA *nd;
1340
1341         if (i == OP_CATCHUP)
1342           nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1343         else
1344           nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1345
1346         if (nd) {
1347           if (menu->current + 1 < menu->max)
1348             menu->current++;
1349           menu->redraw = REDRAW_MOTION_RESYNCH;
1350         }
1351       }
1352       break;
1353
1354     case OP_LOAD_ACTIVE:
1355       if (!option (OPTNEWS))
1356         break;
1357
1358       {
1359         string_list_t *tmp;
1360         NNTP_DATA *data;
1361
1362         for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1363           if ((data = (NNTP_DATA *) tmp->data))
1364             data->deleted = 1;
1365         }
1366       }
1367       nntp_get_active (CurrentNewsSrv);
1368
1369       destroy_state (&state);
1370       if (buffy)
1371         examine_mailboxes (menu, &state);
1372       else
1373         examine_directory (menu, &state, NULL, NULL);
1374       init_menu (&state, menu, title, sizeof (title), buffy);
1375       break;
1376 #endif /* USE_NNTP */
1377
1378     case OP_BROWSER_SUBSCRIBE:
1379     case OP_BROWSER_UNSUBSCRIBE:
1380 #ifdef USE_NNTP
1381     case OP_SUBSCRIBE_PATTERN:
1382     case OP_UNSUBSCRIBE_PATTERN:
1383       if (option (OPTNEWS)) {
1384         regex_t *rx = p_new(regex_t, 1);
1385         char *s = buf;
1386         int j = menu->current;
1387         NNTP_DATA *nd;
1388         NNTP_SERVER *news = CurrentNewsSrv;
1389
1390         if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1391           char tmp[STRING];
1392           int err;
1393
1394           buf[0] = 0;
1395           if (i == OP_SUBSCRIBE_PATTERN)
1396             snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1397           else
1398             snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1399           if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1400             p_delete(&rx);
1401             break;
1402           }
1403
1404           if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1405             regerror (err, rx, buf, sizeof (buf));
1406             regfree (rx);
1407             p_delete(&rx);
1408             mutt_error ("%s", buf);
1409             break;
1410           }
1411           menu->redraw = REDRAW_FULL;
1412           j = 0;
1413         }
1414         else if (!state.entrylen) {
1415           mutt_error _("No newsgroups match the mask");
1416
1417           break;
1418         }
1419
1420         for (; j < state.entrylen; j++) {
1421           struct folder_file *folderf = &state.entry[j];
1422
1423           if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1424               regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1425             if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1426               nd = mutt_newsgroup_subscribe (news, folderf->name);
1427             else
1428               nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1429           }
1430           if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1431             if (menu->current + 1 < menu->max)
1432               menu->current++;
1433             menu->redraw = REDRAW_MOTION_RESYNCH;
1434             break;
1435           }
1436         }
1437         if (i == OP_SUBSCRIBE_PATTERN) {
1438           string_list_t *grouplist = NULL;
1439
1440           if (news)
1441             grouplist = news->list;
1442           for (; grouplist; grouplist = grouplist->next) {
1443             nd = (NNTP_DATA *) grouplist->data;
1444             if (nd && nd->group && !nd->subscribed) {
1445               if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1446                 mutt_newsgroup_subscribe (news, nd->group);
1447                 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1448               }
1449             }
1450           }
1451           init_menu (&state, menu, title, sizeof (title), buffy);
1452         }
1453         mutt_newsrc_update (news);
1454         nntp_clear_cacheindex (news);
1455         if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1456           regfree (rx);
1457         p_delete(&rx);
1458       }
1459       else
1460 #endif /* USE_NNTP */
1461       {
1462         if (i == OP_BROWSER_SUBSCRIBE)
1463           imap_subscribe (state.entry[menu->current].name, 1);
1464         else
1465           imap_subscribe (state.entry[menu->current].name, 0);
1466       }
1467     }
1468   }
1469
1470 bail:
1471
1472   if (!folder)
1473     m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);
1474
1475 }