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/lib-lib.h>
26 #include <lib-ui/curses.h>
27 #include <lib-ui/enter.h>
28 #include <lib-ui/menu.h>
29 #include <lib-ui/sidebar.h>
38 #include <imap/imap.h>
39 #include <imap/mx_imap.h>
46 static struct mapping_t FolderHelp[] = {
47 {N_("Exit"), OP_EXIT},
48 {N_("Chdir"), OP_CHANGE_DIRECTORY},
49 {N_("Mask"), OP_ENTER_MASK},
50 {N_("Help"), OP_HELP},
55 static struct mapping_t FolderNewsHelp[] = {
56 {N_("Exit"), OP_EXIT},
57 {N_("List"), OP_TOGGLE_MAILBOXES},
58 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
59 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
60 {N_("Catchup"), OP_CATCHUP},
61 {N_("Mask"), OP_ENTER_MASK},
62 {N_("Help"), OP_HELP},
67 typedef struct folder_t {
68 struct folder_file *ff;
72 static char LastDir[_POSIX_PATH_MAX] = "";
73 static char LastDirBackup[_POSIX_PATH_MAX] = "";
75 /* Frees up the memory allocated for the local-global variables. */
76 static void destroy_state (struct browser_state *state)
80 for (c = 0; c < state->entrylen; c++) {
81 p_delete(&((state->entry)[c].name));
82 p_delete(&((state->entry)[c].desc));
83 p_delete(&((state->entry)[c].st));
85 p_delete(&state->folder);
86 p_delete(&state->entry);
89 static int browser_compare_subject (const void *a, const void *b)
91 struct folder_file *pa = (struct folder_file *) a;
92 struct folder_file *pb = (struct folder_file *) b;
94 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
96 return ((BrowserSort & SORT_REVERSE) ? -r : r);
99 static int browser_compare_date (const void *a, const void *b)
101 struct folder_file *pa = (struct folder_file *) a;
102 struct folder_file *pb = (struct folder_file *) b;
104 int r = pa->mtime - pb->mtime;
106 return ((BrowserSort & SORT_REVERSE) ? -r : r);
109 static int browser_compare_size (const void *a, const void *b)
111 struct folder_file *pa = (struct folder_file *) a;
112 struct folder_file *pb = (struct folder_file *) b;
114 int r = pa->size - pb->size;
116 return ((BrowserSort & SORT_REVERSE) ? -r : r);
119 static void browser_sort (struct browser_state *state)
121 int (*f) (const void *, const void *);
123 switch (BrowserSort & SORT_MASK) {
128 if (option (OPTNEWS))
131 f = browser_compare_date;
135 if (option (OPTNEWS))
138 f = browser_compare_size;
142 f = browser_compare_subject;
145 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
148 static int link_is_dir (const char *folder, const char *path)
151 char fullpath[_POSIX_PATH_MAX];
153 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
155 if (stat (fullpath, &st) == 0)
156 return (S_ISDIR (st.st_mode));
161 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
162 const char *src, const char *fmt,
163 const char *ifstring,
164 const char *elsestring,
165 unsigned long data, format_flag flags)
167 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
170 FOLDER *folder = (FOLDER *) data;
173 int optional = (flags & M_FORMAT_OPTIONAL);
177 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
178 snprintf (dest, destlen, tmp, folder->num + 1);
182 if (folder->ff->st != NULL) {
185 tnow - folder->ff->st->st_mtime <
186 31536000 ? "%b %d %H:%M" : "%b %d %Y";
187 strftime (date, sizeof (date), t_fmt,
188 localtime (&folder->ff->st->st_mtime));
189 mutt_format_s (dest, destlen, fmt, date);
192 mutt_format_s (dest, destlen, fmt, "");
199 if (folder->ff->imap)
200 s = NONULL (folder->ff->desc);
202 s = NONULL (folder->ff->name);
204 snprintf (fn, sizeof (fn), "%s%s", s,
205 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
206 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
207 ((folder->ff->st->st_mode & S_IXUSR) !=
208 0 ? "*" : ""))) : "");
210 mutt_format_s (dest, destlen, fmt, fn);
214 if (folder->ff->st != NULL) {
215 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
216 S_ISDIR (folder->ff->st->
217 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
218 st_mode) ? 'l' : '-'),
219 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
220 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
221 (folder->ff->st->st_mode & S_ISUID) !=
222 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
224 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
225 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
226 (folder->ff->st->st_mode & S_ISGID) !=
227 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
229 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
230 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
231 (folder->ff->st->st_mode & S_ISVTX) !=
232 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
234 mutt_format_s (dest, destlen, fmt, permission);
236 else if (folder->ff->imap) {
237 /* mark folders with subfolders AND mail */
238 snprintf (permission, sizeof (permission), "IMAP %c",
239 (folder->ff->inferiors
240 && folder->ff->selectable) ? '+' : ' ');
241 mutt_format_s (dest, destlen, fmt, permission);
244 mutt_format_s (dest, destlen, fmt, "");
248 if (folder->ff->st != NULL) {
249 if ((gr = getgrgid (folder->ff->st->st_gid)))
250 mutt_format_s (dest, destlen, fmt, gr->gr_name);
252 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
253 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
257 mutt_format_s (dest, destlen, fmt, "");
261 if (folder->ff->st != NULL) {
262 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
263 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
266 mutt_format_s (dest, destlen, fmt, "");
270 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
272 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
273 snprintf (dest, destlen, tmp, folder->ff->new);
275 else if (!folder->ff->new)
279 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
280 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
284 if (folder->ff->st != NULL) {
285 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
286 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
289 mutt_format_s (dest, destlen, fmt, "");
293 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
294 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
298 if (folder->ff->st != NULL) {
299 if ((pw = getpwuid (folder->ff->st->st_uid)))
300 mutt_format_s (dest, destlen, fmt, pw->pw_name);
302 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
303 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
307 mutt_format_s (dest, destlen, fmt, "");
311 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
312 snprintf (dest, destlen, tmp, op);
317 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
318 else if (flags & M_FORMAT_OPTIONAL)
319 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
325 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
326 const char *src, const char *fmt,
327 const char *ifstring,
328 const char *elsestring,
332 char fn[SHORT_STRING], tmp[SHORT_STRING];
333 FOLDER *folder = (FOLDER *) data;
337 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
338 snprintf (dest, destlen, tmp, folder->num + 1);
342 m_strcpy(fn, sizeof(fn), folder->ff->name);
343 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
344 snprintf (dest, destlen, tmp, fn);
348 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
349 if (folder->ff->nd->subscribed)
350 snprintf (dest, destlen, tmp, ' ');
352 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
356 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
357 if (folder->ff->nd->deleted)
358 snprintf (dest, destlen, tmp, 'D');
360 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
364 if (flags & M_FORMAT_OPTIONAL) {
365 if (folder->ff->nd->unread != 0)
366 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
369 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
372 else if (Context && Context->data == folder->ff->nd) {
373 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
374 snprintf (dest, destlen, tmp, Context->unread);
377 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
378 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
383 if (Context && Context->data == folder->ff->nd) {
384 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
385 snprintf (dest, destlen, tmp, Context->new);
387 else if (option (OPTMARKOLD) &&
388 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
389 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
390 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
391 snprintf (dest, destlen, tmp,
392 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
395 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
396 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
401 if (folder->ff->nd->desc != NULL) {
402 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
403 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
406 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
407 snprintf (dest, destlen, tmp, "");
413 #endif /* USE_NNTP */
416 static void add_folder (MUTTMENU * m, struct browser_state *state,
417 const char *name, const struct stat *s,
420 static void add_folder (MUTTMENU * m, struct browser_state *state,
421 const char *name, const struct stat *s,
425 if (state->entrylen == state->entrymax) {
426 /* need to allocate more space */
427 p_realloc(&state->entry, state->entrymax += 256);
428 p_clear(&state->entry[state->entrylen], 256);
430 m->data = state->entry;
434 (state->entry)[state->entrylen].mode = s->st_mode;
435 (state->entry)[state->entrylen].mtime = s->st_mtime;
436 (state->entry)[state->entrylen].size = s->st_size;
437 (state->entry)[state->entrylen].st = p_dup(s, 1);
440 (state->entry)[state->entrylen].new = new;
441 (state->entry)[state->entrylen].name = m_strdup(name);
442 (state->entry)[state->entrylen].desc = m_strdup(name);
443 (state->entry)[state->entrylen].imap = 0;
445 if (option (OPTNEWS))
446 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
451 static void init_state (struct browser_state *state, MUTTMENU * menu)
454 state->entrymax = 256;
455 state->entry = p_new(struct folder_file, state->entrymax);
456 state->imap_browse = 0;
458 menu->data = state->entry;
461 /* get list of all files/newsgroups with mask */
462 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
463 char *d, const char *prefix)
466 if (option (OPTNEWS)) {
469 NNTP_SERVER *news = CurrentNewsSrv;
471 /* buffy_check (0); */
472 init_state (state, menu);
474 for (tmp = news->list; tmp; tmp = tmp->next) {
475 if (!(data = (NNTP_DATA *) tmp->data))
477 nntp_sync_sidebar (data);
478 if (prefix && *prefix && strncmp (prefix, data->group,
479 m_strlen(prefix)) != 0)
481 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
484 add_folder (menu, state, data->group, NULL, data, data->new);
486 add_folder (menu, state, data->group, NULL, data->new);
489 sidebar_draw (CurrentMenu);
492 #endif /* USE_NNTP */
497 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
500 while (stat (d, &s) == -1) {
501 if (errno == ENOENT) {
502 /* The last used directory is deleted, try to use the parent dir. */
503 char *c = strrchr (d, '/');
514 if (!S_ISDIR (s.st_mode)) {
515 mutt_error (_("%s is not a directory."), d);
521 if ((dp = opendir (d)) == NULL) {
526 init_state (state, menu);
528 while ((de = readdir (dp)) != NULL) {
529 if (m_strcmp(de->d_name, ".") == 0)
530 continue; /* we don't need . */
532 if (prefix && *prefix
533 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
535 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
538 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
539 if (lstat (buffer, &s) == -1)
542 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
543 (!S_ISLNK (s.st_mode)))
546 i = buffy_lookup (buffer);
548 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
550 add_folder (menu, state, de->d_name, &s, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
555 sidebar_draw (CurrentMenu);
556 browser_sort (state);
560 /* get list of mailboxes/subscribed newsgroups */
561 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
564 char buffer[LONG_STRING];
567 if (option (OPTNEWS)) {
570 NNTP_SERVER *news = CurrentNewsSrv;
572 /* buffy_check (0); */
573 init_state (state, menu);
575 for (tmp = news->list; tmp; tmp = tmp->next) {
576 if ((data = (NNTP_DATA*) tmp->data) == NULL)
578 nntp_sync_sidebar (data);
579 if ((data->new || (data->subscribed &&
580 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
581 add_folder (menu, state, data->group, NULL, data, data->new);
583 sidebar_draw (CurrentMenu);
595 init_state (state, menu);
597 for (i = 0; i < Incoming->length; i++) {
598 tmp = (BUFFY*) Incoming->data[i];
599 tmp->magic = mx_get_magic (tmp->path);
600 if (tmp->magic == M_IMAP) {
602 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
604 add_folder (menu, state, tmp->path, NULL, tmp->new);
608 if (tmp->magic == M_POP) {
610 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
612 add_folder (menu, state, tmp->path, NULL, tmp->new);
617 if (tmp->magic == M_NNTP) {
618 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
622 if (lstat (tmp->path, &s) == -1)
625 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
626 (!S_ISLNK (s.st_mode)))
629 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
630 mutt_pretty_mailbox (buffer);
633 add_folder (menu, state, buffer, &s, NULL, tmp->new);
635 add_folder (menu, state, buffer, &s, tmp->new);
639 browser_sort (state);
643 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
646 if (option (OPTNEWS))
648 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
651 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
654 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
658 folder.ff = &((struct folder_file *) menu->data)[num];
662 if (option (OPTNEWS))
663 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
664 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
667 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
668 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
671 static void init_menu (struct browser_state *state, MUTTMENU * menu,
672 char *title, ssize_t titlelen, int buffy)
674 char path[_POSIX_PATH_MAX];
676 menu->max = state->entrylen;
678 if (menu->current >= menu->max)
679 menu->current = menu->max - 1;
680 if (menu->current < 0)
682 if (menu->top > menu->current)
688 if (option (OPTNEWS)) {
690 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
692 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
693 CurrentNewsSrv->conn->account.host);
698 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
700 m_strcpy(path, sizeof(path), LastDir);
701 mutt_pretty_mailbox (path);
702 if (state->imap_browse && option (OPTIMAPLSUB))
703 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
704 path, NONULL (Mask.pattern));
706 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
707 path, NONULL (Mask.pattern));
709 menu->redraw = REDRAW_FULL;
712 static int file_tag (MUTTMENU * menu, int n, int m)
714 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
717 if (S_ISDIR (ff->mode)
718 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
719 mutt_error _("Can't attach a directory!");
725 ff->tagged = (m >= 0 ? m : !ff->tagged);
727 return ff->tagged - ot;
730 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
733 char buf[_POSIX_PATH_MAX];
734 char prefix[_POSIX_PATH_MAX] = "";
735 char helpstr[SHORT_STRING];
737 struct browser_state state;
740 int i, killPrefix = 0;
741 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
742 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
743 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
745 buffy = buffy && folder;
750 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
753 if (option (OPTNEWS)) {
755 m_strcpy(prefix, sizeof(prefix), f);
759 /* default state for news reader mode is browse subscribed newsgroups */
761 for (list = CurrentNewsSrv->list; list; list = list->next) {
762 NNTP_DATA *data = (NNTP_DATA *) list->data;
764 if (data && data->subscribed) {
774 mutt_expand_path (f, flen);
775 if (imap_is_magic (f, NULL) == M_IMAP) {
776 init_state (&state, NULL);
777 state.imap_browse = 1;
778 if (!imap_browse (f, &state))
779 m_strcpy(LastDir, sizeof(LastDir), state.folder);
782 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
785 i = MIN(ssizeof(LastDir) - 1, i);
786 m_strcpy(LastDir, sizeof(LastDir), f);
789 getcwd(LastDir, sizeof(LastDir));
790 m_strcat(LastDir, sizeof(LastDir), "/");
791 m_strncat(LastDir, sizeof(LastDir), f, i);
796 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
798 getcwd (LastDir, sizeof (LastDir));
801 if (i <= 0 && f[0] != '/')
802 m_strcpy(prefix, sizeof(prefix), f);
804 m_strcpy(prefix, sizeof(prefix), f + i + 1);
810 getcwd (LastDir, sizeof (LastDir));
811 else if (!LastDir[0])
812 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);
825 if (examine_mailboxes (NULL, &state) == -1)
829 if (!state.imap_browse)
830 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
833 menu = mutt_new_menu ();
834 menu->menu = MENU_FOLDER;
835 menu->make_entry = folder_entry;
836 menu->search = select_file_search;
838 menu->data = state.entry;
840 menu->tag = file_tag;
842 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
844 (option (OPTNEWS)) ? FolderNewsHelp :
848 init_menu (&state, menu, title, sizeof (title), buffy);
851 switch (i = mutt_menuLoop (menu)) {
852 case OP_GENERIC_SELECT_ENTRY:
854 if (!state.entrylen) {
855 mutt_error _("No files match the file mask");
860 if (S_ISDIR (state.entry[menu->current].mode) ||
861 (S_ISLNK (state.entry[menu->current].mode) &&
862 link_is_dir (LastDir, state.entry[menu->current].name))
863 || state.entry[menu->current].inferiors
865 /* make sure this isn't a MH or maildir mailbox */
867 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
868 mutt_expand_path (buf, sizeof (buf));
870 else if (state.imap_browse) {
871 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
874 mutt_concat_path(buf, sizeof(buf), LastDir,
875 state.entry[menu->current].name);
877 if ((mx_get_magic (buf) <= 0)
878 || state.entry[menu->current].inferiors)
880 char OldLastDir[_POSIX_PATH_MAX];
882 /* save the old directory */
883 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
885 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
886 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
887 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
889 char *p = strrchr (LastDir + 1, '/');
894 if (LastDir[0] == '/')
897 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
902 m_strcpy(LastDir, sizeof(LastDir),
903 state.entry[menu->current].name);
904 mutt_expand_path (LastDir, sizeof (LastDir));
906 else if (state.imap_browse) {
910 m_strcpy(LastDir, sizeof(LastDir),
911 state.entry[menu->current].name);
912 /* tack on delimiter here */
913 n = m_strlen(LastDir) + 1;
915 /* special case "" needs no delimiter */
916 url_parse_ciss (&url, state.entry[menu->current].name);
918 (state.entry[menu->current].delim != '\0') &&
919 (n < ssizeof (LastDir))) {
921 LastDir[n - 1] = state.entry[menu->current].delim;
925 char tmp[_POSIX_PATH_MAX];
927 mutt_concat_path(tmp, sizeof(tmp), LastDir,
928 state.entry[menu->current].name);
929 m_strcpy(LastDir, sizeof(LastDir), tmp);
932 destroy_state (&state);
938 if (state.imap_browse) {
939 init_state (&state, NULL);
940 state.imap_browse = 1;
941 imap_browse (LastDir, &state);
942 browser_sort (&state);
943 menu->data = state.entry;
946 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
947 /* try to restore the old values */
948 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
949 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
950 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
956 init_menu (&state, menu, title, sizeof (title), buffy);
962 if (buffy || option (OPTNEWS)) /* news have not path */
967 m_strcpy(f, flen, state.entry[menu->current].name);
968 mutt_expand_path (f, flen);
970 else if (state.imap_browse)
971 m_strcpy(f, flen, state.entry[menu->current].name);
973 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
975 /* Fall through to OP_EXIT */
985 *numfiles = menu->tagged;
986 tfiles = p_new(char *, *numfiles);
987 for (h = 0, j = 0; h < state.entrylen; i++) {
988 struct folder_file ff = state.entry[i];
989 char full[_POSIX_PATH_MAX];
992 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
993 mutt_expand_path (full, sizeof (full));
994 tfiles[j++] = m_strdup(full);
999 else if (f[0]) { /* no tagged entries. return selected entry */
1001 tfiles = p_new(char *, *numfiles);
1002 mutt_expand_path (f, flen);
1003 tfiles[0] = m_strdup(f);
1008 destroy_state (&state);
1009 mutt_menuDestroy (&menu);
1012 case OP_BROWSER_TELL:
1014 mutt_message ("%s", state.entry[menu->current].name);
1017 case OP_BROWSER_TOGGLE_LSUB:
1018 if (option (OPTIMAPLSUB)) {
1019 unset_option (OPTIMAPLSUB);
1022 set_option (OPTIMAPLSUB);
1024 mutt_ungetch (0, OP_CHECK_NEW);
1027 case OP_CREATE_MAILBOX:
1028 if (!state.imap_browse)
1029 mutt_error (_("Create is only supported for IMAP mailboxes"));
1031 imap_mailbox_create (LastDir);
1032 /* TODO: find a way to detect if the new folder would appear in
1033 * this window, and insert it without starting over. */
1034 destroy_state (&state);
1035 init_state (&state, NULL);
1036 state.imap_browse = 1;
1037 imap_browse (LastDir, &state);
1038 browser_sort (&state);
1039 menu->data = state.entry;
1042 init_menu (&state, menu, title, sizeof (title), buffy);
1043 MAYBE_REDRAW (menu->redraw);
1047 case OP_RENAME_MAILBOX:
1048 if (!state.entry[menu->current].imap)
1049 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1051 int nentry = menu->current;
1053 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1054 destroy_state (&state);
1055 init_state (&state, NULL);
1056 state.imap_browse = 1;
1057 imap_browse (LastDir, &state);
1058 browser_sort (&state);
1059 menu->data = state.entry;
1062 init_menu (&state, menu, title, sizeof (title), buffy);
1063 MAYBE_REDRAW (menu->redraw);
1068 case OP_DELETE_MAILBOX:
1069 if (!state.entry[menu->current].imap)
1070 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1072 char msg[SHORT_STRING];
1074 int nentry = menu->current;
1076 imap_parse_path (state.entry[nentry].name, &mx);
1077 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1079 if (mutt_yesorno (msg, M_NO) == M_YES) {
1080 if (!imap_delete_mailbox (Context, mx)) {
1081 /* free the mailbox from the browser */
1082 p_delete(&((state.entry)[nentry].name));
1083 p_delete(&((state.entry)[nentry].desc));
1084 /* and move all other entries up */
1085 if (nentry + 1 < state.entrylen)
1086 memmove (state.entry + nentry, state.entry + nentry + 1,
1087 sizeof (struct folder_file) * (state.entrylen -
1090 mutt_message _("Mailbox deleted.");
1092 init_menu (&state, menu, title, sizeof (title), buffy);
1093 MAYBE_REDRAW (menu->redraw);
1097 mutt_message _("Mailbox not deleted.");
1102 case OP_CHANGE_DIRECTORY:
1105 if (option (OPTNEWS))
1109 m_strcpy(buf, sizeof(buf), LastDir);
1110 if (!state.imap_browse)
1112 /* add '/' at the end of the directory name if not already there */
1113 ssize_t len = m_strlen(LastDir);
1115 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1119 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1122 mutt_expand_path (buf, sizeof (buf));
1123 if (imap_is_magic (buf, NULL) == M_IMAP) {
1124 m_strcpy(LastDir, sizeof(LastDir), buf);
1125 destroy_state (&state);
1126 init_state (&state, NULL);
1127 state.imap_browse = 1;
1128 imap_browse (LastDir, &state);
1129 browser_sort (&state);
1130 menu->data = state.entry;
1133 init_menu (&state, menu, title, sizeof (title), buffy);
1136 if (stat (buf, &st) == 0) {
1137 if (S_ISDIR (st.st_mode)) {
1138 destroy_state (&state);
1139 if (examine_directory (menu, &state, buf, prefix) == 0)
1140 m_strcpy(LastDir, sizeof(LastDir), buf);
1142 mutt_error _("Error scanning directory.");
1144 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1145 mutt_menuDestroy (&menu);
1151 init_menu (&state, menu, title, sizeof (title), buffy);
1154 mutt_error (_("%s is not a directory."), buf);
1159 MAYBE_REDRAW (menu->redraw);
1164 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1165 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1166 regex_t *rx = p_new(regex_t, 1);
1171 /* assume that the user wants to see everything */
1173 m_strcpy(buf, sizeof(buf), ".");
1176 s = vskipspaces(s + 1);
1180 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1181 regerror (err, rx, buf, sizeof (buf));
1184 mutt_error ("%s", buf);
1187 m_strreplace(&Mask.pattern, buf);
1193 destroy_state (&state);
1194 if (state.imap_browse) {
1195 init_state (&state, NULL);
1196 state.imap_browse = 1;
1197 imap_browse (LastDir, &state);
1198 browser_sort (&state);
1199 menu->data = state.entry;
1200 init_menu (&state, menu, title, sizeof (title), buffy);
1203 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1204 init_menu (&state, menu, title, sizeof (title), buffy);
1206 mutt_error _("Error scanning directory.");
1208 mutt_menuDestroy (&menu);
1212 if (!state.entrylen) {
1213 mutt_error _("No files match the file mask");
1219 MAYBE_REDRAW (menu->redraw);
1223 case OP_SORT_REVERSE:
1227 int reverse = (i == OP_SORT_REVERSE);
1229 switch (mutt_multi_choice ((reverse) ?
1231 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1234 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1236 case -1: /* abort */
1240 case 1: /* (d)ate */
1241 BrowserSort = SORT_DATE;
1244 case 2: /* (a)lpha */
1245 BrowserSort = SORT_SUBJECT;
1248 case 3: /* si(z)e */
1249 BrowserSort = SORT_SIZE;
1252 case 4: /* do(n)'t sort */
1253 BrowserSort = SORT_ORDER;
1258 BrowserSort |= reverse ? SORT_REVERSE : 0;
1259 browser_sort (&state);
1260 menu->redraw = REDRAW_FULL;
1265 case OP_TOGGLE_MAILBOXES:
1269 destroy_state (&state);
1274 if (examine_mailboxes (menu, &state) == -1)
1277 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1278 init_state (&state, NULL);
1279 state.imap_browse = 1;
1280 imap_browse (LastDir, &state);
1281 browser_sort (&state);
1282 menu->data = state.entry;
1284 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1286 init_menu (&state, menu, title, sizeof (title), buffy);
1290 if (option (OPTFORCEBUFFYCHECK))
1295 case OP_BROWSER_NEW_FILE:
1297 snprintf (buf, sizeof (buf), "%s/", LastDir);
1298 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1300 m_strcpy(f, flen, buf);
1301 destroy_state (&state);
1302 mutt_menuDestroy (&menu);
1305 MAYBE_REDRAW (menu->redraw);
1308 case OP_BROWSER_VIEW_FILE:
1309 if (!state.entrylen) {
1310 mutt_error _("No files match the file mask");
1315 if (state.entry[menu->current].selectable) {
1316 m_strcpy(f, flen, state.entry[menu->current].name);
1317 destroy_state (&state);
1318 mutt_menuDestroy (&menu);
1322 if (S_ISDIR (state.entry[menu->current].mode) ||
1323 (S_ISLNK (state.entry[menu->current].mode) &&
1324 link_is_dir (LastDir, state.entry[menu->current].name))) {
1325 mutt_error _("Can't view a directory");
1331 char nbuf[_POSIX_PATH_MAX];
1333 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1334 state.entry[menu->current].name);
1335 b = mutt_make_file_attach (nbuf);
1337 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1339 menu->redraw = REDRAW_FULL;
1342 mutt_error _("Error trying to view file");
1349 if (option (OPTNEWS)) {
1350 struct folder_file *folder_f = &state.entry[menu->current];
1353 if (i == OP_CATCHUP)
1354 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1356 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1359 if (menu->current + 1 < menu->max)
1361 menu->redraw = REDRAW_MOTION_RESYNCH;
1366 case OP_LOAD_ACTIVE:
1367 if (!option (OPTNEWS))
1374 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1375 if ((data = (NNTP_DATA *) tmp->data))
1379 nntp_get_active (CurrentNewsSrv);
1381 destroy_state (&state);
1383 examine_mailboxes (menu, &state);
1385 examine_directory (menu, &state, NULL, NULL);
1386 init_menu (&state, menu, title, sizeof (title), buffy);
1388 #endif /* USE_NNTP */
1390 case OP_BROWSER_SUBSCRIBE:
1391 case OP_BROWSER_UNSUBSCRIBE:
1393 case OP_SUBSCRIBE_PATTERN:
1394 case OP_UNSUBSCRIBE_PATTERN:
1395 if (option (OPTNEWS)) {
1396 regex_t *rx = p_new(regex_t, 1);
1398 int j = menu->current;
1400 NNTP_SERVER *news = CurrentNewsSrv;
1402 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1407 if (i == OP_SUBSCRIBE_PATTERN)
1408 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1410 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1411 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1416 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1417 regerror (err, rx, buf, sizeof (buf));
1420 mutt_error ("%s", buf);
1423 menu->redraw = REDRAW_FULL;
1426 else if (!state.entrylen) {
1427 mutt_error _("No newsgroups match the mask");
1432 for (; j < state.entrylen; j++) {
1433 struct folder_file *folderf = &state.entry[j];
1435 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1436 regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1437 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1438 nd = mutt_newsgroup_subscribe (news, folderf->name);
1440 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1442 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1443 if (menu->current + 1 < menu->max)
1445 menu->redraw = REDRAW_MOTION_RESYNCH;
1449 if (i == OP_SUBSCRIBE_PATTERN) {
1450 string_list_t *grouplist = NULL;
1453 grouplist = news->list;
1454 for (; grouplist; grouplist = grouplist->next) {
1455 nd = (NNTP_DATA *) grouplist->data;
1456 if (nd && nd->group && !nd->subscribed) {
1457 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1458 mutt_newsgroup_subscribe (news, nd->group);
1459 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1463 init_menu (&state, menu, title, sizeof (title), buffy);
1465 mutt_newsrc_update (news);
1466 nntp_clear_cacheindex (news);
1467 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1472 #endif /* USE_NNTP */
1474 if (i == OP_BROWSER_SUBSCRIBE)
1475 imap_subscribe (state.entry[menu->current].name, 1);
1477 imap_subscribe (state.entry[menu->current].name, 0);
1485 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);