2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 #include <lib-lib/lib-lib.h>
16 #include <lib-ui/curses.h>
17 #include <lib-ui/enter.h>
18 #include <lib-ui/menu.h>
19 #include <lib-ui/sidebar.h>
28 #include <imap/imap.h>
29 #include <imap/mx_imap.h>
34 static struct mapping_t FolderHelp[] = {
35 {N_("Exit"), OP_EXIT},
36 {N_("Chdir"), OP_CHANGE_DIRECTORY},
37 {N_("Mask"), OP_ENTER_MASK},
38 {N_("Help"), OP_HELP},
43 static struct mapping_t FolderNewsHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("List"), OP_TOGGLE_MAILBOXES},
46 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
47 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
48 {N_("Catchup"), OP_CATCHUP},
49 {N_("Mask"), OP_ENTER_MASK},
50 {N_("Help"), OP_HELP},
55 typedef struct folder_t {
56 struct folder_file *ff;
60 static char LastDir[_POSIX_PATH_MAX] = "";
61 static char LastDirBackup[_POSIX_PATH_MAX] = "";
63 /* Frees up the memory allocated for the local-global variables. */
64 static void destroy_state (struct browser_state *state)
68 for (c = 0; c < state->entrylen; c++) {
69 p_delete(&((state->entry)[c].name));
70 p_delete(&((state->entry)[c].desc));
71 p_delete(&((state->entry)[c].st));
73 p_delete(&state->folder);
74 p_delete(&state->entry);
77 static int browser_compare_subject (const void *a, const void *b)
79 struct folder_file *pa = (struct folder_file *) a;
80 struct folder_file *pb = (struct folder_file *) b;
82 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
84 return ((BrowserSort & SORT_REVERSE) ? -r : r);
87 static int browser_compare_date (const void *a, const void *b)
89 struct folder_file *pa = (struct folder_file *) a;
90 struct folder_file *pb = (struct folder_file *) b;
92 int r = pa->mtime - pb->mtime;
94 return ((BrowserSort & SORT_REVERSE) ? -r : r);
97 static int browser_compare_size (const void *a, const void *b)
99 struct folder_file *pa = (struct folder_file *) a;
100 struct folder_file *pb = (struct folder_file *) b;
102 int r = pa->size - pb->size;
104 return ((BrowserSort & SORT_REVERSE) ? -r : r);
107 static void browser_sort (struct browser_state *state)
109 int (*f) (const void *, const void *);
111 switch (BrowserSort & SORT_MASK) {
116 if (option (OPTNEWS))
119 f = browser_compare_date;
123 if (option (OPTNEWS))
126 f = browser_compare_size;
130 f = browser_compare_subject;
133 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
136 static int link_is_dir (const char *folder, const char *path)
139 char fullpath[_POSIX_PATH_MAX];
141 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
143 if (stat (fullpath, &st) == 0)
144 return (S_ISDIR (st.st_mode));
149 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
150 const char *src, const char *fmt,
151 const char *ifstring,
152 const char *elsestring,
153 unsigned long data, format_flag flags)
155 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
158 FOLDER *folder = (FOLDER *) data;
161 int optional = (flags & M_FORMAT_OPTIONAL);
165 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
166 snprintf (dest, destlen, tmp, folder->num + 1);
170 if (folder->ff->st != NULL) {
173 tnow - folder->ff->st->st_mtime <
174 31536000 ? "%b %d %H:%M" : "%b %d %Y";
175 strftime (date, sizeof (date), t_fmt,
176 localtime (&folder->ff->st->st_mtime));
177 mutt_format_s (dest, destlen, fmt, date);
180 mutt_format_s (dest, destlen, fmt, "");
187 if (folder->ff->imap)
188 s = NONULL (folder->ff->desc);
190 s = NONULL (folder->ff->name);
192 snprintf (fn, sizeof (fn), "%s%s", s,
193 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
194 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
195 ((folder->ff->st->st_mode & S_IXUSR) !=
196 0 ? "*" : ""))) : "");
198 mutt_format_s (dest, destlen, fmt, fn);
202 if (folder->ff->st != NULL) {
203 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
204 S_ISDIR (folder->ff->st->
205 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
206 st_mode) ? 'l' : '-'),
207 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
208 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
209 (folder->ff->st->st_mode & S_ISUID) !=
210 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
212 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
213 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
214 (folder->ff->st->st_mode & S_ISGID) !=
215 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
217 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
218 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
219 (folder->ff->st->st_mode & S_ISVTX) !=
220 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
222 mutt_format_s (dest, destlen, fmt, permission);
224 else if (folder->ff->imap) {
225 /* mark folders with subfolders AND mail */
226 snprintf (permission, sizeof (permission), "IMAP %c",
227 (folder->ff->inferiors
228 && folder->ff->selectable) ? '+' : ' ');
229 mutt_format_s (dest, destlen, fmt, permission);
232 mutt_format_s (dest, destlen, fmt, "");
236 if (folder->ff->st != NULL) {
237 if ((gr = getgrgid (folder->ff->st->st_gid)))
238 mutt_format_s (dest, destlen, fmt, gr->gr_name);
240 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
241 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
245 mutt_format_s (dest, destlen, fmt, "");
249 if (folder->ff->st != NULL) {
250 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
251 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
254 mutt_format_s (dest, destlen, fmt, "");
258 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
260 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
261 snprintf (dest, destlen, tmp, folder->ff->new);
263 else if (!folder->ff->new)
267 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
268 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
272 if (folder->ff->st != NULL) {
273 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
274 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
277 mutt_format_s (dest, destlen, fmt, "");
281 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
282 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
286 if (folder->ff->st != NULL) {
287 if ((pw = getpwuid (folder->ff->st->st_uid)))
288 mutt_format_s (dest, destlen, fmt, pw->pw_name);
290 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
291 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
295 mutt_format_s (dest, destlen, fmt, "");
299 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
300 snprintf (dest, destlen, tmp, op);
305 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
306 else if (flags & M_FORMAT_OPTIONAL)
307 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
313 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
314 const char *src, const char *fmt,
315 const char *ifstring,
316 const char *elsestring,
320 char fn[SHORT_STRING], tmp[SHORT_STRING];
321 FOLDER *folder = (FOLDER *) data;
325 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
326 snprintf (dest, destlen, tmp, folder->num + 1);
330 m_strcpy(fn, sizeof(fn), folder->ff->name);
331 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
332 snprintf (dest, destlen, tmp, fn);
336 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
337 if (folder->ff->nd->subscribed)
338 snprintf (dest, destlen, tmp, ' ');
340 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
344 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
345 if (folder->ff->nd->deleted)
346 snprintf (dest, destlen, tmp, 'D');
348 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
352 if (flags & M_FORMAT_OPTIONAL) {
353 if (folder->ff->nd->unread != 0)
354 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
357 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
360 else if (Context && Context->data == folder->ff->nd) {
361 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
362 snprintf (dest, destlen, tmp, Context->unread);
365 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
366 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
371 if (Context && Context->data == folder->ff->nd) {
372 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
373 snprintf (dest, destlen, tmp, Context->new);
375 else if (option (OPTMARKOLD) &&
376 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
377 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
378 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
379 snprintf (dest, destlen, tmp,
380 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
383 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
384 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
389 if (folder->ff->nd->desc != NULL) {
390 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
391 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
394 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
395 snprintf (dest, destlen, tmp, "");
401 #endif /* USE_NNTP */
404 static void add_folder (MUTTMENU * m, struct browser_state *state,
405 const char *name, const struct stat *s,
408 static void add_folder (MUTTMENU * m, struct browser_state *state,
409 const char *name, const struct stat *s,
413 if (state->entrylen == state->entrymax) {
414 /* need to allocate more space */
415 p_realloc(&state->entry, state->entrymax += 256);
416 p_clear(&state->entry[state->entrylen], 256);
418 m->data = state->entry;
422 (state->entry)[state->entrylen].mode = s->st_mode;
423 (state->entry)[state->entrylen].mtime = s->st_mtime;
424 (state->entry)[state->entrylen].size = s->st_size;
425 (state->entry)[state->entrylen].st = p_dup(s, 1);
428 (state->entry)[state->entrylen].new = new;
429 (state->entry)[state->entrylen].name = m_strdup(name);
430 (state->entry)[state->entrylen].desc = m_strdup(name);
431 (state->entry)[state->entrylen].imap = 0;
433 if (option (OPTNEWS))
434 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
439 static void init_state (struct browser_state *state, MUTTMENU * menu)
442 state->entrymax = 256;
443 state->entry = p_new(struct folder_file, state->entrymax);
444 state->imap_browse = 0;
446 menu->data = state->entry;
449 /* get list of all files/newsgroups with mask */
450 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
451 char *d, const char *prefix)
454 if (option (OPTNEWS)) {
457 NNTP_SERVER *news = CurrentNewsSrv;
459 /* buffy_check (0); */
460 init_state (state, menu);
462 for (tmp = news->list; tmp; tmp = tmp->next) {
463 if (!(data = (NNTP_DATA *) tmp->data))
465 nntp_sync_sidebar (data);
466 if (prefix && *prefix && strncmp (prefix, data->group,
467 m_strlen(prefix)) != 0)
469 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
472 add_folder (menu, state, data->group, NULL, data, data->new);
474 add_folder (menu, state, data->group, NULL, data->new);
480 #endif /* USE_NNTP */
485 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
488 while (stat (d, &s) == -1) {
489 if (errno == ENOENT) {
490 /* The last used directory is deleted, try to use the parent dir. */
491 char *c = strrchr (d, '/');
502 if (!S_ISDIR (s.st_mode)) {
503 mutt_error (_("%s is not a directory."), d);
509 if ((dp = opendir (d)) == NULL) {
514 init_state (state, menu);
516 while ((de = readdir (dp)) != NULL) {
517 if (m_strcmp(de->d_name, ".") == 0)
518 continue; /* we don't need . */
520 if (prefix && *prefix
521 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
523 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
526 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
527 if (lstat (buffer, &s) == -1)
530 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
531 (!S_ISLNK (s.st_mode)))
534 i = buffy_lookup (buffer);
536 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? Incoming.arr[i]->new : 0);
538 add_folder (menu, state, de->d_name, &s, i >= 0 ? Incoming.arr[i]->new : 0);
544 browser_sort (state);
548 /* get list of mailboxes/subscribed newsgroups */
549 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
552 char buffer[LONG_STRING];
555 if (option (OPTNEWS)) {
558 NNTP_SERVER *news = CurrentNewsSrv;
560 /* buffy_check (0); */
561 init_state (state, menu);
563 for (tmp = news->list; tmp; tmp = tmp->next) {
564 if ((data = (NNTP_DATA*) tmp->data) == NULL)
566 nntp_sync_sidebar (data);
567 if ((data->new || (data->subscribed &&
568 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
569 add_folder (menu, state, data->group, NULL, data, data->new);
583 init_state (state, menu);
585 for (i = 0; i < Incoming.len; i++) {
586 tmp = Incoming.arr[i];
587 tmp->magic = mx_get_magic (tmp->path);
588 if (tmp->magic == M_IMAP) {
590 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
592 add_folder (menu, state, tmp->path, NULL, tmp->new);
596 if (tmp->magic == M_POP) {
598 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
600 add_folder (menu, state, tmp->path, NULL, tmp->new);
605 if (tmp->magic == M_NNTP) {
606 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
610 if (lstat (tmp->path, &s) == -1)
613 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
614 (!S_ISLNK (s.st_mode)))
617 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
618 mutt_pretty_mailbox (buffer);
621 add_folder (menu, state, buffer, &s, NULL, tmp->new);
623 add_folder (menu, state, buffer, &s, tmp->new);
627 browser_sort (state);
631 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
634 if (option (OPTNEWS))
636 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
639 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
642 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
646 folder.ff = &((struct folder_file *) menu->data)[num];
650 if (option (OPTNEWS))
651 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
652 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
655 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
656 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
659 static void init_menu (struct browser_state *state, MUTTMENU * menu,
660 char *title, ssize_t titlelen, int buffy)
662 char path[_POSIX_PATH_MAX];
664 menu->max = state->entrylen;
666 if (menu->current >= menu->max)
667 menu->current = menu->max - 1;
668 if (menu->current < 0)
670 if (menu->top > menu->current)
676 if (option (OPTNEWS)) {
678 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
680 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
681 CurrentNewsSrv->conn->account.host);
686 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
688 m_strcpy(path, sizeof(path), LastDir);
689 mutt_pretty_mailbox (path);
690 if (state->imap_browse && option (OPTIMAPLSUB))
691 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
692 path, NONULL (Mask.pattern));
694 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
695 path, NONULL (Mask.pattern));
697 menu->redraw = REDRAW_FULL;
700 static int file_tag (MUTTMENU * menu, int n, int m)
702 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
705 if (S_ISDIR (ff->mode)
706 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
707 mutt_error _("Can't attach a directory!");
713 ff->tagged = (m >= 0 ? m : !ff->tagged);
715 return ff->tagged - ot;
718 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
721 char buf[_POSIX_PATH_MAX];
722 char prefix[_POSIX_PATH_MAX] = "";
723 char helpstr[SHORT_STRING];
725 struct browser_state state;
728 int i, killPrefix = 0;
729 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
730 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
731 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
733 buffy = buffy && folder;
738 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
741 if (option (OPTNEWS)) {
743 m_strcpy(prefix, sizeof(prefix), f);
747 /* default state for news reader mode is browse subscribed newsgroups */
749 for (list = CurrentNewsSrv->list; list; list = list->next) {
750 NNTP_DATA *data = (NNTP_DATA *) list->data;
752 if (data && data->subscribed) {
762 mutt_expand_path (f, flen);
763 if (imap_is_magic (f, NULL) == M_IMAP) {
764 init_state (&state, NULL);
765 state.imap_browse = 1;
766 if (!imap_browse (f, &state))
767 m_strcpy(LastDir, sizeof(LastDir), state.folder);
770 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
773 i = MIN(ssizeof(LastDir) - 1, i);
774 m_strcpy(LastDir, sizeof(LastDir), f);
777 getcwd(LastDir, sizeof(LastDir));
778 m_strcat(LastDir, sizeof(LastDir), "/");
779 m_strncat(LastDir, sizeof(LastDir), f, i);
784 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
786 getcwd (LastDir, sizeof (LastDir));
789 if (i <= 0 && f[0] != '/')
790 m_strcpy(prefix, sizeof(prefix), f);
792 m_strcpy(prefix, sizeof(prefix), f + i + 1);
798 getcwd (LastDir, sizeof (LastDir));
799 else if (!LastDir[0])
800 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
802 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
803 init_state (&state, NULL);
804 state.imap_browse = 1;
805 imap_browse (LastDir, &state);
806 browser_sort (&state);
813 if (examine_mailboxes (NULL, &state) == -1)
817 if (!state.imap_browse)
818 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
821 menu = mutt_new_menu ();
822 menu->menu = MENU_FOLDER;
823 menu->make_entry = folder_entry;
824 menu->search = select_file_search;
826 menu->data = state.entry;
828 menu->tag = file_tag;
830 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
832 (option (OPTNEWS)) ? FolderNewsHelp :
836 init_menu (&state, menu, title, sizeof (title), buffy);
839 switch (i = mutt_menuLoop (menu)) {
840 case OP_GENERIC_SELECT_ENTRY:
842 if (!state.entrylen) {
843 mutt_error _("No files match the file mask");
848 if (S_ISDIR (state.entry[menu->current].mode) ||
849 (S_ISLNK (state.entry[menu->current].mode) &&
850 link_is_dir (LastDir, state.entry[menu->current].name))
851 || state.entry[menu->current].inferiors
853 /* make sure this isn't a MH or maildir mailbox */
855 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
856 mutt_expand_path (buf, sizeof (buf));
858 else if (state.imap_browse) {
859 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
862 mutt_concat_path(buf, sizeof(buf), LastDir,
863 state.entry[menu->current].name);
865 if ((mx_get_magic (buf) <= 0)
866 || state.entry[menu->current].inferiors)
868 char OldLastDir[_POSIX_PATH_MAX];
870 /* save the old directory */
871 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
873 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
874 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
875 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
877 char *p = strrchr (LastDir + 1, '/');
882 if (LastDir[0] == '/')
885 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
890 m_strcpy(LastDir, sizeof(LastDir),
891 state.entry[menu->current].name);
892 mutt_expand_path (LastDir, sizeof (LastDir));
894 else if (state.imap_browse) {
898 m_strcpy(LastDir, sizeof(LastDir),
899 state.entry[menu->current].name);
900 /* tack on delimiter here */
901 n = m_strlen(LastDir) + 1;
903 /* special case "" needs no delimiter */
904 url_parse_ciss (&url, state.entry[menu->current].name);
906 (state.entry[menu->current].delim != '\0') &&
907 (n < ssizeof (LastDir))) {
909 LastDir[n - 1] = state.entry[menu->current].delim;
913 char tmp[_POSIX_PATH_MAX];
915 mutt_concat_path(tmp, sizeof(tmp), LastDir,
916 state.entry[menu->current].name);
917 m_strcpy(LastDir, sizeof(LastDir), tmp);
920 destroy_state (&state);
926 if (state.imap_browse) {
927 init_state (&state, NULL);
928 state.imap_browse = 1;
929 imap_browse (LastDir, &state);
930 browser_sort (&state);
931 menu->data = state.entry;
934 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
935 /* try to restore the old values */
936 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
937 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
938 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
944 init_menu (&state, menu, title, sizeof (title), buffy);
950 if (buffy || option (OPTNEWS)) /* news have not path */
955 m_strcpy(f, flen, state.entry[menu->current].name);
956 mutt_expand_path (f, flen);
958 else if (state.imap_browse)
959 m_strcpy(f, flen, state.entry[menu->current].name);
961 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
963 /* Fall through to OP_EXIT */
973 *numfiles = menu->tagged;
974 tfiles = p_new(char *, *numfiles);
975 for (h = 0, j = 0; h < state.entrylen; i++) {
976 struct folder_file ff = state.entry[i];
977 char full[_POSIX_PATH_MAX];
980 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
981 mutt_expand_path (full, sizeof (full));
982 tfiles[j++] = m_strdup(full);
987 else if (f[0]) { /* no tagged entries. return selected entry */
989 tfiles = p_new(char *, *numfiles);
990 mutt_expand_path (f, flen);
991 tfiles[0] = m_strdup(f);
996 destroy_state (&state);
997 mutt_menuDestroy (&menu);
1000 case OP_BROWSER_TELL:
1002 mutt_message ("%s", state.entry[menu->current].name);
1005 case OP_BROWSER_TOGGLE_LSUB:
1006 if (option (OPTIMAPLSUB)) {
1007 unset_option (OPTIMAPLSUB);
1010 set_option (OPTIMAPLSUB);
1012 mutt_ungetch (0, OP_CHECK_NEW);
1015 case OP_CREATE_MAILBOX:
1016 if (!state.imap_browse)
1017 mutt_error (_("Create is only supported for IMAP mailboxes"));
1019 imap_mailbox_create (LastDir);
1020 /* TODO: find a way to detect if the new folder would appear in
1021 * this window, and insert it without starting over. */
1022 destroy_state (&state);
1023 init_state (&state, NULL);
1024 state.imap_browse = 1;
1025 imap_browse (LastDir, &state);
1026 browser_sort (&state);
1027 menu->data = state.entry;
1030 init_menu (&state, menu, title, sizeof (title), buffy);
1031 MAYBE_REDRAW (menu->redraw);
1035 case OP_RENAME_MAILBOX:
1036 if (!state.entry[menu->current].imap)
1037 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1039 int nentry = menu->current;
1041 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1042 destroy_state (&state);
1043 init_state (&state, NULL);
1044 state.imap_browse = 1;
1045 imap_browse (LastDir, &state);
1046 browser_sort (&state);
1047 menu->data = state.entry;
1050 init_menu (&state, menu, title, sizeof (title), buffy);
1051 MAYBE_REDRAW (menu->redraw);
1056 case OP_DELETE_MAILBOX:
1057 if (!state.entry[menu->current].imap)
1058 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1060 char msg[SHORT_STRING];
1062 int nentry = menu->current;
1064 imap_parse_path (state.entry[nentry].name, &mx);
1065 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1067 if (mutt_yesorno (msg, M_NO) == M_YES) {
1068 if (!imap_delete_mailbox (Context, mx)) {
1069 /* free the mailbox from the browser */
1070 p_delete(&((state.entry)[nentry].name));
1071 p_delete(&((state.entry)[nentry].desc));
1072 /* and move all other entries up */
1073 if (nentry + 1 < state.entrylen)
1074 memmove (state.entry + nentry, state.entry + nentry + 1,
1075 sizeof (struct folder_file) * (state.entrylen -
1078 mutt_message _("Mailbox deleted.");
1080 init_menu (&state, menu, title, sizeof (title), buffy);
1081 MAYBE_REDRAW (menu->redraw);
1085 mutt_message _("Mailbox not deleted.");
1090 case OP_CHANGE_DIRECTORY:
1093 if (option (OPTNEWS))
1097 m_strcpy(buf, sizeof(buf), LastDir);
1098 if (!state.imap_browse)
1100 /* add '/' at the end of the directory name if not already there */
1101 ssize_t len = m_strlen(LastDir);
1103 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1107 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1110 mutt_expand_path (buf, sizeof (buf));
1111 if (imap_is_magic (buf, NULL) == M_IMAP) {
1112 m_strcpy(LastDir, sizeof(LastDir), buf);
1113 destroy_state (&state);
1114 init_state (&state, NULL);
1115 state.imap_browse = 1;
1116 imap_browse (LastDir, &state);
1117 browser_sort (&state);
1118 menu->data = state.entry;
1121 init_menu (&state, menu, title, sizeof (title), buffy);
1124 if (stat (buf, &st) == 0) {
1125 if (S_ISDIR (st.st_mode)) {
1126 destroy_state (&state);
1127 if (examine_directory (menu, &state, buf, prefix) == 0)
1128 m_strcpy(LastDir, sizeof(LastDir), buf);
1130 mutt_error _("Error scanning directory.");
1132 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1133 mutt_menuDestroy (&menu);
1139 init_menu (&state, menu, title, sizeof (title), buffy);
1142 mutt_error (_("%s is not a directory."), buf);
1147 MAYBE_REDRAW (menu->redraw);
1152 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1153 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1154 regex_t *rx = p_new(regex_t, 1);
1159 /* assume that the user wants to see everything */
1161 m_strcpy(buf, sizeof(buf), ".");
1164 s = vskipspaces(s + 1);
1168 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1169 regerror (err, rx, buf, sizeof (buf));
1172 mutt_error ("%s", buf);
1175 m_strreplace(&Mask.pattern, buf);
1181 destroy_state (&state);
1182 if (state.imap_browse) {
1183 init_state (&state, NULL);
1184 state.imap_browse = 1;
1185 imap_browse (LastDir, &state);
1186 browser_sort (&state);
1187 menu->data = state.entry;
1188 init_menu (&state, menu, title, sizeof (title), buffy);
1191 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1192 init_menu (&state, menu, title, sizeof (title), buffy);
1194 mutt_error _("Error scanning directory.");
1196 mutt_menuDestroy (&menu);
1200 if (!state.entrylen) {
1201 mutt_error _("No files match the file mask");
1207 MAYBE_REDRAW (menu->redraw);
1211 case OP_SORT_REVERSE:
1215 int reverse = (i == OP_SORT_REVERSE);
1217 switch (mutt_multi_choice ((reverse) ?
1219 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1222 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1224 case -1: /* abort */
1228 case 1: /* (d)ate */
1229 BrowserSort = SORT_DATE;
1232 case 2: /* (a)lpha */
1233 BrowserSort = SORT_SUBJECT;
1236 case 3: /* si(z)e */
1237 BrowserSort = SORT_SIZE;
1240 case 4: /* do(n)'t sort */
1241 BrowserSort = SORT_ORDER;
1246 BrowserSort |= reverse ? SORT_REVERSE : 0;
1247 browser_sort (&state);
1248 menu->redraw = REDRAW_FULL;
1253 case OP_TOGGLE_MAILBOXES:
1257 destroy_state (&state);
1262 if (examine_mailboxes (menu, &state) == -1)
1265 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1266 init_state (&state, NULL);
1267 state.imap_browse = 1;
1268 imap_browse (LastDir, &state);
1269 browser_sort (&state);
1270 menu->data = state.entry;
1272 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1274 init_menu (&state, menu, title, sizeof (title), buffy);
1278 if (option (OPTFORCEBUFFYCHECK))
1283 case OP_BROWSER_NEW_FILE:
1285 snprintf (buf, sizeof (buf), "%s/", LastDir);
1286 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1288 m_strcpy(f, flen, buf);
1289 destroy_state (&state);
1290 mutt_menuDestroy (&menu);
1293 MAYBE_REDRAW (menu->redraw);
1296 case OP_BROWSER_VIEW_FILE:
1297 if (!state.entrylen) {
1298 mutt_error _("No files match the file mask");
1303 if (state.entry[menu->current].selectable) {
1304 m_strcpy(f, flen, state.entry[menu->current].name);
1305 destroy_state (&state);
1306 mutt_menuDestroy (&menu);
1310 if (S_ISDIR (state.entry[menu->current].mode) ||
1311 (S_ISLNK (state.entry[menu->current].mode) &&
1312 link_is_dir (LastDir, state.entry[menu->current].name))) {
1313 mutt_error _("Can't view a directory");
1319 char nbuf[_POSIX_PATH_MAX];
1321 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1322 state.entry[menu->current].name);
1323 b = mutt_make_file_attach (nbuf);
1325 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1327 menu->redraw = REDRAW_FULL;
1330 mutt_error _("Error trying to view file");
1337 if (option (OPTNEWS)) {
1338 struct folder_file *folder_f = &state.entry[menu->current];
1341 if (i == OP_CATCHUP)
1342 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1344 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1347 if (menu->current + 1 < menu->max)
1349 menu->redraw = REDRAW_MOTION_RESYNCH;
1354 case OP_LOAD_ACTIVE:
1355 if (!option (OPTNEWS))
1362 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1363 if ((data = (NNTP_DATA *) tmp->data))
1367 nntp_get_active (CurrentNewsSrv);
1369 destroy_state (&state);
1371 examine_mailboxes (menu, &state);
1373 examine_directory (menu, &state, NULL, NULL);
1374 init_menu (&state, menu, title, sizeof (title), buffy);
1376 #endif /* USE_NNTP */
1378 case OP_BROWSER_SUBSCRIBE:
1379 case OP_BROWSER_UNSUBSCRIBE:
1381 case OP_SUBSCRIBE_PATTERN:
1382 case OP_UNSUBSCRIBE_PATTERN:
1383 if (option (OPTNEWS)) {
1384 regex_t *rx = p_new(regex_t, 1);
1386 int j = menu->current;
1388 NNTP_SERVER *news = CurrentNewsSrv;
1390 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1395 if (i == OP_SUBSCRIBE_PATTERN)
1396 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1398 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1399 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1404 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1405 regerror (err, rx, buf, sizeof (buf));
1408 mutt_error ("%s", buf);
1411 menu->redraw = REDRAW_FULL;
1414 else if (!state.entrylen) {
1415 mutt_error _("No newsgroups match the mask");
1420 for (; j < state.entrylen; j++) {
1421 struct folder_file *folderf = &state.entry[j];
1423 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1424 regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1425 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1426 nd = mutt_newsgroup_subscribe (news, folderf->name);
1428 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1430 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1431 if (menu->current + 1 < menu->max)
1433 menu->redraw = REDRAW_MOTION_RESYNCH;
1437 if (i == OP_SUBSCRIBE_PATTERN) {
1438 string_list_t *grouplist = NULL;
1441 grouplist = news->list;
1442 for (; grouplist; grouplist = grouplist->next) {
1443 nd = (NNTP_DATA *) grouplist->data;
1444 if (nd && nd->group && !nd->subscribed) {
1445 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1446 mutt_newsgroup_subscribe (news, nd->group);
1447 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1451 init_menu (&state, menu, title, sizeof (title), buffy);
1453 mutt_newsrc_update (news);
1454 nntp_clear_cacheindex (news);
1455 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1460 #endif /* USE_NNTP */
1462 if (i == OP_BROWSER_SUBSCRIBE)
1463 imap_subscribe (state.entry[menu->current].name, 1);
1465 imap_subscribe (state.entry[menu->current].name, 0);
1473 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);