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.
17 #include "mutt_curses.h"
18 #include "mutt_menu.h"
27 #include "imap/mx_imap.h"
47 static struct mapping_t FolderHelp[] = {
48 {N_("Exit"), OP_EXIT},
49 {N_("Chdir"), OP_CHANGE_DIRECTORY},
50 {N_("Mask"), OP_ENTER_MASK},
51 {N_("Help"), OP_HELP},
56 static struct mapping_t FolderNewsHelp[] = {
57 {N_("Exit"), OP_EXIT},
58 {N_("List"), OP_TOGGLE_MAILBOXES},
59 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
60 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
61 {N_("Catchup"), OP_CATCHUP},
62 {N_("Mask"), OP_ENTER_MASK},
63 {N_("Help"), OP_HELP},
68 typedef struct folder_t {
69 struct folder_file *ff;
73 static char LastDir[_POSIX_PATH_MAX] = "";
74 static char LastDirBackup[_POSIX_PATH_MAX] = "";
76 /* Frees up the memory allocated for the local-global variables. */
77 static void destroy_state (struct browser_state *state)
81 for (c = 0; c < state->entrylen; c++) {
82 mem_free (&((state->entry)[c].name));
83 mem_free (&((state->entry)[c].desc));
84 mem_free (&((state->entry)[c].st));
87 mem_free (&state->folder);
89 mem_free (&state->entry);
92 static int browser_compare_subject (const void *a, const void *b)
94 struct folder_file *pa = (struct folder_file *) a;
95 struct folder_file *pb = (struct folder_file *) b;
97 int r = str_coll (pa->name, pb->name);
99 return ((BrowserSort & SORT_REVERSE) ? -r : r);
102 static int browser_compare_date (const void *a, const void *b)
104 struct folder_file *pa = (struct folder_file *) a;
105 struct folder_file *pb = (struct folder_file *) b;
107 int r = pa->mtime - pb->mtime;
109 return ((BrowserSort & SORT_REVERSE) ? -r : r);
112 static int browser_compare_size (const void *a, const void *b)
114 struct folder_file *pa = (struct folder_file *) a;
115 struct folder_file *pb = (struct folder_file *) b;
117 int r = pa->size - pb->size;
119 return ((BrowserSort & SORT_REVERSE) ? -r : r);
122 static void browser_sort (struct browser_state *state)
124 int (*f) (const void *, const void *);
126 switch (BrowserSort & SORT_MASK) {
131 if (option (OPTNEWS))
134 f = browser_compare_date;
138 if (option (OPTNEWS))
141 f = browser_compare_size;
145 f = browser_compare_subject;
148 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
151 static int link_is_dir (const char *folder, const char *path)
154 char fullpath[_POSIX_PATH_MAX];
156 mutt_concat_path (fullpath, folder, path, sizeof (fullpath));
158 if (stat (fullpath, &st) == 0)
159 return (S_ISDIR (st.st_mode));
164 static const char *folder_format_str (char *dest, size_t destlen, char op,
165 const char *src, const char *fmt,
166 const char *ifstring,
167 const char *elsestring,
168 unsigned long data, format_flag flags)
170 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
171 char date[16], *t_fmt;
173 FOLDER *folder = (FOLDER *) data;
176 int optional = (flags & M_FORMAT_OPTIONAL);
180 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
181 snprintf (dest, destlen, tmp, folder->num + 1);
185 if (folder->ff->st != NULL) {
188 tnow - folder->ff->st->st_mtime <
189 31536000 ? "%b %d %H:%M" : "%b %d %Y";
190 strftime (date, sizeof (date), t_fmt,
191 localtime (&folder->ff->st->st_mtime));
192 mutt_format_s (dest, destlen, fmt, date);
195 mutt_format_s (dest, destlen, fmt, "");
203 if (folder->ff->imap)
204 s = NONULL (folder->ff->desc);
207 s = NONULL (folder->ff->name);
209 snprintf (fn, sizeof (fn), "%s%s", s,
210 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
211 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
212 ((folder->ff->st->st_mode & S_IXUSR) !=
213 0 ? "*" : ""))) : "");
215 mutt_format_s (dest, destlen, fmt, fn);
219 if (folder->ff->st != NULL) {
220 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
221 S_ISDIR (folder->ff->st->
222 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
223 st_mode) ? 'l' : '-'),
224 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
225 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
226 (folder->ff->st->st_mode & S_ISUID) !=
227 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
229 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
230 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
231 (folder->ff->st->st_mode & S_ISGID) !=
232 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
234 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
235 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
236 (folder->ff->st->st_mode & S_ISVTX) !=
237 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
239 mutt_format_s (dest, destlen, fmt, permission);
242 else if (folder->ff->imap) {
243 /* mark folders with subfolders AND mail */
244 snprintf (permission, sizeof (permission), "IMAP %c",
245 (folder->ff->inferiors
246 && folder->ff->selectable) ? '+' : ' ');
247 mutt_format_s (dest, destlen, fmt, permission);
251 mutt_format_s (dest, destlen, fmt, "");
255 if (folder->ff->st != NULL) {
256 if ((gr = getgrgid (folder->ff->st->st_gid)))
257 mutt_format_s (dest, destlen, fmt, gr->gr_name);
259 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
260 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
264 mutt_format_s (dest, destlen, fmt, "");
268 if (folder->ff->st != NULL) {
269 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
270 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
273 mutt_format_s (dest, destlen, fmt, "");
278 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
280 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
281 snprintf (dest, destlen, tmp, folder->ff->new);
283 else if (!folder->ff->new)
288 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
289 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
293 if (folder->ff->st != NULL) {
294 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
295 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
298 mutt_format_s (dest, destlen, fmt, "");
302 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
303 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
307 if (folder->ff->st != NULL) {
308 if ((pw = getpwuid (folder->ff->st->st_uid)))
309 mutt_format_s (dest, destlen, fmt, pw->pw_name);
311 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
312 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
316 mutt_format_s (dest, destlen, fmt, "");
320 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
321 snprintf (dest, destlen, tmp, op);
326 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
327 else if (flags & M_FORMAT_OPTIONAL)
328 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
334 static const char *newsgroup_format_str (char *dest, size_t destlen, char op,
335 const char *src, const char *fmt,
336 const char *ifstring,
337 const char *elsestring,
341 char fn[SHORT_STRING], tmp[SHORT_STRING];
342 FOLDER *folder = (FOLDER *) data;
346 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
347 snprintf (dest, destlen, tmp, folder->num + 1);
351 strncpy (fn, folder->ff->name, sizeof (fn) - 1);
352 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
353 snprintf (dest, destlen, tmp, fn);
357 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
358 if (folder->ff->nd->subscribed)
359 snprintf (dest, destlen, tmp, ' ');
361 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
365 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
366 if (folder->ff->nd->deleted)
367 snprintf (dest, destlen, tmp, 'D');
369 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
373 if (flags & M_FORMAT_OPTIONAL) {
374 if (folder->ff->nd->unread != 0)
375 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
378 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
381 else if (Context && Context->data == folder->ff->nd) {
382 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
383 snprintf (dest, destlen, tmp, Context->unread);
386 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
387 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
392 if (Context && Context->data == folder->ff->nd) {
393 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
394 snprintf (dest, destlen, tmp, Context->new);
396 else if (option (OPTMARKOLD) &&
397 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
398 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
399 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
400 snprintf (dest, destlen, tmp,
401 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
404 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
405 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
410 if (folder->ff->nd->desc != NULL) {
411 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
412 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
415 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
416 snprintf (dest, destlen, tmp, "");
422 #endif /* USE_NNTP */
424 static void add_folder (MUTTMENU * m, struct browser_state *state,
425 const char *name, const struct stat *s,
428 if (state->entrylen == state->entrymax) {
429 /* need to allocate more space */
430 mem_realloc (&state->entry,
431 sizeof (struct folder_file) * (state->entrymax += 256));
432 memset (&state->entry[state->entrylen], 0,
433 sizeof (struct folder_file) * 256);
435 m->data = state->entry;
439 (state->entry)[state->entrylen].mode = s->st_mode;
440 (state->entry)[state->entrylen].mtime = s->st_mtime;
441 (state->entry)[state->entrylen].size = s->st_size;
443 (state->entry)[state->entrylen].st = mem_malloc (sizeof (struct stat));
444 memcpy ((state->entry)[state->entrylen].st, s, sizeof (struct stat));
447 (state->entry)[state->entrylen].new = new;
448 (state->entry)[state->entrylen].name = str_dup (name);
449 (state->entry)[state->entrylen].desc = str_dup (name);
451 (state->entry)[state->entrylen].imap = 0;
454 if (option (OPTNEWS))
455 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
460 static void init_state (struct browser_state *state, MUTTMENU * menu)
463 state->entrymax = 256;
465 (struct folder_file *) mem_calloc (state->entrymax,
466 sizeof (struct folder_file));
468 state->imap_browse = 0;
471 menu->data = state->entry;
474 /* get list of all files/newsgroups with mask */
475 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
476 char *d, const char *prefix)
479 if (option (OPTNEWS)) {
482 NNTP_SERVER *news = CurrentNewsSrv;
484 /* buffy_check (0); */
485 init_state (state, menu);
487 for (tmp = news->list; tmp; tmp = tmp->next) {
488 if (!(data = (NNTP_DATA *) tmp->data))
490 nntp_sync_sidebar (data);
491 if (prefix && *prefix && strncmp (prefix, data->group,
492 str_len (prefix)) != 0)
494 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
496 add_folder (menu, state, data->group, NULL, data, data->new);
498 sidebar_draw (CurrentMenu);
501 #endif /* USE_NNTP */
506 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
509 while (stat (d, &s) == -1) {
510 if (errno == ENOENT) {
511 /* The last used directory is deleted, try to use the parent dir. */
512 char *c = strrchr (d, '/');
523 if (!S_ISDIR (s.st_mode)) {
524 mutt_error (_("%s is not a directory."), d);
530 if ((dp = opendir (d)) == NULL) {
535 init_state (state, menu);
537 while ((de = readdir (dp)) != NULL) {
538 if (str_cmp (de->d_name, ".") == 0)
539 continue; /* we don't need . */
541 if (prefix && *prefix
542 && str_ncmp (prefix, de->d_name, str_len (prefix)) != 0)
544 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
547 mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
548 if (lstat (buffer, &s) == -1)
551 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
552 (!S_ISLNK (s.st_mode)))
555 i = buffy_lookup (buffer);
556 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
560 sidebar_draw (CurrentMenu);
561 browser_sort (state);
565 /* get list of mailboxes/subscribed newsgroups */
566 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
569 char buffer[LONG_STRING];
572 if (option (OPTNEWS)) {
575 NNTP_SERVER *news = CurrentNewsSrv;
577 /* buffy_check (0); */
578 init_state (state, menu);
580 for (tmp = news->list; tmp; tmp = tmp->next) {
581 if ((data = (NNTP_DATA*) tmp->data) == NULL)
583 nntp_sync_sidebar (data);
584 if ((data->new || (data->subscribed &&
585 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
586 add_folder (menu, state, data->group, NULL, data, data->new);
588 sidebar_draw (CurrentMenu);
600 init_state (state, menu);
602 for (i = 0; i < Incoming->length; i++) {
603 tmp = (BUFFY*) Incoming->data[i];
604 tmp->magic = mx_get_magic (tmp->path);
606 if (tmp->magic == M_IMAP) {
607 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
612 if (tmp->magic == M_POP) {
613 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
618 if (tmp->magic == M_NNTP) {
619 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
623 if (lstat (tmp->path, &s) == -1)
626 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
627 (!S_ISLNK (s.st_mode)))
630 strfcpy (buffer, NONULL (tmp->path), sizeof (buffer));
631 mutt_pretty_mailbox (buffer);
633 add_folder (menu, state, buffer, &s, NULL, tmp->new);
636 browser_sort (state);
640 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
643 if (option (OPTNEWS))
645 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
648 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
651 static void folder_entry (char *s, size_t slen, MUTTMENU * menu, int num)
655 folder.ff = &((struct folder_file *) menu->data)[num];
659 if (option (OPTNEWS))
660 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
661 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
664 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
665 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
668 static void init_menu (struct browser_state *state, MUTTMENU * menu,
669 char *title, size_t titlelen, int buffy)
671 char path[_POSIX_PATH_MAX];
673 menu->max = state->entrylen;
675 if (menu->current >= menu->max)
676 menu->current = menu->max - 1;
677 if (menu->current < 0)
679 if (menu->top > menu->current)
685 if (option (OPTNEWS)) {
687 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
689 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
690 CurrentNewsSrv->conn->account.host);
695 snprintf (title, titlelen, _("Mailboxes [%d]"), buffy_check (0));
697 strfcpy (path, LastDir, sizeof (path));
698 mutt_pretty_mailbox (path);
700 if (state->imap_browse && option (OPTIMAPLSUB))
701 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
702 path, NONULL (Mask.pattern));
705 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
706 path, NONULL (Mask.pattern));
708 menu->redraw = REDRAW_FULL;
711 static int file_tag (MUTTMENU * menu, int n, int m)
713 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
716 if (S_ISDIR (ff->mode)
717 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
718 mutt_error _("Can't attach a directory!");
724 ff->tagged = (m >= 0 ? m : !ff->tagged);
726 return ff->tagged - ot;
729 void _mutt_select_file (char *f, size_t flen, int flags, char ***files,
732 char buf[_POSIX_PATH_MAX];
733 char prefix[_POSIX_PATH_MAX] = "";
734 char helpstr[SHORT_STRING];
736 struct browser_state state;
739 int i, killPrefix = 0;
740 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
741 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
742 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
744 buffy = buffy && folder;
746 memset (&state, 0, sizeof (struct browser_state));
749 strfcpy (LastDirBackup, LastDir, sizeof (LastDirBackup));
752 if (option (OPTNEWS)) {
754 strfcpy (prefix, f, sizeof (prefix));
758 /* default state for news reader mode is browse subscribed newsgroups */
760 for (list = CurrentNewsSrv->list; list; list = list->next) {
761 NNTP_DATA *data = (NNTP_DATA *) list->data;
763 if (data && data->subscribed) {
773 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 strfcpy (LastDir, state.folder, sizeof (LastDir));
783 for (i = str_len (f) - 1; i > 0 && f[i] != '/'; i--);
786 if (i > sizeof (LastDir) - 1)
787 i = sizeof (LastDir) - 1;
788 strncpy (LastDir, f, i);
792 getcwd (LastDir, sizeof (LastDir));
793 str_cat (LastDir, sizeof (LastDir), "/");
794 str_ncat (LastDir, sizeof (LastDir), f, i);
799 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
801 getcwd (LastDir, sizeof (LastDir));
804 if (i <= 0 && f[0] != '/')
805 strfcpy (prefix, f, sizeof (prefix));
807 strfcpy (prefix, f + i + 1, sizeof (prefix));
815 getcwd (LastDir, sizeof (LastDir));
816 else if (!LastDir[0])
817 strfcpy (LastDir, NONULL (Maildir), sizeof (LastDir));
820 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
821 init_state (&state, NULL);
822 state.imap_browse = 1;
823 imap_browse (LastDir, &state);
824 browser_sort (&state);
832 if (examine_mailboxes (NULL, &state) == -1)
837 if (!state.imap_browse)
839 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
842 menu = mutt_new_menu ();
843 menu->menu = MENU_FOLDER;
844 menu->make_entry = folder_entry;
845 menu->search = select_file_search;
847 menu->data = state.entry;
849 menu->tag = file_tag;
851 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
853 (option (OPTNEWS)) ? FolderNewsHelp :
857 init_menu (&state, menu, title, sizeof (title), buffy);
860 switch (i = mutt_menuLoop (menu)) {
861 case OP_GENERIC_SELECT_ENTRY:
863 if (!state.entrylen) {
864 mutt_error _("No files match the file mask");
869 if (S_ISDIR (state.entry[menu->current].mode) ||
870 (S_ISLNK (state.entry[menu->current].mode) &&
871 link_is_dir (LastDir, state.entry[menu->current].name))
873 || state.entry[menu->current].inferiors
876 /* make sure this isn't a MH or maildir mailbox */
878 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
879 mutt_expand_path (buf, sizeof (buf));
882 else if (state.imap_browse) {
883 strfcpy (buf, state.entry[menu->current].name, sizeof (buf));
887 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
890 if ((mx_get_magic (buf) <= 0)
892 || state.entry[menu->current].inferiors
895 char OldLastDir[_POSIX_PATH_MAX];
897 /* save the old directory */
898 strfcpy (OldLastDir, LastDir, sizeof (OldLastDir));
900 if (str_cmp (state.entry[menu->current].name, "..") == 0) {
901 if (str_cmp ("..", LastDir + str_len (LastDir) - 2) == 0)
902 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
904 char *p = strrchr (LastDir + 1, '/');
909 if (LastDir[0] == '/')
912 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
917 strfcpy (LastDir, state.entry[menu->current].name,
919 mutt_expand_path (LastDir, sizeof (LastDir));
922 else if (state.imap_browse) {
926 strfcpy (LastDir, state.entry[menu->current].name,
928 /* tack on delimiter here */
929 n = str_len (LastDir) + 1;
931 /* special case "" needs no delimiter */
932 url_parse_ciss (&url, state.entry[menu->current].name);
934 (state.entry[menu->current].delim != '\0') &&
935 (n < sizeof (LastDir))) {
937 LastDir[n - 1] = state.entry[menu->current].delim;
942 char tmp[_POSIX_PATH_MAX];
944 mutt_concat_path (tmp, LastDir, state.entry[menu->current].name,
946 strfcpy (LastDir, tmp, sizeof (LastDir));
949 destroy_state (&state);
956 if (state.imap_browse) {
957 init_state (&state, NULL);
958 state.imap_browse = 1;
959 imap_browse (LastDir, &state);
960 browser_sort (&state);
961 menu->data = state.entry;
965 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
966 /* try to restore the old values */
967 strfcpy (LastDir, OldLastDir, sizeof (LastDir));
968 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
969 strfcpy (LastDir, NONULL (Homedir), sizeof (LastDir));
975 init_menu (&state, menu, title, sizeof (title), buffy);
981 if (buffy || option (OPTNEWS)) /* news have not path */
986 strfcpy (f, state.entry[menu->current].name, flen);
987 mutt_expand_path (f, flen);
990 else if (state.imap_browse)
991 strfcpy (f, state.entry[menu->current].name, flen);
994 mutt_concat_path (f, LastDir, state.entry[menu->current].name, flen);
996 /* Fall through to OP_EXIT */
1005 *numfiles = menu->tagged;
1006 tfiles = mem_calloc (*numfiles, sizeof (char *));
1007 for (i = 0, j = 0; i < state.entrylen; i++) {
1008 struct folder_file ff = state.entry[i];
1009 char full[_POSIX_PATH_MAX];
1012 mutt_concat_path (full, LastDir, ff.name, sizeof (full));
1013 mutt_expand_path (full, sizeof (full));
1014 tfiles[j++] = str_dup (full);
1019 else if (f[0]) { /* no tagged entries. return selected entry */
1021 tfiles = mem_calloc (*numfiles, sizeof (char *));
1022 mutt_expand_path (f, flen);
1023 tfiles[0] = str_dup (f);
1028 destroy_state (&state);
1029 mutt_menuDestroy (&menu);
1032 case OP_BROWSER_TELL:
1034 mutt_message ("%s", state.entry[menu->current].name);
1038 case OP_BROWSER_TOGGLE_LSUB:
1039 if (option (OPTIMAPLSUB)) {
1040 unset_option (OPTIMAPLSUB);
1043 set_option (OPTIMAPLSUB);
1045 mutt_ungetch (0, OP_CHECK_NEW);
1048 case OP_CREATE_MAILBOX:
1049 if (!state.imap_browse)
1050 mutt_error (_("Create is only supported for IMAP mailboxes"));
1052 imap_mailbox_create (LastDir);
1053 /* TODO: find a way to detect if the new folder would appear in
1054 * this window, and insert it without starting over. */
1055 destroy_state (&state);
1056 init_state (&state, NULL);
1057 state.imap_browse = 1;
1058 imap_browse (LastDir, &state);
1059 browser_sort (&state);
1060 menu->data = state.entry;
1063 init_menu (&state, menu, title, sizeof (title), buffy);
1064 MAYBE_REDRAW (menu->redraw);
1068 case OP_RENAME_MAILBOX:
1069 if (!state.entry[menu->current].imap)
1070 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1072 int nentry = menu->current;
1074 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1075 destroy_state (&state);
1076 init_state (&state, NULL);
1077 state.imap_browse = 1;
1078 imap_browse (LastDir, &state);
1079 browser_sort (&state);
1080 menu->data = state.entry;
1083 init_menu (&state, menu, title, sizeof (title), buffy);
1084 MAYBE_REDRAW (menu->redraw);
1089 case OP_DELETE_MAILBOX:
1090 if (!state.entry[menu->current].imap)
1091 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1093 char msg[SHORT_STRING];
1095 int nentry = menu->current;
1097 imap_parse_path (state.entry[nentry].name, &mx);
1098 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1100 if (mutt_yesorno (msg, M_NO) == M_YES) {
1101 if (!imap_delete_mailbox (Context, mx)) {
1102 /* free the mailbox from the browser */
1103 mem_free (&((state.entry)[nentry].name));
1104 mem_free (&((state.entry)[nentry].desc));
1105 /* and move all other entries up */
1106 if (nentry + 1 < state.entrylen)
1107 memmove (state.entry + nentry, state.entry + nentry + 1,
1108 sizeof (struct folder_file) * (state.entrylen -
1111 mutt_message _("Mailbox deleted.");
1113 init_menu (&state, menu, title, sizeof (title), buffy);
1114 MAYBE_REDRAW (menu->redraw);
1118 mutt_message _("Mailbox not deleted.");
1119 mem_free (&mx.mbox);
1124 case OP_CHANGE_DIRECTORY:
1127 if (option (OPTNEWS))
1131 strfcpy (buf, LastDir, sizeof (buf));
1133 if (!state.imap_browse)
1136 /* add '/' at the end of the directory name if not already there */
1137 int len = str_len (LastDir);
1139 if (len && LastDir[len - 1] != '/' && sizeof (buf) > len)
1143 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1146 mutt_expand_path (buf, sizeof (buf));
1148 if (imap_is_magic (buf, NULL) == M_IMAP) {
1149 strfcpy (LastDir, buf, sizeof (LastDir));
1150 destroy_state (&state);
1151 init_state (&state, NULL);
1152 state.imap_browse = 1;
1153 imap_browse (LastDir, &state);
1154 browser_sort (&state);
1155 menu->data = state.entry;
1158 init_menu (&state, menu, title, sizeof (title), buffy);
1162 if (stat (buf, &st) == 0) {
1163 if (S_ISDIR (st.st_mode)) {
1164 destroy_state (&state);
1165 if (examine_directory (menu, &state, buf, prefix) == 0)
1166 strfcpy (LastDir, buf, sizeof (LastDir));
1168 mutt_error _("Error scanning directory.");
1170 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1171 mutt_menuDestroy (&menu);
1177 init_menu (&state, menu, title, sizeof (title), buffy);
1180 mutt_error (_("%s is not a directory."), buf);
1185 MAYBE_REDRAW (menu->redraw);
1190 strfcpy (buf, NONULL (Mask.pattern), sizeof (buf));
1191 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1192 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1197 /* assume that the user wants to see everything */
1199 strfcpy (buf, ".", sizeof (buf));
1207 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1208 regerror (err, rx, buf, sizeof (buf));
1211 mutt_error ("%s", buf);
1214 str_replace (&Mask.pattern, buf);
1216 mem_free (&Mask.rx);
1220 destroy_state (&state);
1222 if (state.imap_browse) {
1223 init_state (&state, NULL);
1224 state.imap_browse = 1;
1225 imap_browse (LastDir, &state);
1226 browser_sort (&state);
1227 menu->data = state.entry;
1228 init_menu (&state, menu, title, sizeof (title), buffy);
1232 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1233 init_menu (&state, menu, title, sizeof (title), buffy);
1235 mutt_error _("Error scanning directory.");
1237 mutt_menuDestroy (&menu);
1241 if (!state.entrylen) {
1242 mutt_error _("No files match the file mask");
1248 MAYBE_REDRAW (menu->redraw);
1252 case OP_SORT_REVERSE:
1256 int reverse = (i == OP_SORT_REVERSE);
1258 switch (mutt_multi_choice ((reverse) ?
1260 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1263 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1265 case -1: /* abort */
1269 case 1: /* (d)ate */
1270 BrowserSort = SORT_DATE;
1273 case 2: /* (a)lpha */
1274 BrowserSort = SORT_SUBJECT;
1277 case 3: /* si(z)e */
1278 BrowserSort = SORT_SIZE;
1281 case 4: /* do(n)'t sort */
1282 BrowserSort = SORT_ORDER;
1287 BrowserSort |= reverse ? SORT_REVERSE : 0;
1288 browser_sort (&state);
1289 menu->redraw = REDRAW_FULL;
1294 case OP_TOGGLE_MAILBOXES:
1298 destroy_state (&state);
1303 if (examine_mailboxes (menu, &state) == -1)
1307 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1308 init_state (&state, NULL);
1309 state.imap_browse = 1;
1310 imap_browse (LastDir, &state);
1311 browser_sort (&state);
1312 menu->data = state.entry;
1315 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1317 init_menu (&state, menu, title, sizeof (title), buffy);
1321 if (option (OPTFORCEBUFFYCHECK))
1326 case OP_BROWSER_NEW_FILE:
1328 snprintf (buf, sizeof (buf), "%s/", LastDir);
1329 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1331 strfcpy (f, buf, flen);
1332 destroy_state (&state);
1333 mutt_menuDestroy (&menu);
1336 MAYBE_REDRAW (menu->redraw);
1339 case OP_BROWSER_VIEW_FILE:
1340 if (!state.entrylen) {
1341 mutt_error _("No files match the file mask");
1347 if (state.entry[menu->current].selectable) {
1348 strfcpy (f, state.entry[menu->current].name, flen);
1349 destroy_state (&state);
1350 mutt_menuDestroy (&menu);
1355 if (S_ISDIR (state.entry[menu->current].mode) ||
1356 (S_ISLNK (state.entry[menu->current].mode) &&
1357 link_is_dir (LastDir, state.entry[menu->current].name))) {
1358 mutt_error _("Can't view a directory");
1364 char buf[_POSIX_PATH_MAX];
1366 mutt_concat_path (buf, LastDir, state.entry[menu->current].name,
1368 b = mutt_make_file_attach (buf);
1370 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1371 mutt_free_body (&b);
1372 menu->redraw = REDRAW_FULL;
1375 mutt_error _("Error trying to view file");
1382 if (option (OPTNEWS)) {
1383 struct folder_file *f = &state.entry[menu->current];
1386 if (i == OP_CATCHUP)
1387 nd = mutt_newsgroup_catchup (CurrentNewsSrv, f->name);
1389 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, f->name);
1393 struct folder_file ff;
1394 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1397 folder.ff->name = f->name;
1398 folder.ff->st = NULL;
1399 folder.ff->is_new = nd->new;
1401 mem_free (&f->desc);
1402 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1403 newsgroup_format_str, (unsigned long) &folder,
1404 M_FORMAT_ARROWCURSOR);
1405 f->desc = str_dup (buffer); */
1406 if (menu->current + 1 < menu->max)
1408 menu->redraw = REDRAW_MOTION_RESYNCH;
1413 case OP_LOAD_ACTIVE:
1414 if (!option (OPTNEWS))
1421 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1422 if ((data = (NNTP_DATA *) tmp->data))
1426 nntp_get_active (CurrentNewsSrv);
1428 destroy_state (&state);
1430 examine_mailboxes (menu, &state);
1432 examine_directory (menu, &state, NULL, NULL);
1433 init_menu (&state, menu, title, sizeof (title), buffy);
1435 #endif /* USE_NNTP */
1437 #if defined USE_IMAP || defined USE_NNTP
1438 case OP_BROWSER_SUBSCRIBE:
1439 case OP_BROWSER_UNSUBSCRIBE:
1442 case OP_SUBSCRIBE_PATTERN:
1443 case OP_UNSUBSCRIBE_PATTERN:
1444 if (option (OPTNEWS)) {
1445 regex_t *rx = (regex_t *) mem_malloc (sizeof (regex_t));
1447 int j = menu->current;
1449 NNTP_SERVER *news = CurrentNewsSrv;
1451 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1456 if (i == OP_SUBSCRIBE_PATTERN)
1457 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1459 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1460 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1465 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1466 regerror (err, rx, buf, sizeof (buf));
1469 mutt_error ("%s", buf);
1472 menu->redraw = REDRAW_FULL;
1475 else if (!state.entrylen) {
1476 mutt_error _("No newsgroups match the mask");
1481 for (; j < state.entrylen; j++) {
1482 struct folder_file *f = &state.entry[j];
1484 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1485 regexec (rx, f->name, 0, NULL, 0) == 0) {
1486 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1487 nd = mutt_newsgroup_subscribe (news, f->name);
1489 nd = mutt_newsgroup_unsubscribe (news, f->name);
1493 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
1495 folder.name = f->name;
1497 folder.new = nd->new;
1499 mem_free (&f->desc);
1500 mutt_FormatString (buffer, sizeof (buffer), NONULL(GroupFormat),
1501 newsgroup_format_str, (unsigned long) &folder,
1502 M_FORMAT_ARROWCURSOR);
1503 f->desc = str_dup (buffer);
1506 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1507 if (menu->current + 1 < menu->max)
1509 menu->redraw = REDRAW_MOTION_RESYNCH;
1513 if (i == OP_SUBSCRIBE_PATTERN) {
1514 LIST *grouplist = NULL;
1517 grouplist = news->list;
1518 for (; grouplist; grouplist = grouplist->next) {
1519 nd = (NNTP_DATA *) grouplist->data;
1520 if (nd && nd->group && !nd->subscribed) {
1521 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1522 mutt_newsgroup_subscribe (news, nd->group);
1523 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1527 init_menu (&state, menu, title, sizeof (title), buffy);
1529 mutt_newsrc_update (news);
1530 nntp_clear_cacheindex (news);
1531 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1537 #endif /* USE_IMAP && USE_NNTP */
1538 #endif /* USE_NNTP */
1541 if (i == OP_BROWSER_SUBSCRIBE)
1542 imap_subscribe (state.entry[menu->current].name, 1);
1544 imap_subscribe (state.entry[menu->current].name, 0);
1546 #endif /* USE_IMAP */
1553 strfcpy (LastDir, LastDirBackup, sizeof (LastDir));