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 "browser.h"
21 #ifdef USE_IMAP
22 #include "imap.h"
23 #endif
24 #ifdef USE_NNTP
25 #include "nntp.h"
26 #endif
27 #include "sidebar.h"
28
29 #include "lib/mem.h"
30 #include "lib/intl.h"
31 #include "lib/str.h"
32 #include "lib/list.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 = safe_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                                         safe_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     int i = -1;
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 (safe_strcmp (de->d_name, ".") == 0)
532         continue;               /* we don't need . */
533
534       if (prefix && *prefix
535           && safe_strncmp (prefix, de->d_name, safe_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       i = buffy_lookup (buffer);
549       add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
550     }
551     closedir (dp);
552   }
553   sidebar_draw (CurrentMenu);
554   browser_sort (state);
555   return 0;
556 }
557
558 /* get list of mailboxes/subscribed newsgroups */
559 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
560 {
561   struct stat s;
562   char buffer[LONG_STRING];
563
564 #ifdef USE_NNTP
565   if (option (OPTNEWS)) {
566     LIST *tmp;
567     NNTP_DATA *data;
568     NNTP_SERVER *news = CurrentNewsSrv;
569
570 /*  mutt_buffy_check (0); */
571     init_state (state, menu);
572
573     for (tmp = news->list; tmp; tmp = tmp->next) {
574       if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
575                                                        (data->subscribed
576                                                         &&
577                                                         (!option
578                                                          (OPTSHOWONLYUNREAD)
579                                                          || data->unread))))
580         add_folder (menu, state, data->group, NULL, data, data->new);
581     }
582   }
583   else
584 #endif
585   {
586     int i = 0;
587     BUFFY* tmp;
588
589     if (!Incoming)
590       return (-1);
591     mutt_buffy_check (0);
592
593     init_state (state, menu);
594
595     for (i = 0; i < Incoming->length; i++) {
596       tmp = (BUFFY*) Incoming->data[i];
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   }
628   browser_sort (state);
629   return 0;
630 }
631
632 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
633 {
634 #ifdef USE_NNTP
635   if (option (OPTNEWS))
636     return (regexec
637             (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
638 #endif
639   return (regexec
640           (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
641 }
642
643 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
644 {
645   FOLDER folder;
646
647   folder.ff = &((struct folder_file *) menu->data)[num];
648   folder.num = num;
649
650 #ifdef USE_NNTP
651   if (option (OPTNEWS))
652     mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
653                        (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
654   else
655 #endif
656     mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
657                        (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
658 }
659
660 static void init_menu (struct browser_state *state, MUTTMENU * menu,
661                        char *title, size_t titlelen, int buffy)
662 {
663   char path[_POSIX_PATH_MAX];
664
665   menu->max = state->entrylen;
666
667   if (menu->current >= menu->max)
668     menu->current = menu->max - 1;
669   if (menu->current < 0)
670     menu->current = 0;
671   if (menu->top > menu->current)
672     menu->top = 0;
673
674   menu->tagged = 0;
675
676 #ifdef USE_NNTP
677   if (option (OPTNEWS)) {
678     if (buffy)
679       snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
680     else
681       snprintf (title, titlelen, _("Newsgroups on server [%s]"),
682                 CurrentNewsSrv->conn->account.host);
683   }
684   else
685 #endif
686   if (buffy)
687     snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
688   else {
689     strfcpy (path, LastDir, sizeof (path));
690     mutt_pretty_mailbox (path);
691 #ifdef USE_IMAP
692     if (state->imap_browse && option (OPTIMAPLSUB))
693       snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
694                 path, NONULL (Mask.pattern));
695     else
696 #endif
697       snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
698                 path, NONULL (Mask.pattern));
699   }
700   menu->redraw = REDRAW_FULL;
701 }
702
703 static int file_tag (MUTTMENU * menu, int n, int m)
704 {
705   struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
706   int ot;
707
708   if (S_ISDIR (ff->mode)
709       || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
710     mutt_error _("Can't attach a directory!");
711
712     return 0;
713   }
714
715   ot = ff->tagged;
716   ff->tagged = (m >= 0 ? m : !ff->tagged);
717
718   return ff->tagged - ot;
719 }
720
721 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
722                         int *numfiles)
723 {
724   char buf[_POSIX_PATH_MAX];
725   char prefix[_POSIX_PATH_MAX] = "";
726   char helpstr[SHORT_STRING];
727   char title[STRING];
728   struct browser_state state;
729   MUTTMENU *menu;
730   struct stat st;
731   int i, killPrefix = 0;
732   int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
733   int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
734   int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
735
736   buffy = buffy && folder;
737
738   memset (&state, 0, sizeof (struct browser_state));
739
740   if (!folder)
741     strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
742
743 #ifdef USE_NNTP
744   if (option (OPTNEWS)) {
745     if (*f)
746       strfcpy (prefix, f, sizeof (prefix));
747     else {
748       LIST *list;
749
750       /* default state for news reader mode is browse subscribed newsgroups */
751       buffy = 0;
752       for (list = CurrentNewsSrv->list; list; list = list->next) {
753         NNTP_DATA *data = (NNTP_DATA *) list->data;
754
755         if (data && data->subscribed) {
756           buffy = 1;
757           break;
758         }
759       }
760     }
761   }
762   else
763 #endif
764   if (*f) {
765     mutt_expand_path (f, flen);
766 #ifdef USE_IMAP
767     if (mx_is_imap (f)) {
768       init_state (&state, NULL);
769       state.imap_browse = 1;
770       imap_browse (f, &state);
771       strfcpy (LastDir, state.folder, sizeof (LastDir));
772     }
773     else {
774 #endif
775       for (i = safe_strlen (f) - 1; i > 0 && f[i] != '/'; i--);
776       if (i > 0) {
777         if (f[0] == '/') {
778           if (i > sizeof (LastDir) - 1)
779             i = sizeof (LastDir) - 1;
780           strncpy (LastDir, f, i);
781           LastDir[i] = 0;
782         }
783         else {
784           getcwd (LastDir, sizeof (LastDir));
785           safe_strcat (LastDir, sizeof (LastDir), "/");
786           safe_strncat (LastDir, sizeof (LastDir), f, i);
787         }
788       }
789       else {
790         if (f[0] == '/')
791           strcpy (LastDir, "/");        /* __STRCPY_CHECKED__ */
792         else
793           getcwd (LastDir, sizeof (LastDir));
794       }
795
796       if (i <= 0 && f[0] != '/')
797         strfcpy (prefix, f, sizeof (prefix));
798       else
799         strfcpy (prefix, f + i + 1, sizeof (prefix));
800       killPrefix = 1;
801 #ifdef USE_IMAP
802     }
803 #endif
804   }
805   else {
806     if (!folder)
807       getcwd (LastDir, sizeof (LastDir));
808     else if (!LastDir[0])
809       strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
810
811 #ifdef USE_IMAP
812     if (!buffy && mx_is_imap (LastDir)) {
813       init_state (&state, NULL);
814       state.imap_browse = 1;
815       imap_browse (LastDir, &state);
816     }
817 #endif
818   }
819
820   *f = 0;
821
822   if (buffy) {
823     if (examine_mailboxes (NULL, &state) == -1)
824       goto bail;
825   }
826   else
827 #ifdef USE_IMAP
828   if (!state.imap_browse)
829 #endif
830     if (examine_directory (NULL, &state, LastDir, prefix) == -1)
831       goto bail;
832
833   menu = mutt_new_menu ();
834   menu->menu = MENU_FOLDER;
835   menu->make_entry = folder_entry;
836   menu->search = select_file_search;
837   menu->title = title;
838   menu->data = state.entry;
839   if (multiple)
840     menu->tag = file_tag;
841
842   menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
843 #ifdef USE_NNTP
844                                   (option (OPTNEWS)) ? FolderNewsHelp :
845 #endif
846                                   FolderHelp);
847
848   init_menu (&state, menu, title, sizeof (title), buffy);
849
850   FOREVER {
851     switch (i = mutt_menuLoop (menu)) {
852     case OP_GENERIC_SELECT_ENTRY:
853
854       if (!state.entrylen) {
855         mutt_error _("No files match the file mask");
856
857         break;
858       }
859
860       if (S_ISDIR (state.entry[menu->current].mode) ||
861           (S_ISLNK (state.entry[menu->current].mode) &&
862            link_is_dir (LastDir, state.entry[menu->current].name))
863 #ifdef USE_IMAP
864           || state.entry[menu->current].inferiors
865 #endif
866         ) {
867         /* make sure this isn't a MH or maildir mailbox */
868         if (buffy) {
869           strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
870           mutt_expand_path (buf, sizeof (buf));
871         }
872 #ifdef USE_IMAP
873         else if (state.imap_browse) {
874           strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
875         }
876 #endif
877         else
878           mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
879                             sizeof (buf));
880
881         if ((mx_get_magic (buf) <= 0)
882 #ifdef USE_IMAP
883             || state.entry[menu->current].inferiors
884 #endif
885           ) {
886           char OldLastDir[_POSIX_PATH_MAX];
887
888           /* save the old directory */
889           strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
890
891           if (safe_strcmp (state.entry[menu->current].name, "..") == 0) {
892             if (safe_strcmp ("..", LastDir + safe_strlen (LastDir) - 2) == 0)
893               strcat (LastDir, "/..");  /* __STRCAT_CHECKED__ */
894             else {
895               char *p = strrchr (LastDir + 1, '/');
896
897               if (p)
898                 *p = 0;
899               else {
900                 if (LastDir[0] == '/')
901                   LastDir[1] = 0;
902                 else
903                   strcat (LastDir, "/..");      /* __STRCAT_CHECKED__ */
904               }
905             }
906           }
907           else if (buffy) {
908             strfcpy (LastDir, state.entry[menu->current].name,
909                      sizeof (LastDir));
910             mutt_expand_path (LastDir, sizeof (LastDir));
911           }
912 #ifdef USE_IMAP
913           else if (state.imap_browse) {
914             int n;
915             ciss_url_t url;
916
917             strfcpy (LastDir, state.entry[menu->current].name,
918                      sizeof (LastDir));
919             /* tack on delimiter here */
920             n = safe_strlen (LastDir) + 1;
921
922             /* special case "" needs no delimiter */
923             url_parse_ciss (&url, state.entry[menu->current].name);
924             if (url.path &&
925                 (state.entry[menu->current].delim != '\0') &&
926                 (n < sizeof (LastDir))) {
927               LastDir[n] = '\0';
928               LastDir[n - 1] = state.entry[menu->current].delim;
929             }
930           }
931 #endif
932           else {
933             char tmp[_POSIX_PATH_MAX];
934
935             mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
936                               sizeof (tmp));
937             strfcpy (LastDir, tmp, sizeof (LastDir));
938           }
939
940           destroy_state (&state);
941           if (killPrefix) {
942             prefix[0] = 0;
943             killPrefix = 0;
944           }
945           buffy = 0;
946 #ifdef USE_IMAP
947           if (state.imap_browse) {
948             init_state (&state, NULL);
949             state.imap_browse = 1;
950             imap_browse (LastDir, &state);
951             menu->data = state.entry;
952           }
953           else
954 #endif
955           if (examine_directory (menu, &state, LastDir, prefix) == -1) {
956             /* try to restore the old values */
957             strfcpy (LastDir, OldLastDir, sizeof (LastDir));
958             if (examine_directory (menu, &state, LastDir, prefix) == -1) {
959               strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
960               goto bail;
961             }
962           }
963           menu->current = 0;
964           menu->top = 0;
965           init_menu (&state, menu, title, sizeof (title), buffy);
966           break;
967         }
968       }
969
970 #ifdef USE_NNTP
971       if (buffy || option (OPTNEWS))    /* news have not path */
972 #else
973       if (buffy)
974 #endif
975       {
976         strfcpy (f, state.entry[menu->current].name, flen);
977         mutt_expand_path (f, flen);
978       }
979 #ifdef USE_IMAP
980       else if (state.imap_browse)
981         strfcpy (f, state.entry[menu->current].name, flen);
982 #endif
983       else
984         mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
985
986       /* Fall through to OP_EXIT */
987
988     case OP_EXIT:
989
990       if (multiple) {
991         char **tfiles;
992         int i, j;
993
994         if (menu->tagged) {
995           *numfiles = menu->tagged;
996           tfiles = safe_calloc (*numfiles, sizeof (char *));
997           for (i = 0, j = 0; i < state.entrylen; i++) {
998             struct folder_file ff = state.entry[i];
999             char full[_POSIX_PATH_MAX];
1000
1001             if (ff.tagged) {
1002               mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1003               mutt_expand_path (full, sizeof (full));
1004               tfiles[j++] = safe_strdup (full);
1005             }
1006           }
1007           *files = tfiles;
1008         }
1009         else if (f[0]) {        /* no tagged entries. return selected entry */
1010           *numfiles = 1;
1011           tfiles = safe_calloc (*numfiles, sizeof (char *));
1012           mutt_expand_path (f, flen);
1013           tfiles[0] = safe_strdup (f);
1014           *files = tfiles;
1015         }
1016       }
1017
1018       destroy_state (&state);
1019       mutt_menuDestroy (&menu);
1020       goto bail;
1021
1022     case OP_BROWSER_TELL:
1023       if (state.entrylen)
1024         mutt_message ("%s", state.entry[menu->current].name);
1025       break;
1026
1027 #ifdef USE_IMAP
1028     case OP_BROWSER_TOGGLE_LSUB:
1029       if (option (OPTIMAPLSUB)) {
1030         unset_option (OPTIMAPLSUB);
1031       }
1032       else {
1033         set_option (OPTIMAPLSUB);
1034       }
1035       mutt_ungetch (0, OP_CHECK_NEW);
1036       break;
1037
1038     case OP_CREATE_MAILBOX:
1039       if (!state.imap_browse)
1040         mutt_error (_("Create is only supported for IMAP mailboxes"));
1041       else {
1042         imap_mailbox_create (LastDir);
1043         /* TODO: find a way to detect if the new folder would appear in
1044          *   this window, and insert it without starting over. */
1045         destroy_state (&state);
1046         init_state (&state, NULL);
1047         state.imap_browse = 1;
1048         imap_browse (LastDir, &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       break;
1056
1057     case OP_RENAME_MAILBOX:
1058       if (!state.entry[menu->current].imap)
1059         mutt_error (_("Rename is only supported for IMAP mailboxes"));
1060       else {
1061         int nentry = menu->current;
1062
1063         if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1064           destroy_state (&state);
1065           init_state (&state, NULL);
1066           state.imap_browse = 1;
1067           imap_browse (LastDir, &state);
1068           menu->data = state.entry;
1069           menu->current = 0;
1070           menu->top = 0;
1071           init_menu (&state, menu, title, sizeof (title), buffy);
1072           MAYBE_REDRAW (menu->redraw);
1073         }
1074       }
1075       break;
1076
1077     case OP_DELETE_MAILBOX:
1078       if (!state.entry[menu->current].imap)
1079         mutt_error (_("Delete is only supported for IMAP mailboxes"));
1080       else {
1081         char msg[SHORT_STRING];
1082         IMAP_MBOX mx;
1083         int nentry = menu->current;
1084
1085         imap_parse_path (state.entry[nentry].name, &mx);
1086         snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1087                   mx.mbox);
1088         if (mutt_yesorno (msg, M_NO) == M_YES) {
1089           if (!imap_delete_mailbox (Context, mx)) {
1090             /* free the mailbox from the browser */
1091             FREE (&((state.entry)[nentry].name));
1092             FREE (&((state.entry)[nentry].desc));
1093             /* and move all other entries up */
1094             if (nentry + 1 < state.entrylen)
1095               memmove (state.entry + nentry, state.entry + nentry + 1,
1096                        sizeof (struct folder_file) * (state.entrylen -
1097                                                       (nentry + 1)));
1098             state.entrylen--;
1099             mutt_message _("Mailbox deleted.");
1100
1101             init_menu (&state, menu, title, sizeof (title), buffy);
1102             MAYBE_REDRAW (menu->redraw);
1103           }
1104         }
1105         else
1106           mutt_message _("Mailbox not deleted.");
1107         FREE (&mx.mbox);
1108       }
1109       break;
1110 #endif
1111
1112     case OP_CHANGE_DIRECTORY:
1113
1114 #ifdef USE_NNTP
1115       if (option (OPTNEWS))
1116         break;
1117 #endif
1118
1119       strfcpy (buf, LastDir, sizeof (buf));
1120 #ifdef USE_IMAP
1121       if (!state.imap_browse)
1122 #endif
1123       {
1124         /* add '/' at the end of the directory name if not already there */
1125         int len = safe_strlen (LastDir);
1126
1127         if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1128           buf[len] = '/';
1129       }
1130
1131       if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1132           buf[0]) {
1133         buffy = 0;
1134         mutt_expand_path (buf, sizeof (buf));
1135 #ifdef USE_IMAP
1136         if (mx_is_imap (buf)) {
1137           strfcpy (LastDir, buf, sizeof (LastDir));
1138           destroy_state (&state);
1139           init_state (&state, NULL);
1140           state.imap_browse = 1;
1141           imap_browse (LastDir, &state);
1142           menu->data = state.entry;
1143           menu->current = 0;
1144           menu->top = 0;
1145           init_menu (&state, menu, title, sizeof (title), buffy);
1146         }
1147         else
1148 #endif
1149         if (stat (buf, &st) == 0) {
1150           if (S_ISDIR (st.st_mode)) {
1151             destroy_state (&state);
1152             if (examine_directory (menu, &state, buf, prefix) == 0)
1153               strfcpy (LastDir, buf, sizeof (LastDir));
1154             else {
1155               mutt_error _("Error scanning directory.");
1156
1157               if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1158                 mutt_menuDestroy (&menu);
1159                 goto bail;
1160               }
1161             }
1162             menu->current = 0;
1163             menu->top = 0;
1164             init_menu (&state, menu, title, sizeof (title), buffy);
1165           }
1166           else
1167             mutt_error (_("%s is not a directory."), buf);
1168         }
1169         else
1170           mutt_perror (buf);
1171       }
1172       MAYBE_REDRAW (menu->redraw);
1173       break;
1174
1175     case OP_ENTER_MASK:
1176
1177       strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1178       if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1179         regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1180         char *s = buf;
1181         int not = 0, err;
1182
1183         buffy = 0;
1184         /* assume that the user wants to see everything */
1185         if (!buf[0])
1186           strfcpy (buf, ".", sizeof (buf));
1187         SKIPWS (s);
1188         if (*s == '!') {
1189           s++;
1190           SKIPWS (s);
1191           not = 1;
1192         }
1193
1194         if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1195           regerror (err, rx, buf, sizeof (buf));
1196           regfree (rx);
1197           FREE (&rx);
1198           mutt_error ("%s", buf);
1199         }
1200         else {
1201           str_replace (&Mask.pattern, buf);
1202           regfree (Mask.rx);
1203           FREE (&Mask.rx);
1204           Mask.rx = rx;
1205           Mask.not = not;
1206
1207           destroy_state (&state);
1208 #ifdef USE_IMAP
1209           if (state.imap_browse) {
1210             init_state (&state, NULL);
1211             state.imap_browse = 1;
1212             imap_browse (LastDir, &state);
1213             menu->data = state.entry;
1214             init_menu (&state, menu, title, sizeof (title), buffy);
1215           }
1216           else
1217 #endif
1218           if (examine_directory (menu, &state, LastDir, NULL) == 0)
1219             init_menu (&state, menu, title, sizeof (title), buffy);
1220           else {
1221             mutt_error _("Error scanning directory.");
1222
1223             mutt_menuDestroy (&menu);
1224             goto bail;
1225           }
1226           killPrefix = 0;
1227           if (!state.entrylen) {
1228             mutt_error _("No files match the file mask");
1229
1230             break;
1231           }
1232         }
1233       }
1234       MAYBE_REDRAW (menu->redraw);
1235       break;
1236
1237     case OP_SORT:
1238     case OP_SORT_REVERSE:
1239
1240       {
1241         int resort = 1;
1242         int reverse = (i == OP_SORT_REVERSE);
1243
1244         switch (mutt_multi_choice ((reverse) ?
1245                                    _
1246                                    ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1247                                    :
1248                                    _
1249                                    ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1250                                    _("dazn"))) {
1251         case -1:               /* abort */
1252           resort = 0;
1253           break;
1254
1255         case 1:                /* (d)ate */
1256           BrowserSort = SORT_DATE;
1257           break;
1258
1259         case 2:                /* (a)lpha */
1260           BrowserSort = SORT_SUBJECT;
1261           break;
1262
1263         case 3:                /* si(z)e */
1264           BrowserSort = SORT_SIZE;
1265           break;
1266
1267         case 4:                /* do(n)'t sort */
1268           BrowserSort = SORT_ORDER;
1269           resort = 0;
1270           break;
1271         }
1272         if (resort) {
1273           BrowserSort |= reverse ? SORT_REVERSE : 0;
1274           browser_sort (&state);
1275           menu->redraw = REDRAW_FULL;
1276         }
1277         break;
1278       }
1279
1280     case OP_TOGGLE_MAILBOXES:
1281       buffy = 1 - buffy;
1282
1283     case OP_CHECK_NEW:
1284       destroy_state (&state);
1285       prefix[0] = 0;
1286       killPrefix = 0;
1287
1288       if (buffy) {
1289         if (examine_mailboxes (menu, &state) == -1)
1290           goto bail;
1291       }
1292 #ifdef USE_IMAP
1293       else if (mx_is_imap (LastDir)) {
1294         init_state (&state, NULL);
1295         state.imap_browse = 1;
1296         imap_browse (LastDir, &state);
1297         menu->data = state.entry;
1298       }
1299 #endif
1300       else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1301         goto bail;
1302       init_menu (&state, menu, title, sizeof (title), buffy);
1303       break;
1304
1305     case OP_BUFFY_LIST:
1306       mutt_buffy_list ();
1307       break;
1308
1309     case OP_BROWSER_NEW_FILE:
1310
1311       snprintf (buf, sizeof (buf), "%s/", LastDir);
1312       if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1313           0) {
1314         strfcpy (f, buf, flen);
1315         destroy_state (&state);
1316         mutt_menuDestroy (&menu);
1317         goto bail;
1318       }
1319       MAYBE_REDRAW (menu->redraw);
1320       break;
1321
1322     case OP_BROWSER_VIEW_FILE:
1323       if (!state.entrylen) {
1324         mutt_error _("No files match the file mask");
1325
1326         break;
1327       }
1328
1329 #ifdef USE_IMAP
1330       if (state.entry[menu->current].selectable) {
1331         strfcpy (f, state.entry[menu->current].name, flen);
1332         destroy_state (&state);
1333         mutt_menuDestroy (&menu);
1334         goto bail;
1335       }
1336       else
1337 #endif
1338       if (S_ISDIR (state.entry[menu->current].mode) ||
1339             (S_ISLNK (state.entry[menu->current].mode) &&
1340                link_is_dir (LastDir, state.entry[menu->current].name))) {
1341         mutt_error _("Can't view a directory");
1342
1343         break;
1344       }
1345       else {
1346         BODY *b;
1347         char buf[_POSIX_PATH_MAX];
1348
1349         mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1350                           sizeof (buf));
1351         b = mutt_make_file_attach (buf);
1352         if (b != NULL) {
1353           mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1354           mutt_free_body (&b);
1355           menu->redraw = REDRAW_FULL;
1356         }
1357         else
1358           mutt_error _("Error trying to view file");
1359       }
1360       break;
1361
1362 #ifdef USE_NNTP
1363     case OP_CATCHUP:
1364     case OP_UNCATCHUP:
1365       if (option (OPTNEWS)) {
1366         struct folder_file *f = &state.entry[menu->current];
1367         NNTP_DATA *nd;
1368
1369         if (i == OP_CATCHUP)
1370           nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1371         else
1372           nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1373
1374         if (nd) {
1375 /*          FOLDER folder;
1376             struct folder_file ff;
1377             char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1378
1379             folder.ff = &ff;
1380             folder.ff->name = f->name;
1381             folder.ff->st = NULL;
1382             folder.ff->is_new = nd->new;
1383             folder.ff->nd = nd;
1384             FREE (&f->desc);
1385             mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1386                   newsgroup_format_str, (unsigned long) &folder,
1387                   M_FORMAT_ARROWCURSOR);
1388             f->desc = safe_strdup (buffer); */
1389           if (menu->current + 1 < menu->max)
1390             menu->current++;
1391           menu->redraw = REDRAW_MOTION_RESYNCH;
1392         }
1393       }
1394       break;
1395
1396     case OP_LOAD_ACTIVE:
1397       if (!option (OPTNEWS))
1398         break;
1399
1400       {
1401         LIST *tmp;
1402         NNTP_DATA *data;
1403
1404         for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1405           if ((data = (NNTP_DATA *) tmp->data))
1406             data->deleted = 1;
1407         }
1408       }
1409       nntp_get_active (CurrentNewsSrv);
1410
1411       destroy_state (&state);
1412       if (buffy)
1413         examine_mailboxes (menu, &state);
1414       else
1415         examine_directory (menu, &state, NULL, NULL);
1416       init_menu (&state, menu, title, sizeof (title), buffy);
1417       break;
1418 #endif /* USE_NNTP */
1419
1420 #if defined USE_IMAP || defined USE_NNTP
1421     case OP_BROWSER_SUBSCRIBE:
1422     case OP_BROWSER_UNSUBSCRIBE:
1423 #endif
1424 #ifdef USE_NNTP
1425     case OP_SUBSCRIBE_PATTERN:
1426     case OP_UNSUBSCRIBE_PATTERN:
1427       if (option (OPTNEWS)) {
1428         regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1429         char *s = buf;
1430         int j = menu->current;
1431         NNTP_DATA *nd;
1432         NNTP_SERVER *news = CurrentNewsSrv;
1433
1434         if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1435           char tmp[STRING];
1436           int err;
1437
1438           buf[0] = 0;
1439           if (i == OP_SUBSCRIBE_PATTERN)
1440             snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1441           else
1442             snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1443           if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1444             FREE (&rx);
1445             break;
1446           }
1447
1448           if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1449             regerror (err, rx, buf, sizeof (buf));
1450             regfree (rx);
1451             FREE (&rx);
1452             mutt_error ("%s", buf);
1453             break;
1454           }
1455           menu->redraw = REDRAW_FULL;
1456           j = 0;
1457         }
1458         else if (!state.entrylen) {
1459           mutt_error _("No newsgroups match the mask");
1460
1461           break;
1462         }
1463
1464         for (; j < state.entrylen; j++) {
1465           struct folder_file *f = &state.entry[j];
1466
1467           if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1468               regexec (rx, f->name, 0, NULL, 0) == 0) {
1469             if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1470               nd = mutt_newsgroup_subscribe (news, f->name);
1471             else
1472               nd = mutt_newsgroup_unsubscribe (news, f->name);
1473 /*            if (nd)
1474               {
1475                 FOLDER folder;
1476                 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1477
1478                 folder.name = f->name;
1479                 folder.f = NULL;
1480                 folder.new = nd->new;
1481                 folder.nd = nd;
1482                 FREE (&f->desc);
1483                 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1484                         newsgroup_format_str, (unsigned long) &folder,
1485                         M_FORMAT_ARROWCURSOR);
1486                 f->desc = safe_strdup (buffer);
1487               } */
1488           }
1489           if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1490             if (menu->current + 1 < menu->max)
1491               menu->current++;
1492             menu->redraw = REDRAW_MOTION_RESYNCH;
1493             break;
1494           }
1495         }
1496         if (i == OP_SUBSCRIBE_PATTERN) {
1497           LIST *grouplist = NULL;
1498
1499           if (news)
1500             grouplist = news->list;
1501           for (; grouplist; grouplist = grouplist->next) {
1502             nd = (NNTP_DATA *) grouplist->data;
1503             if (nd && nd->group && !nd->subscribed) {
1504               if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1505                 mutt_newsgroup_subscribe (news, nd->group);
1506                 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1507               }
1508             }
1509           }
1510           init_menu (&state, menu, title, sizeof (title), buffy);
1511         }
1512         mutt_newsrc_update (news);
1513         nntp_clear_cacheindex (news);
1514         if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1515           regfree (rx);
1516         FREE (&rx);
1517       }
1518 #ifdef USE_IMAP
1519       else
1520 #endif /* USE_IMAP && USE_NNTP */
1521 #endif /* USE_NNTP */
1522 #ifdef USE_IMAP
1523       {
1524         if (i == OP_BROWSER_SUBSCRIBE)
1525           imap_subscribe (state.entry[menu->current].name, 1);
1526         else
1527           imap_subscribe (state.entry[menu->current].name, 0);
1528       }
1529 #endif /* USE_IMAP */
1530     }
1531   }
1532
1533 bail:
1534
1535   if (!folder)
1536     strfcpy (LastDir, LastDirBackup, sizeof (LastDir));
1537
1538 }