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