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.
14 #include <lib-lib/mem.h>
15 #include <lib-lib/str.h>
16 #include <lib-lib/macros.h>
21 #include "mutt_curses.h"
22 #include "mutt_menu.h"
31 #include "imap/mx_imap.h"
48 static struct mapping_t FolderHelp[] = {
49 {N_("Exit"), OP_EXIT},
50 {N_("Chdir"), OP_CHANGE_DIRECTORY},
51 {N_("Mask"), OP_ENTER_MASK},
52 {N_("Help"), OP_HELP},
57 static struct mapping_t FolderNewsHelp[] = {
58 {N_("Exit"), OP_EXIT},
59 {N_("List"), OP_TOGGLE_MAILBOXES},
60 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
61 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
62 {N_("Catchup"), OP_CATCHUP},
63 {N_("Mask"), OP_ENTER_MASK},
64 {N_("Help"), OP_HELP},
69 typedef struct folder_t {
70 struct folder_file *ff;
74 static char LastDir[_POSIX_PATH_MAX] = "";
75 static char LastDirBackup[_POSIX_PATH_MAX] = "";
77 /* Frees up the memory allocated for the local-global variables. */
78 static void destroy_state (struct browser_state *state)
82 for (c = 0; c < state->entrylen; c++) {
83 p_delete(&((state->entry)[c].name));
84 p_delete(&((state->entry)[c].desc));
85 p_delete(&((state->entry)[c].st));
88 p_delete(&state->folder);
90 p_delete(&state->entry);
93 static int browser_compare_subject (const void *a, const void *b)
95 struct folder_file *pa = (struct folder_file *) a;
96 struct folder_file *pb = (struct folder_file *) b;
98 int r = str_coll (pa->name, pb->name);
100 return ((BrowserSort & SORT_REVERSE) ? -r : r);
103 static int browser_compare_date (const void *a, const void *b)
105 struct folder_file *pa = (struct folder_file *) a;
106 struct folder_file *pb = (struct folder_file *) b;
108 int r = pa->mtime - pb->mtime;
110 return ((BrowserSort & SORT_REVERSE) ? -r : r);
113 static int browser_compare_size (const void *a, const void *b)
115 struct folder_file *pa = (struct folder_file *) a;
116 struct folder_file *pb = (struct folder_file *) b;
118 int r = pa->size - pb->size;
120 return ((BrowserSort & SORT_REVERSE) ? -r : r);
123 static void browser_sort (struct browser_state *state)
125 int (*f) (const void *, const void *);
127 switch (BrowserSort & SORT_MASK) {
132 if (option (OPTNEWS))
135 f = browser_compare_date;
139 if (option (OPTNEWS))
142 f = browser_compare_size;
146 f = browser_compare_subject;
149 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
152 static int link_is_dir (const char *folder, const char *path)
155 char fullpath[_POSIX_PATH_MAX];
157 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
159 if (stat (fullpath, &st) == 0)
160 return (S_ISDIR (st.st_mode));
165 static const char *folder_format_str (char *dest, size_t destlen, char op,
166 const char *src, const char *fmt,
167 const char *ifstring,
168 const char *elsestring,
169 unsigned long data, format_flag flags)
171 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
174 FOLDER *folder = (FOLDER *) data;
177 int optional = (flags & M_FORMAT_OPTIONAL);
181 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
182 snprintf (dest, destlen, tmp, folder->num + 1);
186 if (folder->ff->st != NULL) {
189 tnow - folder->ff->st->st_mtime <
190 31536000 ? "%b %d %H:%M" : "%b %d %Y";
191 strftime (date, sizeof (date), t_fmt,
192 localtime (&folder->ff->st->st_mtime));
193 mutt_format_s (dest, destlen, fmt, date);
196 mutt_format_s (dest, destlen, fmt, "");
204 if (folder->ff->imap)
205 s = NONULL (folder->ff->desc);
208 s = NONULL (folder->ff->name);
210 snprintf (fn, sizeof (fn), "%s%s", s,
211 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
212 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
213 ((folder->ff->st->st_mode & S_IXUSR) !=
214 0 ? "*" : ""))) : "");
216 mutt_format_s (dest, destlen, fmt, fn);
220 if (folder->ff->st != NULL) {
221 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
222 S_ISDIR (folder->ff->st->
223 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
224 st_mode) ? 'l' : '-'),
225 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
226 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
227 (folder->ff->st->st_mode & S_ISUID) !=
228 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
230 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
231 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
232 (folder->ff->st->st_mode & S_ISGID) !=
233 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
235 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
236 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
237 (folder->ff->st->st_mode & S_ISVTX) !=
238 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
240 mutt_format_s (dest, destlen, fmt, permission);
243 else if (folder->ff->imap) {
244 /* mark folders with subfolders AND mail */
245 snprintf (permission, sizeof (permission), "IMAP %c",
246 (folder->ff->inferiors
247 && folder->ff->selectable) ? '+' : ' ');
248 mutt_format_s (dest, destlen, fmt, permission);
252 mutt_format_s (dest, destlen, fmt, "");
256 if (folder->ff->st != NULL) {
257 if ((gr = getgrgid (folder->ff->st->st_gid)))
258 mutt_format_s (dest, destlen, fmt, gr->gr_name);
260 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
261 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
265 mutt_format_s (dest, destlen, fmt, "");
269 if (folder->ff->st != NULL) {
270 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
271 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
274 mutt_format_s (dest, destlen, fmt, "");
279 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
281 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
282 snprintf (dest, destlen, tmp, folder->ff->new);
284 else if (!folder->ff->new)
289 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
290 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
294 if (folder->ff->st != NULL) {
295 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
296 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
299 mutt_format_s (dest, destlen, fmt, "");
303 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
304 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
308 if (folder->ff->st != NULL) {
309 if ((pw = getpwuid (folder->ff->st->st_uid)))
310 mutt_format_s (dest, destlen, fmt, pw->pw_name);
312 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
313 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
317 mutt_format_s (dest, destlen, fmt, "");
321 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
322 snprintf (dest, destlen, tmp, op);
327 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
328 else if (flags & M_FORMAT_OPTIONAL)
329 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
335 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
336 const char *src, const char *fmt,
337 const char *ifstring,
338 const char *elsestring,
342 char fn[SHORT_STRING], tmp[SHORT_STRING];
343 FOLDER *folder = (FOLDER *) data;
347 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
348 snprintf (dest, destlen, tmp, folder->num + 1);
352 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
353 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
354 snprintf (dest, destlen, tmp, fn);
358 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
359 if (folder->ff->nd->subscribed)
360 snprintf (dest, destlen, tmp, ' ');
362 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
366 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
367 if (folder->ff->nd->deleted)
368 snprintf (dest, destlen, tmp, 'D');
370 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
374 if (flags & M_FORMAT_OPTIONAL) {
375 if (folder->ff->nd->unread != 0)
376 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
379 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
382 else if (Context && Context->data == folder->ff->nd) {
383 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384 snprintf (dest, destlen, tmp, Context->unread);
387 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
388 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
393 if (Context && Context->data == folder->ff->nd) {
394 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
395 snprintf (dest, destlen, tmp, Context->new);
397 else if (option (OPTMARKOLD) &&
398 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
399 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
400 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
401 snprintf (dest, destlen, tmp,
402 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
405 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
406 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
411 if (folder->ff->nd->desc != NULL) {
412 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
413 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
416 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
417 snprintf (dest, destlen, tmp, "");
423 #endif /* USE_NNTP */
425 static void add_folder (MUTTMENU * m, struct browser_state *state,
426 const char *name, const struct stat *s,
429 if (state->entrylen == state->entrymax) {
430 /* need to allocate more space */
431 p_realloc(&state->entry, state->entrymax += 256);
432 p_clear(&state->entry[state->entrylen], 256);
434 m->data = state->entry;
438 (state->entry)[state->entrylen].mode = s->st_mode;
439 (state->entry)[state->entrylen].mtime = s->st_mtime;
440 (state->entry)[state->entrylen].size = s->st_size;
441 (state->entry)[state->entrylen].st = p_dup(s, 1);
444 (state->entry)[state->entrylen].new = new;
445 (state->entry)[state->entrylen].name = m_strdup(name);
446 (state->entry)[state->entrylen].desc = m_strdup(name);
448 (state->entry)[state->entrylen].imap = 0;
451 if (option (OPTNEWS))
452 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
457 static void init_state (struct browser_state *state, MUTTMENU * menu)
460 state->entrymax = 256;
461 state->entry = p_new(struct folder_file, state->entrymax);
463 state->imap_browse = 0;
466 menu->data = state->entry;
469 /* get list of all files/newsgroups with mask */
470 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
471 char *d, const char *prefix)
474 if (option (OPTNEWS)) {
477 NNTP_SERVER *news = CurrentNewsSrv;
479 /* buffy_check (0); */
480 init_state (state, menu);
482 for (tmp = news->list; tmp; tmp = tmp->next) {
483 if (!(data = (NNTP_DATA *) tmp->data))
485 nntp_sync_sidebar (data);
486 if (prefix && *prefix && strncmp (prefix, data->group,
487 m_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);
493 sidebar_draw (CurrentMenu);
496 #endif /* USE_NNTP */
501 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
504 while (stat (d, &s) == -1) {
505 if (errno == ENOENT) {
506 /* The last used directory is deleted, try to use the parent dir. */
507 char *c = strrchr (d, '/');
518 if (!S_ISDIR (s.st_mode)) {
519 mutt_error (_("%s is not a directory."), d);
525 if ((dp = opendir (d)) == NULL) {
530 init_state (state, menu);
532 while ((de = readdir (dp)) != NULL) {
533 if (str_cmp (de->d_name, ".") == 0)
534 continue; /* we don't need . */
536 if (prefix && *prefix
537 && str_ncmp (prefix, de->d_name, m_strlen(prefix)) != 0)
539 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
542 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
543 if (lstat (buffer, &s) == -1)
546 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
547 (!S_ISLNK (s.st_mode)))
550 i = buffy_lookup (buffer);
551 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
555 sidebar_draw (CurrentMenu);
556 browser_sort (state);
560 /* get list of mailboxes/subscribed newsgroups */
561 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
564 char buffer[LONG_STRING];
567 if (option (OPTNEWS)) {
570 NNTP_SERVER *news = CurrentNewsSrv;
572 /* buffy_check (0); */
573 init_state (state, menu);
575 for (tmp = news->list; tmp; tmp = tmp->next) {
576 if ((data = (NNTP_DATA*) tmp->data) == NULL)
578 nntp_sync_sidebar (data);
579 if ((data->new || (data->subscribed &&
580 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
581 add_folder (menu, state, data->group, NULL, data, data->new);
583 sidebar_draw (CurrentMenu);
595 init_state (state, menu);
597 for (i = 0; i < Incoming->length; i++) {
598 tmp = (BUFFY*) Incoming->data[i];
599 tmp->magic = mx_get_magic (tmp->path);
601 if (tmp->magic == M_IMAP) {
602 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
607 if (tmp->magic == M_POP) {
608 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
613 if (tmp->magic == M_NNTP) {
614 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
618 if (lstat (tmp->path, &s) == -1)
621 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
622 (!S_ISLNK (s.st_mode)))
625 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
626 mutt_pretty_mailbox (buffer);
628 add_folder (menu, state, buffer, &s, NULL, tmp->new);
631 browser_sort (state);
635 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
638 if (option (OPTNEWS))
640 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
643 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
646 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
650 folder.ff = &((struct folder_file *) menu->data)[num];
654 if (option (OPTNEWS))
655 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
656 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
659 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
660 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
663 static void init_menu (struct browser_state *state, MUTTMENU * menu,
664 char *title, size_t titlelen, int buffy)
666 char path[_POSIX_PATH_MAX];
668 menu->max = state->entrylen;
670 if (menu->current >= menu->max)
671 menu->current = menu->max - 1;
672 if (menu->current < 0)
674 if (menu->top > menu->current)
680 if (option (OPTNEWS)) {
682 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
684 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
685 CurrentNewsSrv->conn->account.host);
690 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
692 strfcpy (path, LastDir, sizeof (path));
693 mutt_pretty_mailbox (path);
695 if (state->imap_browse && option (OPTIMAPLSUB))
696 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
697 path, NONULL (Mask.pattern));
700 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
701 path, NONULL (Mask.pattern));
703 menu->redraw = REDRAW_FULL;
706 static int file_tag (MUTTMENU * menu, int n, int m)
708 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
711 if (S_ISDIR (ff->mode)
712 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
713 mutt_error _("Can't attach a directory!");
719 ff->tagged = (m >= 0 ? m : !ff->tagged);
721 return ff->tagged - ot;
724 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
727 char buf[_POSIX_PATH_MAX];
728 char prefix[_POSIX_PATH_MAX] = "";
729 char helpstr[SHORT_STRING];
731 struct browser_state state;
734 int i, killPrefix = 0;
735 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
736 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
737 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
739 buffy = buffy && folder;
744 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
747 if (option (OPTNEWS)) {
749 strfcpy (prefix, f, sizeof (prefix));
753 /* default state for news reader mode is browse subscribed newsgroups */
755 for (list = CurrentNewsSrv->list; list; list = list->next) {
756 NNTP_DATA *data = (NNTP_DATA *) list->data;
758 if (data && data->subscribed) {
768 mutt_expand_path (f, flen);
770 if (imap_is_magic (f, NULL) == M_IMAP) {
771 init_state (&state, NULL);
772 state.imap_browse = 1;
773 if (!imap_browse (f, &state))
774 strfcpy (LastDir, state.folder, sizeof (LastDir));
778 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
781 if (i > sizeof (LastDir) - 1)
782 i = sizeof (LastDir) - 1;
783 strncpy (LastDir, f, i);
787 getcwd (LastDir, sizeof (LastDir));
788 str_cat (LastDir, sizeof (LastDir), "/");
789 str_ncat (LastDir, sizeof (LastDir), f, i);
794 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
796 getcwd (LastDir, sizeof (LastDir));
799 if (i <= 0 && f[0] != '/')
800 strfcpy (prefix, f, sizeof (prefix));
802 strfcpy (prefix, f + i + 1, sizeof (prefix));
810 getcwd (LastDir, sizeof (LastDir));
811 else if (!LastDir[0])
812 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
815 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
816 init_state (&state, NULL);
817 state.imap_browse = 1;
818 imap_browse (LastDir, &state);
819 browser_sort (&state);
827 if (examine_mailboxes (NULL, &state) == -1)
832 if (!state.imap_browse)
834 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
837 menu = mutt_new_menu ();
838 menu->menu = MENU_FOLDER;
839 menu->make_entry = folder_entry;
840 menu->search = select_file_search;
842 menu->data = state.entry;
844 menu->tag = file_tag;
846 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
848 (option (OPTNEWS)) ? FolderNewsHelp :
852 init_menu (&state, menu, title, sizeof (title), buffy);
855 switch (i = mutt_menuLoop (menu)) {
856 case OP_GENERIC_SELECT_ENTRY:
858 if (!state.entrylen) {
859 mutt_error _("No files match the file mask");
864 if (S_ISDIR (state.entry[menu->current].mode) ||
865 (S_ISLNK (state.entry[menu->current].mode) &&
866 link_is_dir (LastDir, state.entry[menu->current].name))
868 || state.entry[menu->current].inferiors
871 /* make sure this isn't a MH or maildir mailbox */
873 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
874 mutt_expand_path (buf, sizeof (buf));
877 else if (state.imap_browse) {
878 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
882 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
885 if ((mx_get_magic (buf) <= 0)
887 || state.entry[menu->current].inferiors
890 char OldLastDir[_POSIX_PATH_MAX];
892 /* save the old directory */
893 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
895 if (str_cmp (state.entry[menu->current].name, "..") == 0) {
896 if (str_cmp ("..", LastDir + m_strlen(LastDir) - 2) == 0)
897 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
899 char *p = strrchr (LastDir + 1, '/');
904 if (LastDir[0] == '/')
907 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
912 strfcpy (LastDir, state.entry[menu->current].name,
914 mutt_expand_path (LastDir, sizeof (LastDir));
917 else if (state.imap_browse) {
921 strfcpy (LastDir, state.entry[menu->current].name,
923 /* tack on delimiter here */
924 n = m_strlen(LastDir) + 1;
926 /* special case "" needs no delimiter */
927 url_parse_ciss (&url, state.entry[menu->current].name);
929 (state.entry[menu->current].delim != '\0') &&
930 (n < sizeof (LastDir))) {
932 LastDir[n - 1] = state.entry[menu->current].delim;
937 char tmp[_POSIX_PATH_MAX];
939 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
941 strfcpy (LastDir, tmp, sizeof (LastDir));
944 destroy_state (&state);
951 if (state.imap_browse) {
952 init_state (&state, NULL);
953 state.imap_browse = 1;
954 imap_browse (LastDir, &state);
955 browser_sort (&state);
956 menu->data = state.entry;
960 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
961 /* try to restore the old values */
962 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
963 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
964 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
970 init_menu (&state, menu, title, sizeof (title), buffy);
976 if (buffy || option (OPTNEWS)) /* news have not path */
981 strfcpy (f, state.entry[menu->current].name, flen);
982 mutt_expand_path (f, flen);
985 else if (state.imap_browse)
986 strfcpy (f, state.entry[menu->current].name, flen);
989 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
991 /* Fall through to OP_EXIT */
1000 *numfiles = menu->tagged;
1001 tfiles = p_new(char *, *numfiles);
1002 for (i = 0, j = 0; i < state.entrylen; i++) {
1003 struct folder_file ff = state.entry[i];
1004 char full[_POSIX_PATH_MAX];
1007 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1008 mutt_expand_path (full, sizeof (full));
1009 tfiles[j++] = m_strdup(full);
1014 else if (f[0]) { /* no tagged entries. return selected entry */
1016 tfiles = p_new(char *, *numfiles);
1017 mutt_expand_path (f, flen);
1018 tfiles[0] = m_strdup(f);
1023 destroy_state (&state);
1024 mutt_menuDestroy (&menu);
1027 case OP_BROWSER_TELL:
1029 mutt_message ("%s", state.entry[menu->current].name);
1033 case OP_BROWSER_TOGGLE_LSUB:
1034 if (option (OPTIMAPLSUB)) {
1035 unset_option (OPTIMAPLSUB);
1038 set_option (OPTIMAPLSUB);
1040 mutt_ungetch (0, OP_CHECK_NEW);
1043 case OP_CREATE_MAILBOX:
1044 if (!state.imap_browse)
1045 mutt_error (_("Create is only supported for IMAP mailboxes"));
1047 imap_mailbox_create (LastDir);
1048 /* TODO: find a way to detect if the new folder would appear in
1049 * this window, and insert it without starting over. */
1050 destroy_state (&state);
1051 init_state (&state, NULL);
1052 state.imap_browse = 1;
1053 imap_browse (LastDir, &state);
1054 browser_sort (&state);
1055 menu->data = state.entry;
1058 init_menu (&state, menu, title, sizeof (title), buffy);
1059 MAYBE_REDRAW (menu->redraw);
1063 case OP_RENAME_MAILBOX:
1064 if (!state.entry[menu->current].imap)
1065 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1067 int nentry = menu->current;
1069 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1070 destroy_state (&state);
1071 init_state (&state, NULL);
1072 state.imap_browse = 1;
1073 imap_browse (LastDir, &state);
1074 browser_sort (&state);
1075 menu->data = state.entry;
1078 init_menu (&state, menu, title, sizeof (title), buffy);
1079 MAYBE_REDRAW (menu->redraw);
1084 case OP_DELETE_MAILBOX:
1085 if (!state.entry[menu->current].imap)
1086 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1088 char msg[SHORT_STRING];
1090 int nentry = menu->current;
1092 imap_parse_path (state.entry[nentry].name, &mx);
1093 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1095 if (mutt_yesorno (msg, M_NO) == M_YES) {
1096 if (!imap_delete_mailbox (Context, mx)) {
1097 /* free the mailbox from the browser */
1098 p_delete(&((state.entry)[nentry].name));
1099 p_delete(&((state.entry)[nentry].desc));
1100 /* and move all other entries up */
1101 if (nentry + 1 < state.entrylen)
1102 memmove (state.entry + nentry, state.entry + nentry + 1,
1103 sizeof (struct folder_file) * (state.entrylen -
1106 mutt_message _("Mailbox deleted.");
1108 init_menu (&state, menu, title, sizeof (title), buffy);
1109 MAYBE_REDRAW (menu->redraw);
1113 mutt_message _("Mailbox not deleted.");
1119 case OP_CHANGE_DIRECTORY:
1122 if (option (OPTNEWS))
1126 strfcpy (buf, LastDir, sizeof (buf));
1128 if (!state.imap_browse)
1131 /* add '/' at the end of the directory name if not already there */
1132 int len = m_strlen(LastDir);
1134 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1138 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1141 mutt_expand_path (buf, sizeof (buf));
1143 if (imap_is_magic (buf, NULL) == M_IMAP) {
1144 strfcpy (LastDir, buf, sizeof (LastDir));
1145 destroy_state (&state);
1146 init_state (&state, NULL);
1147 state.imap_browse = 1;
1148 imap_browse (LastDir, &state);
1149 browser_sort (&state);
1150 menu->data = state.entry;
1153 init_menu (&state, menu, title, sizeof (title), buffy);
1157 if (stat (buf, &st) == 0) {
1158 if (S_ISDIR (st.st_mode)) {
1159 destroy_state (&state);
1160 if (examine_directory (menu, &state, buf, prefix) == 0)
1161 strfcpy (LastDir, buf, sizeof (LastDir));
1163 mutt_error _("Error scanning directory.");
1165 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1166 mutt_menuDestroy (&menu);
1172 init_menu (&state, menu, title, sizeof (title), buffy);
1175 mutt_error (_("%s is not a directory."), buf);
1180 MAYBE_REDRAW (menu->redraw);
1185 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1186 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1187 regex_t *rx = p_new(regex_t, 1);
1192 /* assume that the user wants to see everything */
1194 strfcpy (buf, ".", sizeof (buf));
1202 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1203 regerror (err, rx, buf, sizeof (buf));
1206 mutt_error ("%s", buf);
1209 str_replace (&Mask.pattern, buf);
1215 destroy_state (&state);
1217 if (state.imap_browse) {
1218 init_state (&state, NULL);
1219 state.imap_browse = 1;
1220 imap_browse (LastDir, &state);
1221 browser_sort (&state);
1222 menu->data = state.entry;
1223 init_menu (&state, menu, title, sizeof (title), buffy);
1227 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1228 init_menu (&state, menu, title, sizeof (title), buffy);
1230 mutt_error _("Error scanning directory.");
1232 mutt_menuDestroy (&menu);
1236 if (!state.entrylen) {
1237 mutt_error _("No files match the file mask");
1243 MAYBE_REDRAW (menu->redraw);
1247 case OP_SORT_REVERSE:
1251 int reverse = (i == OP_SORT_REVERSE);
1253 switch (mutt_multi_choice ((reverse) ?
1255 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1258 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1260 case -1: /* abort */
1264 case 1: /* (d)ate */
1265 BrowserSort = SORT_DATE;
1268 case 2: /* (a)lpha */
1269 BrowserSort = SORT_SUBJECT;
1272 case 3: /* si(z)e */
1273 BrowserSort = SORT_SIZE;
1276 case 4: /* do(n)'t sort */
1277 BrowserSort = SORT_ORDER;
1282 BrowserSort |= reverse ? SORT_REVERSE : 0;
1283 browser_sort (&state);
1284 menu->redraw = REDRAW_FULL;
1289 case OP_TOGGLE_MAILBOXES:
1293 destroy_state (&state);
1298 if (examine_mailboxes (menu, &state) == -1)
1302 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1303 init_state (&state, NULL);
1304 state.imap_browse = 1;
1305 imap_browse (LastDir, &state);
1306 browser_sort (&state);
1307 menu->data = state.entry;
1310 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1312 init_menu (&state, menu, title, sizeof (title), buffy);
1316 if (option (OPTFORCEBUFFYCHECK))
1321 case OP_BROWSER_NEW_FILE:
1323 snprintf (buf, sizeof (buf), "%s/", LastDir);
1324 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1326 strfcpy (f, buf, flen);
1327 destroy_state (&state);
1328 mutt_menuDestroy (&menu);
1331 MAYBE_REDRAW (menu->redraw);
1334 case OP_BROWSER_VIEW_FILE:
1335 if (!state.entrylen) {
1336 mutt_error _("No files match the file mask");
1342 if (state.entry[menu->current].selectable) {
1343 strfcpy (f, state.entry[menu->current].name, flen);
1344 destroy_state (&state);
1345 mutt_menuDestroy (&menu);
1350 if (S_ISDIR (state.entry[menu->current].mode) ||
1351 (S_ISLNK (state.entry[menu->current].mode) &&
1352 link_is_dir (LastDir, state.entry[menu->current].name))) {
1353 mutt_error _("Can't view a directory");
1359 char buf[_POSIX_PATH_MAX];
1361 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1363 b = mutt_make_file_attach (buf);
1365 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1366 mutt_free_body (&b);
1367 menu->redraw = REDRAW_FULL;
1370 mutt_error _("Error trying to view file");
1377 if (option (OPTNEWS)) {
1378 struct folder_file *f = &state.entry[menu->current];
1381 if (i == OP_CATCHUP)
1382 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1384 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1388 struct folder_file ff;
1389 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1392 folder.ff->name = f->name;
1393 folder.ff->st = NULL;
1394 folder.ff->is_new = nd->new;
1397 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1398 newsgroup_format_str, (unsigned long) &folder,
1399 M_FORMAT_ARROWCURSOR);
1400 f->desc = m_strdup(buffer); */
1401 if (menu->current + 1 < menu->max)
1403 menu->redraw = REDRAW_MOTION_RESYNCH;
1408 case OP_LOAD_ACTIVE:
1409 if (!option (OPTNEWS))
1416 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1417 if ((data = (NNTP_DATA *) tmp->data))
1421 nntp_get_active (CurrentNewsSrv);
1423 destroy_state (&state);
1425 examine_mailboxes (menu, &state);
1427 examine_directory (menu, &state, NULL, NULL);
1428 init_menu (&state, menu, title, sizeof (title), buffy);
1430 #endif /* USE_NNTP */
1432 #if defined USE_IMAP || defined USE_NNTP
1433 case OP_BROWSER_SUBSCRIBE:
1434 case OP_BROWSER_UNSUBSCRIBE:
1437 case OP_SUBSCRIBE_PATTERN:
1438 case OP_UNSUBSCRIBE_PATTERN:
1439 if (option (OPTNEWS)) {
1440 regex_t *rx = p_new(regex_t, 1);
1442 int j = menu->current;
1444 NNTP_SERVER *news = CurrentNewsSrv;
1446 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1451 if (i == OP_SUBSCRIBE_PATTERN)
1452 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1454 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1455 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1460 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1461 regerror (err, rx, buf, sizeof (buf));
1464 mutt_error ("%s", buf);
1467 menu->redraw = REDRAW_FULL;
1470 else if (!state.entrylen) {
1471 mutt_error _("No newsgroups match the mask");
1476 for (; j < state.entrylen; j++) {
1477 struct folder_file *f = &state.entry[j];
1479 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1480 regexec (rx, f->name, 0, NULL, 0) == 0) {
1481 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1482 nd = mutt_newsgroup_subscribe (news, f->name);
1484 nd = mutt_newsgroup_unsubscribe (news, f->name);
1488 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1490 folder.name = f->name;
1492 folder.new = nd->new;
1495 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1496 newsgroup_format_str, (unsigned long) &folder,
1497 M_FORMAT_ARROWCURSOR);
1498 f->desc = m_strdup(buffer);
1501 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1502 if (menu->current + 1 < menu->max)
1504 menu->redraw = REDRAW_MOTION_RESYNCH;
1508 if (i == OP_SUBSCRIBE_PATTERN) {
1509 LIST *grouplist = NULL;
1512 grouplist = news->list;
1513 for (; grouplist; grouplist = grouplist->next) {
1514 nd = (NNTP_DATA *) grouplist->data;
1515 if (nd && nd->group && !nd->subscribed) {
1516 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1517 mutt_newsgroup_subscribe (news, nd->group);
1518 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1522 init_menu (&state, menu, title, sizeof (title), buffy);
1524 mutt_newsrc_update (news);
1525 nntp_clear_cacheindex (news);
1526 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1532 #endif /* USE_IMAP && USE_NNTP */
1533 #endif /* USE_NNTP */
1536 if (i == OP_BROWSER_SUBSCRIBE)
1537 imap_subscribe (state.entry[menu->current].name, 1);
1539 imap_subscribe (state.entry[menu->current].name, 0);
1541 #endif /* USE_IMAP */
1548 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));