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>
30 #include <nntp/nntp.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 = (NNTP_DATA *) 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)) {
454 NNTP_SERVER *news = CurrentNewsSrv;
456 /* buffy_check (0); */
457 init_state (state, menu);
459 for (tmp = news->list; tmp; tmp = tmp->next) {
460 if (!(data = (NNTP_DATA *) tmp->data))
462 nntp_sync_sidebar (data);
463 if (m_strncmp (prefix, data->group, m_strlen(prefix)) != 0)
465 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.neg))
467 add_folder (menu, state, data->group, NULL, data, data->new);
472 #endif /* USE_NNTP */
477 char buffer[_POSIX_PATH_MAX + STRING];
480 while (stat (d, &s) == -1) {
481 if (errno == ENOENT) {
482 /* The last used directory is deleted, try to use the parent dir. */
483 char *c = strrchr (d, '/');
494 if (!S_ISDIR (s.st_mode)) {
495 mutt_error (_("%s is not a directory."), d);
501 if ((dp = opendir (d)) == NULL) {
506 init_state (state, menu);
508 while ((de = readdir (dp)) != NULL) {
509 if (m_strcmp(de->d_name, ".") == 0)
510 continue; /* we don't need . */
512 if (m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
514 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.neg))
517 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
518 if (lstat (buffer, &s) == -1)
521 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
522 (!S_ISLNK (s.st_mode)))
525 i = buffy_lookup (buffer);
527 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? Incoming.arr[i]->new : 0);
529 add_folder (menu, state, de->d_name, &s, i >= 0 ? Incoming.arr[i]->new : 0);
535 browser_sort (state);
539 /* get list of mailboxes/subscribed newsgroups */
540 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
543 char buffer[LONG_STRING];
546 if (option (OPTNEWS)) {
549 NNTP_SERVER *news = CurrentNewsSrv;
551 /* buffy_check (0); */
552 init_state (state, menu);
554 for (tmp = news->list; tmp; tmp = tmp->next) {
555 if ((data = (NNTP_DATA*) tmp->data) == NULL)
557 nntp_sync_sidebar (data);
558 if ((data->new || (data->subscribed &&
559 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
560 add_folder (menu, state, data->group, NULL, data, data->new);
574 init_state (state, menu);
576 for (i = 0; i < Incoming.len; i++) {
577 tmp = Incoming.arr[i];
578 tmp->magic = mx_get_magic (tmp->path);
579 if (tmp->magic == M_IMAP) {
581 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
583 add_folder (menu, state, tmp->path, NULL, tmp->new);
587 if (tmp->magic == M_POP) {
589 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
591 add_folder (menu, state, tmp->path, NULL, tmp->new);
596 if (tmp->magic == M_NNTP) {
597 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
601 if (lstat (tmp->path, &s) == -1)
604 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
605 (!S_ISLNK (s.st_mode)))
608 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
609 mutt_pretty_mailbox (buffer);
612 add_folder (menu, state, buffer, &s, NULL, tmp->new);
614 add_folder (menu, state, buffer, &s, tmp->new);
618 browser_sort (state);
622 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
625 if (option (OPTNEWS))
627 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
630 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
633 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
637 folder.ff = &((struct folder_file *) menu->data)[num];
641 if (option (OPTNEWS))
642 m_strformat(s, slen, COLS - SW, GroupFormat, newsgroup_format_str,
646 m_strformat(s, slen, COLS - SW, FolderFormat, folder_format_str,
650 static void init_menu (struct browser_state *state, MUTTMENU * menu,
651 char *title, ssize_t titlelen, int buffy)
653 char path[_POSIX_PATH_MAX];
655 menu->max = state->entrylen;
657 if (menu->current >= menu->max)
658 menu->current = menu->max - 1;
659 if (menu->current < 0)
661 if (menu->top > menu->current)
667 if (option (OPTNEWS)) {
669 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
671 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
672 CurrentNewsSrv->conn->account.host);
677 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
679 m_strcpy(path, sizeof(path), LastDir);
680 mutt_pretty_mailbox (path);
681 if (state->imap_browse && option (OPTIMAPLSUB))
682 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
683 path, NONULL (Mask.pattern));
685 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
686 path, NONULL (Mask.pattern));
688 menu->redraw = REDRAW_FULL;
691 static int file_tag (MUTTMENU * menu, int n, int m)
693 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
696 if (S_ISDIR (ff->mode)
697 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
698 mutt_error _("Can't attach a directory!");
704 ff->tagged = (m >= 0 ? m : !ff->tagged);
706 return ff->tagged - ot;
709 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
712 char buf[_POSIX_PATH_MAX];
713 char prefix[_POSIX_PATH_MAX] = "";
714 char helpstr[STRING];
716 struct browser_state state;
719 int i, killPrefix = 0;
720 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
721 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
722 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
724 buffy = buffy && folder;
729 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
732 if (option (OPTNEWS)) {
734 m_strcpy(prefix, sizeof(prefix), f);
738 /* default state for news reader mode is browse subscribed newsgroups */
740 for (list = CurrentNewsSrv->list; list; list = list->next) {
741 NNTP_DATA *data = (NNTP_DATA *) list->data;
743 if (data && data->subscribed) {
753 mutt_expand_path (f, flen);
754 if (imap_is_magic (f, NULL) == M_IMAP) {
755 init_state (&state, NULL);
756 state.imap_browse = 1;
757 if (!imap_browse (f, &state))
758 m_strcpy(LastDir, sizeof(LastDir), state.folder);
761 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
764 i = MIN(ssizeof(LastDir) - 1, i);
765 m_strcpy(LastDir, sizeof(LastDir), f);
768 getcwd(LastDir, sizeof(LastDir));
769 m_strcat(LastDir, sizeof(LastDir), "/");
770 m_strncat(LastDir, sizeof(LastDir), f, i);
775 m_strcpy(LastDir, sizeof(LastDir), "/");
777 getcwd (LastDir, sizeof (LastDir));
780 if (i <= 0 && f[0] != '/')
781 m_strcpy(prefix, sizeof(prefix), f);
783 m_strcpy(prefix, sizeof(prefix), f + i + 1);
789 getcwd (LastDir, sizeof (LastDir));
790 else if (!LastDir[0])
791 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
793 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
794 init_state (&state, NULL);
795 state.imap_browse = 1;
796 imap_browse (LastDir, &state);
797 browser_sort (&state);
804 if (examine_mailboxes (NULL, &state) == -1)
808 if (!state.imap_browse)
809 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
812 menu = mutt_new_menu ();
813 menu->menu = MENU_FOLDER;
814 menu->make_entry = folder_entry;
815 menu->search = select_file_search;
817 menu->data = state.entry;
819 menu->tag = file_tag;
821 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
823 (option (OPTNEWS)) ? FolderNewsHelp :
827 init_menu (&state, menu, title, sizeof (title), buffy);
830 switch (i = mutt_menuLoop (menu)) {
831 case OP_GENERIC_SELECT_ENTRY:
833 if (!state.entrylen) {
834 mutt_error _("No files match the file mask");
839 if (S_ISDIR (state.entry[menu->current].mode) ||
840 (S_ISLNK (state.entry[menu->current].mode) &&
841 link_is_dir (LastDir, state.entry[menu->current].name))
842 || state.entry[menu->current].inferiors
844 /* make sure this isn't a MH or maildir mailbox */
846 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
847 mutt_expand_path (buf, sizeof (buf));
849 else if (state.imap_browse) {
850 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
853 mutt_concat_path(buf, sizeof(buf), LastDir,
854 state.entry[menu->current].name);
856 if ((mx_get_magic (buf) <= 0)
857 || state.entry[menu->current].inferiors)
859 char OldLastDir[_POSIX_PATH_MAX];
861 /* save the old directory */
862 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
864 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
865 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
866 m_strcat(LastDir, sizeof(LastDir), "/..");
868 char *p = strrchr (LastDir + 1, '/');
873 if (LastDir[0] == '/')
876 m_strcat(LastDir, sizeof(LastDir), "/..");
881 m_strcpy(LastDir, sizeof(LastDir),
882 state.entry[menu->current].name);
883 mutt_expand_path (LastDir, sizeof (LastDir));
885 else if (state.imap_browse) {
889 m_strcpy(LastDir, sizeof(LastDir),
890 state.entry[menu->current].name);
891 /* tack on delimiter here */
892 n = m_strlen(LastDir) + 1;
894 /* special case "" needs no delimiter */
895 url_parse_ciss (&url, state.entry[menu->current].name);
897 (state.entry[menu->current].delim != '\0') &&
898 (n < ssizeof (LastDir))) {
900 LastDir[n - 1] = state.entry[menu->current].delim;
904 char tmp[_POSIX_PATH_MAX];
906 mutt_concat_path(tmp, sizeof(tmp), LastDir,
907 state.entry[menu->current].name);
908 m_strcpy(LastDir, sizeof(LastDir), tmp);
911 destroy_state (&state);
917 if (state.imap_browse) {
918 init_state (&state, NULL);
919 state.imap_browse = 1;
920 imap_browse (LastDir, &state);
921 browser_sort (&state);
922 menu->data = state.entry;
925 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
926 /* try to restore the old values */
927 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
928 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
929 m_strcpy(LastDir, sizeof(LastDir), NONULL(mod_core.homedir));
935 init_menu (&state, menu, title, sizeof (title), buffy);
941 if (buffy || option (OPTNEWS)) /* news have not path */
946 m_strcpy(f, flen, state.entry[menu->current].name);
947 mutt_expand_path (f, flen);
949 else if (state.imap_browse)
950 m_strcpy(f, flen, state.entry[menu->current].name);
952 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
954 /* Fall through to OP_EXIT */
964 *numfiles = menu->tagged;
965 tfiles = p_new(char *, *numfiles);
966 for (h = 0, j = 0; h < state.entrylen; i++) {
967 struct folder_file ff = state.entry[i];
968 char full[_POSIX_PATH_MAX];
971 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
972 mutt_expand_path (full, sizeof (full));
973 tfiles[j++] = m_strdup(full);
978 else if (f[0]) { /* no tagged entries. return selected entry */
980 tfiles = p_new(char *, *numfiles);
981 mutt_expand_path (f, flen);
982 tfiles[0] = m_strdup(f);
987 destroy_state (&state);
988 mutt_menuDestroy (&menu);
991 case OP_BROWSER_TELL:
993 mutt_message ("%s", state.entry[menu->current].name);
996 case OP_BROWSER_TOGGLE_LSUB:
997 if (option (OPTIMAPLSUB)) {
998 unset_option (OPTIMAPLSUB);
1001 set_option (OPTIMAPLSUB);
1003 mutt_ungetch (0, OP_CHECK_NEW);
1006 case OP_CREATE_MAILBOX:
1007 if (!state.imap_browse)
1008 mutt_error (_("Create is only supported for IMAP mailboxes"));
1010 imap_mailbox_create (LastDir);
1011 /* TODO: find a way to detect if the new folder would appear in
1012 * this window, and insert it without starting over. */
1013 destroy_state (&state);
1014 init_state (&state, NULL);
1015 state.imap_browse = 1;
1016 imap_browse (LastDir, &state);
1017 browser_sort (&state);
1018 menu->data = state.entry;
1021 init_menu (&state, menu, title, sizeof (title), buffy);
1022 MAYBE_REDRAW (menu->redraw);
1026 case OP_RENAME_MAILBOX:
1027 if (!state.entry[menu->current].imap)
1028 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1030 int nentry = menu->current;
1032 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1033 destroy_state (&state);
1034 init_state (&state, NULL);
1035 state.imap_browse = 1;
1036 imap_browse (LastDir, &state);
1037 browser_sort (&state);
1038 menu->data = state.entry;
1041 init_menu (&state, menu, title, sizeof (title), buffy);
1042 MAYBE_REDRAW (menu->redraw);
1047 case OP_DELETE_MAILBOX:
1048 if (!state.entry[menu->current].imap)
1049 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1053 int nentry = menu->current;
1055 imap_parse_path (state.entry[nentry].name, &mx);
1056 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1058 if (mutt_yesorno (msg, M_NO) == M_YES) {
1059 if (!imap_delete_mailbox (Context, mx)) {
1060 /* free the mailbox from the browser */
1061 p_delete(&((state.entry)[nentry].name));
1062 p_delete(&((state.entry)[nentry].desc));
1063 /* and move all other entries up */
1064 if (nentry + 1 < state.entrylen)
1065 memmove (state.entry + nentry, state.entry + nentry + 1,
1066 sizeof (struct folder_file) * (state.entrylen -
1069 mutt_message _("Mailbox deleted.");
1071 init_menu (&state, menu, title, sizeof (title), buffy);
1072 MAYBE_REDRAW (menu->redraw);
1076 mutt_message _("Mailbox not deleted.");
1081 case OP_CHANGE_DIRECTORY:
1084 if (option (OPTNEWS))
1088 m_strcpy(buf, sizeof(buf), LastDir);
1089 if (!state.imap_browse)
1091 /* add '/' at the end of the directory name if not already there */
1092 ssize_t len = m_strlen(LastDir);
1094 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1098 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1101 mutt_expand_path (buf, sizeof (buf));
1102 if (imap_is_magic (buf, NULL) == M_IMAP) {
1103 m_strcpy(LastDir, sizeof(LastDir), buf);
1104 destroy_state (&state);
1105 init_state (&state, NULL);
1106 state.imap_browse = 1;
1107 imap_browse (LastDir, &state);
1108 browser_sort (&state);
1109 menu->data = state.entry;
1112 init_menu (&state, menu, title, sizeof (title), buffy);
1115 if (stat (buf, &st) == 0) {
1116 if (S_ISDIR (st.st_mode)) {
1117 destroy_state (&state);
1118 if (examine_directory (menu, &state, buf, prefix) == 0)
1119 m_strcpy(LastDir, sizeof(LastDir), buf);
1121 mutt_error _("Error scanning directory.");
1123 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1124 mutt_menuDestroy (&menu);
1130 init_menu (&state, menu, title, sizeof (title), buffy);
1133 mutt_error (_("%s is not a directory."), buf);
1138 MAYBE_REDRAW (menu->redraw);
1143 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1144 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1145 regex_t *rx = p_new(regex_t, 1);
1150 /* assume that the user wants to see everything */
1152 m_strcpy(buf, sizeof(buf), ".");
1155 s = vskipspaces(s + 1);
1159 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1160 regerror (err, rx, buf, sizeof (buf));
1163 mutt_error ("%s", buf);
1166 m_strreplace(&Mask.pattern, buf);
1172 destroy_state (&state);
1173 if (state.imap_browse) {
1174 init_state (&state, NULL);
1175 state.imap_browse = 1;
1176 imap_browse (LastDir, &state);
1177 browser_sort (&state);
1178 menu->data = state.entry;
1179 init_menu (&state, menu, title, sizeof (title), buffy);
1182 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1183 init_menu (&state, menu, title, sizeof (title), buffy);
1185 mutt_error _("Error scanning directory.");
1187 mutt_menuDestroy (&menu);
1191 if (!state.entrylen) {
1192 mutt_error _("No files match the file mask");
1198 MAYBE_REDRAW (menu->redraw);
1202 case OP_SORT_REVERSE:
1206 int reverse = (i == OP_SORT_REVERSE);
1208 switch (mutt_multi_choice ((reverse) ?
1210 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1213 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1215 case -1: /* abort */
1219 case 1: /* (d)ate */
1220 BrowserSort = SORT_DATE;
1223 case 2: /* (a)lpha */
1224 BrowserSort = SORT_SUBJECT;
1227 case 3: /* si(z)e */
1228 BrowserSort = SORT_SIZE;
1231 case 4: /* do(n)'t sort */
1232 BrowserSort = SORT_ORDER;
1237 BrowserSort |= reverse ? SORT_REVERSE : 0;
1238 browser_sort (&state);
1239 menu->redraw = REDRAW_FULL;
1244 case OP_TOGGLE_MAILBOXES:
1248 destroy_state (&state);
1253 if (examine_mailboxes (menu, &state) == -1)
1256 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1257 init_state (&state, NULL);
1258 state.imap_browse = 1;
1259 imap_browse (LastDir, &state);
1260 browser_sort (&state);
1261 menu->data = state.entry;
1263 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1265 init_menu (&state, menu, title, sizeof (title), buffy);
1269 if (option (OPTFORCEBUFFYCHECK))
1274 case OP_BROWSER_NEW_FILE:
1276 snprintf (buf, sizeof (buf), "%s/", LastDir);
1277 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1279 m_strcpy(f, flen, buf);
1280 destroy_state (&state);
1281 mutt_menuDestroy (&menu);
1284 MAYBE_REDRAW (menu->redraw);
1287 case OP_BROWSER_VIEW_FILE:
1288 if (!state.entrylen) {
1289 mutt_error _("No files match the file mask");
1294 if (state.entry[menu->current].selectable) {
1295 m_strcpy(f, flen, state.entry[menu->current].name);
1296 destroy_state (&state);
1297 mutt_menuDestroy (&menu);
1301 if (S_ISDIR (state.entry[menu->current].mode) ||
1302 (S_ISLNK (state.entry[menu->current].mode) &&
1303 link_is_dir (LastDir, state.entry[menu->current].name))) {
1304 mutt_error _("Can't view a directory");
1310 char nbuf[_POSIX_PATH_MAX];
1312 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1313 state.entry[menu->current].name);
1314 b = mutt_make_file_attach (nbuf);
1316 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1318 menu->redraw = REDRAW_FULL;
1321 mutt_error _("Error trying to view file");
1328 if (option (OPTNEWS)) {
1329 struct folder_file *folder_f = &state.entry[menu->current];
1332 if (i == OP_CATCHUP)
1333 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1335 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1338 if (menu->current + 1 < menu->max)
1340 menu->redraw = REDRAW_MOTION_RESYNCH;
1345 case OP_LOAD_ACTIVE:
1346 if (!option (OPTNEWS))
1353 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1354 if ((data = (NNTP_DATA *) tmp->data))
1358 nntp_get_active (CurrentNewsSrv);
1360 destroy_state (&state);
1362 examine_mailboxes (menu, &state);
1364 examine_directory (menu, &state, NULL, NULL);
1365 init_menu (&state, menu, title, sizeof (title), buffy);
1367 #endif /* USE_NNTP */
1369 case OP_BROWSER_SUBSCRIBE:
1370 case OP_BROWSER_UNSUBSCRIBE:
1372 case OP_SUBSCRIBE_PATTERN:
1373 case OP_UNSUBSCRIBE_PATTERN:
1374 if (option (OPTNEWS)) {
1375 regex_t *rx = p_new(regex_t, 1);
1377 int j = menu->current;
1379 NNTP_SERVER *news = CurrentNewsSrv;
1381 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1386 if (i == OP_SUBSCRIBE_PATTERN)
1387 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1389 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1390 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1395 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1396 regerror (err, rx, buf, sizeof (buf));
1399 mutt_error ("%s", buf);
1402 menu->redraw = REDRAW_FULL;
1405 else if (!state.entrylen) {
1406 mutt_error _("No newsgroups match the mask");
1411 for (; j < state.entrylen; j++) {
1412 struct folder_file *folderf = &state.entry[j];
1414 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1415 regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1416 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1417 nd = mutt_newsgroup_subscribe (news, folderf->name);
1419 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1421 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1422 if (menu->current + 1 < menu->max)
1424 menu->redraw = REDRAW_MOTION_RESYNCH;
1428 if (i == OP_SUBSCRIBE_PATTERN) {
1429 string_list_t *grouplist = NULL;
1432 grouplist = news->list;
1433 for (; grouplist; grouplist = grouplist->next) {
1434 nd = (NNTP_DATA *) grouplist->data;
1435 if (nd && nd->group && !nd->subscribed) {
1436 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1437 mutt_newsgroup_subscribe (news, nd->group);
1438 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1442 init_menu (&state, menu, title, sizeof (title), buffy);
1444 mutt_newsrc_update (news);
1445 nntp_clear_cacheindex (news);
1446 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1451 #endif /* USE_NNTP */
1453 if (i == OP_BROWSER_SUBSCRIBE)
1454 imap_subscribe (state.entry[menu->current].name, 1);
1456 imap_subscribe (state.entry[menu->current].name, 0);
1464 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);