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.
24 #include <lib-lib/mem.h>
25 #include <lib-lib/str.h>
26 #include <lib-lib/macros.h>
27 #include <lib-lib/file.h>
28 #include <lib-lib/mapping.h>
30 #include <lib-ui/curses.h>
31 #include <lib-ui/enter.h>
32 #include <lib-ui/menu.h>
41 #include <imap/imap.h>
42 #include <imap/mx_imap.h>
50 static struct mapping_t FolderHelp[] = {
51 {N_("Exit"), OP_EXIT},
52 {N_("Chdir"), OP_CHANGE_DIRECTORY},
53 {N_("Mask"), OP_ENTER_MASK},
54 {N_("Help"), OP_HELP},
59 static struct mapping_t FolderNewsHelp[] = {
60 {N_("Exit"), OP_EXIT},
61 {N_("List"), OP_TOGGLE_MAILBOXES},
62 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
63 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
64 {N_("Catchup"), OP_CATCHUP},
65 {N_("Mask"), OP_ENTER_MASK},
66 {N_("Help"), OP_HELP},
71 typedef struct folder_t {
72 struct folder_file *ff;
76 static char LastDir[_POSIX_PATH_MAX] = "";
77 static char LastDirBackup[_POSIX_PATH_MAX] = "";
79 /* Frees up the memory allocated for the local-global variables. */
80 static void destroy_state (struct browser_state *state)
84 for (c = 0; c < state->entrylen; c++) {
85 p_delete(&((state->entry)[c].name));
86 p_delete(&((state->entry)[c].desc));
87 p_delete(&((state->entry)[c].st));
89 p_delete(&state->folder);
90 p_delete(&state->entry);
93 static int browser_compare_subject (const void *a, const void *b)
95 struct folder_file *pa = (struct folder_file *) a;
96 struct folder_file *pb = (struct folder_file *) b;
98 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
100 return ((BrowserSort & SORT_REVERSE) ? -r : r);
103 static int browser_compare_date (const void *a, const void *b)
105 struct folder_file *pa = (struct folder_file *) a;
106 struct folder_file *pb = (struct folder_file *) b;
108 int r = pa->mtime - pb->mtime;
110 return ((BrowserSort & SORT_REVERSE) ? -r : r);
113 static int browser_compare_size (const void *a, const void *b)
115 struct folder_file *pa = (struct folder_file *) a;
116 struct folder_file *pb = (struct folder_file *) b;
118 int r = pa->size - pb->size;
120 return ((BrowserSort & SORT_REVERSE) ? -r : r);
123 static void browser_sort (struct browser_state *state)
125 int (*f) (const void *, const void *);
127 switch (BrowserSort & SORT_MASK) {
132 if (option (OPTNEWS))
135 f = browser_compare_date;
139 if (option (OPTNEWS))
142 f = browser_compare_size;
146 f = browser_compare_subject;
149 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
152 static int link_is_dir (const char *folder, const char *path)
155 char fullpath[_POSIX_PATH_MAX];
157 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
159 if (stat (fullpath, &st) == 0)
160 return (S_ISDIR (st.st_mode));
165 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
166 const char *src, const char *fmt,
167 const char *ifstring,
168 const char *elsestring,
169 unsigned long data, format_flag flags)
171 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
174 FOLDER *folder = (FOLDER *) data;
177 int optional = (flags & M_FORMAT_OPTIONAL);
181 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
182 snprintf (dest, destlen, tmp, folder->num + 1);
186 if (folder->ff->st != NULL) {
189 tnow - folder->ff->st->st_mtime <
190 31536000 ? "%b %d %H:%M" : "%b %d %Y";
191 strftime (date, sizeof (date), t_fmt,
192 localtime (&folder->ff->st->st_mtime));
193 mutt_format_s (dest, destlen, fmt, date);
196 mutt_format_s (dest, destlen, fmt, "");
203 if (folder->ff->imap)
204 s = NONULL (folder->ff->desc);
206 s = NONULL (folder->ff->name);
208 snprintf (fn, sizeof (fn), "%s%s", s,
209 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
210 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
211 ((folder->ff->st->st_mode & S_IXUSR) !=
212 0 ? "*" : ""))) : "");
214 mutt_format_s (dest, destlen, fmt, fn);
218 if (folder->ff->st != NULL) {
219 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
220 S_ISDIR (folder->ff->st->
221 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
222 st_mode) ? 'l' : '-'),
223 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
224 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
225 (folder->ff->st->st_mode & S_ISUID) !=
226 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
228 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
229 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
230 (folder->ff->st->st_mode & S_ISGID) !=
231 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
233 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
234 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
235 (folder->ff->st->st_mode & S_ISVTX) !=
236 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
238 mutt_format_s (dest, destlen, fmt, permission);
240 else if (folder->ff->imap) {
241 /* mark folders with subfolders AND mail */
242 snprintf (permission, sizeof (permission), "IMAP %c",
243 (folder->ff->inferiors
244 && folder->ff->selectable) ? '+' : ' ');
245 mutt_format_s (dest, destlen, fmt, permission);
248 mutt_format_s (dest, destlen, fmt, "");
252 if (folder->ff->st != NULL) {
253 if ((gr = getgrgid (folder->ff->st->st_gid)))
254 mutt_format_s (dest, destlen, fmt, gr->gr_name);
256 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
257 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
261 mutt_format_s (dest, destlen, fmt, "");
265 if (folder->ff->st != NULL) {
266 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
267 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
270 mutt_format_s (dest, destlen, fmt, "");
274 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
276 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
277 snprintf (dest, destlen, tmp, folder->ff->new);
279 else if (!folder->ff->new)
283 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
284 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
288 if (folder->ff->st != NULL) {
289 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
290 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
293 mutt_format_s (dest, destlen, fmt, "");
297 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
298 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
302 if (folder->ff->st != NULL) {
303 if ((pw = getpwuid (folder->ff->st->st_uid)))
304 mutt_format_s (dest, destlen, fmt, pw->pw_name);
306 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
307 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
311 mutt_format_s (dest, destlen, fmt, "");
315 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
316 snprintf (dest, destlen, tmp, op);
321 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
322 else if (flags & M_FORMAT_OPTIONAL)
323 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
329 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
330 const char *src, const char *fmt,
331 const char *ifstring,
332 const char *elsestring,
336 char fn[SHORT_STRING], tmp[SHORT_STRING];
337 FOLDER *folder = (FOLDER *) data;
341 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
342 snprintf (dest, destlen, tmp, folder->num + 1);
346 m_strcpy(fn, sizeof(fn), folder->ff->name);
347 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
348 snprintf (dest, destlen, tmp, fn);
352 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
353 if (folder->ff->nd->subscribed)
354 snprintf (dest, destlen, tmp, ' ');
356 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
360 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
361 if (folder->ff->nd->deleted)
362 snprintf (dest, destlen, tmp, 'D');
364 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
368 if (flags & M_FORMAT_OPTIONAL) {
369 if (folder->ff->nd->unread != 0)
370 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
373 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
376 else if (Context && Context->data == folder->ff->nd) {
377 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
378 snprintf (dest, destlen, tmp, Context->unread);
381 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
382 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
387 if (Context && Context->data == folder->ff->nd) {
388 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
389 snprintf (dest, destlen, tmp, Context->new);
391 else if (option (OPTMARKOLD) &&
392 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
393 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
394 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
395 snprintf (dest, destlen, tmp,
396 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
399 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
400 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
405 if (folder->ff->nd->desc != NULL) {
406 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
407 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
410 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
411 snprintf (dest, destlen, tmp, "");
417 #endif /* USE_NNTP */
420 static void add_folder (MUTTMENU * m, struct browser_state *state,
421 const char *name, const struct stat *s,
424 static void add_folder (MUTTMENU * m, struct browser_state *state,
425 const char *name, const struct stat *s,
429 if (state->entrylen == state->entrymax) {
430 /* need to allocate more space */
431 p_realloc(&state->entry, state->entrymax += 256);
432 p_clear(&state->entry[state->entrylen], 256);
434 m->data = state->entry;
438 (state->entry)[state->entrylen].mode = s->st_mode;
439 (state->entry)[state->entrylen].mtime = s->st_mtime;
440 (state->entry)[state->entrylen].size = s->st_size;
441 (state->entry)[state->entrylen].st = p_dup(s, 1);
444 (state->entry)[state->entrylen].new = new;
445 (state->entry)[state->entrylen].name = m_strdup(name);
446 (state->entry)[state->entrylen].desc = m_strdup(name);
447 (state->entry)[state->entrylen].imap = 0;
449 if (option (OPTNEWS))
450 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
455 static void init_state (struct browser_state *state, MUTTMENU * menu)
458 state->entrymax = 256;
459 state->entry = p_new(struct folder_file, state->entrymax);
460 state->imap_browse = 0;
462 menu->data = state->entry;
465 /* get list of all files/newsgroups with mask */
466 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
467 char *d, const char *prefix)
470 if (option (OPTNEWS)) {
473 NNTP_SERVER *news = CurrentNewsSrv;
475 /* buffy_check (0); */
476 init_state (state, menu);
478 for (tmp = news->list; tmp; tmp = tmp->next) {
479 if (!(data = (NNTP_DATA *) tmp->data))
481 nntp_sync_sidebar (data);
482 if (prefix && *prefix && strncmp (prefix, data->group,
483 m_strlen(prefix)) != 0)
485 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
488 add_folder (menu, state, data->group, NULL, data, data->new);
490 add_folder (menu, state, data->group, NULL, data->new);
493 sidebar_draw (CurrentMenu);
496 #endif /* USE_NNTP */
501 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
504 while (stat (d, &s) == -1) {
505 if (errno == ENOENT) {
506 /* The last used directory is deleted, try to use the parent dir. */
507 char *c = strrchr (d, '/');
518 if (!S_ISDIR (s.st_mode)) {
519 mutt_error (_("%s is not a directory."), d);
525 if ((dp = opendir (d)) == NULL) {
530 init_state (state, menu);
532 while ((de = readdir (dp)) != NULL) {
533 if (m_strcmp(de->d_name, ".") == 0)
534 continue; /* we don't need . */
536 if (prefix && *prefix
537 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
539 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
542 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
543 if (lstat (buffer, &s) == -1)
546 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
547 (!S_ISLNK (s.st_mode)))
550 i = buffy_lookup (buffer);
552 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
554 add_folder (menu, state, de->d_name, &s, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
559 sidebar_draw (CurrentMenu);
560 browser_sort (state);
564 /* get list of mailboxes/subscribed newsgroups */
565 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
568 char buffer[LONG_STRING];
571 if (option (OPTNEWS)) {
574 NNTP_SERVER *news = CurrentNewsSrv;
576 /* buffy_check (0); */
577 init_state (state, menu);
579 for (tmp = news->list; tmp; tmp = tmp->next) {
580 if ((data = (NNTP_DATA*) tmp->data) == NULL)
582 nntp_sync_sidebar (data);
583 if ((data->new || (data->subscribed &&
584 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
585 add_folder (menu, state, data->group, NULL, data, data->new);
587 sidebar_draw (CurrentMenu);
599 init_state (state, menu);
601 for (i = 0; i < Incoming->length; i++) {
602 tmp = (BUFFY*) Incoming->data[i];
603 tmp->magic = mx_get_magic (tmp->path);
604 if (tmp->magic == M_IMAP) {
606 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
608 add_folder (menu, state, tmp->path, NULL, tmp->new);
612 if (tmp->magic == M_POP) {
614 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
616 add_folder (menu, state, tmp->path, NULL, tmp->new);
621 if (tmp->magic == M_NNTP) {
622 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
626 if (lstat (tmp->path, &s) == -1)
629 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
630 (!S_ISLNK (s.st_mode)))
633 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
634 mutt_pretty_mailbox (buffer);
637 add_folder (menu, state, buffer, &s, NULL, tmp->new);
639 add_folder (menu, state, buffer, &s, tmp->new);
643 browser_sort (state);
647 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
650 if (option (OPTNEWS))
652 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
655 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
658 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
662 folder.ff = &((struct folder_file *) menu->data)[num];
666 if (option (OPTNEWS))
667 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
668 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
671 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
672 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
675 static void init_menu (struct browser_state *state, MUTTMENU * menu,
676 char *title, ssize_t titlelen, int buffy)
678 char path[_POSIX_PATH_MAX];
680 menu->max = state->entrylen;
682 if (menu->current >= menu->max)
683 menu->current = menu->max - 1;
684 if (menu->current < 0)
686 if (menu->top > menu->current)
692 if (option (OPTNEWS)) {
694 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
696 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
697 CurrentNewsSrv->conn->account.host);
702 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
704 m_strcpy(path, sizeof(path), LastDir);
705 mutt_pretty_mailbox (path);
706 if (state->imap_browse && option (OPTIMAPLSUB))
707 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
708 path, NONULL (Mask.pattern));
710 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
711 path, NONULL (Mask.pattern));
713 menu->redraw = REDRAW_FULL;
716 static int file_tag (MUTTMENU * menu, int n, int m)
718 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
721 if (S_ISDIR (ff->mode)
722 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
723 mutt_error _("Can't attach a directory!");
729 ff->tagged = (m >= 0 ? m : !ff->tagged);
731 return ff->tagged - ot;
734 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
737 char buf[_POSIX_PATH_MAX];
738 char prefix[_POSIX_PATH_MAX] = "";
739 char helpstr[SHORT_STRING];
741 struct browser_state state;
744 int i, killPrefix = 0;
745 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
746 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
747 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
749 buffy = buffy && folder;
754 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
757 if (option (OPTNEWS)) {
759 m_strcpy(prefix, sizeof(prefix), f);
763 /* default state for news reader mode is browse subscribed newsgroups */
765 for (list = CurrentNewsSrv->list; list; list = list->next) {
766 NNTP_DATA *data = (NNTP_DATA *) list->data;
768 if (data && data->subscribed) {
778 mutt_expand_path (f, flen);
779 if (imap_is_magic (f, NULL) == M_IMAP) {
780 init_state (&state, NULL);
781 state.imap_browse = 1;
782 if (!imap_browse (f, &state))
783 m_strcpy(LastDir, sizeof(LastDir), state.folder);
786 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
789 i = MIN(ssizeof(LastDir) - 1, i);
790 m_strcpy(LastDir, sizeof(LastDir), f);
793 getcwd(LastDir, sizeof(LastDir));
794 m_strcat(LastDir, sizeof(LastDir), "/");
795 m_strncat(LastDir, sizeof(LastDir), f, i);
800 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
802 getcwd (LastDir, sizeof (LastDir));
805 if (i <= 0 && f[0] != '/')
806 m_strcpy(prefix, sizeof(prefix), f);
808 m_strcpy(prefix, sizeof(prefix), f + i + 1);
814 getcwd (LastDir, sizeof (LastDir));
815 else if (!LastDir[0])
816 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
818 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
819 init_state (&state, NULL);
820 state.imap_browse = 1;
821 imap_browse (LastDir, &state);
822 browser_sort (&state);
829 if (examine_mailboxes (NULL, &state) == -1)
833 if (!state.imap_browse)
834 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
837 menu = mutt_new_menu ();
838 menu->menu = MENU_FOLDER;
839 menu->make_entry = folder_entry;
840 menu->search = select_file_search;
842 menu->data = state.entry;
844 menu->tag = file_tag;
846 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
848 (option (OPTNEWS)) ? FolderNewsHelp :
852 init_menu (&state, menu, title, sizeof (title), buffy);
855 switch (i = mutt_menuLoop (menu)) {
856 case OP_GENERIC_SELECT_ENTRY:
858 if (!state.entrylen) {
859 mutt_error _("No files match the file mask");
864 if (S_ISDIR (state.entry[menu->current].mode) ||
865 (S_ISLNK (state.entry[menu->current].mode) &&
866 link_is_dir (LastDir, state.entry[menu->current].name))
867 || state.entry[menu->current].inferiors
869 /* make sure this isn't a MH or maildir mailbox */
871 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
872 mutt_expand_path (buf, sizeof (buf));
874 else if (state.imap_browse) {
875 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
878 mutt_concat_path(buf, sizeof(buf), LastDir,
879 state.entry[menu->current].name);
881 if ((mx_get_magic (buf) <= 0)
882 || state.entry[menu->current].inferiors)
884 char OldLastDir[_POSIX_PATH_MAX];
886 /* save the old directory */
887 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
889 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
890 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
891 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
893 char *p = strrchr (LastDir + 1, '/');
898 if (LastDir[0] == '/')
901 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
906 m_strcpy(LastDir, sizeof(LastDir),
907 state.entry[menu->current].name);
908 mutt_expand_path (LastDir, sizeof (LastDir));
910 else if (state.imap_browse) {
914 m_strcpy(LastDir, sizeof(LastDir),
915 state.entry[menu->current].name);
916 /* tack on delimiter here */
917 n = m_strlen(LastDir) + 1;
919 /* special case "" needs no delimiter */
920 url_parse_ciss (&url, state.entry[menu->current].name);
922 (state.entry[menu->current].delim != '\0') &&
923 (n < ssizeof (LastDir))) {
925 LastDir[n - 1] = state.entry[menu->current].delim;
929 char tmp[_POSIX_PATH_MAX];
931 mutt_concat_path(tmp, sizeof(tmp), LastDir,
932 state.entry[menu->current].name);
933 m_strcpy(LastDir, sizeof(LastDir), tmp);
936 destroy_state (&state);
942 if (state.imap_browse) {
943 init_state (&state, NULL);
944 state.imap_browse = 1;
945 imap_browse (LastDir, &state);
946 browser_sort (&state);
947 menu->data = state.entry;
950 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
951 /* try to restore the old values */
952 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
953 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
954 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
960 init_menu (&state, menu, title, sizeof (title), buffy);
966 if (buffy || option (OPTNEWS)) /* news have not path */
971 m_strcpy(f, flen, state.entry[menu->current].name);
972 mutt_expand_path (f, flen);
974 else if (state.imap_browse)
975 m_strcpy(f, flen, state.entry[menu->current].name);
977 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
979 /* Fall through to OP_EXIT */
989 *numfiles = menu->tagged;
990 tfiles = p_new(char *, *numfiles);
991 for (h = 0, j = 0; h < state.entrylen; i++) {
992 struct folder_file ff = state.entry[i];
993 char full[_POSIX_PATH_MAX];
996 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
997 mutt_expand_path (full, sizeof (full));
998 tfiles[j++] = m_strdup(full);
1003 else if (f[0]) { /* no tagged entries. return selected entry */
1005 tfiles = p_new(char *, *numfiles);
1006 mutt_expand_path (f, flen);
1007 tfiles[0] = m_strdup(f);
1012 destroy_state (&state);
1013 mutt_menuDestroy (&menu);
1016 case OP_BROWSER_TELL:
1018 mutt_message ("%s", state.entry[menu->current].name);
1021 case OP_BROWSER_TOGGLE_LSUB:
1022 if (option (OPTIMAPLSUB)) {
1023 unset_option (OPTIMAPLSUB);
1026 set_option (OPTIMAPLSUB);
1028 mutt_ungetch (0, OP_CHECK_NEW);
1031 case OP_CREATE_MAILBOX:
1032 if (!state.imap_browse)
1033 mutt_error (_("Create is only supported for IMAP mailboxes"));
1035 imap_mailbox_create (LastDir);
1036 /* TODO: find a way to detect if the new folder would appear in
1037 * this window, and insert it without starting over. */
1038 destroy_state (&state);
1039 init_state (&state, NULL);
1040 state.imap_browse = 1;
1041 imap_browse (LastDir, &state);
1042 browser_sort (&state);
1043 menu->data = state.entry;
1046 init_menu (&state, menu, title, sizeof (title), buffy);
1047 MAYBE_REDRAW (menu->redraw);
1051 case OP_RENAME_MAILBOX:
1052 if (!state.entry[menu->current].imap)
1053 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1055 int nentry = menu->current;
1057 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1058 destroy_state (&state);
1059 init_state (&state, NULL);
1060 state.imap_browse = 1;
1061 imap_browse (LastDir, &state);
1062 browser_sort (&state);
1063 menu->data = state.entry;
1066 init_menu (&state, menu, title, sizeof (title), buffy);
1067 MAYBE_REDRAW (menu->redraw);
1072 case OP_DELETE_MAILBOX:
1073 if (!state.entry[menu->current].imap)
1074 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1076 char msg[SHORT_STRING];
1078 int nentry = menu->current;
1080 imap_parse_path (state.entry[nentry].name, &mx);
1081 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1083 if (mutt_yesorno (msg, M_NO) == M_YES) {
1084 if (!imap_delete_mailbox (Context, mx)) {
1085 /* free the mailbox from the browser */
1086 p_delete(&((state.entry)[nentry].name));
1087 p_delete(&((state.entry)[nentry].desc));
1088 /* and move all other entries up */
1089 if (nentry + 1 < state.entrylen)
1090 memmove (state.entry + nentry, state.entry + nentry + 1,
1091 sizeof (struct folder_file) * (state.entrylen -
1094 mutt_message _("Mailbox deleted.");
1096 init_menu (&state, menu, title, sizeof (title), buffy);
1097 MAYBE_REDRAW (menu->redraw);
1101 mutt_message _("Mailbox not deleted.");
1106 case OP_CHANGE_DIRECTORY:
1109 if (option (OPTNEWS))
1113 m_strcpy(buf, sizeof(buf), LastDir);
1114 if (!state.imap_browse)
1116 /* add '/' at the end of the directory name if not already there */
1117 ssize_t len = m_strlen(LastDir);
1119 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1123 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1126 mutt_expand_path (buf, sizeof (buf));
1127 if (imap_is_magic (buf, NULL) == M_IMAP) {
1128 m_strcpy(LastDir, sizeof(LastDir), buf);
1129 destroy_state (&state);
1130 init_state (&state, NULL);
1131 state.imap_browse = 1;
1132 imap_browse (LastDir, &state);
1133 browser_sort (&state);
1134 menu->data = state.entry;
1137 init_menu (&state, menu, title, sizeof (title), buffy);
1140 if (stat (buf, &st) == 0) {
1141 if (S_ISDIR (st.st_mode)) {
1142 destroy_state (&state);
1143 if (examine_directory (menu, &state, buf, prefix) == 0)
1144 m_strcpy(LastDir, sizeof(LastDir), buf);
1146 mutt_error _("Error scanning directory.");
1148 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1149 mutt_menuDestroy (&menu);
1155 init_menu (&state, menu, title, sizeof (title), buffy);
1158 mutt_error (_("%s is not a directory."), buf);
1163 MAYBE_REDRAW (menu->redraw);
1168 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1169 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1170 regex_t *rx = p_new(regex_t, 1);
1175 /* assume that the user wants to see everything */
1177 m_strcpy(buf, sizeof(buf), ".");
1180 s = vskipspaces(s + 1);
1184 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1185 regerror (err, rx, buf, sizeof (buf));
1188 mutt_error ("%s", buf);
1191 m_strreplace(&Mask.pattern, buf);
1197 destroy_state (&state);
1198 if (state.imap_browse) {
1199 init_state (&state, NULL);
1200 state.imap_browse = 1;
1201 imap_browse (LastDir, &state);
1202 browser_sort (&state);
1203 menu->data = state.entry;
1204 init_menu (&state, menu, title, sizeof (title), buffy);
1207 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1208 init_menu (&state, menu, title, sizeof (title), buffy);
1210 mutt_error _("Error scanning directory.");
1212 mutt_menuDestroy (&menu);
1216 if (!state.entrylen) {
1217 mutt_error _("No files match the file mask");
1223 MAYBE_REDRAW (menu->redraw);
1227 case OP_SORT_REVERSE:
1231 int reverse = (i == OP_SORT_REVERSE);
1233 switch (mutt_multi_choice ((reverse) ?
1235 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1238 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1240 case -1: /* abort */
1244 case 1: /* (d)ate */
1245 BrowserSort = SORT_DATE;
1248 case 2: /* (a)lpha */
1249 BrowserSort = SORT_SUBJECT;
1252 case 3: /* si(z)e */
1253 BrowserSort = SORT_SIZE;
1256 case 4: /* do(n)'t sort */
1257 BrowserSort = SORT_ORDER;
1262 BrowserSort |= reverse ? SORT_REVERSE : 0;
1263 browser_sort (&state);
1264 menu->redraw = REDRAW_FULL;
1269 case OP_TOGGLE_MAILBOXES:
1273 destroy_state (&state);
1278 if (examine_mailboxes (menu, &state) == -1)
1281 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1282 init_state (&state, NULL);
1283 state.imap_browse = 1;
1284 imap_browse (LastDir, &state);
1285 browser_sort (&state);
1286 menu->data = state.entry;
1288 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1290 init_menu (&state, menu, title, sizeof (title), buffy);
1294 if (option (OPTFORCEBUFFYCHECK))
1299 case OP_BROWSER_NEW_FILE:
1301 snprintf (buf, sizeof (buf), "%s/", LastDir);
1302 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1304 m_strcpy(f, flen, buf);
1305 destroy_state (&state);
1306 mutt_menuDestroy (&menu);
1309 MAYBE_REDRAW (menu->redraw);
1312 case OP_BROWSER_VIEW_FILE:
1313 if (!state.entrylen) {
1314 mutt_error _("No files match the file mask");
1319 if (state.entry[menu->current].selectable) {
1320 m_strcpy(f, flen, state.entry[menu->current].name);
1321 destroy_state (&state);
1322 mutt_menuDestroy (&menu);
1326 if (S_ISDIR (state.entry[menu->current].mode) ||
1327 (S_ISLNK (state.entry[menu->current].mode) &&
1328 link_is_dir (LastDir, state.entry[menu->current].name))) {
1329 mutt_error _("Can't view a directory");
1335 char nbuf[_POSIX_PATH_MAX];
1337 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1338 state.entry[menu->current].name);
1339 b = mutt_make_file_attach (nbuf);
1341 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1342 mutt_free_body (&b);
1343 menu->redraw = REDRAW_FULL;
1346 mutt_error _("Error trying to view file");
1353 if (option (OPTNEWS)) {
1354 struct folder_file *f = &state.entry[menu->current];
1357 if (i == OP_CATCHUP)
1358 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1360 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1364 struct folder_file ff;
1365 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1368 folder.ff->name = f->name;
1369 folder.ff->st = NULL;
1370 folder.ff->is_new = nd->new;
1373 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1374 newsgroup_format_str, (unsigned long) &folder,
1375 M_FORMAT_ARROWCURSOR);
1376 f->desc = m_strdup(buffer); */
1377 if (menu->current + 1 < menu->max)
1379 menu->redraw = REDRAW_MOTION_RESYNCH;
1384 case OP_LOAD_ACTIVE:
1385 if (!option (OPTNEWS))
1392 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1393 if ((data = (NNTP_DATA *) tmp->data))
1397 nntp_get_active (CurrentNewsSrv);
1399 destroy_state (&state);
1401 examine_mailboxes (menu, &state);
1403 examine_directory (menu, &state, NULL, NULL);
1404 init_menu (&state, menu, title, sizeof (title), buffy);
1406 #endif /* USE_NNTP */
1408 case OP_BROWSER_SUBSCRIBE:
1409 case OP_BROWSER_UNSUBSCRIBE:
1411 case OP_SUBSCRIBE_PATTERN:
1412 case OP_UNSUBSCRIBE_PATTERN:
1413 if (option (OPTNEWS)) {
1414 regex_t *rx = p_new(regex_t, 1);
1416 int j = menu->current;
1418 NNTP_SERVER *news = CurrentNewsSrv;
1420 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1425 if (i == OP_SUBSCRIBE_PATTERN)
1426 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1428 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1429 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1434 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1435 regerror (err, rx, buf, sizeof (buf));
1438 mutt_error ("%s", buf);
1441 menu->redraw = REDRAW_FULL;
1444 else if (!state.entrylen) {
1445 mutt_error _("No newsgroups match the mask");
1450 for (; j < state.entrylen; j++) {
1451 struct folder_file *f = &state.entry[j];
1453 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1454 regexec (rx, f->name, 0, NULL, 0) == 0) {
1455 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1456 nd = mutt_newsgroup_subscribe (news, f->name);
1458 nd = mutt_newsgroup_unsubscribe (news, f->name);
1462 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1464 folder.name = f->name;
1466 folder.new = nd->new;
1469 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1470 newsgroup_format_str, (unsigned long) &folder,
1471 M_FORMAT_ARROWCURSOR);
1472 f->desc = m_strdup(buffer);
1475 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1476 if (menu->current + 1 < menu->max)
1478 menu->redraw = REDRAW_MOTION_RESYNCH;
1482 if (i == OP_SUBSCRIBE_PATTERN) {
1483 string_list_t *grouplist = NULL;
1486 grouplist = news->list;
1487 for (; grouplist; grouplist = grouplist->next) {
1488 nd = (NNTP_DATA *) grouplist->data;
1489 if (nd && nd->group && !nd->subscribed) {
1490 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1491 mutt_newsgroup_subscribe (news, nd->group);
1492 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1496 init_menu (&state, menu, title, sizeof (title), buffy);
1498 mutt_newsrc_update (news);
1499 nntp_clear_cacheindex (news);
1500 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1505 #endif /* USE_NNTP */
1507 if (i == OP_BROWSER_SUBSCRIBE)
1508 imap_subscribe (state.entry[menu->current].name, 1);
1510 imap_subscribe (state.entry[menu->current].name, 0);
1518 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);