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>
17 #include <lib-lib/file.h>
18 #include <lib-lib/mapping.h>
23 #include "mutt_curses.h"
24 #include "mutt_menu.h"
30 #include <imap/imap.h>
31 #include <imap/mx_imap.h>
47 static struct mapping_t FolderHelp[] = {
48 {N_("Exit"), OP_EXIT},
49 {N_("Chdir"), OP_CHANGE_DIRECTORY},
50 {N_("Mask"), OP_ENTER_MASK},
51 {N_("Help"), OP_HELP},
56 static struct mapping_t FolderNewsHelp[] = {
57 {N_("Exit"), OP_EXIT},
58 {N_("List"), OP_TOGGLE_MAILBOXES},
59 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
60 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
61 {N_("Catchup"), OP_CATCHUP},
62 {N_("Mask"), OP_ENTER_MASK},
63 {N_("Help"), OP_HELP},
68 typedef struct folder_t {
69 struct folder_file *ff;
73 static char LastDir[_POSIX_PATH_MAX] = "";
74 static char LastDirBackup[_POSIX_PATH_MAX] = "";
76 /* Frees up the memory allocated for the local-global variables. */
77 static void destroy_state (struct browser_state *state)
81 for (c = 0; c < state->entrylen; c++) {
82 p_delete(&((state->entry)[c].name));
83 p_delete(&((state->entry)[c].desc));
84 p_delete(&((state->entry)[c].st));
86 p_delete(&state->folder);
87 p_delete(&state->entry);
90 static int browser_compare_subject (const void *a, const void *b)
92 struct folder_file *pa = (struct folder_file *) a;
93 struct folder_file *pb = (struct folder_file *) b;
95 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
97 return ((BrowserSort & SORT_REVERSE) ? -r : r);
100 static int browser_compare_date (const void *a, const void *b)
102 struct folder_file *pa = (struct folder_file *) a;
103 struct folder_file *pb = (struct folder_file *) b;
105 int r = pa->mtime - pb->mtime;
107 return ((BrowserSort & SORT_REVERSE) ? -r : r);
110 static int browser_compare_size (const void *a, const void *b)
112 struct folder_file *pa = (struct folder_file *) a;
113 struct folder_file *pb = (struct folder_file *) b;
115 int r = pa->size - pb->size;
117 return ((BrowserSort & SORT_REVERSE) ? -r : r);
120 static void browser_sort (struct browser_state *state)
122 int (*f) (const void *, const void *);
124 switch (BrowserSort & SORT_MASK) {
129 if (option (OPTNEWS))
132 f = browser_compare_date;
136 if (option (OPTNEWS))
139 f = browser_compare_size;
143 f = browser_compare_subject;
146 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
149 static int link_is_dir (const char *folder, const char *path)
152 char fullpath[_POSIX_PATH_MAX];
154 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
156 if (stat (fullpath, &st) == 0)
157 return (S_ISDIR (st.st_mode));
162 static const char *folder_format_str (char *dest, size_t destlen, char op,
163 const char *src, const char *fmt,
164 const char *ifstring,
165 const char *elsestring,
166 unsigned long data, format_flag flags)
168 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
171 FOLDER *folder = (FOLDER *) data;
174 int optional = (flags & M_FORMAT_OPTIONAL);
178 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
179 snprintf (dest, destlen, tmp, folder->num + 1);
183 if (folder->ff->st != NULL) {
186 tnow - folder->ff->st->st_mtime <
187 31536000 ? "%b %d %H:%M" : "%b %d %Y";
188 strftime (date, sizeof (date), t_fmt,
189 localtime (&folder->ff->st->st_mtime));
190 mutt_format_s (dest, destlen, fmt, date);
193 mutt_format_s (dest, destlen, fmt, "");
200 if (folder->ff->imap)
201 s = NONULL (folder->ff->desc);
203 s = NONULL (folder->ff->name);
205 snprintf (fn, sizeof (fn), "%s%s", s,
206 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
207 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
208 ((folder->ff->st->st_mode & S_IXUSR) !=
209 0 ? "*" : ""))) : "");
211 mutt_format_s (dest, destlen, fmt, fn);
215 if (folder->ff->st != NULL) {
216 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
217 S_ISDIR (folder->ff->st->
218 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
219 st_mode) ? 'l' : '-'),
220 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
221 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
222 (folder->ff->st->st_mode & S_ISUID) !=
223 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
225 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
226 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
227 (folder->ff->st->st_mode & S_ISGID) !=
228 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
230 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
231 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
232 (folder->ff->st->st_mode & S_ISVTX) !=
233 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
235 mutt_format_s (dest, destlen, fmt, permission);
237 else if (folder->ff->imap) {
238 /* mark folders with subfolders AND mail */
239 snprintf (permission, sizeof (permission), "IMAP %c",
240 (folder->ff->inferiors
241 && folder->ff->selectable) ? '+' : ' ');
242 mutt_format_s (dest, destlen, fmt, permission);
245 mutt_format_s (dest, destlen, fmt, "");
249 if (folder->ff->st != NULL) {
250 if ((gr = getgrgid (folder->ff->st->st_gid)))
251 mutt_format_s (dest, destlen, fmt, gr->gr_name);
253 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
254 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
258 mutt_format_s (dest, destlen, fmt, "");
262 if (folder->ff->st != NULL) {
263 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
264 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
267 mutt_format_s (dest, destlen, fmt, "");
271 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
273 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
274 snprintf (dest, destlen, tmp, folder->ff->new);
276 else if (!folder->ff->new)
280 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
281 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
285 if (folder->ff->st != NULL) {
286 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
287 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
290 mutt_format_s (dest, destlen, fmt, "");
294 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
295 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
299 if (folder->ff->st != NULL) {
300 if ((pw = getpwuid (folder->ff->st->st_uid)))
301 mutt_format_s (dest, destlen, fmt, pw->pw_name);
303 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
304 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
308 mutt_format_s (dest, destlen, fmt, "");
312 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
313 snprintf (dest, destlen, tmp, op);
318 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
319 else if (flags & M_FORMAT_OPTIONAL)
320 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
326 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
327 const char *src, const char *fmt,
328 const char *ifstring,
329 const char *elsestring,
333 char fn[SHORT_STRING], tmp[SHORT_STRING];
334 FOLDER *folder = (FOLDER *) data;
338 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
339 snprintf (dest, destlen, tmp, folder->num + 1);
343 m_strcpy(fn, sizeof(fn), folder->ff->name);
344 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
345 snprintf (dest, destlen, tmp, fn);
349 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
350 if (folder->ff->nd->subscribed)
351 snprintf (dest, destlen, tmp, ' ');
353 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
357 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
358 if (folder->ff->nd->deleted)
359 snprintf (dest, destlen, tmp, 'D');
361 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
365 if (flags & M_FORMAT_OPTIONAL) {
366 if (folder->ff->nd->unread != 0)
367 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
370 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
373 else if (Context && Context->data == folder->ff->nd) {
374 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
375 snprintf (dest, destlen, tmp, Context->unread);
378 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
384 if (Context && Context->data == folder->ff->nd) {
385 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
386 snprintf (dest, destlen, tmp, Context->new);
388 else if (option (OPTMARKOLD) &&
389 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
390 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
391 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
392 snprintf (dest, destlen, tmp,
393 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
396 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
397 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
402 if (folder->ff->nd->desc != NULL) {
403 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
404 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
407 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
408 snprintf (dest, destlen, tmp, "");
414 #endif /* USE_NNTP */
417 static void add_folder (MUTTMENU * m, struct browser_state *state,
418 const char *name, const struct stat *s,
421 static void add_folder (MUTTMENU * m, struct browser_state *state,
422 const char *name, const struct stat *s,
426 if (state->entrylen == state->entrymax) {
427 /* need to allocate more space */
428 p_realloc(&state->entry, state->entrymax += 256);
429 p_clear(&state->entry[state->entrylen], 256);
431 m->data = state->entry;
435 (state->entry)[state->entrylen].mode = s->st_mode;
436 (state->entry)[state->entrylen].mtime = s->st_mtime;
437 (state->entry)[state->entrylen].size = s->st_size;
438 (state->entry)[state->entrylen].st = p_dup(s, 1);
441 (state->entry)[state->entrylen].new = new;
442 (state->entry)[state->entrylen].name = m_strdup(name);
443 (state->entry)[state->entrylen].desc = m_strdup(name);
444 (state->entry)[state->entrylen].imap = 0;
446 if (option (OPTNEWS))
447 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
452 static void init_state (struct browser_state *state, MUTTMENU * menu)
455 state->entrymax = 256;
456 state->entry = p_new(struct folder_file, state->entrymax);
457 state->imap_browse = 0;
459 menu->data = state->entry;
462 /* get list of all files/newsgroups with mask */
463 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
464 char *d, const char *prefix)
467 if (option (OPTNEWS)) {
470 NNTP_SERVER *news = CurrentNewsSrv;
472 /* buffy_check (0); */
473 init_state (state, menu);
475 for (tmp = news->list; tmp; tmp = tmp->next) {
476 if (!(data = (NNTP_DATA *) tmp->data))
478 nntp_sync_sidebar (data);
479 if (prefix && *prefix && strncmp (prefix, data->group,
480 m_strlen(prefix)) != 0)
482 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
485 add_folder (menu, state, data->group, NULL, data, data->new);
487 add_folder (menu, state, data->group, NULL, data->new);
490 sidebar_draw (CurrentMenu);
493 #endif /* USE_NNTP */
498 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
501 while (stat (d, &s) == -1) {
502 if (errno == ENOENT) {
503 /* The last used directory is deleted, try to use the parent dir. */
504 char *c = strrchr (d, '/');
515 if (!S_ISDIR (s.st_mode)) {
516 mutt_error (_("%s is not a directory."), d);
522 if ((dp = opendir (d)) == NULL) {
527 init_state (state, menu);
529 while ((de = readdir (dp)) != NULL) {
530 if (m_strcmp(de->d_name, ".") == 0)
531 continue; /* we don't need . */
533 if (prefix && *prefix
534 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
536 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
539 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
540 if (lstat (buffer, &s) == -1)
543 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
544 (!S_ISLNK (s.st_mode)))
547 i = buffy_lookup (buffer);
549 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
551 add_folder (menu, state, de->d_name, &s, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
556 sidebar_draw (CurrentMenu);
557 browser_sort (state);
561 /* get list of mailboxes/subscribed newsgroups */
562 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
565 char buffer[LONG_STRING];
568 if (option (OPTNEWS)) {
571 NNTP_SERVER *news = CurrentNewsSrv;
573 /* buffy_check (0); */
574 init_state (state, menu);
576 for (tmp = news->list; tmp; tmp = tmp->next) {
577 if ((data = (NNTP_DATA*) tmp->data) == NULL)
579 nntp_sync_sidebar (data);
580 if ((data->new || (data->subscribed &&
581 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
582 add_folder (menu, state, data->group, NULL, data, data->new);
584 sidebar_draw (CurrentMenu);
596 init_state (state, menu);
598 for (i = 0; i < Incoming->length; i++) {
599 tmp = (BUFFY*) Incoming->data[i];
600 tmp->magic = mx_get_magic (tmp->path);
601 if (tmp->magic == M_IMAP) {
603 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
605 add_folder (menu, state, tmp->path, NULL, tmp->new);
609 if (tmp->magic == M_POP) {
611 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
613 add_folder (menu, state, tmp->path, NULL, tmp->new);
618 if (tmp->magic == M_NNTP) {
619 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
623 if (lstat (tmp->path, &s) == -1)
626 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
627 (!S_ISLNK (s.st_mode)))
630 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
631 mutt_pretty_mailbox (buffer);
634 add_folder (menu, state, buffer, &s, NULL, tmp->new);
636 add_folder (menu, state, buffer, &s, tmp->new);
640 browser_sort (state);
644 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
647 if (option (OPTNEWS))
649 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
652 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
655 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
659 folder.ff = &((struct folder_file *) menu->data)[num];
663 if (option (OPTNEWS))
664 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
665 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
668 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
669 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
672 static void init_menu (struct browser_state *state, MUTTMENU * menu,
673 char *title, size_t titlelen, int buffy)
675 char path[_POSIX_PATH_MAX];
677 menu->max = state->entrylen;
679 if (menu->current >= menu->max)
680 menu->current = menu->max - 1;
681 if (menu->current < 0)
683 if (menu->top > menu->current)
689 if (option (OPTNEWS)) {
691 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
693 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
694 CurrentNewsSrv->conn->account.host);
699 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
701 m_strcpy(path, sizeof(path), LastDir);
702 mutt_pretty_mailbox (path);
703 if (state->imap_browse && option (OPTIMAPLSUB))
704 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
705 path, NONULL (Mask.pattern));
707 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
708 path, NONULL (Mask.pattern));
710 menu->redraw = REDRAW_FULL;
713 static int file_tag (MUTTMENU * menu, int n, int m)
715 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
718 if (S_ISDIR (ff->mode)
719 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
720 mutt_error _("Can't attach a directory!");
726 ff->tagged = (m >= 0 ? m : !ff->tagged);
728 return ff->tagged - ot;
731 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
734 char buf[_POSIX_PATH_MAX];
735 char prefix[_POSIX_PATH_MAX] = "";
736 char helpstr[SHORT_STRING];
738 struct browser_state state;
741 int i, killPrefix = 0;
742 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
743 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
744 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
746 buffy = buffy && folder;
751 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
754 if (option (OPTNEWS)) {
756 m_strcpy(prefix, sizeof(prefix), f);
760 /* default state for news reader mode is browse subscribed newsgroups */
762 for (list = CurrentNewsSrv->list; list; list = list->next) {
763 NNTP_DATA *data = (NNTP_DATA *) list->data;
765 if (data && data->subscribed) {
775 mutt_expand_path (f, flen);
776 if (imap_is_magic (f, NULL) == M_IMAP) {
777 init_state (&state, NULL);
778 state.imap_browse = 1;
779 if (!imap_browse (f, &state))
780 m_strcpy(LastDir, sizeof(LastDir), state.folder);
783 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
786 i = MIN(ssizeof(LastDir) - 1, i);
787 m_strcpy(LastDir, sizeof(LastDir), f);
790 getcwd(LastDir, sizeof(LastDir));
791 m_strcat(LastDir, sizeof(LastDir), "/");
792 m_strncat(LastDir, sizeof(LastDir), f, i);
797 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
799 getcwd (LastDir, sizeof (LastDir));
802 if (i <= 0 && f[0] != '/')
803 m_strcpy(prefix, sizeof(prefix), f);
805 m_strcpy(prefix, sizeof(prefix), f + i + 1);
811 getcwd (LastDir, sizeof (LastDir));
812 else if (!LastDir[0])
813 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
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);
826 if (examine_mailboxes (NULL, &state) == -1)
830 if (!state.imap_browse)
831 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
834 menu = mutt_new_menu ();
835 menu->menu = MENU_FOLDER;
836 menu->make_entry = folder_entry;
837 menu->search = select_file_search;
839 menu->data = state.entry;
841 menu->tag = file_tag;
843 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
845 (option (OPTNEWS)) ? FolderNewsHelp :
849 init_menu (&state, menu, title, sizeof (title), buffy);
852 switch (i = mutt_menuLoop (menu)) {
853 case OP_GENERIC_SELECT_ENTRY:
855 if (!state.entrylen) {
856 mutt_error _("No files match the file mask");
861 if (S_ISDIR (state.entry[menu->current].mode) ||
862 (S_ISLNK (state.entry[menu->current].mode) &&
863 link_is_dir (LastDir, state.entry[menu->current].name))
864 || state.entry[menu->current].inferiors
866 /* make sure this isn't a MH or maildir mailbox */
868 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
869 mutt_expand_path (buf, sizeof (buf));
871 else if (state.imap_browse) {
872 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
875 mutt_concat_path(buf, sizeof(buf), LastDir,
876 state.entry[menu->current].name);
878 if ((mx_get_magic (buf) <= 0)
879 || state.entry[menu->current].inferiors)
881 char OldLastDir[_POSIX_PATH_MAX];
883 /* save the old directory */
884 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
886 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
887 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
888 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
890 char *p = strrchr (LastDir + 1, '/');
895 if (LastDir[0] == '/')
898 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
903 m_strcpy(LastDir, sizeof(LastDir),
904 state.entry[menu->current].name);
905 mutt_expand_path (LastDir, sizeof (LastDir));
907 else if (state.imap_browse) {
911 m_strcpy(LastDir, sizeof(LastDir),
912 state.entry[menu->current].name);
913 /* tack on delimiter here */
914 n = m_strlen(LastDir) + 1;
916 /* special case "" needs no delimiter */
917 url_parse_ciss (&url, state.entry[menu->current].name);
919 (state.entry[menu->current].delim != '\0') &&
920 (n < sizeof (LastDir))) {
922 LastDir[n - 1] = state.entry[menu->current].delim;
926 char tmp[_POSIX_PATH_MAX];
928 mutt_concat_path(tmp, sizeof(tmp), LastDir,
929 state.entry[menu->current].name);
930 m_strcpy(LastDir, sizeof(LastDir), tmp);
933 destroy_state (&state);
939 if (state.imap_browse) {
940 init_state (&state, NULL);
941 state.imap_browse = 1;
942 imap_browse (LastDir, &state);
943 browser_sort (&state);
944 menu->data = state.entry;
947 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
948 /* try to restore the old values */
949 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
950 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
951 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
957 init_menu (&state, menu, title, sizeof (title), buffy);
963 if (buffy || option (OPTNEWS)) /* news have not path */
968 m_strcpy(f, flen, state.entry[menu->current].name);
969 mutt_expand_path (f, flen);
971 else if (state.imap_browse)
972 m_strcpy(f, flen, state.entry[menu->current].name);
974 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
976 /* Fall through to OP_EXIT */
986 *numfiles = menu->tagged;
987 tfiles = p_new(char *, *numfiles);
988 for (h = 0, j = 0; h < state.entrylen; i++) {
989 struct folder_file ff = state.entry[i];
990 char full[_POSIX_PATH_MAX];
993 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
994 mutt_expand_path (full, sizeof (full));
995 tfiles[j++] = m_strdup(full);
1000 else if (f[0]) { /* no tagged entries. return selected entry */
1002 tfiles = p_new(char *, *numfiles);
1003 mutt_expand_path (f, flen);
1004 tfiles[0] = m_strdup(f);
1009 destroy_state (&state);
1010 mutt_menuDestroy (&menu);
1013 case OP_BROWSER_TELL:
1015 mutt_message ("%s", state.entry[menu->current].name);
1018 case OP_BROWSER_TOGGLE_LSUB:
1019 if (option (OPTIMAPLSUB)) {
1020 unset_option (OPTIMAPLSUB);
1023 set_option (OPTIMAPLSUB);
1025 mutt_ungetch (0, OP_CHECK_NEW);
1028 case OP_CREATE_MAILBOX:
1029 if (!state.imap_browse)
1030 mutt_error (_("Create is only supported for IMAP mailboxes"));
1032 imap_mailbox_create (LastDir);
1033 /* TODO: find a way to detect if the new folder would appear in
1034 * this window, and insert it without starting over. */
1035 destroy_state (&state);
1036 init_state (&state, NULL);
1037 state.imap_browse = 1;
1038 imap_browse (LastDir, &state);
1039 browser_sort (&state);
1040 menu->data = state.entry;
1043 init_menu (&state, menu, title, sizeof (title), buffy);
1044 MAYBE_REDRAW (menu->redraw);
1048 case OP_RENAME_MAILBOX:
1049 if (!state.entry[menu->current].imap)
1050 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1052 int nentry = menu->current;
1054 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1055 destroy_state (&state);
1056 init_state (&state, NULL);
1057 state.imap_browse = 1;
1058 imap_browse (LastDir, &state);
1059 browser_sort (&state);
1060 menu->data = state.entry;
1063 init_menu (&state, menu, title, sizeof (title), buffy);
1064 MAYBE_REDRAW (menu->redraw);
1069 case OP_DELETE_MAILBOX:
1070 if (!state.entry[menu->current].imap)
1071 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1073 char msg[SHORT_STRING];
1075 int nentry = menu->current;
1077 imap_parse_path (state.entry[nentry].name, &mx);
1078 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1080 if (mutt_yesorno (msg, M_NO) == M_YES) {
1081 if (!imap_delete_mailbox (Context, mx)) {
1082 /* free the mailbox from the browser */
1083 p_delete(&((state.entry)[nentry].name));
1084 p_delete(&((state.entry)[nentry].desc));
1085 /* and move all other entries up */
1086 if (nentry + 1 < state.entrylen)
1087 memmove (state.entry + nentry, state.entry + nentry + 1,
1088 sizeof (struct folder_file) * (state.entrylen -
1091 mutt_message _("Mailbox deleted.");
1093 init_menu (&state, menu, title, sizeof (title), buffy);
1094 MAYBE_REDRAW (menu->redraw);
1098 mutt_message _("Mailbox not deleted.");
1103 case OP_CHANGE_DIRECTORY:
1106 if (option (OPTNEWS))
1110 m_strcpy(buf, sizeof(buf), LastDir);
1111 if (!state.imap_browse)
1113 /* add '/' at the end of the directory name if not already there */
1114 size_t len = m_strlen(LastDir);
1116 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1120 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1123 mutt_expand_path (buf, sizeof (buf));
1124 if (imap_is_magic (buf, NULL) == M_IMAP) {
1125 m_strcpy(LastDir, sizeof(LastDir), buf);
1126 destroy_state (&state);
1127 init_state (&state, NULL);
1128 state.imap_browse = 1;
1129 imap_browse (LastDir, &state);
1130 browser_sort (&state);
1131 menu->data = state.entry;
1134 init_menu (&state, menu, title, sizeof (title), buffy);
1137 if (stat (buf, &st) == 0) {
1138 if (S_ISDIR (st.st_mode)) {
1139 destroy_state (&state);
1140 if (examine_directory (menu, &state, buf, prefix) == 0)
1141 m_strcpy(LastDir, sizeof(LastDir), buf);
1143 mutt_error _("Error scanning directory.");
1145 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1146 mutt_menuDestroy (&menu);
1152 init_menu (&state, menu, title, sizeof (title), buffy);
1155 mutt_error (_("%s is not a directory."), buf);
1160 MAYBE_REDRAW (menu->redraw);
1165 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1166 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1167 regex_t *rx = p_new(regex_t, 1);
1172 /* assume that the user wants to see everything */
1174 m_strcpy(buf, sizeof(buf), ".");
1177 s = vskipspaces(s + 1);
1181 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1182 regerror (err, rx, buf, sizeof (buf));
1185 mutt_error ("%s", buf);
1188 m_strreplace(&Mask.pattern, buf);
1194 destroy_state (&state);
1195 if (state.imap_browse) {
1196 init_state (&state, NULL);
1197 state.imap_browse = 1;
1198 imap_browse (LastDir, &state);
1199 browser_sort (&state);
1200 menu->data = state.entry;
1201 init_menu (&state, menu, title, sizeof (title), buffy);
1204 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1205 init_menu (&state, menu, title, sizeof (title), buffy);
1207 mutt_error _("Error scanning directory.");
1209 mutt_menuDestroy (&menu);
1213 if (!state.entrylen) {
1214 mutt_error _("No files match the file mask");
1220 MAYBE_REDRAW (menu->redraw);
1224 case OP_SORT_REVERSE:
1228 int reverse = (i == OP_SORT_REVERSE);
1230 switch (mutt_multi_choice ((reverse) ?
1232 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1235 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1237 case -1: /* abort */
1241 case 1: /* (d)ate */
1242 BrowserSort = SORT_DATE;
1245 case 2: /* (a)lpha */
1246 BrowserSort = SORT_SUBJECT;
1249 case 3: /* si(z)e */
1250 BrowserSort = SORT_SIZE;
1253 case 4: /* do(n)'t sort */
1254 BrowserSort = SORT_ORDER;
1259 BrowserSort |= reverse ? SORT_REVERSE : 0;
1260 browser_sort (&state);
1261 menu->redraw = REDRAW_FULL;
1266 case OP_TOGGLE_MAILBOXES:
1270 destroy_state (&state);
1275 if (examine_mailboxes (menu, &state) == -1)
1278 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1279 init_state (&state, NULL);
1280 state.imap_browse = 1;
1281 imap_browse (LastDir, &state);
1282 browser_sort (&state);
1283 menu->data = state.entry;
1285 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1287 init_menu (&state, menu, title, sizeof (title), buffy);
1291 if (option (OPTFORCEBUFFYCHECK))
1296 case OP_BROWSER_NEW_FILE:
1298 snprintf (buf, sizeof (buf), "%s/", LastDir);
1299 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1301 m_strcpy(f, flen, buf);
1302 destroy_state (&state);
1303 mutt_menuDestroy (&menu);
1306 MAYBE_REDRAW (menu->redraw);
1309 case OP_BROWSER_VIEW_FILE:
1310 if (!state.entrylen) {
1311 mutt_error _("No files match the file mask");
1316 if (state.entry[menu->current].selectable) {
1317 m_strcpy(f, flen, state.entry[menu->current].name);
1318 destroy_state (&state);
1319 mutt_menuDestroy (&menu);
1323 if (S_ISDIR (state.entry[menu->current].mode) ||
1324 (S_ISLNK (state.entry[menu->current].mode) &&
1325 link_is_dir (LastDir, state.entry[menu->current].name))) {
1326 mutt_error _("Can't view a directory");
1332 char nbuf[_POSIX_PATH_MAX];
1334 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1335 state.entry[menu->current].name);
1336 b = mutt_make_file_attach (nbuf);
1338 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1339 mutt_free_body (&b);
1340 menu->redraw = REDRAW_FULL;
1343 mutt_error _("Error trying to view file");
1350 if (option (OPTNEWS)) {
1351 struct folder_file *f = &state.entry[menu->current];
1354 if (i == OP_CATCHUP)
1355 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1357 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1361 struct folder_file ff;
1362 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1365 folder.ff->name = f->name;
1366 folder.ff->st = NULL;
1367 folder.ff->is_new = nd->new;
1370 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1371 newsgroup_format_str, (unsigned long) &folder,
1372 M_FORMAT_ARROWCURSOR);
1373 f->desc = m_strdup(buffer); */
1374 if (menu->current + 1 < menu->max)
1376 menu->redraw = REDRAW_MOTION_RESYNCH;
1381 case OP_LOAD_ACTIVE:
1382 if (!option (OPTNEWS))
1389 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1390 if ((data = (NNTP_DATA *) tmp->data))
1394 nntp_get_active (CurrentNewsSrv);
1396 destroy_state (&state);
1398 examine_mailboxes (menu, &state);
1400 examine_directory (menu, &state, NULL, NULL);
1401 init_menu (&state, menu, title, sizeof (title), buffy);
1403 #endif /* USE_NNTP */
1405 case OP_BROWSER_SUBSCRIBE:
1406 case OP_BROWSER_UNSUBSCRIBE:
1408 case OP_SUBSCRIBE_PATTERN:
1409 case OP_UNSUBSCRIBE_PATTERN:
1410 if (option (OPTNEWS)) {
1411 regex_t *rx = p_new(regex_t, 1);
1413 int j = menu->current;
1415 NNTP_SERVER *news = CurrentNewsSrv;
1417 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1422 if (i == OP_SUBSCRIBE_PATTERN)
1423 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1425 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1426 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1431 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1432 regerror (err, rx, buf, sizeof (buf));
1435 mutt_error ("%s", buf);
1438 menu->redraw = REDRAW_FULL;
1441 else if (!state.entrylen) {
1442 mutt_error _("No newsgroups match the mask");
1447 for (; j < state.entrylen; j++) {
1448 struct folder_file *f = &state.entry[j];
1450 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1451 regexec (rx, f->name, 0, NULL, 0) == 0) {
1452 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1453 nd = mutt_newsgroup_subscribe (news, f->name);
1455 nd = mutt_newsgroup_unsubscribe (news, f->name);
1459 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1461 folder.name = f->name;
1463 folder.new = nd->new;
1466 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1467 newsgroup_format_str, (unsigned long) &folder,
1468 M_FORMAT_ARROWCURSOR);
1469 f->desc = m_strdup(buffer);
1472 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1473 if (menu->current + 1 < menu->max)
1475 menu->redraw = REDRAW_MOTION_RESYNCH;
1479 if (i == OP_SUBSCRIBE_PATTERN) {
1480 LIST *grouplist = NULL;
1483 grouplist = news->list;
1484 for (; grouplist; grouplist = grouplist->next) {
1485 nd = (NNTP_DATA *) grouplist->data;
1486 if (nd && nd->group && !nd->subscribed) {
1487 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1488 mutt_newsgroup_subscribe (news, nd->group);
1489 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1493 init_menu (&state, menu, title, sizeof (title), buffy);
1495 mutt_newsrc_update (news);
1496 nntp_clear_cacheindex (news);
1497 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1502 #endif /* USE_NNTP */
1504 if (i == OP_BROWSER_SUBSCRIBE)
1505 imap_subscribe (state.entry[menu->current].name, 1);
1507 imap_subscribe (state.entry[menu->current].name, 0);
1515 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);