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