2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
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.
15 #include "mutt_curses.h"
16 #include "mutt_menu.h"
23 #include "imap/mx_imap.h"
43 static struct mapping_t FolderHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Chdir"), OP_CHANGE_DIRECTORY},
46 {N_("Mask"), OP_ENTER_MASK},
47 {N_("Help"), OP_HELP},
52 static struct mapping_t FolderNewsHelp[] = {
53 {N_("Exit"), OP_EXIT},
54 {N_("List"), OP_TOGGLE_MAILBOXES},
55 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
56 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
57 {N_("Catchup"), OP_CATCHUP},
58 {N_("Mask"), OP_ENTER_MASK},
59 {N_("Help"), OP_HELP},
64 typedef struct folder_t {
65 struct folder_file *ff;
69 static char LastDir[_POSIX_PATH_MAX] = "";
70 static char LastDirBackup[_POSIX_PATH_MAX] = "";
72 /* Frees up the memory allocated for the local-global variables. */
73 static void destroy_state (struct browser_state *state)
77 for (c = 0; c < state->entrylen; c++) {
78 FREE (&((state->entry)[c].name));
79 FREE (&((state->entry)[c].desc));
80 FREE (&((state->entry)[c].st));
83 FREE (&state->folder);
88 static int browser_compare_subject (const void *a, const void *b)
90 struct folder_file *pa = (struct folder_file *) a;
91 struct folder_file *pb = (struct folder_file *) b;
93 int r = safe_strcoll (pa->name, pb->name);
95 return ((BrowserSort & SORT_REVERSE) ? -r : r);
98 static int browser_compare_date (const void *a, const void *b)
100 struct folder_file *pa = (struct folder_file *) a;
101 struct folder_file *pb = (struct folder_file *) b;
103 int r = pa->mtime - pb->mtime;
105 return ((BrowserSort & SORT_REVERSE) ? -r : r);
108 static int browser_compare_size (const void *a, const void *b)
110 struct folder_file *pa = (struct folder_file *) a;
111 struct folder_file *pb = (struct folder_file *) b;
113 int r = pa->size - pb->size;
115 return ((BrowserSort & SORT_REVERSE) ? -r : r);
118 static void browser_sort (struct browser_state *state)
120 int (*f) (const void *, const void *);
122 switch (BrowserSort & SORT_MASK) {
127 if (option (OPTNEWS))
130 f = browser_compare_date;
134 if (option (OPTNEWS))
137 f = browser_compare_size;
141 f = browser_compare_subject;
144 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
147 static int link_is_dir (const char *folder, const char *path)
150 char fullpath[_POSIX_PATH_MAX];
152 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
154 if (stat (fullpath, &st) == 0)
155 return (S_ISDIR (st.st_mode));
160 static const char *folder_format_str (char *dest, size_t destlen, char op,
161 const char *src, const char *fmt,
162 const char *ifstring,
163 const char *elsestring,
164 unsigned long data, format_flag flags)
166 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
167 char date[16], *t_fmt;
169 FOLDER *folder = (FOLDER *) data;
172 int optional = (flags & M_FORMAT_OPTIONAL);
176 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
177 snprintf (dest, destlen, tmp, folder->num + 1);
181 if (folder->ff->st != NULL) {
184 tnow - folder->ff->st->st_mtime <
185 31536000 ? "%b %d %H:%M" : "%b %d %Y";
186 strftime (date, sizeof (date), t_fmt,
187 localtime (&folder->ff->st->st_mtime));
188 mutt_format_s (dest, destlen, fmt, date);
191 mutt_format_s (dest, destlen, fmt, "");
199 if (folder->ff->imap)
200 s = NONULL (folder->ff->desc);
203 s = NONULL (folder->ff->name);
205 snprintf (fn, sizeof (fn), "%s%s", s,
206 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
207 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
208 ((folder->ff->st->st_mode & S_IXUSR) !=
209 0 ? "*" : ""))) : "");
211 mutt_format_s (dest, destlen, fmt, fn);
215 if (folder->ff->st != NULL) {
216 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
217 S_ISDIR (folder->ff->st->
218 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
219 st_mode) ? 'l' : '-'),
220 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
221 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
222 (folder->ff->st->st_mode & S_ISUID) !=
223 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
225 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
226 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
227 (folder->ff->st->st_mode & S_ISGID) !=
228 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
230 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
231 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
232 (folder->ff->st->st_mode & S_ISVTX) !=
233 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
235 mutt_format_s (dest, destlen, fmt, permission);
238 else if (folder->ff->imap) {
239 /* mark folders with subfolders AND mail */
240 snprintf (permission, sizeof (permission), "IMAP %c",
241 (folder->ff->inferiors
242 && folder->ff->selectable) ? '+' : ' ');
243 mutt_format_s (dest, destlen, fmt, permission);
247 mutt_format_s (dest, destlen, fmt, "");
251 if (folder->ff->st != NULL) {
252 if ((gr = getgrgid (folder->ff->st->st_gid)))
253 mutt_format_s (dest, destlen, fmt, gr->gr_name);
255 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
256 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
260 mutt_format_s (dest, destlen, fmt, "");
264 if (folder->ff->st != NULL) {
265 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
266 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
269 mutt_format_s (dest, destlen, fmt, "");
274 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
276 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
277 snprintf (dest, destlen, tmp, folder->ff->new);
279 else if (!folder->ff->new)
284 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
285 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
289 if (folder->ff->st != NULL) {
290 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
291 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
294 mutt_format_s (dest, destlen, fmt, "");
298 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
299 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
303 if (folder->ff->st != NULL) {
304 if ((pw = getpwuid (folder->ff->st->st_uid)))
305 mutt_format_s (dest, destlen, fmt, pw->pw_name);
307 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
308 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
312 mutt_format_s (dest, destlen, fmt, "");
316 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
317 snprintf (dest, destlen, tmp, op);
322 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
323 else if (flags & M_FORMAT_OPTIONAL)
324 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
330 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
331 const char *src, const char *fmt,
332 const char *ifstring,
333 const char *elsestring,
337 char fn[SHORT_STRING], tmp[SHORT_STRING];
338 FOLDER *folder = (FOLDER *) data;
342 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
343 snprintf (dest, destlen, tmp, folder->num + 1);
347 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
348 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
349 snprintf (dest, destlen, tmp, fn);
353 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
354 if (folder->ff->nd->subscribed)
355 snprintf (dest, destlen, tmp, ' ');
357 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
361 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
362 if (folder->ff->nd->deleted)
363 snprintf (dest, destlen, tmp, 'D');
365 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
369 if (flags & M_FORMAT_OPTIONAL) {
370 if (folder->ff->nd->unread != 0)
371 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
374 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
377 else if (Context && Context->data == folder->ff->nd) {
378 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379 snprintf (dest, destlen, tmp, Context->unread);
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
388 if (Context && Context->data == folder->ff->nd) {
389 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
390 snprintf (dest, destlen, tmp, Context->new);
392 else if (option (OPTMARKOLD) &&
393 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
394 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp,
397 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
400 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
401 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
406 if (folder->ff->nd->desc != NULL) {
407 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
408 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
411 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
412 snprintf (dest, destlen, tmp, "");
418 #endif /* USE_NNTP */
420 static void add_folder (MUTTMENU * m, struct browser_state *state,
421 const char *name, const struct stat *s,
424 if (state->entrylen == state->entrymax) {
425 /* need to allocate more space */
426 safe_realloc (&state->entry,
427 sizeof (struct folder_file) * (state->entrymax += 256));
428 memset (&state->entry[state->entrylen], 0,
429 sizeof (struct folder_file) * 256);
431 m->data = state->entry;
435 (state->entry)[state->entrylen].mode = s->st_mode;
436 (state->entry)[state->entrylen].mtime = s->st_mtime;
437 (state->entry)[state->entrylen].size = s->st_size;
439 (state->entry)[state->entrylen].st = safe_malloc (sizeof (struct stat));
440 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
443 (state->entry)[state->entrylen].new = new;
444 (state->entry)[state->entrylen].name = safe_strdup (name);
445 (state->entry)[state->entrylen].desc = safe_strdup (name);
447 (state->entry)[state->entrylen].imap = 0;
450 if (option (OPTNEWS))
451 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
456 static void init_state (struct browser_state *state, MUTTMENU * menu)
459 state->entrymax = 256;
461 (struct folder_file *) safe_calloc (state->entrymax,
462 sizeof (struct folder_file));
464 state->imap_browse = 0;
467 menu->data = state->entry;
470 /* get list of all files/newsgroups with mask */
471 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
472 char *d, const char *prefix)
475 if (option (OPTNEWS)) {
478 NNTP_SERVER *news = CurrentNewsSrv;
480 /* mutt_buffy_check (0); */
481 init_state (state, menu);
483 for (tmp = news->list; tmp; tmp = tmp->next) {
484 if (!(data = (NNTP_DATA *) tmp->data))
486 if (prefix && *prefix && strncmp (prefix, data->group,
487 safe_strlen (prefix)) != 0)
489 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
491 add_folder (menu, state, data->group, NULL, data, data->new);
495 #endif /* USE_NNTP */
500 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
503 while (stat (d, &s) == -1) {
504 if (errno == ENOENT) {
505 /* The last used directory is deleted, try to use the parent dir. */
506 char *c = strrchr (d, '/');
517 if (!S_ISDIR (s.st_mode)) {
518 mutt_error (_("%s is not a directory."), d);
522 mutt_buffy_check (0);
524 if ((dp = opendir (d)) == NULL) {
529 init_state (state, menu);
531 while ((de = readdir (dp)) != NULL) {
532 if (safe_strcmp (de->d_name, ".") == 0)
533 continue; /* we don't need . */
535 if (prefix && *prefix
536 && safe_strncmp (prefix, de->d_name, safe_strlen (prefix)) != 0)
538 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
541 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
542 if (lstat (buffer, &s) == -1)
545 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
546 (!S_ISLNK (s.st_mode)))
549 i = buffy_lookup (buffer);
550 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
554 sidebar_draw (CurrentMenu);
555 browser_sort (state);
559 /* get list of mailboxes/subscribed newsgroups */
560 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
563 char buffer[LONG_STRING];
566 if (option (OPTNEWS)) {
569 NNTP_SERVER *news = CurrentNewsSrv;
571 /* mutt_buffy_check (0); */
572 init_state (state, menu);
574 for (tmp = news->list; tmp; tmp = tmp->next) {
575 if ((data = (NNTP_DATA *) tmp->data) != NULL && (data->new ||
581 add_folder (menu, state, data->group, NULL, data, data->new);
592 mutt_buffy_check (0);
594 init_state (state, menu);
596 for (i = 0; i < Incoming->length; i++) {
597 tmp = (BUFFY*) Incoming->data[i];
598 tmp->magic = mx_get_magic (tmp->path);
600 if (tmp->magic == M_IMAP) {
601 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
606 if (tmp->magic == M_POP) {
607 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
612 if (tmp->magic == M_NNTP) {
613 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
617 if (lstat (tmp->path, &s) == -1)
620 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
621 (!S_ISLNK (s.st_mode)))
624 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
625 mutt_pretty_mailbox (buffer);
627 add_folder (menu, state, buffer, &s, NULL, tmp->new);
630 browser_sort (state);
634 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
637 if (option (OPTNEWS))
639 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
642 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
645 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
649 folder.ff = &((struct folder_file *) menu->data)[num];
653 if (option (OPTNEWS))
654 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
655 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
658 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
659 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
662 static void init_menu (struct browser_state *state, MUTTMENU * menu,
663 char *title, size_t titlelen, int buffy)
665 char path[_POSIX_PATH_MAX];
667 menu->max = state->entrylen;
669 if (menu->current >= menu->max)
670 menu->current = menu->max - 1;
671 if (menu->current < 0)
673 if (menu->top > menu->current)
679 if (option (OPTNEWS)) {
681 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
683 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
684 CurrentNewsSrv->conn->account.host);
689 snprintf (title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check (0));
691 strfcpy (path, LastDir, sizeof (path));
692 mutt_pretty_mailbox (path);
694 if (state->imap_browse && option (OPTIMAPLSUB))
695 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
696 path, NONULL (Mask.pattern));
699 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
700 path, NONULL (Mask.pattern));
702 menu->redraw = REDRAW_FULL;
705 static int file_tag (MUTTMENU * menu, int n, int m)
707 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
710 if (S_ISDIR (ff->mode)
711 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
712 mutt_error _("Can't attach a directory!");
718 ff->tagged = (m >= 0 ? m : !ff->tagged);
720 return ff->tagged - ot;
723 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
726 char buf[_POSIX_PATH_MAX];
727 char prefix[_POSIX_PATH_MAX] = "";
728 char helpstr[SHORT_STRING];
730 struct browser_state state;
733 int i, killPrefix = 0;
734 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
735 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
736 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
738 buffy = buffy && folder;
740 memset (&state, 0, sizeof (struct browser_state));
743 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
746 if (option (OPTNEWS)) {
748 strfcpy (prefix, f, sizeof (prefix));
752 /* default state for news reader mode is browse subscribed newsgroups */
754 for (list = CurrentNewsSrv->list; list; list = list->next) {
755 NNTP_DATA *data = (NNTP_DATA *) list->data;
757 if (data && data->subscribed) {
767 mutt_expand_path (f, flen);
769 if (imap_is_magic (f, NULL) == M_IMAP) {
770 init_state (&state, NULL);
771 state.imap_browse = 1;
772 imap_browse (f, &state);
773 strfcpy (LastDir, state.folder, sizeof (LastDir));
777 for (i = safe_strlen (f) - 1; i > 0 && f[i] != '/'; i--);
780 if (i > sizeof (LastDir) - 1)
781 i = sizeof (LastDir) - 1;
782 strncpy (LastDir, f, i);
786 getcwd (LastDir, sizeof (LastDir));
787 safe_strcat (LastDir, sizeof (LastDir), "/");
788 safe_strncat (LastDir, sizeof (LastDir), f, i);
793 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
795 getcwd (LastDir, sizeof (LastDir));
798 if (i <= 0 && f[0] != '/')
799 strfcpy (prefix, f, sizeof (prefix));
801 strfcpy (prefix, f + i + 1, sizeof (prefix));
809 getcwd (LastDir, sizeof (LastDir));
810 else if (!LastDir[0])
811 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
814 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
815 init_state (&state, NULL);
816 state.imap_browse = 1;
817 imap_browse (LastDir, &state);
825 if (examine_mailboxes (NULL, &state) == -1)
830 if (!state.imap_browse)
832 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
835 menu = mutt_new_menu ();
836 menu->menu = MENU_FOLDER;
837 menu->make_entry = folder_entry;
838 menu->search = select_file_search;
840 menu->data = state.entry;
842 menu->tag = file_tag;
844 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
846 (option (OPTNEWS)) ? FolderNewsHelp :
850 init_menu (&state, menu, title, sizeof (title), buffy);
853 switch (i = mutt_menuLoop (menu)) {
854 case OP_GENERIC_SELECT_ENTRY:
856 if (!state.entrylen) {
857 mutt_error _("No files match the file mask");
862 if (S_ISDIR (state.entry[menu->current].mode) ||
863 (S_ISLNK (state.entry[menu->current].mode) &&
864 link_is_dir (LastDir, state.entry[menu->current].name))
866 || state.entry[menu->current].inferiors
869 /* make sure this isn't a MH or maildir mailbox */
871 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
872 mutt_expand_path (buf, sizeof (buf));
875 else if (state.imap_browse) {
876 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
880 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
883 if ((mx_get_magic (buf) <= 0)
885 || state.entry[menu->current].inferiors
888 char OldLastDir[_POSIX_PATH_MAX];
890 /* save the old directory */
891 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
893 if (safe_strcmp (state.entry[menu->current].name, "..") == 0) {
894 if (safe_strcmp ("..", LastDir + safe_strlen (LastDir) - 2) == 0)
895 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
897 char *p = strrchr (LastDir + 1, '/');
902 if (LastDir[0] == '/')
905 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
910 strfcpy (LastDir, state.entry[menu->current].name,
912 mutt_expand_path (LastDir, sizeof (LastDir));
915 else if (state.imap_browse) {
919 strfcpy (LastDir, state.entry[menu->current].name,
921 /* tack on delimiter here */
922 n = safe_strlen (LastDir) + 1;
924 /* special case "" needs no delimiter */
925 url_parse_ciss (&url, state.entry[menu->current].name);
927 (state.entry[menu->current].delim != '\0') &&
928 (n < sizeof (LastDir))) {
930 LastDir[n - 1] = state.entry[menu->current].delim;
935 char tmp[_POSIX_PATH_MAX];
937 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
939 strfcpy (LastDir, tmp, sizeof (LastDir));
942 destroy_state (&state);
949 if (state.imap_browse) {
950 init_state (&state, NULL);
951 state.imap_browse = 1;
952 imap_browse (LastDir, &state);
953 menu->data = state.entry;
957 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
958 /* try to restore the old values */
959 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
960 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
961 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
967 init_menu (&state, menu, title, sizeof (title), buffy);
973 if (buffy || option (OPTNEWS)) /* news have not path */
978 strfcpy (f, state.entry[menu->current].name, flen);
979 mutt_expand_path (f, flen);
982 else if (state.imap_browse)
983 strfcpy (f, state.entry[menu->current].name, flen);
986 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
988 /* Fall through to OP_EXIT */
997 *numfiles = menu->tagged;
998 tfiles = safe_calloc (*numfiles, sizeof (char *));
999 for (i = 0, j = 0; i < state.entrylen; i++) {
1000 struct folder_file ff = state.entry[i];
1001 char full[_POSIX_PATH_MAX];
1004 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1005 mutt_expand_path (full, sizeof (full));
1006 tfiles[j++] = safe_strdup (full);
1011 else if (f[0]) { /* no tagged entries. return selected entry */
1013 tfiles = safe_calloc (*numfiles, sizeof (char *));
1014 mutt_expand_path (f, flen);
1015 tfiles[0] = safe_strdup (f);
1020 destroy_state (&state);
1021 mutt_menuDestroy (&menu);
1024 case OP_BROWSER_TELL:
1026 mutt_message ("%s", state.entry[menu->current].name);
1030 case OP_BROWSER_TOGGLE_LSUB:
1031 if (option (OPTIMAPLSUB)) {
1032 unset_option (OPTIMAPLSUB);
1035 set_option (OPTIMAPLSUB);
1037 mutt_ungetch (0, OP_CHECK_NEW);
1040 case OP_CREATE_MAILBOX:
1041 if (!state.imap_browse)
1042 mutt_error (_("Create is only supported for IMAP mailboxes"));
1044 imap_mailbox_create (LastDir);
1045 /* TODO: find a way to detect if the new folder would appear in
1046 * this window, and insert it without starting over. */
1047 destroy_state (&state);
1048 init_state (&state, NULL);
1049 state.imap_browse = 1;
1050 imap_browse (LastDir, &state);
1051 menu->data = state.entry;
1054 init_menu (&state, menu, title, sizeof (title), buffy);
1055 MAYBE_REDRAW (menu->redraw);
1059 case OP_RENAME_MAILBOX:
1060 if (!state.entry[menu->current].imap)
1061 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1063 int nentry = menu->current;
1065 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1066 destroy_state (&state);
1067 init_state (&state, NULL);
1068 state.imap_browse = 1;
1069 imap_browse (LastDir, &state);
1070 menu->data = state.entry;
1073 init_menu (&state, menu, title, sizeof (title), buffy);
1074 MAYBE_REDRAW (menu->redraw);
1079 case OP_DELETE_MAILBOX:
1080 if (!state.entry[menu->current].imap)
1081 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1083 char msg[SHORT_STRING];
1085 int nentry = menu->current;
1087 imap_parse_path (state.entry[nentry].name, &mx);
1088 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1090 if (mutt_yesorno (msg, M_NO) == M_YES) {
1091 if (!imap_delete_mailbox (Context, mx)) {
1092 /* free the mailbox from the browser */
1093 FREE (&((state.entry)[nentry].name));
1094 FREE (&((state.entry)[nentry].desc));
1095 /* and move all other entries up */
1096 if (nentry + 1 < state.entrylen)
1097 memmove (state.entry + nentry, state.entry + nentry + 1,
1098 sizeof (struct folder_file) * (state.entrylen -
1101 mutt_message _("Mailbox deleted.");
1103 init_menu (&state, menu, title, sizeof (title), buffy);
1104 MAYBE_REDRAW (menu->redraw);
1108 mutt_message _("Mailbox not deleted.");
1114 case OP_CHANGE_DIRECTORY:
1117 if (option (OPTNEWS))
1121 strfcpy (buf, LastDir, sizeof (buf));
1123 if (!state.imap_browse)
1126 /* add '/' at the end of the directory name if not already there */
1127 int len = safe_strlen (LastDir);
1129 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1133 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1136 mutt_expand_path (buf, sizeof (buf));
1138 if (imap_is_magic (buf, NULL) == M_IMAP) {
1139 strfcpy (LastDir, buf, sizeof (LastDir));
1140 destroy_state (&state);
1141 init_state (&state, NULL);
1142 state.imap_browse = 1;
1143 imap_browse (LastDir, &state);
1144 menu->data = state.entry;
1147 init_menu (&state, menu, title, sizeof (title), buffy);
1151 if (stat (buf, &st) == 0) {
1152 if (S_ISDIR (st.st_mode)) {
1153 destroy_state (&state);
1154 if (examine_directory (menu, &state, buf, prefix) == 0)
1155 strfcpy (LastDir, buf, sizeof (LastDir));
1157 mutt_error _("Error scanning directory.");
1159 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1160 mutt_menuDestroy (&menu);
1166 init_menu (&state, menu, title, sizeof (title), buffy);
1169 mutt_error (_("%s is not a directory."), buf);
1174 MAYBE_REDRAW (menu->redraw);
1179 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1180 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1181 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1186 /* assume that the user wants to see everything */
1188 strfcpy (buf, ".", sizeof (buf));
1196 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1197 regerror (err, rx, buf, sizeof (buf));
1200 mutt_error ("%s", buf);
1203 str_replace (&Mask.pattern, buf);
1209 destroy_state (&state);
1211 if (state.imap_browse) {
1212 init_state (&state, NULL);
1213 state.imap_browse = 1;
1214 imap_browse (LastDir, &state);
1215 menu->data = state.entry;
1216 init_menu (&state, menu, title, sizeof (title), buffy);
1220 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1221 init_menu (&state, menu, title, sizeof (title), buffy);
1223 mutt_error _("Error scanning directory.");
1225 mutt_menuDestroy (&menu);
1229 if (!state.entrylen) {
1230 mutt_error _("No files match the file mask");
1236 MAYBE_REDRAW (menu->redraw);
1240 case OP_SORT_REVERSE:
1244 int reverse = (i == OP_SORT_REVERSE);
1246 switch (mutt_multi_choice ((reverse) ?
1248 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1251 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1253 case -1: /* abort */
1257 case 1: /* (d)ate */
1258 BrowserSort = SORT_DATE;
1261 case 2: /* (a)lpha */
1262 BrowserSort = SORT_SUBJECT;
1265 case 3: /* si(z)e */
1266 BrowserSort = SORT_SIZE;
1269 case 4: /* do(n)'t sort */
1270 BrowserSort = SORT_ORDER;
1275 BrowserSort |= reverse ? SORT_REVERSE : 0;
1276 browser_sort (&state);
1277 menu->redraw = REDRAW_FULL;
1282 case OP_TOGGLE_MAILBOXES:
1286 destroy_state (&state);
1291 if (examine_mailboxes (menu, &state) == -1)
1295 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1296 init_state (&state, NULL);
1297 state.imap_browse = 1;
1298 imap_browse (LastDir, &state);
1299 menu->data = state.entry;
1302 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1304 init_menu (&state, menu, title, sizeof (title), buffy);
1311 case OP_BROWSER_NEW_FILE:
1313 snprintf (buf, sizeof (buf), "%s/", LastDir);
1314 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1316 strfcpy (f, buf, flen);
1317 destroy_state (&state);
1318 mutt_menuDestroy (&menu);
1321 MAYBE_REDRAW (menu->redraw);
1324 case OP_BROWSER_VIEW_FILE:
1325 if (!state.entrylen) {
1326 mutt_error _("No files match the file mask");
1332 if (state.entry[menu->current].selectable) {
1333 strfcpy (f, state.entry[menu->current].name, flen);
1334 destroy_state (&state);
1335 mutt_menuDestroy (&menu);
1340 if (S_ISDIR (state.entry[menu->current].mode) ||
1341 (S_ISLNK (state.entry[menu->current].mode) &&
1342 link_is_dir (LastDir, state.entry[menu->current].name))) {
1343 mutt_error _("Can't view a directory");
1349 char buf[_POSIX_PATH_MAX];
1351 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1353 b = mutt_make_file_attach (buf);
1355 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1356 mutt_free_body (&b);
1357 menu->redraw = REDRAW_FULL;
1360 mutt_error _("Error trying to view file");
1367 if (option (OPTNEWS)) {
1368 struct folder_file *f = &state.entry[menu->current];
1371 if (i == OP_CATCHUP)
1372 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1374 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1378 struct folder_file ff;
1379 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1382 folder.ff->name = f->name;
1383 folder.ff->st = NULL;
1384 folder.ff->is_new = nd->new;
1387 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1388 newsgroup_format_str, (unsigned long) &folder,
1389 M_FORMAT_ARROWCURSOR);
1390 f->desc = safe_strdup (buffer); */
1391 if (menu->current + 1 < menu->max)
1393 menu->redraw = REDRAW_MOTION_RESYNCH;
1398 case OP_LOAD_ACTIVE:
1399 if (!option (OPTNEWS))
1406 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1407 if ((data = (NNTP_DATA *) tmp->data))
1411 nntp_get_active (CurrentNewsSrv);
1413 destroy_state (&state);
1415 examine_mailboxes (menu, &state);
1417 examine_directory (menu, &state, NULL, NULL);
1418 init_menu (&state, menu, title, sizeof (title), buffy);
1420 #endif /* USE_NNTP */
1422 #if defined USE_IMAP || defined USE_NNTP
1423 case OP_BROWSER_SUBSCRIBE:
1424 case OP_BROWSER_UNSUBSCRIBE:
1427 case OP_SUBSCRIBE_PATTERN:
1428 case OP_UNSUBSCRIBE_PATTERN:
1429 if (option (OPTNEWS)) {
1430 regex_t *rx = (regex_t *) safe_malloc (sizeof (regex_t));
1432 int j = menu->current;
1434 NNTP_SERVER *news = CurrentNewsSrv;
1436 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1441 if (i == OP_SUBSCRIBE_PATTERN)
1442 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1444 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1445 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1450 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1451 regerror (err, rx, buf, sizeof (buf));
1454 mutt_error ("%s", buf);
1457 menu->redraw = REDRAW_FULL;
1460 else if (!state.entrylen) {
1461 mutt_error _("No newsgroups match the mask");
1466 for (; j < state.entrylen; j++) {
1467 struct folder_file *f = &state.entry[j];
1469 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1470 regexec (rx, f->name, 0, NULL, 0) == 0) {
1471 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1472 nd = mutt_newsgroup_subscribe (news, f->name);
1474 nd = mutt_newsgroup_unsubscribe (news, f->name);
1478 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1480 folder.name = f->name;
1482 folder.new = nd->new;
1485 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1486 newsgroup_format_str, (unsigned long) &folder,
1487 M_FORMAT_ARROWCURSOR);
1488 f->desc = safe_strdup (buffer);
1491 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1492 if (menu->current + 1 < menu->max)
1494 menu->redraw = REDRAW_MOTION_RESYNCH;
1498 if (i == OP_SUBSCRIBE_PATTERN) {
1499 LIST *grouplist = NULL;
1502 grouplist = news->list;
1503 for (; grouplist; grouplist = grouplist->next) {
1504 nd = (NNTP_DATA *) grouplist->data;
1505 if (nd && nd->group && !nd->subscribed) {
1506 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1507 mutt_newsgroup_subscribe (news, nd->group);
1508 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1512 init_menu (&state, menu, title, sizeof (title), buffy);
1514 mutt_newsrc_update (news);
1515 nntp_clear_cacheindex (news);
1516 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1522 #endif /* USE_IMAP && USE_NNTP */
1523 #endif /* USE_NNTP */
1526 if (i == OP_BROWSER_SUBSCRIBE)
1527 imap_subscribe (state.entry[menu->current].name, 1);
1529 imap_subscribe (state.entry[menu->current].name, 0);
1531 #endif /* USE_IMAP */
1538 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));