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