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