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