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