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));
148 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
149 const char *src, const char *fmt,
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->
204 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
205 st_mode) ? 'l' : '-'),
206 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
207 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
208 (folder->ff->st->st_mode & S_ISUID) !=
209 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
211 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
212 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
213 (folder->ff->st->st_mode & S_ISGID) !=
214 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
216 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
217 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
218 (folder->ff->st->st_mode & S_ISVTX) !=
219 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
221 mutt_format_s (dest, destlen, fmt, permission);
223 else if (folder->ff->imap) {
224 /* mark folders with subfolders AND mail */
225 snprintf (permission, sizeof (permission), "IMAP %c",
226 (folder->ff->inferiors
227 && folder->ff->selectable) ? '+' : ' ');
228 mutt_format_s (dest, destlen, fmt, permission);
231 mutt_format_s (dest, destlen, fmt, "");
235 if (folder->ff->st != NULL) {
236 if ((gr = getgrgid (folder->ff->st->st_gid)))
237 mutt_format_s (dest, destlen, fmt, gr->gr_name);
239 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
240 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
244 mutt_format_s (dest, destlen, fmt, "");
248 if (folder->ff->st != NULL) {
249 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
250 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
253 mutt_format_s (dest, destlen, fmt, "");
257 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
259 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
260 snprintf (dest, destlen, tmp, folder->ff->new);
262 else if (!folder->ff->new)
266 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
267 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
271 if (folder->ff->st != NULL) {
272 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
273 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
276 mutt_format_s (dest, destlen, fmt, "");
280 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
281 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
285 if (folder->ff->st != NULL) {
286 if ((pw = getpwuid (folder->ff->st->st_uid)))
287 mutt_format_s (dest, destlen, fmt, pw->pw_name);
289 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
290 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
294 mutt_format_s (dest, destlen, fmt, "");
298 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
299 snprintf (dest, destlen, tmp, op);
304 m_strformat (dest, destlen, ifstr, folder_format_str, data, 0);
305 else if (flags & M_FORMAT_OPTIONAL)
306 m_strformat (dest, destlen, elstr, folder_format_str, data, 0);
312 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
313 const char *src, const char *fmt,
316 anytype data, format_flag flags)
318 char fn[STRING], tmp[STRING];
319 FOLDER *folder = data.ptr;
323 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
324 snprintf (dest, destlen, tmp, folder->num + 1);
328 m_strcpy(fn, sizeof(fn), folder->ff->name);
329 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
330 snprintf (dest, destlen, tmp, fn);
334 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
335 if (folder->ff->nd->subscribed)
336 snprintf (dest, destlen, tmp, ' ');
338 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
342 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
343 if (folder->ff->nd->deleted)
344 snprintf (dest, destlen, tmp, 'D');
346 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
350 if (flags & M_FORMAT_OPTIONAL) {
351 if (folder->ff->nd->unread != 0)
352 m_strformat (dest, destlen, ifstr, newsgroup_format_str,
355 m_strformat (dest, destlen, elstr, newsgroup_format_str,
358 else if (Context && Context->data == folder->ff->nd) {
359 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
360 snprintf (dest, destlen, tmp, Context->unread);
363 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
364 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
369 if (Context && Context->data == folder->ff->nd) {
370 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
371 snprintf (dest, destlen, tmp, Context->new);
373 else if (option (OPTMARKOLD) &&
374 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
375 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
376 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
377 snprintf (dest, destlen, tmp,
378 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
381 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
382 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
387 if (folder->ff->nd->desc != NULL) {
388 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
389 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
392 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
393 snprintf (dest, destlen, tmp, "");
399 #endif /* USE_NNTP */
402 static void add_folder (MUTTMENU * m, struct browser_state *state,
403 const char *name, const struct stat *s,
406 static void add_folder (MUTTMENU * m, struct browser_state *state,
407 const char *name, const struct stat *s,
411 if (state->entrylen == state->entrymax) {
412 /* need to allocate more space */
413 p_realloc(&state->entry, state->entrymax += 256);
414 p_clear(&state->entry[state->entrylen], 256);
416 m->data = state->entry;
420 (state->entry)[state->entrylen].mode = s->st_mode;
421 (state->entry)[state->entrylen].mtime = s->st_mtime;
422 (state->entry)[state->entrylen].size = s->st_size;
423 (state->entry)[state->entrylen].st = p_dup(s, 1);
426 (state->entry)[state->entrylen].new = new;
427 (state->entry)[state->entrylen].name = m_strdup(name);
428 (state->entry)[state->entrylen].desc = m_strdup(name);
429 (state->entry)[state->entrylen].imap = 0;
431 if (option (OPTNEWS))
432 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
437 static void init_state (struct browser_state *state, MUTTMENU * menu)
440 state->entrymax = 256;
441 state->entry = p_new(struct folder_file, state->entrymax);
442 state->imap_browse = 0;
444 menu->data = state->entry;
447 /* get list of all files/newsgroups with mask */
448 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
449 char *d, const char *prefix)
452 if (option (OPTNEWS)) {
455 NNTP_SERVER *news = CurrentNewsSrv;
457 /* buffy_check (0); */
458 init_state (state, menu);
460 for (tmp = news->list; tmp; tmp = tmp->next) {
461 if (!(data = (NNTP_DATA *) tmp->data))
463 nntp_sync_sidebar (data);
464 if (m_strncmp (prefix, data->group, m_strlen(prefix)) != 0)
466 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
469 add_folder (menu, state, data->group, NULL, data, data->new);
471 add_folder (menu, state, data->group, NULL, data->new);
477 #endif /* USE_NNTP */
482 char buffer[_POSIX_PATH_MAX + STRING];
485 while (stat (d, &s) == -1) {
486 if (errno == ENOENT) {
487 /* The last used directory is deleted, try to use the parent dir. */
488 char *c = strrchr (d, '/');
499 if (!S_ISDIR (s.st_mode)) {
500 mutt_error (_("%s is not a directory."), d);
506 if ((dp = opendir (d)) == NULL) {
511 init_state (state, menu);
513 while ((de = readdir (dp)) != NULL) {
514 if (m_strcmp(de->d_name, ".") == 0)
515 continue; /* we don't need . */
517 if (m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
519 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
522 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
523 if (lstat (buffer, &s) == -1)
526 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
527 (!S_ISLNK (s.st_mode)))
530 i = buffy_lookup (buffer);
532 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? Incoming.arr[i]->new : 0);
534 add_folder (menu, state, de->d_name, &s, i >= 0 ? Incoming.arr[i]->new : 0);
540 browser_sort (state);
544 /* get list of mailboxes/subscribed newsgroups */
545 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
548 char buffer[LONG_STRING];
551 if (option (OPTNEWS)) {
554 NNTP_SERVER *news = CurrentNewsSrv;
556 /* buffy_check (0); */
557 init_state (state, menu);
559 for (tmp = news->list; tmp; tmp = tmp->next) {
560 if ((data = (NNTP_DATA*) tmp->data) == NULL)
562 nntp_sync_sidebar (data);
563 if ((data->new || (data->subscribed &&
564 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
565 add_folder (menu, state, data->group, NULL, data, data->new);
579 init_state (state, menu);
581 for (i = 0; i < Incoming.len; i++) {
582 tmp = Incoming.arr[i];
583 tmp->magic = mx_get_magic (tmp->path);
584 if (tmp->magic == M_IMAP) {
586 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
588 add_folder (menu, state, tmp->path, NULL, tmp->new);
592 if (tmp->magic == M_POP) {
594 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
596 add_folder (menu, state, tmp->path, NULL, tmp->new);
601 if (tmp->magic == M_NNTP) {
602 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
606 if (lstat (tmp->path, &s) == -1)
609 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
610 (!S_ISLNK (s.st_mode)))
613 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
614 mutt_pretty_mailbox (buffer);
617 add_folder (menu, state, buffer, &s, NULL, tmp->new);
619 add_folder (menu, state, buffer, &s, tmp->new);
623 browser_sort (state);
627 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
630 if (option (OPTNEWS))
632 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
635 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
638 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
642 folder.ff = &((struct folder_file *) menu->data)[num];
646 if (option (OPTNEWS))
647 m_strformat(s, slen, NONULL (GroupFormat), newsgroup_format_str,
649 option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
652 m_strformat(s, slen, NONULL (FolderFormat), folder_format_str,
654 option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
657 static void init_menu (struct browser_state *state, MUTTMENU * menu,
658 char *title, ssize_t titlelen, int buffy)
660 char path[_POSIX_PATH_MAX];
662 menu->max = state->entrylen;
664 if (menu->current >= menu->max)
665 menu->current = menu->max - 1;
666 if (menu->current < 0)
668 if (menu->top > menu->current)
674 if (option (OPTNEWS)) {
676 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
678 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
679 CurrentNewsSrv->conn->account.host);
684 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
686 m_strcpy(path, sizeof(path), LastDir);
687 mutt_pretty_mailbox (path);
688 if (state->imap_browse && option (OPTIMAPLSUB))
689 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
690 path, NONULL (Mask.pattern));
692 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
693 path, NONULL (Mask.pattern));
695 menu->redraw = REDRAW_FULL;
698 static int file_tag (MUTTMENU * menu, int n, int m)
700 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
703 if (S_ISDIR (ff->mode)
704 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
705 mutt_error _("Can't attach a directory!");
711 ff->tagged = (m >= 0 ? m : !ff->tagged);
713 return ff->tagged - ot;
716 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
719 char buf[_POSIX_PATH_MAX];
720 char prefix[_POSIX_PATH_MAX] = "";
721 char helpstr[STRING];
723 struct browser_state state;
726 int i, killPrefix = 0;
727 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
728 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
729 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
731 buffy = buffy && folder;
736 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
739 if (option (OPTNEWS)) {
741 m_strcpy(prefix, sizeof(prefix), f);
745 /* default state for news reader mode is browse subscribed newsgroups */
747 for (list = CurrentNewsSrv->list; list; list = list->next) {
748 NNTP_DATA *data = (NNTP_DATA *) list->data;
750 if (data && data->subscribed) {
760 mutt_expand_path (f, flen);
761 if (imap_is_magic (f, NULL) == M_IMAP) {
762 init_state (&state, NULL);
763 state.imap_browse = 1;
764 if (!imap_browse (f, &state))
765 m_strcpy(LastDir, sizeof(LastDir), state.folder);
768 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
771 i = MIN(ssizeof(LastDir) - 1, i);
772 m_strcpy(LastDir, sizeof(LastDir), f);
775 getcwd(LastDir, sizeof(LastDir));
776 m_strcat(LastDir, sizeof(LastDir), "/");
777 m_strncat(LastDir, sizeof(LastDir), f, i);
782 m_strcpy(LastDir, sizeof(LastDir), "/");
784 getcwd (LastDir, sizeof (LastDir));
787 if (i <= 0 && f[0] != '/')
788 m_strcpy(prefix, sizeof(prefix), f);
790 m_strcpy(prefix, sizeof(prefix), f + i + 1);
796 getcwd (LastDir, sizeof (LastDir));
797 else if (!LastDir[0])
798 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
800 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
801 init_state (&state, NULL);
802 state.imap_browse = 1;
803 imap_browse (LastDir, &state);
804 browser_sort (&state);
811 if (examine_mailboxes (NULL, &state) == -1)
815 if (!state.imap_browse)
816 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
819 menu = mutt_new_menu ();
820 menu->menu = MENU_FOLDER;
821 menu->make_entry = folder_entry;
822 menu->search = select_file_search;
824 menu->data = state.entry;
826 menu->tag = file_tag;
828 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
830 (option (OPTNEWS)) ? FolderNewsHelp :
834 init_menu (&state, menu, title, sizeof (title), buffy);
837 switch (i = mutt_menuLoop (menu)) {
838 case OP_GENERIC_SELECT_ENTRY:
840 if (!state.entrylen) {
841 mutt_error _("No files match the file mask");
846 if (S_ISDIR (state.entry[menu->current].mode) ||
847 (S_ISLNK (state.entry[menu->current].mode) &&
848 link_is_dir (LastDir, state.entry[menu->current].name))
849 || state.entry[menu->current].inferiors
851 /* make sure this isn't a MH or maildir mailbox */
853 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
854 mutt_expand_path (buf, sizeof (buf));
856 else if (state.imap_browse) {
857 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
860 mutt_concat_path(buf, sizeof(buf), LastDir,
861 state.entry[menu->current].name);
863 if ((mx_get_magic (buf) <= 0)
864 || state.entry[menu->current].inferiors)
866 char OldLastDir[_POSIX_PATH_MAX];
868 /* save the old directory */
869 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
871 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
872 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
873 m_strcat(LastDir, sizeof(LastDir), "/..");
875 char *p = strrchr (LastDir + 1, '/');
880 if (LastDir[0] == '/')
883 m_strcat(LastDir, sizeof(LastDir), "/..");
888 m_strcpy(LastDir, sizeof(LastDir),
889 state.entry[menu->current].name);
890 mutt_expand_path (LastDir, sizeof (LastDir));
892 else if (state.imap_browse) {
896 m_strcpy(LastDir, sizeof(LastDir),
897 state.entry[menu->current].name);
898 /* tack on delimiter here */
899 n = m_strlen(LastDir) + 1;
901 /* special case "" needs no delimiter */
902 url_parse_ciss (&url, state.entry[menu->current].name);
904 (state.entry[menu->current].delim != '\0') &&
905 (n < ssizeof (LastDir))) {
907 LastDir[n - 1] = state.entry[menu->current].delim;
911 char tmp[_POSIX_PATH_MAX];
913 mutt_concat_path(tmp, sizeof(tmp), LastDir,
914 state.entry[menu->current].name);
915 m_strcpy(LastDir, sizeof(LastDir), tmp);
918 destroy_state (&state);
924 if (state.imap_browse) {
925 init_state (&state, NULL);
926 state.imap_browse = 1;
927 imap_browse (LastDir, &state);
928 browser_sort (&state);
929 menu->data = state.entry;
932 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
933 /* try to restore the old values */
934 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
935 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
936 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
942 init_menu (&state, menu, title, sizeof (title), buffy);
948 if (buffy || option (OPTNEWS)) /* news have not path */
953 m_strcpy(f, flen, state.entry[menu->current].name);
954 mutt_expand_path (f, flen);
956 else if (state.imap_browse)
957 m_strcpy(f, flen, state.entry[menu->current].name);
959 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
961 /* Fall through to OP_EXIT */
971 *numfiles = menu->tagged;
972 tfiles = p_new(char *, *numfiles);
973 for (h = 0, j = 0; h < state.entrylen; i++) {
974 struct folder_file ff = state.entry[i];
975 char full[_POSIX_PATH_MAX];
978 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
979 mutt_expand_path (full, sizeof (full));
980 tfiles[j++] = m_strdup(full);
985 else if (f[0]) { /* no tagged entries. return selected entry */
987 tfiles = p_new(char *, *numfiles);
988 mutt_expand_path (f, flen);
989 tfiles[0] = m_strdup(f);
994 destroy_state (&state);
995 mutt_menuDestroy (&menu);
998 case OP_BROWSER_TELL:
1000 mutt_message ("%s", state.entry[menu->current].name);
1003 case OP_BROWSER_TOGGLE_LSUB:
1004 if (option (OPTIMAPLSUB)) {
1005 unset_option (OPTIMAPLSUB);
1008 set_option (OPTIMAPLSUB);
1010 mutt_ungetch (0, OP_CHECK_NEW);
1013 case OP_CREATE_MAILBOX:
1014 if (!state.imap_browse)
1015 mutt_error (_("Create is only supported for IMAP mailboxes"));
1017 imap_mailbox_create (LastDir);
1018 /* TODO: find a way to detect if the new folder would appear in
1019 * this window, and insert it without starting over. */
1020 destroy_state (&state);
1021 init_state (&state, NULL);
1022 state.imap_browse = 1;
1023 imap_browse (LastDir, &state);
1024 browser_sort (&state);
1025 menu->data = state.entry;
1028 init_menu (&state, menu, title, sizeof (title), buffy);
1029 MAYBE_REDRAW (menu->redraw);
1033 case OP_RENAME_MAILBOX:
1034 if (!state.entry[menu->current].imap)
1035 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1037 int nentry = menu->current;
1039 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1040 destroy_state (&state);
1041 init_state (&state, NULL);
1042 state.imap_browse = 1;
1043 imap_browse (LastDir, &state);
1044 browser_sort (&state);
1045 menu->data = state.entry;
1048 init_menu (&state, menu, title, sizeof (title), buffy);
1049 MAYBE_REDRAW (menu->redraw);
1054 case OP_DELETE_MAILBOX:
1055 if (!state.entry[menu->current].imap)
1056 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1060 int nentry = menu->current;
1062 imap_parse_path (state.entry[nentry].name, &mx);
1063 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1065 if (mutt_yesorno (msg, M_NO) == M_YES) {
1066 if (!imap_delete_mailbox (Context, mx)) {
1067 /* free the mailbox from the browser */
1068 p_delete(&((state.entry)[nentry].name));
1069 p_delete(&((state.entry)[nentry].desc));
1070 /* and move all other entries up */
1071 if (nentry + 1 < state.entrylen)
1072 memmove (state.entry + nentry, state.entry + nentry + 1,
1073 sizeof (struct folder_file) * (state.entrylen -
1076 mutt_message _("Mailbox deleted.");
1078 init_menu (&state, menu, title, sizeof (title), buffy);
1079 MAYBE_REDRAW (menu->redraw);
1083 mutt_message _("Mailbox not deleted.");
1088 case OP_CHANGE_DIRECTORY:
1091 if (option (OPTNEWS))
1095 m_strcpy(buf, sizeof(buf), LastDir);
1096 if (!state.imap_browse)
1098 /* add '/' at the end of the directory name if not already there */
1099 ssize_t len = m_strlen(LastDir);
1101 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1105 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1108 mutt_expand_path (buf, sizeof (buf));
1109 if (imap_is_magic (buf, NULL) == M_IMAP) {
1110 m_strcpy(LastDir, sizeof(LastDir), buf);
1111 destroy_state (&state);
1112 init_state (&state, NULL);
1113 state.imap_browse = 1;
1114 imap_browse (LastDir, &state);
1115 browser_sort (&state);
1116 menu->data = state.entry;
1119 init_menu (&state, menu, title, sizeof (title), buffy);
1122 if (stat (buf, &st) == 0) {
1123 if (S_ISDIR (st.st_mode)) {
1124 destroy_state (&state);
1125 if (examine_directory (menu, &state, buf, prefix) == 0)
1126 m_strcpy(LastDir, sizeof(LastDir), buf);
1128 mutt_error _("Error scanning directory.");
1130 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1131 mutt_menuDestroy (&menu);
1137 init_menu (&state, menu, title, sizeof (title), buffy);
1140 mutt_error (_("%s is not a directory."), buf);
1145 MAYBE_REDRAW (menu->redraw);
1150 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1151 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1152 regex_t *rx = p_new(regex_t, 1);
1157 /* assume that the user wants to see everything */
1159 m_strcpy(buf, sizeof(buf), ".");
1162 s = vskipspaces(s + 1);
1166 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1167 regerror (err, rx, buf, sizeof (buf));
1170 mutt_error ("%s", buf);
1173 m_strreplace(&Mask.pattern, buf);
1179 destroy_state (&state);
1180 if (state.imap_browse) {
1181 init_state (&state, NULL);
1182 state.imap_browse = 1;
1183 imap_browse (LastDir, &state);
1184 browser_sort (&state);
1185 menu->data = state.entry;
1186 init_menu (&state, menu, title, sizeof (title), buffy);
1189 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1190 init_menu (&state, menu, title, sizeof (title), buffy);
1192 mutt_error _("Error scanning directory.");
1194 mutt_menuDestroy (&menu);
1198 if (!state.entrylen) {
1199 mutt_error _("No files match the file mask");
1205 MAYBE_REDRAW (menu->redraw);
1209 case OP_SORT_REVERSE:
1213 int reverse = (i == OP_SORT_REVERSE);
1215 switch (mutt_multi_choice ((reverse) ?
1217 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1220 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1222 case -1: /* abort */
1226 case 1: /* (d)ate */
1227 BrowserSort = SORT_DATE;
1230 case 2: /* (a)lpha */
1231 BrowserSort = SORT_SUBJECT;
1234 case 3: /* si(z)e */
1235 BrowserSort = SORT_SIZE;
1238 case 4: /* do(n)'t sort */
1239 BrowserSort = SORT_ORDER;
1244 BrowserSort |= reverse ? SORT_REVERSE : 0;
1245 browser_sort (&state);
1246 menu->redraw = REDRAW_FULL;
1251 case OP_TOGGLE_MAILBOXES:
1255 destroy_state (&state);
1260 if (examine_mailboxes (menu, &state) == -1)
1263 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1264 init_state (&state, NULL);
1265 state.imap_browse = 1;
1266 imap_browse (LastDir, &state);
1267 browser_sort (&state);
1268 menu->data = state.entry;
1270 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1272 init_menu (&state, menu, title, sizeof (title), buffy);
1276 if (option (OPTFORCEBUFFYCHECK))
1281 case OP_BROWSER_NEW_FILE:
1283 snprintf (buf, sizeof (buf), "%s/", LastDir);
1284 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1286 m_strcpy(f, flen, buf);
1287 destroy_state (&state);
1288 mutt_menuDestroy (&menu);
1291 MAYBE_REDRAW (menu->redraw);
1294 case OP_BROWSER_VIEW_FILE:
1295 if (!state.entrylen) {
1296 mutt_error _("No files match the file mask");
1301 if (state.entry[menu->current].selectable) {
1302 m_strcpy(f, flen, state.entry[menu->current].name);
1303 destroy_state (&state);
1304 mutt_menuDestroy (&menu);
1308 if (S_ISDIR (state.entry[menu->current].mode) ||
1309 (S_ISLNK (state.entry[menu->current].mode) &&
1310 link_is_dir (LastDir, state.entry[menu->current].name))) {
1311 mutt_error _("Can't view a directory");
1317 char nbuf[_POSIX_PATH_MAX];
1319 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1320 state.entry[menu->current].name);
1321 b = mutt_make_file_attach (nbuf);
1323 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1325 menu->redraw = REDRAW_FULL;
1328 mutt_error _("Error trying to view file");
1335 if (option (OPTNEWS)) {
1336 struct folder_file *folder_f = &state.entry[menu->current];
1339 if (i == OP_CATCHUP)
1340 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1342 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1345 if (menu->current + 1 < menu->max)
1347 menu->redraw = REDRAW_MOTION_RESYNCH;
1352 case OP_LOAD_ACTIVE:
1353 if (!option (OPTNEWS))
1360 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1361 if ((data = (NNTP_DATA *) tmp->data))
1365 nntp_get_active (CurrentNewsSrv);
1367 destroy_state (&state);
1369 examine_mailboxes (menu, &state);
1371 examine_directory (menu, &state, NULL, NULL);
1372 init_menu (&state, menu, title, sizeof (title), buffy);
1374 #endif /* USE_NNTP */
1376 case OP_BROWSER_SUBSCRIBE:
1377 case OP_BROWSER_UNSUBSCRIBE:
1379 case OP_SUBSCRIBE_PATTERN:
1380 case OP_UNSUBSCRIBE_PATTERN:
1381 if (option (OPTNEWS)) {
1382 regex_t *rx = p_new(regex_t, 1);
1384 int j = menu->current;
1386 NNTP_SERVER *news = CurrentNewsSrv;
1388 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1393 if (i == OP_SUBSCRIBE_PATTERN)
1394 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1396 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1397 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1402 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1403 regerror (err, rx, buf, sizeof (buf));
1406 mutt_error ("%s", buf);
1409 menu->redraw = REDRAW_FULL;
1412 else if (!state.entrylen) {
1413 mutt_error _("No newsgroups match the mask");
1418 for (; j < state.entrylen; j++) {
1419 struct folder_file *folderf = &state.entry[j];
1421 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1422 regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1423 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1424 nd = mutt_newsgroup_subscribe (news, folderf->name);
1426 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1428 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1429 if (menu->current + 1 < menu->max)
1431 menu->redraw = REDRAW_MOTION_RESYNCH;
1435 if (i == OP_SUBSCRIBE_PATTERN) {
1436 string_list_t *grouplist = NULL;
1439 grouplist = news->list;
1440 for (; grouplist; grouplist = grouplist->next) {
1441 nd = (NNTP_DATA *) grouplist->data;
1442 if (nd && nd->group && !nd->subscribed) {
1443 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1444 mutt_newsgroup_subscribe (news, nd->group);
1445 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1449 init_menu (&state, menu, title, sizeof (title), buffy);
1451 mutt_newsrc_update (news);
1452 nntp_clear_cacheindex (news);
1453 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1458 #endif /* USE_NNTP */
1460 if (i == OP_BROWSER_SUBSCRIBE)
1461 imap_subscribe (state.entry[menu->current].name, 1);
1463 imap_subscribe (state.entry[menu->current].name, 0);
1471 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);