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