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>
36 static struct mapping_t FolderHelp[] = {
37 {N_("Exit"), OP_EXIT},
38 {N_("Chdir"), OP_CHANGE_DIRECTORY},
39 {N_("Mask"), OP_ENTER_MASK},
40 {N_("Help"), OP_HELP},
45 static struct mapping_t FolderNewsHelp[] = {
46 {N_("Exit"), OP_EXIT},
47 {N_("List"), OP_TOGGLE_MAILBOXES},
48 {N_("Subscribe"), OP_BROWSER_SUBSCRIBE},
49 {N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE},
50 {N_("Catchup"), OP_CATCHUP},
51 {N_("Mask"), OP_ENTER_MASK},
52 {N_("Help"), OP_HELP},
57 typedef struct folder_t {
58 struct folder_file *ff;
62 static char LastDir[_POSIX_PATH_MAX] = "";
63 static char LastDirBackup[_POSIX_PATH_MAX] = "";
65 /* Frees up the memory allocated for the local-global variables. */
66 static void destroy_state (struct browser_state *state)
70 for (c = 0; c < state->entrylen; c++) {
71 p_delete(&((state->entry)[c].name));
72 p_delete(&((state->entry)[c].desc));
73 p_delete(&((state->entry)[c].st));
75 p_delete(&state->folder);
76 p_delete(&state->entry);
79 static int browser_compare_subject (const void *a, const void *b)
81 struct folder_file *pa = (struct folder_file *) a;
82 struct folder_file *pb = (struct folder_file *) b;
84 int r = strcoll(NONULL(pa->name), NONULL(pb->name));
86 return ((BrowserSort & SORT_REVERSE) ? -r : r);
89 static int browser_compare_date (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 = pa->mtime - pb->mtime;
96 return ((BrowserSort & SORT_REVERSE) ? -r : r);
99 static int browser_compare_size (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->size - pb->size;
106 return ((BrowserSort & SORT_REVERSE) ? -r : r);
109 static void browser_sort (struct browser_state *state)
111 int (*f) (const void *, const void *);
113 switch (BrowserSort & SORT_MASK) {
118 if (option (OPTNEWS))
121 f = browser_compare_date;
125 if (option (OPTNEWS))
128 f = browser_compare_size;
132 f = browser_compare_subject;
135 qsort (state->entry, state->entrylen, sizeof (struct folder_file), f);
138 static int link_is_dir (const char *folder, const char *path)
141 char fullpath[_POSIX_PATH_MAX];
143 mutt_concat_path(fullpath, sizeof(fullpath), folder, path);
145 if (stat (fullpath, &st) == 0)
146 return (S_ISDIR (st.st_mode));
151 static const char *folder_format_str (char *dest, ssize_t destlen, char op,
152 const char *src, const char *fmt,
153 const char *ifstring,
154 const char *elsestring,
155 unsigned long data, format_flag flags)
157 char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11], date[16];
160 FOLDER *folder = (FOLDER *) data;
163 int optional = (flags & M_FORMAT_OPTIONAL);
167 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
168 snprintf (dest, destlen, tmp, folder->num + 1);
172 if (folder->ff->st != NULL) {
175 tnow - folder->ff->st->st_mtime <
176 31536000 ? "%b %d %H:%M" : "%b %d %Y";
177 strftime (date, sizeof (date), t_fmt,
178 localtime (&folder->ff->st->st_mtime));
179 mutt_format_s (dest, destlen, fmt, date);
182 mutt_format_s (dest, destlen, fmt, "");
189 if (folder->ff->imap)
190 s = NONULL (folder->ff->desc);
192 s = NONULL (folder->ff->name);
194 snprintf (fn, sizeof (fn), "%s%s", s,
195 folder->ff->st ? (S_ISLNK (folder->ff->st->st_mode) ? "@" :
196 (S_ISDIR (folder->ff->st->st_mode) ? "/" :
197 ((folder->ff->st->st_mode & S_IXUSR) !=
198 0 ? "*" : ""))) : "");
200 mutt_format_s (dest, destlen, fmt, fn);
204 if (folder->ff->st != NULL) {
205 snprintf (permission, sizeof (permission), "%c%c%c%c%c%c%c%c%c%c",
206 S_ISDIR (folder->ff->st->
207 st_mode) ? 'd' : (S_ISLNK (folder->ff->st->
208 st_mode) ? 'l' : '-'),
209 (folder->ff->st->st_mode & S_IRUSR) != 0 ? 'r' : '-',
210 (folder->ff->st->st_mode & S_IWUSR) != 0 ? 'w' : '-',
211 (folder->ff->st->st_mode & S_ISUID) !=
212 0 ? 's' : (folder->ff->st->st_mode & S_IXUSR) !=
214 (folder->ff->st->st_mode & S_IRGRP) != 0 ? 'r' : '-',
215 (folder->ff->st->st_mode & S_IWGRP) != 0 ? 'w' : '-',
216 (folder->ff->st->st_mode & S_ISGID) !=
217 0 ? 's' : (folder->ff->st->st_mode & S_IXGRP) !=
219 (folder->ff->st->st_mode & S_IROTH) != 0 ? 'r' : '-',
220 (folder->ff->st->st_mode & S_IWOTH) != 0 ? 'w' : '-',
221 (folder->ff->st->st_mode & S_ISVTX) !=
222 0 ? 't' : (folder->ff->st->st_mode & S_IXOTH) !=
224 mutt_format_s (dest, destlen, fmt, permission);
226 else if (folder->ff->imap) {
227 /* mark folders with subfolders AND mail */
228 snprintf (permission, sizeof (permission), "IMAP %c",
229 (folder->ff->inferiors
230 && folder->ff->selectable) ? '+' : ' ');
231 mutt_format_s (dest, destlen, fmt, permission);
234 mutt_format_s (dest, destlen, fmt, "");
238 if (folder->ff->st != NULL) {
239 if ((gr = getgrgid (folder->ff->st->st_gid)))
240 mutt_format_s (dest, destlen, fmt, gr->gr_name);
242 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
243 snprintf (dest, destlen, tmp, folder->ff->st->st_gid);
247 mutt_format_s (dest, destlen, fmt, "");
251 if (folder->ff->st != NULL) {
252 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
253 snprintf (dest, destlen, tmp, folder->ff->st->st_nlink);
256 mutt_format_s (dest, destlen, fmt, "");
260 if (imap_is_magic (folder->ff->desc, NULL) == M_IMAP) {
262 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
263 snprintf (dest, destlen, tmp, folder->ff->new);
265 else if (!folder->ff->new)
269 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
270 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
274 if (folder->ff->st != NULL) {
275 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
276 snprintf (dest, destlen, tmp, (long) folder->ff->st->st_size);
279 mutt_format_s (dest, destlen, fmt, "");
283 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
284 snprintf (dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
288 if (folder->ff->st != NULL) {
289 if ((pw = getpwuid (folder->ff->st->st_uid)))
290 mutt_format_s (dest, destlen, fmt, pw->pw_name);
292 snprintf (tmp, sizeof (tmp), "%%%sld", fmt);
293 snprintf (dest, destlen, tmp, folder->ff->st->st_uid);
297 mutt_format_s (dest, destlen, fmt, "");
301 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
302 snprintf (dest, destlen, tmp, op);
307 mutt_FormatString (dest, destlen, ifstring, folder_format_str, data, 0);
308 else if (flags & M_FORMAT_OPTIONAL)
309 mutt_FormatString (dest, destlen, elsestring, folder_format_str, data, 0);
315 static const char *newsgroup_format_str (char *dest, ssize_t destlen, char op,
316 const char *src, const char *fmt,
317 const char *ifstring,
318 const char *elsestring,
322 char fn[SHORT_STRING], tmp[SHORT_STRING];
323 FOLDER *folder = (FOLDER *) data;
327 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
328 snprintf (dest, destlen, tmp, folder->num + 1);
332 m_strcpy(fn, sizeof(fn), folder->ff->name);
333 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
334 snprintf (dest, destlen, tmp, fn);
338 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
339 if (folder->ff->nd->subscribed)
340 snprintf (dest, destlen, tmp, ' ');
342 snprintf (dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
346 snprintf (tmp, sizeof (tmp), "%%%sc", fmt);
347 if (folder->ff->nd->deleted)
348 snprintf (dest, destlen, tmp, 'D');
350 snprintf (dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
354 if (flags & M_FORMAT_OPTIONAL) {
355 if (folder->ff->nd->unread != 0)
356 mutt_FormatString (dest, destlen, ifstring, newsgroup_format_str,
359 mutt_FormatString (dest, destlen, elsestring, newsgroup_format_str,
362 else if (Context && Context->data == folder->ff->nd) {
363 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
364 snprintf (dest, destlen, tmp, Context->unread);
367 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
368 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
373 if (Context && Context->data == folder->ff->nd) {
374 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
375 snprintf (dest, destlen, tmp, Context->new);
377 else if (option (OPTMARKOLD) &&
378 folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
379 folder->ff->nd->lastCached <= folder->ff->nd->lastMessage) {
380 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
381 snprintf (dest, destlen, tmp,
382 folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
385 snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
386 snprintf (dest, destlen, tmp, folder->ff->nd->unread);
391 if (folder->ff->nd->desc != NULL) {
392 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
393 snprintf (dest, destlen, tmp, folder->ff->nd->desc);
396 snprintf (tmp, sizeof (tmp), "%%%ss", fmt);
397 snprintf (dest, destlen, tmp, "");
403 #endif /* USE_NNTP */
406 static void add_folder (MUTTMENU * m, struct browser_state *state,
407 const char *name, const struct stat *s,
410 static void add_folder (MUTTMENU * m, struct browser_state *state,
411 const char *name, const struct stat *s,
415 if (state->entrylen == state->entrymax) {
416 /* need to allocate more space */
417 p_realloc(&state->entry, state->entrymax += 256);
418 p_clear(&state->entry[state->entrylen], 256);
420 m->data = state->entry;
424 (state->entry)[state->entrylen].mode = s->st_mode;
425 (state->entry)[state->entrylen].mtime = s->st_mtime;
426 (state->entry)[state->entrylen].size = s->st_size;
427 (state->entry)[state->entrylen].st = p_dup(s, 1);
430 (state->entry)[state->entrylen].new = new;
431 (state->entry)[state->entrylen].name = m_strdup(name);
432 (state->entry)[state->entrylen].desc = m_strdup(name);
433 (state->entry)[state->entrylen].imap = 0;
435 if (option (OPTNEWS))
436 (state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
441 static void init_state (struct browser_state *state, MUTTMENU * menu)
444 state->entrymax = 256;
445 state->entry = p_new(struct folder_file, state->entrymax);
446 state->imap_browse = 0;
448 menu->data = state->entry;
451 /* get list of all files/newsgroups with mask */
452 static int examine_directory (MUTTMENU * menu, struct browser_state *state,
453 char *d, const char *prefix)
456 if (option (OPTNEWS)) {
459 NNTP_SERVER *news = CurrentNewsSrv;
461 /* buffy_check (0); */
462 init_state (state, menu);
464 for (tmp = news->list; tmp; tmp = tmp->next) {
465 if (!(data = (NNTP_DATA *) tmp->data))
467 nntp_sync_sidebar (data);
468 if (prefix && *prefix && strncmp (prefix, data->group,
469 m_strlen(prefix)) != 0)
471 if (!((regexec (Mask.rx, data->group, 0, NULL, 0) == 0) ^ Mask.not))
474 add_folder (menu, state, data->group, NULL, data, data->new);
476 add_folder (menu, state, data->group, NULL, data->new);
479 sidebar_draw (CurrentMenu);
482 #endif /* USE_NNTP */
487 char buffer[_POSIX_PATH_MAX + SHORT_STRING];
490 while (stat (d, &s) == -1) {
491 if (errno == ENOENT) {
492 /* The last used directory is deleted, try to use the parent dir. */
493 char *c = strrchr (d, '/');
504 if (!S_ISDIR (s.st_mode)) {
505 mutt_error (_("%s is not a directory."), d);
511 if ((dp = opendir (d)) == NULL) {
516 init_state (state, menu);
518 while ((de = readdir (dp)) != NULL) {
519 if (m_strcmp(de->d_name, ".") == 0)
520 continue; /* we don't need . */
522 if (prefix && *prefix
523 && m_strncmp(prefix, de->d_name, m_strlen(prefix)) != 0)
525 if (!((regexec (Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
528 mutt_concat_path(buffer, sizeof(buffer), d, de->d_name);
529 if (lstat (buffer, &s) == -1)
532 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
533 (!S_ISLNK (s.st_mode)))
536 i = buffy_lookup (buffer);
538 add_folder (menu, state, de->d_name, &s, NULL, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
540 add_folder (menu, state, de->d_name, &s, i >= 0 ? ((BUFFY*) Incoming->data[i])->new : 0);
545 sidebar_draw (CurrentMenu);
546 browser_sort (state);
550 /* get list of mailboxes/subscribed newsgroups */
551 static int examine_mailboxes (MUTTMENU * menu, struct browser_state *state)
554 char buffer[LONG_STRING];
557 if (option (OPTNEWS)) {
560 NNTP_SERVER *news = CurrentNewsSrv;
562 /* buffy_check (0); */
563 init_state (state, menu);
565 for (tmp = news->list; tmp; tmp = tmp->next) {
566 if ((data = (NNTP_DATA*) tmp->data) == NULL)
568 nntp_sync_sidebar (data);
569 if ((data->new || (data->subscribed &&
570 (!option (OPTSHOWONLYUNREAD)|| data->unread))))
571 add_folder (menu, state, data->group, NULL, data, data->new);
573 sidebar_draw (CurrentMenu);
585 init_state (state, menu);
587 for (i = 0; i < Incoming->length; i++) {
588 tmp = (BUFFY*) Incoming->data[i];
589 tmp->magic = mx_get_magic (tmp->path);
590 if (tmp->magic == M_IMAP) {
592 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
594 add_folder (menu, state, tmp->path, NULL, tmp->new);
598 if (tmp->magic == M_POP) {
600 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
602 add_folder (menu, state, tmp->path, NULL, tmp->new);
607 if (tmp->magic == M_NNTP) {
608 add_folder (menu, state, tmp->path, NULL, NULL, tmp->new);
612 if (lstat (tmp->path, &s) == -1)
615 if ((!S_ISREG (s.st_mode)) && (!S_ISDIR (s.st_mode)) &&
616 (!S_ISLNK (s.st_mode)))
619 m_strcpy(buffer, sizeof(buffer), NONULL(tmp->path));
620 mutt_pretty_mailbox (buffer);
623 add_folder (menu, state, buffer, &s, NULL, tmp->new);
625 add_folder (menu, state, buffer, &s, tmp->new);
629 browser_sort (state);
633 static int select_file_search (MUTTMENU * menu, regex_t * re, int n)
636 if (option (OPTNEWS))
638 (re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
641 (re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
644 static void folder_entry (char *s, ssize_t slen, MUTTMENU * menu, int num)
648 folder.ff = &((struct folder_file *) menu->data)[num];
652 if (option (OPTNEWS))
653 mutt_FormatString (s, slen, NONULL (GroupFormat), newsgroup_format_str,
654 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
657 mutt_FormatString (s, slen, NONULL (FolderFormat), folder_format_str,
658 (unsigned long) &folder, M_FORMAT_ARROWCURSOR);
661 static void init_menu (struct browser_state *state, MUTTMENU * menu,
662 char *title, ssize_t titlelen, int buffy)
664 char path[_POSIX_PATH_MAX];
666 menu->max = state->entrylen;
668 if (menu->current >= menu->max)
669 menu->current = menu->max - 1;
670 if (menu->current < 0)
672 if (menu->top > menu->current)
678 if (option (OPTNEWS)) {
680 snprintf (title, titlelen, "%s", _("Subscribed newsgroups"));
682 snprintf (title, titlelen, _("Newsgroups on server [%s]"),
683 CurrentNewsSrv->conn->account.host);
688 snprintf(title, titlelen, _("Mailboxes [%d]"), buffy_check(0));
690 m_strcpy(path, sizeof(path), LastDir);
691 mutt_pretty_mailbox (path);
692 if (state->imap_browse && option (OPTIMAPLSUB))
693 snprintf (title, titlelen, _("Subscribed [%s], File mask: %s"),
694 path, NONULL (Mask.pattern));
696 snprintf (title, titlelen, _("Directory [%s], File mask: %s"),
697 path, NONULL (Mask.pattern));
699 menu->redraw = REDRAW_FULL;
702 static int file_tag (MUTTMENU * menu, int n, int m)
704 struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
707 if (S_ISDIR (ff->mode)
708 || (S_ISLNK (ff->mode) && link_is_dir (LastDir, ff->name))) {
709 mutt_error _("Can't attach a directory!");
715 ff->tagged = (m >= 0 ? m : !ff->tagged);
717 return ff->tagged - ot;
720 void _mutt_select_file (char *f, ssize_t flen, int flags, char ***files,
723 char buf[_POSIX_PATH_MAX];
724 char prefix[_POSIX_PATH_MAX] = "";
725 char helpstr[SHORT_STRING];
727 struct browser_state state;
730 int i, killPrefix = 0;
731 int multiple = (flags & M_SEL_MULTI) ? 1 : 0;
732 int folder = (flags & M_SEL_FOLDER) ? 1 : 0;
733 int buffy = (flags & M_SEL_BUFFY) ? 1 : 0;
735 buffy = buffy && folder;
740 m_strcpy(LastDirBackup, sizeof(LastDirBackup), LastDir);
743 if (option (OPTNEWS)) {
745 m_strcpy(prefix, sizeof(prefix), f);
749 /* default state for news reader mode is browse subscribed newsgroups */
751 for (list = CurrentNewsSrv->list; list; list = list->next) {
752 NNTP_DATA *data = (NNTP_DATA *) list->data;
754 if (data && data->subscribed) {
764 mutt_expand_path (f, flen);
765 if (imap_is_magic (f, NULL) == M_IMAP) {
766 init_state (&state, NULL);
767 state.imap_browse = 1;
768 if (!imap_browse (f, &state))
769 m_strcpy(LastDir, sizeof(LastDir), state.folder);
772 for (i = m_strlen(f) - 1; i > 0 && f[i] != '/'; i--);
775 i = MIN(ssizeof(LastDir) - 1, i);
776 m_strcpy(LastDir, sizeof(LastDir), f);
779 getcwd(LastDir, sizeof(LastDir));
780 m_strcat(LastDir, sizeof(LastDir), "/");
781 m_strncat(LastDir, sizeof(LastDir), f, i);
786 strcpy (LastDir, "/"); /* __STRCPY_CHECKED__ */
788 getcwd (LastDir, sizeof (LastDir));
791 if (i <= 0 && f[0] != '/')
792 m_strcpy(prefix, sizeof(prefix), f);
794 m_strcpy(prefix, sizeof(prefix), f + i + 1);
800 getcwd (LastDir, sizeof (LastDir));
801 else if (!LastDir[0])
802 m_strcpy(LastDir, sizeof(LastDir), NONULL(Maildir));
804 if (!buffy && imap_is_magic (LastDir, NULL) == M_IMAP) {
805 init_state (&state, NULL);
806 state.imap_browse = 1;
807 imap_browse (LastDir, &state);
808 browser_sort (&state);
815 if (examine_mailboxes (NULL, &state) == -1)
819 if (!state.imap_browse)
820 if (examine_directory (NULL, &state, LastDir, prefix) == -1)
823 menu = mutt_new_menu ();
824 menu->menu = MENU_FOLDER;
825 menu->make_entry = folder_entry;
826 menu->search = select_file_search;
828 menu->data = state.entry;
830 menu->tag = file_tag;
832 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_FOLDER,
834 (option (OPTNEWS)) ? FolderNewsHelp :
838 init_menu (&state, menu, title, sizeof (title), buffy);
841 switch (i = mutt_menuLoop (menu)) {
842 case OP_GENERIC_SELECT_ENTRY:
844 if (!state.entrylen) {
845 mutt_error _("No files match the file mask");
850 if (S_ISDIR (state.entry[menu->current].mode) ||
851 (S_ISLNK (state.entry[menu->current].mode) &&
852 link_is_dir (LastDir, state.entry[menu->current].name))
853 || state.entry[menu->current].inferiors
855 /* make sure this isn't a MH or maildir mailbox */
857 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
858 mutt_expand_path (buf, sizeof (buf));
860 else if (state.imap_browse) {
861 m_strcpy(buf, sizeof(buf), state.entry[menu->current].name);
864 mutt_concat_path(buf, sizeof(buf), LastDir,
865 state.entry[menu->current].name);
867 if ((mx_get_magic (buf) <= 0)
868 || state.entry[menu->current].inferiors)
870 char OldLastDir[_POSIX_PATH_MAX];
872 /* save the old directory */
873 m_strcpy(OldLastDir, sizeof(OldLastDir), LastDir);
875 if (m_strcmp(state.entry[menu->current].name, "..") == 0) {
876 if (m_strcmp("..", LastDir + m_strlen(LastDir) - 2) == 0)
877 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
879 char *p = strrchr (LastDir + 1, '/');
884 if (LastDir[0] == '/')
887 strcat (LastDir, "/.."); /* __STRCAT_CHECKED__ */
892 m_strcpy(LastDir, sizeof(LastDir),
893 state.entry[menu->current].name);
894 mutt_expand_path (LastDir, sizeof (LastDir));
896 else if (state.imap_browse) {
900 m_strcpy(LastDir, sizeof(LastDir),
901 state.entry[menu->current].name);
902 /* tack on delimiter here */
903 n = m_strlen(LastDir) + 1;
905 /* special case "" needs no delimiter */
906 url_parse_ciss (&url, state.entry[menu->current].name);
908 (state.entry[menu->current].delim != '\0') &&
909 (n < ssizeof (LastDir))) {
911 LastDir[n - 1] = state.entry[menu->current].delim;
915 char tmp[_POSIX_PATH_MAX];
917 mutt_concat_path(tmp, sizeof(tmp), LastDir,
918 state.entry[menu->current].name);
919 m_strcpy(LastDir, sizeof(LastDir), tmp);
922 destroy_state (&state);
928 if (state.imap_browse) {
929 init_state (&state, NULL);
930 state.imap_browse = 1;
931 imap_browse (LastDir, &state);
932 browser_sort (&state);
933 menu->data = state.entry;
936 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
937 /* try to restore the old values */
938 m_strcpy(LastDir, sizeof(LastDir), OldLastDir);
939 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
940 m_strcpy(LastDir, sizeof(LastDir), NONULL(Homedir));
946 init_menu (&state, menu, title, sizeof (title), buffy);
952 if (buffy || option (OPTNEWS)) /* news have not path */
957 m_strcpy(f, flen, state.entry[menu->current].name);
958 mutt_expand_path (f, flen);
960 else if (state.imap_browse)
961 m_strcpy(f, flen, state.entry[menu->current].name);
963 mutt_concat_path(f, flen, LastDir, state.entry[menu->current].name);
965 /* Fall through to OP_EXIT */
975 *numfiles = menu->tagged;
976 tfiles = p_new(char *, *numfiles);
977 for (h = 0, j = 0; h < state.entrylen; i++) {
978 struct folder_file ff = state.entry[i];
979 char full[_POSIX_PATH_MAX];
982 mutt_concat_path(full, sizeof(full), LastDir, ff.name);
983 mutt_expand_path (full, sizeof (full));
984 tfiles[j++] = m_strdup(full);
989 else if (f[0]) { /* no tagged entries. return selected entry */
991 tfiles = p_new(char *, *numfiles);
992 mutt_expand_path (f, flen);
993 tfiles[0] = m_strdup(f);
998 destroy_state (&state);
999 mutt_menuDestroy (&menu);
1002 case OP_BROWSER_TELL:
1004 mutt_message ("%s", state.entry[menu->current].name);
1007 case OP_BROWSER_TOGGLE_LSUB:
1008 if (option (OPTIMAPLSUB)) {
1009 unset_option (OPTIMAPLSUB);
1012 set_option (OPTIMAPLSUB);
1014 mutt_ungetch (0, OP_CHECK_NEW);
1017 case OP_CREATE_MAILBOX:
1018 if (!state.imap_browse)
1019 mutt_error (_("Create is only supported for IMAP mailboxes"));
1021 imap_mailbox_create (LastDir);
1022 /* TODO: find a way to detect if the new folder would appear in
1023 * this window, and insert it without starting over. */
1024 destroy_state (&state);
1025 init_state (&state, NULL);
1026 state.imap_browse = 1;
1027 imap_browse (LastDir, &state);
1028 browser_sort (&state);
1029 menu->data = state.entry;
1032 init_menu (&state, menu, title, sizeof (title), buffy);
1033 MAYBE_REDRAW (menu->redraw);
1037 case OP_RENAME_MAILBOX:
1038 if (!state.entry[menu->current].imap)
1039 mutt_error (_("Rename is only supported for IMAP mailboxes"));
1041 int nentry = menu->current;
1043 if (imap_mailbox_rename (state.entry[nentry].name) >= 0) {
1044 destroy_state (&state);
1045 init_state (&state, NULL);
1046 state.imap_browse = 1;
1047 imap_browse (LastDir, &state);
1048 browser_sort (&state);
1049 menu->data = state.entry;
1052 init_menu (&state, menu, title, sizeof (title), buffy);
1053 MAYBE_REDRAW (menu->redraw);
1058 case OP_DELETE_MAILBOX:
1059 if (!state.entry[menu->current].imap)
1060 mutt_error (_("Delete is only supported for IMAP mailboxes"));
1062 char msg[SHORT_STRING];
1064 int nentry = menu->current;
1066 imap_parse_path (state.entry[nentry].name, &mx);
1067 snprintf (msg, sizeof (msg), _("Really delete mailbox \"%s\"?"),
1069 if (mutt_yesorno (msg, M_NO) == M_YES) {
1070 if (!imap_delete_mailbox (Context, mx)) {
1071 /* free the mailbox from the browser */
1072 p_delete(&((state.entry)[nentry].name));
1073 p_delete(&((state.entry)[nentry].desc));
1074 /* and move all other entries up */
1075 if (nentry + 1 < state.entrylen)
1076 memmove (state.entry + nentry, state.entry + nentry + 1,
1077 sizeof (struct folder_file) * (state.entrylen -
1080 mutt_message _("Mailbox deleted.");
1082 init_menu (&state, menu, title, sizeof (title), buffy);
1083 MAYBE_REDRAW (menu->redraw);
1087 mutt_message _("Mailbox not deleted.");
1092 case OP_CHANGE_DIRECTORY:
1095 if (option (OPTNEWS))
1099 m_strcpy(buf, sizeof(buf), LastDir);
1100 if (!state.imap_browse)
1102 /* add '/' at the end of the directory name if not already there */
1103 ssize_t len = m_strlen(LastDir);
1105 if (len && LastDir[len - 1] != '/' && ssizeof(buf) > len)
1109 if (mutt_get_field (_("Chdir to: "), buf, sizeof (buf), M_FILE) == 0 &&
1112 mutt_expand_path (buf, sizeof (buf));
1113 if (imap_is_magic (buf, NULL) == M_IMAP) {
1114 m_strcpy(LastDir, sizeof(LastDir), buf);
1115 destroy_state (&state);
1116 init_state (&state, NULL);
1117 state.imap_browse = 1;
1118 imap_browse (LastDir, &state);
1119 browser_sort (&state);
1120 menu->data = state.entry;
1123 init_menu (&state, menu, title, sizeof (title), buffy);
1126 if (stat (buf, &st) == 0) {
1127 if (S_ISDIR (st.st_mode)) {
1128 destroy_state (&state);
1129 if (examine_directory (menu, &state, buf, prefix) == 0)
1130 m_strcpy(LastDir, sizeof(LastDir), buf);
1132 mutt_error _("Error scanning directory.");
1134 if (examine_directory (menu, &state, LastDir, prefix) == -1) {
1135 mutt_menuDestroy (&menu);
1141 init_menu (&state, menu, title, sizeof (title), buffy);
1144 mutt_error (_("%s is not a directory."), buf);
1149 MAYBE_REDRAW (menu->redraw);
1154 m_strcpy(buf, sizeof(buf), NONULL(Mask.pattern));
1155 if (mutt_get_field (_("File Mask: "), buf, sizeof (buf), 0) == 0) {
1156 regex_t *rx = p_new(regex_t, 1);
1161 /* assume that the user wants to see everything */
1163 m_strcpy(buf, sizeof(buf), ".");
1166 s = vskipspaces(s + 1);
1170 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1171 regerror (err, rx, buf, sizeof (buf));
1174 mutt_error ("%s", buf);
1177 m_strreplace(&Mask.pattern, buf);
1183 destroy_state (&state);
1184 if (state.imap_browse) {
1185 init_state (&state, NULL);
1186 state.imap_browse = 1;
1187 imap_browse (LastDir, &state);
1188 browser_sort (&state);
1189 menu->data = state.entry;
1190 init_menu (&state, menu, title, sizeof (title), buffy);
1193 if (examine_directory (menu, &state, LastDir, NULL) == 0)
1194 init_menu (&state, menu, title, sizeof (title), buffy);
1196 mutt_error _("Error scanning directory.");
1198 mutt_menuDestroy (&menu);
1202 if (!state.entrylen) {
1203 mutt_error _("No files match the file mask");
1209 MAYBE_REDRAW (menu->redraw);
1213 case OP_SORT_REVERSE:
1217 int reverse = (i == OP_SORT_REVERSE);
1219 switch (mutt_multi_choice ((reverse) ?
1221 ("Reverse sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? ")
1224 ("Sort by (d)ate, (a)lpha, si(z)e or do(n)'t sort? "),
1226 case -1: /* abort */
1230 case 1: /* (d)ate */
1231 BrowserSort = SORT_DATE;
1234 case 2: /* (a)lpha */
1235 BrowserSort = SORT_SUBJECT;
1238 case 3: /* si(z)e */
1239 BrowserSort = SORT_SIZE;
1242 case 4: /* do(n)'t sort */
1243 BrowserSort = SORT_ORDER;
1248 BrowserSort |= reverse ? SORT_REVERSE : 0;
1249 browser_sort (&state);
1250 menu->redraw = REDRAW_FULL;
1255 case OP_TOGGLE_MAILBOXES:
1259 destroy_state (&state);
1264 if (examine_mailboxes (menu, &state) == -1)
1267 else if (imap_is_magic (LastDir, NULL) == M_IMAP) {
1268 init_state (&state, NULL);
1269 state.imap_browse = 1;
1270 imap_browse (LastDir, &state);
1271 browser_sort (&state);
1272 menu->data = state.entry;
1274 else if (examine_directory (menu, &state, LastDir, prefix) == -1)
1276 init_menu (&state, menu, title, sizeof (title), buffy);
1280 if (option (OPTFORCEBUFFYCHECK))
1285 case OP_BROWSER_NEW_FILE:
1287 snprintf (buf, sizeof (buf), "%s/", LastDir);
1288 if (mutt_get_field (_("New file name: "), buf, sizeof (buf), M_FILE) ==
1290 m_strcpy(f, flen, buf);
1291 destroy_state (&state);
1292 mutt_menuDestroy (&menu);
1295 MAYBE_REDRAW (menu->redraw);
1298 case OP_BROWSER_VIEW_FILE:
1299 if (!state.entrylen) {
1300 mutt_error _("No files match the file mask");
1305 if (state.entry[menu->current].selectable) {
1306 m_strcpy(f, flen, state.entry[menu->current].name);
1307 destroy_state (&state);
1308 mutt_menuDestroy (&menu);
1312 if (S_ISDIR (state.entry[menu->current].mode) ||
1313 (S_ISLNK (state.entry[menu->current].mode) &&
1314 link_is_dir (LastDir, state.entry[menu->current].name))) {
1315 mutt_error _("Can't view a directory");
1321 char nbuf[_POSIX_PATH_MAX];
1323 mutt_concat_path(nbuf, sizeof(nbuf), LastDir,
1324 state.entry[menu->current].name);
1325 b = mutt_make_file_attach (nbuf);
1327 mutt_view_attachment (NULL, b, M_REGULAR, NULL, NULL, 0);
1329 menu->redraw = REDRAW_FULL;
1332 mutt_error _("Error trying to view file");
1339 if (option (OPTNEWS)) {
1340 struct folder_file *folder_f = &state.entry[menu->current];
1343 if (i == OP_CATCHUP)
1344 nd = mutt_newsgroup_catchup (CurrentNewsSrv, folder_f->name);
1346 nd = mutt_newsgroup_uncatchup (CurrentNewsSrv, folder_f->name);
1349 if (menu->current + 1 < menu->max)
1351 menu->redraw = REDRAW_MOTION_RESYNCH;
1356 case OP_LOAD_ACTIVE:
1357 if (!option (OPTNEWS))
1364 for (tmp = CurrentNewsSrv->list; tmp; tmp = tmp->next) {
1365 if ((data = (NNTP_DATA *) tmp->data))
1369 nntp_get_active (CurrentNewsSrv);
1371 destroy_state (&state);
1373 examine_mailboxes (menu, &state);
1375 examine_directory (menu, &state, NULL, NULL);
1376 init_menu (&state, menu, title, sizeof (title), buffy);
1378 #endif /* USE_NNTP */
1380 case OP_BROWSER_SUBSCRIBE:
1381 case OP_BROWSER_UNSUBSCRIBE:
1383 case OP_SUBSCRIBE_PATTERN:
1384 case OP_UNSUBSCRIBE_PATTERN:
1385 if (option (OPTNEWS)) {
1386 regex_t *rx = p_new(regex_t, 1);
1388 int j = menu->current;
1390 NNTP_SERVER *news = CurrentNewsSrv;
1392 if (i == OP_SUBSCRIBE_PATTERN || i == OP_UNSUBSCRIBE_PATTERN) {
1397 if (i == OP_SUBSCRIBE_PATTERN)
1398 snprintf (tmp, sizeof (tmp), _("Subscribe pattern: "));
1400 snprintf (tmp, sizeof (tmp), _("Unsubscribe pattern: "));
1401 if (mutt_get_field (tmp, buf, sizeof (buf), 0) != 0 || !buf[0]) {
1406 if ((err = REGCOMP (rx, s, REG_NOSUB)) != 0) {
1407 regerror (err, rx, buf, sizeof (buf));
1410 mutt_error ("%s", buf);
1413 menu->redraw = REDRAW_FULL;
1416 else if (!state.entrylen) {
1417 mutt_error _("No newsgroups match the mask");
1422 for (; j < state.entrylen; j++) {
1423 struct folder_file *folderf = &state.entry[j];
1425 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE ||
1426 regexec (rx, folderf->name, 0, NULL, 0) == 0) {
1427 if (i == OP_BROWSER_SUBSCRIBE || i == OP_SUBSCRIBE_PATTERN)
1428 nd = mutt_newsgroup_subscribe (news, folderf->name);
1430 nd = mutt_newsgroup_unsubscribe (news, folderf->name);
1432 if (i == OP_BROWSER_SUBSCRIBE || i == OP_BROWSER_UNSUBSCRIBE) {
1433 if (menu->current + 1 < menu->max)
1435 menu->redraw = REDRAW_MOTION_RESYNCH;
1439 if (i == OP_SUBSCRIBE_PATTERN) {
1440 string_list_t *grouplist = NULL;
1443 grouplist = news->list;
1444 for (; grouplist; grouplist = grouplist->next) {
1445 nd = (NNTP_DATA *) grouplist->data;
1446 if (nd && nd->group && !nd->subscribed) {
1447 if (regexec (rx, nd->group, 0, NULL, 0) == 0) {
1448 mutt_newsgroup_subscribe (news, nd->group);
1449 add_folder (menu, &state, nd->group, NULL, nd, nd->new);
1453 init_menu (&state, menu, title, sizeof (title), buffy);
1455 mutt_newsrc_update (news);
1456 nntp_clear_cacheindex (news);
1457 if (i != OP_BROWSER_SUBSCRIBE && i != OP_BROWSER_UNSUBSCRIBE)
1462 #endif /* USE_NNTP */
1464 if (i == OP_BROWSER_SUBSCRIBE)
1465 imap_subscribe (state.entry[menu->current].name, 1);
1467 imap_subscribe (state.entry[menu->current].name, 0);
1475 m_strcpy(LastDir, sizeof(LastDir), LastDirBackup);