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