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"
32 #include "imap/mx_imap.h"
49 static struct mapping_t FolderHelp[] = {
50 {N_("Exit"), OP_EXIT},
51 {N_("Chdir"), OP_CHANGE_DIRECTORY},
52 {N_("Mask"), OP_ENTER_MASK},
53 {N_("Help"), OP_HELP},
58 static struct mapping_t FolderNewsHelp[] = {
59 {N_("Exit"), OP_EXIT},
60 {N_("List"), OP_TOGGLE_MAILBOXES},
61 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
62 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
63 {N_("Catchup"), OP_CATCHUP},
64 {N_("Mask"), OP_ENTER_MASK},
65 {N_("Help"), OP_HELP},
70 typedef struct folder_t {
71 struct folder_file *ff;
75 static char LastDir[_POSIX_PATH_MAX] = "";
76 static char LastDirBackup[_POSIX_PATH_MAX] = "";
78 /* Frees up the memory allocated for the local-global variables. */
79 static void destroy_state (struct browser_state *state)
83 for (c = 0; c < state->entrylen; c++) {
84 p_delete(&((state->entry)[c].name));
85 p_delete(&((state->entry)[c].desc));
86 p_delete(&((state->entry)[c].st));
89 p_delete(&state->folder);
91 p_delete(&state->entry);
94 static int browser_compare_subject (const void *a, const void *b)
96 struct folder_file *pa = (struct folder_file *) a;
97 struct folder_file *pb = (struct folder_file *) b;
99 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
101 return ((BrowserSort & SORT_REVERSE) ? -r : r);
104 static int browser_compare_date (const void *a, const void *b)
106 struct folder_file *pa = (struct folder_file *) a;
107 struct folder_file *pb = (struct folder_file *) b;
109 int r = pa->mtime - pb->mtime;
111 return ((BrowserSort & SORT_REVERSE) ? -r : r);
114 static int browser_compare_size (const void *a, const void *b)
116 struct folder_file *pa = (struct folder_file *) a;
117 struct folder_file *pb = (struct folder_file *) b;
119 int r = pa->size - pb->size;
121 return ((BrowserSort & SORT_REVERSE) ? -r : r);
124 static void browser_sort (struct browser_state *state)
126 int (*f) (const void *, const void *);
128 switch (BrowserSort & SORT_MASK) {
133 if (option (OPTNEWS))
136 f = browser_compare_date;
140 if (option (OPTNEWS))
143 f = browser_compare_size;
147 f = browser_compare_subject;
150 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
153 static int link_is_dir (const char *folder, const char *path)
156 char fullpath[_POSIX_PATH_MAX];
158 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
160 if (stat (fullpath, &st) == 0)
161 return (S_ISDIR (st.st_mode));
166 static const char *folder_format_str (char *dest, size_t destlen, char op,
167 const char *src, const char *fmt,
168 const char *ifstring,
169 const char *elsestring,
170 unsigned long data, format_flag flags)
172 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
175 FOLDER *folder = (FOLDER *) data;
178 int optional = (flags & M_FORMAT_OPTIONAL);
182 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
183 snprintf (dest, destlen, tmp, folder->num + 1);
187 if (folder->ff->st != NULL) {
190 tnow - folder->ff->st->st_mtime <
191 31536000 ? "%b %d %H:%M" : "%b %d %Y";
192 strftime (date, sizeof (date), t_fmt,
193 localtime (&folder->ff->st->st_mtime));
194 mutt_format_s (dest, destlen, fmt, date);
197 mutt_format_s (dest, destlen, fmt, "");
205 if (folder->ff->imap)
206 s = NONULL (folder->ff->desc);
209 s = NONULL (folder->ff->name);
211 snprintf (fn, sizeof (fn), "%s%s", s,
212 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
213 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
214 ((folder->ff->st->st_mode & S_IXUSR) !=
215 0 ? "*" : ""))) : "");
217 mutt_format_s (dest, destlen, fmt, fn);
221 if (folder->ff->st != NULL) {
222 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
223 S_ISDIR (folder->ff->st->
224 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
225 st_mode) ? 'l' : '-'),
226 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
227 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
228 (folder->ff->st->st_mode & S_ISUID) !=
229 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
231 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
232 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
233 (folder->ff->st->st_mode & S_ISGID) !=
234 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
236 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
237 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
238 (folder->ff->st->st_mode & S_ISVTX) !=
239 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
241 mutt_format_s (dest, destlen, fmt, permission);
244 else if (folder->ff->imap) {
245 /* mark folders with subfolders AND mail */
246 snprintf (permission, sizeof (permission), "IMAP %c",
247 (folder->ff->inferiors
248 && folder->ff->selectable) ? '+' : ' ');
249 mutt_format_s (dest, destlen, fmt, permission);
253 mutt_format_s (dest, destlen, fmt, "");
257 if (folder->ff->st != NULL) {
258 if ((gr = getgrgid (folder->ff->st->st_gid)))
259 mutt_format_s (dest, destlen, fmt, gr->gr_name);
261 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
262 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
266 mutt_format_s (dest, destlen, fmt, "");
270 if (folder->ff->st != NULL) {
271 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
272 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
275 mutt_format_s (dest, destlen, fmt, "");
280 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
282 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
283 snprintf (dest, destlen, tmp, folder->ff->new);
285 else if (!folder->ff->new)
290 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
291 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
295 if (folder->ff->st != NULL) {
296 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
297 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
300 mutt_format_s (dest, destlen, fmt, "");
304 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
305 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
309 if (folder->ff->st != NULL) {
310 if ((pw = getpwuid (folder->ff->st->st_uid)))
311 mutt_format_s (dest, destlen, fmt, pw->pw_name);
313 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
314 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
318 mutt_format_s (dest, destlen, fmt, "");
322 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
323 snprintf (dest, destlen, tmp, op);
328 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
329 else if (flags & M_FORMAT_OPTIONAL)
330 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
336 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
337 const char *src, const char *fmt,
338 const char *ifstring,
339 const char *elsestring,
343 char fn[SHORT_STRING], tmp[SHORT_STRING];
344 FOLDER *folder = (FOLDER *) data;
348 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
349 snprintf (dest, destlen, tmp, folder->num + 1);
353 m_strcpy(fn, sizeof(fn), folder->ff->name);
354 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
355 snprintf (dest, destlen, tmp, fn);
359 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
360 if (folder->ff->nd->subscribed)
361 snprintf (dest, destlen, tmp, ' ');
363 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
367 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
368 if (folder->ff->nd->deleted)
369 snprintf (dest, destlen, tmp, 'D');
371 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
375 if (flags & M_FORMAT_OPTIONAL) {
376 if (folder->ff->nd->unread != 0)
377 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
380 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
383 else if (Context && Context->data == folder->ff->nd) {
384 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
385 snprintf (dest, destlen, tmp, Context->unread);
388 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
389 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
394 if (Context && Context->data == folder->ff->nd) {
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp, Context->new);
398 else if (option (OPTMARKOLD) &&
399 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
400 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
401 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
402 snprintf (dest, destlen, tmp,
403 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
406 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
407 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
412 if (folder->ff->nd->desc != NULL) {
413 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
414 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
417 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
418 snprintf (dest, destlen, tmp, "");
424 #endif /* USE_NNTP */
426 static void add_folder (MUTTMENU * m, struct browser_state *state,
427 const char *name, const struct stat *s,
430 if (state->entrylen == state->entrymax) {
431 /* need to allocate more space */
432 p_realloc(&state->entry, state->entrymax += 256);
433 p_clear(&state->entry[state->entrylen], 256);
435 m->data = state->entry;
439 (state->entry)[state->entrylen].mode = s->st_mode;
440 (state->entry)[state->entrylen].mtime = s->st_mtime;
441 (state->entry)[state->entrylen].size = s->st_size;
442 (state->entry)[state->entrylen].st = p_dup(s, 1);
445 (state->entry)[state->entrylen].new = new;
446 (state->entry)[state->entrylen].name = m_strdup(name);
447 (state->entry)[state->entrylen].desc = m_strdup(name);
449 (state->entry)[state->entrylen].imap = 0;
452 if (option (OPTNEWS))
453 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
458 static void init_state (struct browser_state *state, MUTTMENU * menu)
461 state->entrymax = 256;
462 state->entry = p_new(struct folder_file, state->entrymax);
464 state->imap_browse = 0;
467 menu->data = state->entry;
470 /* get list of all files/newsgroups with mask */
471 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
472 char *d, const char *prefix)
475 if (option (OPTNEWS)) {
478 NNTP_SERVER *news = CurrentNewsSrv;
480 /* buffy_check (0); */
481 init_state (state, menu);
483 for (tmp = news->list; tmp; tmp = tmp->next) {
484 if (!(data = (NNTP_DATA *) tmp->data))
486 nntp_sync_sidebar (data);
487 if (prefix && *prefix && strncmp (prefix, data->group,
488 m_strlen(prefix)) != 0)
490 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
492 add_folder (menu, state, data->group, NULL, data, data->new);
494 sidebar_draw (CurrentMenu);
497 #endif /* USE_NNTP */
502 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
505 while (stat (d, &s) == -1) {
506 if (errno == ENOENT) {
507 /* The last used directory is deleted, try to use the parent dir. */
508 char *c = strrchr (d, '/');
519 if (!S_ISDIR (s.st_mode)) {
520 mutt_error (_("%s is not a directory."), d);
526 if ((dp = opendir (d)) == NULL) {
531 init_state (state, menu);
533 while ((de = readdir (dp)) != NULL) {
534 if (m_strcmp(de->d_name, ".") == 0)
535 continue; /* we don't need . */
537 if (prefix && *prefix
538 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
540 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
543 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
544 if (lstat (buffer, &s) == -1)
547 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
548 (!S_ISLNK (s.st_mode)))
551 i = buffy_lookup (buffer);
552 add_folder (menu, state, de->d_name, &s, NULL, 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);
602 if (tmp->magic == M_IMAP) {
603 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
608 if (tmp->magic == M_POP) {
609 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
614 if (tmp->magic == M_NNTP) {
615 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
619 if (lstat (tmp->path, &s) == -1)
622 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
623 (!S_ISLNK (s.st_mode)))
626 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
627 mutt_pretty_mailbox (buffer);
629 add_folder (menu, state, buffer, &s, NULL, tmp->new);
632 browser_sort (state);
636 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
639 if (option (OPTNEWS))
641 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
644 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
647 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
651 folder.ff = &((struct folder_file *) menu->data)[num];
655 if (option (OPTNEWS))
656 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
657 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
660 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
661 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
664 static void init_menu (struct browser_state *state, MUTTMENU * menu,
665 char *title, size_t titlelen, int buffy)
667 char path[_POSIX_PATH_MAX];
669 menu->max = state->entrylen;
671 if (menu->current >= menu->max)
672 menu->current = menu->max - 1;
673 if (menu->current < 0)
675 if (menu->top > menu->current)
681 if (option (OPTNEWS)) {
683 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
685 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
686 CurrentNewsSrv->conn->account.host);
691 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
693 m_strcpy(path, sizeof(path), LastDir);
694 mutt_pretty_mailbox (path);
696 if (state->imap_browse && option (OPTIMAPLSUB))
697 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
698 path, NONULL (Mask.pattern));
701 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
702 path, NONULL (Mask.pattern));
704 menu->redraw = REDRAW_FULL;
707 static int file_tag (MUTTMENU * menu, int n, int m)
709 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
712 if (S_ISDIR (ff->mode)
713 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
714 mutt_error _("Can't attach a directory!");
720 ff->tagged = (m >= 0 ? m : !ff->tagged);
722 return ff->tagged - ot;
725 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
728 char buf[_POSIX_PATH_MAX];
729 char prefix[_POSIX_PATH_MAX] = "";
730 char helpstr[SHORT_STRING];
732 struct browser_state state;
735 int i, killPrefix = 0;
736 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
737 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
738 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
740 buffy = buffy && folder;
745 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
748 if (option (OPTNEWS)) {
750 m_strcpy(prefix, sizeof(prefix), f);
754 /* default state for news reader mode is browse subscribed newsgroups */
756 for (list = CurrentNewsSrv->list; list; list = list->next) {
757 NNTP_DATA *data = (NNTP_DATA *) list->data;
759 if (data && data->subscribed) {
769 mutt_expand_path (f, flen);
771 if (imap_is_magic (f, NULL) == M_IMAP) {
772 init_state (&state, NULL);
773 state.imap_browse = 1;
774 if (!imap_browse (f, &state))
775 m_strcpy(LastDir, sizeof(LastDir), state.folder);
779 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
782 i = MIN(sizeof(LastDir) - 1, i);
783 m_strcpy(LastDir, sizeof(LastDir), f);
786 getcwd(LastDir, sizeof(LastDir));
787 m_strcat(LastDir, sizeof(LastDir), "/");
788 m_strncat(LastDir, sizeof(LastDir), f, i);
793 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
795 getcwd (LastDir, sizeof (LastDir));
798 if (i <= 0 && f[0] != '/')
799 m_strcpy(prefix, sizeof(prefix), f);
801 m_strcpy(prefix, sizeof(prefix), f + i + 1);
809 getcwd (LastDir, sizeof (LastDir));
810 else if (!LastDir[0])
811 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
814 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
815 init_state (&state, NULL);
816 state.imap_browse = 1;
817 imap_browse (LastDir, &state);
818 browser_sort (&state);
826 if (examine_mailboxes (NULL, &state) == -1)
831 if (!state.imap_browse)
833 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
836 menu = mutt_new_menu ();
837 menu->menu = MENU_FOLDER;
838 menu->make_entry = folder_entry;
839 menu->search = select_file_search;
841 menu->data = state.entry;
843 menu->tag = file_tag;
845 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
847 (option (OPTNEWS)) ? FolderNewsHelp :
851 init_menu (&state, menu, title, sizeof (title), buffy);
854 switch (i = mutt_menuLoop (menu)) {
855 case OP_GENERIC_SELECT_ENTRY:
857 if (!state.entrylen) {
858 mutt_error _("No files match the file mask");
863 if (S_ISDIR (state.entry[menu->current].mode) ||
864 (S_ISLNK (state.entry[menu->current].mode) &&
865 link_is_dir (LastDir, state.entry[menu->current].name))
867 || state.entry[menu->current].inferiors
870 /* make sure this isn't a MH or maildir mailbox */
872 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
873 mutt_expand_path (buf, sizeof (buf));
876 else if (state.imap_browse) {
877 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
881 mutt_concat_path(buf, sizeof(buf), LastDir,
882 state.entry[menu->current].name);
884 if ((mx_get_magic (buf) <= 0)
886 || state.entry[menu->current].inferiors
889 char OldLastDir[_POSIX_PATH_MAX];
891 /* save the old directory */
892 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
894 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
895 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
896 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
898 char *p = strrchr (LastDir + 1, '/');
903 if (LastDir[0] == '/')
906 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
911 m_strcpy(LastDir, sizeof(LastDir),
912 state.entry[menu->current].name);
913 mutt_expand_path (LastDir, sizeof (LastDir));
916 else if (state.imap_browse) {
920 m_strcpy(LastDir, sizeof(LastDir),
921 state.entry[menu->current].name);
922 /* tack on delimiter here */
923 n = m_strlen(LastDir) + 1;
925 /* special case "" needs no delimiter */
926 url_parse_ciss (&url, state.entry[menu->current].name);
928 (state.entry[menu->current].delim != '\0') &&
929 (n < sizeof (LastDir))) {
931 LastDir[n - 1] = state.entry[menu->current].delim;
936 char tmp[_POSIX_PATH_MAX];
938 mutt_concat_path(tmp, sizeof(tmp), LastDir,
939 state.entry[menu->current].name);
940 m_strcpy(LastDir, sizeof(LastDir), tmp);
943 destroy_state (&state);
950 if (state.imap_browse) {
951 init_state (&state, NULL);
952 state.imap_browse = 1;
953 imap_browse (LastDir, &state);
954 browser_sort (&state);
955 menu->data = state.entry;
959 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
960 /* try to restore the old values */
961 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
962 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
963 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
969 init_menu (&state, menu, title, sizeof (title), buffy);
975 if (buffy || option (OPTNEWS)) /* news have not path */
980 m_strcpy(f, flen, state.entry[menu->current].name);
981 mutt_expand_path (f, flen);
984 else if (state.imap_browse)
985 m_strcpy(f, flen, state.entry[menu->current].name);
988 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
990 /* Fall through to OP_EXIT */
999 *numfiles = menu->tagged;
1000 tfiles = p_new(char *, *numfiles);
1001 for (i = 0, j = 0; i < state.entrylen; i++) {
1002 struct folder_file ff = state.entry[i];
1003 char full[_POSIX_PATH_MAX];
1006 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
1007 mutt_expand_path (full, sizeof (full));
1008 tfiles[j++] = m_strdup(full);
1013 else if (f[0]) { /* no tagged entries. return selected entry */
1015 tfiles = p_new(char *, *numfiles);
1016 mutt_expand_path (f, flen);
1017 tfiles[0] = m_strdup(f);
1022 destroy_state (&state);
1023 mutt_menuDestroy (&menu);
1026 case OP_BROWSER_TELL:
1028 mutt_message ("%s", state.entry[menu->current].name);
1032 case OP_BROWSER_TOGGLE_LSUB:
1033 if (option (OPTIMAPLSUB)) {
1034 unset_option (OPTIMAPLSUB);
1037 set_option (OPTIMAPLSUB);
1039 mutt_ungetch (0, OP_CHECK_NEW);
1042 case OP_CREATE_MAILBOX:
1043 if (!state.imap_browse)
1044 mutt_error (_("Create is only supported for IMAP mailboxes"));
1046 imap_mailbox_create (LastDir);
1047 /* TODO: find a way to detect if the new folder would appear in
1048 * this window, and insert it without starting over. */
1049 destroy_state (&state);
1050 init_state (&state, NULL);
1051 state.imap_browse = 1;
1052 imap_browse (LastDir, &state);
1053 browser_sort (&state);
1054 menu->data = state.entry;
1057 init_menu (&state, menu, title, sizeof (title), buffy);
1058 MAYBE_REDRAW (menu->redraw);
1062 case OP_RENAME_MAILBOX:
1063 if (!state.entry[menu->current].imap)
1064 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1066 int nentry = menu->current;
1068 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1069 destroy_state (&state);
1070 init_state (&state, NULL);
1071 state.imap_browse = 1;
1072 imap_browse (LastDir, &state);
1073 browser_sort (&state);
1074 menu->data = state.entry;
1077 init_menu (&state, menu, title, sizeof (title), buffy);
1078 MAYBE_REDRAW (menu->redraw);
1083 case OP_DELETE_MAILBOX:
1084 if (!state.entry[menu->current].imap)
1085 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1087 char msg[SHORT_STRING];
1089 int nentry = menu->current;
1091 imap_parse_path (state.entry[nentry].name, &mx);
1092 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1094 if (mutt_yesorno (msg, M_NO) == M_YES) {
1095 if (!imap_delete_mailbox (Context, mx)) {
1096 /* free the mailbox from the browser */
1097 p_delete(&((state.entry)[nentry].name));
1098 p_delete(&((state.entry)[nentry].desc));
1099 /* and move all other entries up */
1100 if (nentry + 1 < state.entrylen)
1101 memmove (state.entry + nentry, state.entry + nentry + 1,
1102 sizeof (struct folder_file) * (state.entrylen -
1105 mutt_message _("Mailbox deleted.");
1107 init_menu (&state, menu, title, sizeof (title), buffy);
1108 MAYBE_REDRAW (menu->redraw);
1112 mutt_message _("Mailbox not deleted.");
1118 case OP_CHANGE_DIRECTORY:
1121 if (option (OPTNEWS))
1125 m_strcpy(buf, sizeof(buf), LastDir);
1127 if (!state.imap_browse)
1130 /* add '/' at the end of the directory name if not already there */
1131 int len = m_strlen(LastDir);
1133 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1137 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1140 mutt_expand_path (buf, sizeof (buf));
1142 if (imap_is_magic (buf, NULL) == M_IMAP) {
1143 m_strcpy(LastDir, sizeof(LastDir), buf);
1144 destroy_state (&state);
1145 init_state (&state, NULL);
1146 state.imap_browse = 1;
1147 imap_browse (LastDir, &state);
1148 browser_sort (&state);
1149 menu->data = state.entry;
1152 init_menu (&state, menu, title, sizeof (title), buffy);
1156 if (stat (buf, &st) == 0) {
1157 if (S_ISDIR (st.st_mode)) {
1158 destroy_state (&state);
1159 if (examine_directory (menu, &state, buf, prefix) == 0)
1160 m_strcpy(LastDir, sizeof(LastDir), buf);
1162 mutt_error _("Error scanning directory.");
1164 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1165 mutt_menuDestroy (&menu);
1171 init_menu (&state, menu, title, sizeof (title), buffy);
1174 mutt_error (_("%s is not a directory."), buf);
1179 MAYBE_REDRAW (menu->redraw);
1184 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1185 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1186 regex_t *rx = p_new(regex_t, 1);
1191 /* assume that the user wants to see everything */
1193 m_strcpy(buf, sizeof(buf), ".");
1196 s = vskipspaces(s + 1);
1200 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1201 regerror (err, rx, buf, sizeof (buf));
1204 mutt_error ("%s", buf);
1207 str_replace (&Mask.pattern, buf);
1213 destroy_state (&state);
1215 if (state.imap_browse) {
1216 init_state (&state, NULL);
1217 state.imap_browse = 1;
1218 imap_browse (LastDir, &state);
1219 browser_sort (&state);
1220 menu->data = state.entry;
1221 init_menu (&state, menu, title, sizeof (title), buffy);
1225 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1226 init_menu (&state, menu, title, sizeof (title), buffy);
1228 mutt_error _("Error scanning directory.");
1230 mutt_menuDestroy (&menu);
1234 if (!state.entrylen) {
1235 mutt_error _("No files match the file mask");
1241 MAYBE_REDRAW (menu->redraw);
1245 case OP_SORT_REVERSE:
1249 int reverse = (i == OP_SORT_REVERSE);
1251 switch (mutt_multi_choice ((reverse) ?
1253 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1256 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1258 case -1: /* abort */
1262 case 1: /* (d)ate */
1263 BrowserSort = SORT_DATE;
1266 case 2: /* (a)lpha */
1267 BrowserSort = SORT_SUBJECT;
1270 case 3: /* si(z)e */
1271 BrowserSort = SORT_SIZE;
1274 case 4: /* do(n)'t sort */
1275 BrowserSort = SORT_ORDER;
1280 BrowserSort |= reverse ? SORT_REVERSE : 0;
1281 browser_sort (&state);
1282 menu->redraw = REDRAW_FULL;
1287 case OP_TOGGLE_MAILBOXES:
1291 destroy_state (&state);
1296 if (examine_mailboxes (menu, &state) == -1)
1300 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1301 init_state (&state, NULL);
1302 state.imap_browse = 1;
1303 imap_browse (LastDir, &state);
1304 browser_sort (&state);
1305 menu->data = state.entry;
1308 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1310 init_menu (&state, menu, title, sizeof (title), buffy);
1314 if (option (OPTFORCEBUFFYCHECK))
1319 case OP_BROWSER_NEW_FILE:
1321 snprintf (buf, sizeof (buf), "%s/", LastDir);
1322 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1324 m_strcpy(f, flen, buf);
1325 destroy_state (&state);
1326 mutt_menuDestroy (&menu);
1329 MAYBE_REDRAW (menu->redraw);
1332 case OP_BROWSER_VIEW_FILE:
1333 if (!state.entrylen) {
1334 mutt_error _("No files match the file mask");
1340 if (state.entry[menu->current].selectable) {
1341 m_strcpy(f, flen, state.entry[menu->current].name);
1342 destroy_state (&state);
1343 mutt_menuDestroy (&menu);
1348 if (S_ISDIR (state.entry[menu->current].mode) ||
1349 (S_ISLNK (state.entry[menu->current].mode) &&
1350 link_is_dir (LastDir, state.entry[menu->current].name))) {
1351 mutt_error _("Can't view a directory");
1357 char buf[_POSIX_PATH_MAX];
1359 mutt_concat_path(buf, sizeof(buf), LastDir,
1360 state.entry[menu->current].name);
1361 b = mutt_make_file_attach (buf);
1363 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1364 mutt_free_body (&b);
1365 menu->redraw = REDRAW_FULL;
1368 mutt_error _("Error trying to view file");
1375 if (option (OPTNEWS)) {
1376 struct folder_file *f = &state.entry[menu->current];
1379 if (i == OP_CATCHUP)
1380 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1382 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1386 struct folder_file ff;
1387 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1390 folder.ff->name = f->name;
1391 folder.ff->st = NULL;
1392 folder.ff->is_new = nd->new;
1395 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1396 newsgroup_format_str, (unsigned long) &folder,
1397 M_FORMAT_ARROWCURSOR);
1398 f->desc = m_strdup(buffer); */
1399 if (menu->current + 1 < menu->max)
1401 menu->redraw = REDRAW_MOTION_RESYNCH;
1406 case OP_LOAD_ACTIVE:
1407 if (!option (OPTNEWS))
1414 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1415 if ((data = (NNTP_DATA *) tmp->data))
1419 nntp_get_active (CurrentNewsSrv);
1421 destroy_state (&state);
1423 examine_mailboxes (menu, &state);
1425 examine_directory (menu, &state, NULL, NULL);
1426 init_menu (&state, menu, title, sizeof (title), buffy);
1428 #endif /* USE_NNTP */
1430 #if defined USE_IMAP || defined USE_NNTP
1431 case OP_BROWSER_SUBSCRIBE:
1432 case OP_BROWSER_UNSUBSCRIBE:
1435 case OP_SUBSCRIBE_PATTERN:
1436 case OP_UNSUBSCRIBE_PATTERN:
1437 if (option (OPTNEWS)) {
1438 regex_t *rx = p_new(regex_t, 1);
1440 int j = menu->current;
1442 NNTP_SERVER *news = CurrentNewsSrv;
1444 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1449 if (i == OP_SUBSCRIBE_PATTERN)
1450 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1452 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1453 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1458 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1459 regerror (err, rx, buf, sizeof (buf));
1462 mutt_error ("%s", buf);
1465 menu->redraw = REDRAW_FULL;
1468 else if (!state.entrylen) {
1469 mutt_error _("No newsgroups match the mask");
1474 for (; j < state.entrylen; j++) {
1475 struct folder_file *f = &state.entry[j];
1477 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1478 regexec (rx, f->name, 0, NULL, 0) == 0) {
1479 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1480 nd = mutt_newsgroup_subscribe (news, f->name);
1482 nd = mutt_newsgroup_unsubscribe (news, f->name);
1486 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1488 folder.name = f->name;
1490 folder.new = nd->new;
1493 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1494 newsgroup_format_str, (unsigned long) &folder,
1495 M_FORMAT_ARROWCURSOR);
1496 f->desc = m_strdup(buffer);
1499 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1500 if (menu->current + 1 < menu->max)
1502 menu->redraw = REDRAW_MOTION_RESYNCH;
1506 if (i == OP_SUBSCRIBE_PATTERN) {
1507 LIST *grouplist = NULL;
1510 grouplist = news->list;
1511 for (; grouplist; grouplist = grouplist->next) {
1512 nd = (NNTP_DATA *) grouplist->data;
1513 if (nd && nd->group && !nd->subscribed) {
1514 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1515 mutt_newsgroup_subscribe (news, nd->group);
1516 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1520 init_menu (&state, menu, title, sizeof (title), buffy);
1522 mutt_newsrc_update (news);
1523 nntp_clear_cacheindex (news);
1524 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1530 #endif /* USE_IMAP && USE_NNTP */
1531 #endif /* USE_NNTP */
1534 if (i == OP_BROWSER_SUBSCRIBE)
1535 imap_subscribe (state.entry[menu->current].name, 1);
1537 imap_subscribe (state.entry[menu->current].name, 0);
1539 #endif /* USE_IMAP */
1546 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);