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