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