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.
10 #include <lib-lib/lib-lib.h>
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>
28 #include <imap/imap.h>
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},
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},
54 typedef struct folder_t {
55 struct folder_file *ff;
59 static char LastDir[_POSIX_PATH_MAX] = "";
60 static char LastDirBackup[_POSIX_PATH_MAX] = "";
62 /* Frees up the memory allocated for the local-global variables. */
63 static void destroy_state (struct browser_state *state)
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);
72 p_delete(&state->folder);
73 p_delete(&state->entry);
76 static int browser_compare_subject (const void *a, const void *b)
78 struct folder_file *pa = (struct folder_file *) a;
79 struct folder_file *pb = (struct folder_file *) b;
81 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
83 return ((BrowserSort & SORT_REVERSE) ? -r : r);
86 static int browser_compare_date (const void *a, const void *b)
88 struct folder_file *pa = (struct folder_file *) a;
89 struct folder_file *pb = (struct folder_file *) b;
91 int r = pa->mtime - pb->mtime;
93 return ((BrowserSort & SORT_REVERSE) ? -r : r);
96 static int browser_compare_size (const void *a, const void *b)
98 struct folder_file *pa = (struct folder_file *) a;
99 struct folder_file *pb = (struct folder_file *) b;
101 int r = pa->size - pb->size;
103 return ((BrowserSort & SORT_REVERSE) ? -r : r);
106 static void browser_sort (struct browser_state *state)
108 int (*f) (const void *, const void *);
110 switch (BrowserSort & SORT_MASK) {
115 if (option (OPTNEWS))
118 f = browser_compare_date;
122 if (option (OPTNEWS))
125 f = browser_compare_size;
129 f = browser_compare_subject;
132 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
135 static int link_is_dir (const char *folder, const char *path)
138 char fullpath[_POSIX_PATH_MAX];
140 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
142 if (stat (fullpath, &st) == 0)
143 return (S_ISDIR (st.st_mode));
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)
154 char fn[STRING], tmp[STRING], permission[11], date[16];
157 FOLDER *folder = data.ptr;
160 int optional = (flags & M_FORMAT_OPTIONAL);
164 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
165 snprintf (dest, destlen, tmp, folder->num + 1);
169 if (folder->ff->st != NULL) {
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);
179 mutt_format_s (dest, destlen, fmt, "");
186 if (folder->ff->imap)
187 s = NONULL(folder->ff->desc);
189 s = NONULL(folder->ff->name);
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 ? "*" : ""))) : "");
197 mutt_format_s (dest, destlen, fmt, fn);
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) !=
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) !=
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) !=
220 mutt_format_s (dest, destlen, fmt, permission);
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);
230 mutt_format_s (dest, destlen, fmt, "");
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);
238 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
239 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
243 mutt_format_s (dest, destlen, fmt, "");
247 if (folder->ff->st != NULL) {
248 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
249 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
252 mutt_format_s (dest, destlen, fmt, "");
256 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
258 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
259 snprintf (dest, destlen, tmp, folder->ff->new);
261 else if (!folder->ff->new)
265 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
266 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
270 if (folder->ff->st != NULL) {
271 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
272 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
275 mutt_format_s (dest, destlen, fmt, "");
279 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
280 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
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);
288 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
289 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
293 mutt_format_s (dest, destlen, fmt, "");
297 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
298 snprintf (dest, destlen, tmp, op);
303 if (flags & M_FORMAT_OPTIONAL)
304 m_strformat(dest, destlen, 0, optional ? ifstr : elstr,
305 folder_format_str, data, 0);
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)
317 char fn[STRING], tmp[STRING];
318 FOLDER *folder = data.ptr;
322 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
323 snprintf (dest, destlen, tmp, folder->num + 1);
327 m_strcpy(fn, sizeof(fn), folder->ff->name);
328 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
329 snprintf (dest, destlen, tmp, fn);
333 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
334 if (folder->ff->nd->subscribed)
335 snprintf (dest, destlen, tmp, ' ');
337 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
341 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
342 if (folder->ff->nd->deleted)
343 snprintf (dest, destlen, tmp, 'D');
345 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
349 if (flags & M_FORMAT_OPTIONAL) {
350 if (folder->ff->nd->unread != 0)
351 m_strformat(dest, destlen, 0, ifstr, newsgroup_format_str,
354 m_strformat (dest, destlen, 0, elstr, newsgroup_format_str,
357 else if (Context && Context->data == folder->ff->nd) {
358 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
359 snprintf (dest, destlen, tmp, Context->unread);
362 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
363 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
368 if (Context && Context->data == folder->ff->nd) {
369 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
370 snprintf (dest, destlen, tmp, Context->new);
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);
380 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
381 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
386 if (folder->ff->nd->desc != NULL) {
387 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
388 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
391 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
392 snprintf (dest, destlen, tmp, "");
398 #endif /* USE_NNTP */
401 static void add_folder (MUTTMENU * m, struct browser_state *state,
402 const char *name, const struct stat *s,
405 static void add_folder (MUTTMENU * m, struct browser_state *state,
406 const char *name, const struct stat *s,
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);
415 m->data = state->entry;
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);
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;
430 if (option (OPTNEWS))
431 (state->entry)[state->entrylen].nd = data;
436 static void init_state (struct browser_state *state, MUTTMENU * menu)
439 state->entrymax = 256;
440 state->entry = p_new(struct folder_file, state->entrymax);
441 state->imap_browse = 0;
443 menu->data = state->entry;
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)
451 if (option (OPTNEWS)) {
453 nntp_server_t *news = CurrentNewsSrv;
455 /* buffy_check (0); */
456 init_state (state, menu);
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)
462 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.neg))
464 add_folder (menu, state, data->group, NULL, data, data->new);
469 #endif /* USE_NNTP */
474 char buffer[_POSIX_PATH_MAX + STRING];
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, '/');
491 if (!S_ISDIR (s.st_mode)) {
492 mutt_error (_("%s is not a directory."), d);
498 if ((dp = opendir (d)) == NULL) {
503 init_state (state, menu);
505 while ((de = readdir (dp)) != NULL) {
506 if (m_strcmp(de->d_name, ".") == 0)
507 continue; /* we don't need . */
509 if (m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
511 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.neg))
514 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
515 if (lstat (buffer, &s) == -1)
518 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
519 (!S_ISLNK (s.st_mode)))
522 i = buffy_lookup (buffer);
524 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? Incoming.arr[i]->new : 0);
526 add_folder (menu, state, de->d_name, &s, i >= 0 ? Incoming.arr[i]->new : 0);
532 browser_sort (state);
536 /* get list of mailboxes/subscribed newsgroups */
537 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
540 char buffer[LONG_STRING];
543 if (option (OPTNEWS)) {
545 nntp_server_t *news = CurrentNewsSrv;
547 /* buffy_check (0); */
548 init_state (state, menu);
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);
568 init_state (state, menu);
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) {
575 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
577 add_folder (menu, state, tmp->path, NULL, tmp->new);
581 if (tmp->magic == M_POP) {
583 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
585 add_folder (menu, state, tmp->path, NULL, tmp->new);
590 if (tmp->magic == M_NNTP) {
591 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
595 if (lstat (tmp->path, &s) == -1)
598 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
599 (!S_ISLNK (s.st_mode)))
602 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
603 mutt_pretty_mailbox (buffer);
606 add_folder (menu, state, buffer, &s, NULL, tmp->new);
608 add_folder (menu, state, buffer, &s, tmp->new);
612 browser_sort (state);
616 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
619 if (option (OPTNEWS))
621 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
624 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
627 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
631 folder.ff = &((struct folder_file *) menu->data)[num];
635 if (option (OPTNEWS))
636 m_strformat(s, slen, COLS - SW, GroupFormat, newsgroup_format_str,
640 m_strformat(s, slen, COLS - SW, FolderFormat, folder_format_str,
644 static void init_menu (struct browser_state *state, MUTTMENU * menu,
645 char *title, ssize_t titlelen, int buffy)
647 char path[_POSIX_PATH_MAX];
649 menu->max = state->entrylen;
651 if (menu->current >= menu->max)
652 menu->current = menu->max - 1;
653 if (menu->current < 0)
655 if (menu->top > menu->current)
661 if (option (OPTNEWS)) {
663 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
665 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
666 CurrentNewsSrv->conn->account.host);
671 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
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));
679 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
680 path, NONULL (Mask.pattern));
682 menu->redraw = REDRAW_FULL;
685 static int file_tag (MUTTMENU * menu, int n, int m)
687 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
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!");
698 ff->tagged = (m >= 0 ? m : !ff->tagged);
700 return ff->tagged - ot;
703 void mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
706 char buf[_POSIX_PATH_MAX];
707 char prefix[_POSIX_PATH_MAX] = "";
708 char helpstr[STRING];
710 struct browser_state state;
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;
718 buffy = buffy && folder;
723 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
726 if (option (OPTNEWS)) {
728 m_strcpy(prefix, sizeof(prefix), f);
732 /* default state for news reader mode is browse subscribed newsgroups */
734 for (list = CurrentNewsSrv->list; list; list = list->next) {
735 if (list->subscribed) {
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);
753 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
756 i = MIN(ssizeof(LastDir) - 1, i);
757 m_strcpy(LastDir, sizeof(LastDir), f);
760 getcwd(LastDir, sizeof(LastDir));
761 m_strcat(LastDir, sizeof(LastDir), "/");
762 m_strncat(LastDir, sizeof(LastDir), f, i);
767 m_strcpy(LastDir, sizeof(LastDir), "/");
769 getcwd (LastDir, sizeof (LastDir));
772 if (i <= 0 && f[0] != '/')
773 m_strcpy(prefix, sizeof(prefix), f);
775 m_strcpy(prefix, sizeof(prefix), f + i + 1);
781 getcwd (LastDir, sizeof (LastDir));
782 else if (!LastDir[0])
783 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
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);
796 if (examine_mailboxes (NULL, &state) == -1)
800 if (!state.imap_browse)
801 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
804 menu = mutt_new_menu ();
805 menu->menu = MENU_FOLDER;
806 menu->make_entry = folder_entry;
807 menu->search = select_file_search;
809 menu->data = state.entry;
811 menu->tag = file_tag;
813 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
815 (option (OPTNEWS)) ? FolderNewsHelp :
819 init_menu (&state, menu, title, sizeof (title), buffy);
822 switch (i = mutt_menuLoop (menu)) {
823 case OP_GENERIC_SELECT_ENTRY:
825 if (!state.entrylen) {
826 mutt_error _("No files match the file mask");
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
836 /* make sure this isn't a MH or maildir mailbox */
838 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
839 mutt_expand_path (buf, sizeof (buf));
841 else if (state.imap_browse) {
842 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
845 mutt_concat_path(buf, sizeof(buf), LastDir,
846 state.entry[menu->current].name);
848 if ((mx_get_magic (buf) <= 0)
849 || state.entry[menu->current].inferiors)
851 char OldLastDir[_POSIX_PATH_MAX];
853 /* save the old directory */
854 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
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), "/..");
860 char *p = strrchr (LastDir + 1, '/');
865 if (LastDir[0] == '/')
868 m_strcat(LastDir, sizeof(LastDir), "/..");
873 m_strcpy(LastDir, sizeof(LastDir),
874 state.entry[menu->current].name);
875 mutt_expand_path (LastDir, sizeof (LastDir));
877 else if (state.imap_browse) {
881 m_strcpy(LastDir, sizeof(LastDir),
882 state.entry[menu->current].name);
883 /* tack on delimiter here */
884 n = m_strlen(LastDir) + 1;
886 /* special case "" needs no delimiter */
887 url_parse_ciss (&url, state.entry[menu->current].name);
889 (state.entry[menu->current].delim != '\0') &&
890 (n < ssizeof (LastDir))) {
892 LastDir[n - 1] = state.entry[menu->current].delim;
896 char tmp[_POSIX_PATH_MAX];
898 mutt_concat_path(tmp, sizeof(tmp), LastDir,
899 state.entry[menu->current].name);
900 m_strcpy(LastDir, sizeof(LastDir), tmp);
903 destroy_state (&state);
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;
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));
927 init_menu (&state, menu, title, sizeof (title), buffy);
933 if (buffy || option (OPTNEWS)) /* news have not path */
938 m_strcpy(f, flen, state.entry[menu->current].name);
939 mutt_expand_path (f, flen);
941 else if (state.imap_browse)
942 m_strcpy(f, flen, state.entry[menu->current].name);
944 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
946 /* Fall through to OP_EXIT */
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];
963 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
964 mutt_expand_path (full, sizeof (full));
965 tfiles[j++] = m_strdup(full);
970 else if (f[0]) { /* no tagged entries. return selected entry */
972 tfiles = p_new(char *, *numfiles);
973 mutt_expand_path (f, flen);
974 tfiles[0] = m_strdup(f);
979 destroy_state (&state);
980 mutt_menuDestroy (&menu);
983 case OP_BROWSER_TELL:
985 mutt_message ("%s", state.entry[menu->current].name);
988 case OP_BROWSER_TOGGLE_LSUB:
989 if (option (OPTIMAPLSUB)) {
990 unset_option (OPTIMAPLSUB);
993 set_option (OPTIMAPLSUB);
995 mutt_ungetch (0, OP_CHECK_NEW);
998 case OP_CREATE_MAILBOX:
999 if (!state.imap_browse)
1000 mutt_error (_("Create is only supported for IMAP mailboxes"));
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;
1013 init_menu (&state, menu, title, sizeof (title), buffy);
1014 MAYBE_REDRAW (menu->redraw);
1018 case OP_RENAME_MAILBOX:
1019 if (!state.entry[menu->current].imap)
1020 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1022 int nentry = menu->current;
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;
1033 init_menu (&state, menu, title, sizeof (title), buffy);
1034 MAYBE_REDRAW (menu->redraw);
1039 case OP_DELETE_MAILBOX:
1040 if (!state.entry[menu->current].imap)
1041 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1045 int nentry = menu->current;
1047 imap_parse_path (state.entry[nentry].name, &mx);
1048 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
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 -
1061 mutt_message _("Mailbox deleted.");
1063 init_menu (&state, menu, title, sizeof (title), buffy);
1064 MAYBE_REDRAW (menu->redraw);
1068 mutt_message _("Mailbox not deleted.");
1073 case OP_CHANGE_DIRECTORY:
1076 if (option (OPTNEWS))
1080 m_strcpy(buf, sizeof(buf), LastDir);
1081 if (!state.imap_browse)
1083 /* add '/' at the end of the directory name if not already there */
1084 ssize_t len = m_strlen(LastDir);
1086 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1090 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 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;
1104 init_menu (&state, menu, title, sizeof (title), buffy);
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);
1113 mutt_error _("Error scanning directory.");
1115 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1116 mutt_menuDestroy (&menu);
1122 init_menu (&state, menu, title, sizeof (title), buffy);
1125 mutt_error (_("%s is not a directory."), buf);
1130 MAYBE_REDRAW (menu->redraw);
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);
1142 /* assume that the user wants to see everything */
1144 m_strcpy(buf, sizeof(buf), ".");
1147 s = vskipspaces(s + 1);
1151 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1152 regerror (err, rx, buf, sizeof (buf));
1155 mutt_error ("%s", buf);
1158 m_strreplace(&Mask.pattern, buf);
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);
1174 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1175 init_menu (&state, menu, title, sizeof (title), buffy);
1177 mutt_error _("Error scanning directory.");
1179 mutt_menuDestroy (&menu);
1183 if (!state.entrylen) {
1184 mutt_error _("No files match the file mask");
1190 MAYBE_REDRAW (menu->redraw);
1194 case OP_SORT_REVERSE:
1198 int reverse = (i == OP_SORT_REVERSE);
1200 switch (mutt_multi_choice ((reverse) ?
1202 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1205 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1207 case -1: /* abort */
1211 case 1: /* (d)ate */
1212 BrowserSort = SORT_DATE;
1215 case 2: /* (a)lpha */
1216 BrowserSort = SORT_SUBJECT;
1219 case 3: /* si(z)e */
1220 BrowserSort = SORT_SIZE;
1223 case 4: /* do(n)'t sort */
1224 BrowserSort = SORT_ORDER;
1229 BrowserSort |= reverse ? SORT_REVERSE : 0;
1230 browser_sort (&state);
1231 menu->redraw = REDRAW_FULL;
1236 case OP_TOGGLE_MAILBOXES:
1240 destroy_state (&state);
1245 if (examine_mailboxes (menu, &state) == -1)
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;
1255 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1257 init_menu (&state, menu, title, sizeof (title), buffy);
1261 if (option (OPTFORCEBUFFYCHECK))
1266 case OP_BROWSER_NEW_FILE:
1268 snprintf (buf, sizeof (buf), "%s/", LastDir);
1269 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1271 m_strcpy(f, flen, buf);
1272 destroy_state (&state);
1273 mutt_menuDestroy (&menu);
1276 MAYBE_REDRAW (menu->redraw);
1279 case OP_BROWSER_VIEW_FILE:
1280 if (!state.entrylen) {
1281 mutt_error _("No files match the file mask");
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);
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");
1302 char nbuf[_POSIX_PATH_MAX];
1304 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1305 state.entry[menu->current].name);
1306 b = mutt_make_file_attach (nbuf);
1308 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1310 menu->redraw = REDRAW_FULL;
1313 mutt_error _("Error trying to view file");
1320 if (option (OPTNEWS)) {
1321 struct folder_file *folder_f = &state.entry[menu->current];
1324 if (i == OP_CATCHUP)
1325 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1327 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1330 if (menu->current + 1 < menu->max)
1332 menu->redraw = REDRAW_MOTION_RESYNCH;
1337 case OP_LOAD_ACTIVE:
1338 if (!option (OPTNEWS))
1344 for (data = CurrentNewsSrv->list; data; data = data->next) {
1348 nntp_get_active (CurrentNewsSrv);
1350 destroy_state (&state);
1352 examine_mailboxes (menu, &state);
1354 examine_directory (menu, &state, NULL, NULL);
1355 init_menu (&state, menu, title, sizeof (title), buffy);
1357 #endif /* USE_NNTP */
1359 case OP_BROWSER_SUBSCRIBE:
1360 case OP_BROWSER_UNSUBSCRIBE:
1362 case OP_SUBSCRIBE_PATTERN:
1363 case OP_UNSUBSCRIBE_PATTERN:
1364 if (option (OPTNEWS)) {
1365 regex_t *rx = p_new(regex_t, 1);
1367 int j = menu->current;
1369 nntp_server_t *news = CurrentNewsSrv;
1371 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1376 if (i == OP_SUBSCRIBE_PATTERN)
1377 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1379 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1380 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1385 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1386 regerror (err, rx, buf, sizeof (buf));
1389 mutt_error ("%s", buf);
1392 menu->redraw = REDRAW_FULL;
1395 else if (!state.entrylen) {
1396 mutt_error _("No newsgroups match the mask");
1401 for (; j < state.entrylen; j++) {
1402 struct folder_file *folderf = &state.entry[j];
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);
1409 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1411 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1412 if (menu->current + 1 < menu->max)
1414 menu->redraw = REDRAW_MOTION_RESYNCH;
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);
1428 init_menu (&state, menu, title, sizeof (title), buffy);
1430 mutt_newsrc_update (news);
1431 nntp_clear_cacheindex (news);
1432 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1437 #endif /* USE_NNTP */
1439 if (i == OP_BROWSER_SUBSCRIBE)
1440 imap_subscribe (state.entry[menu->current].name, 1);
1442 imap_subscribe (state.entry[menu->current].name, 0);
1450 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);