forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.c
2056 lines (1846 loc) · 56.2 KB
/
browser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 1996-2000,2007,2010,2013 Michael R. Elkins <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "mutt.h"
#include "browser.h"
#include "attach.h"
#include "buffy.h"
#include "mailbox.h"
#include "mapping.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mx.h"
#include "sort.h"
#ifdef USE_IMAP
#include "imap/imap.h"
#endif
#ifdef USE_NNTP
#include "nntp.h"
#endif
#ifdef USE_NOTMUCH
#include "mutt_notmuch.h"
#endif
static const struct mapping_t FolderHelp[] = {
{ N_("Exit"), OP_EXIT },
{ N_("Chdir"), OP_CHANGE_DIRECTORY },
{ N_("Goto"), OP_BROWSER_GOTO_FOLDER },
{ N_("Mask"), OP_ENTER_MASK },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#ifdef USE_NNTP
static struct mapping_t FolderNewsHelp[] = {
{ N_("Exit"), OP_EXIT },
{ N_("List"), OP_TOGGLE_MAILBOXES },
{ N_("Subscribe"), OP_BROWSER_SUBSCRIBE },
{ N_("Unsubscribe"), OP_BROWSER_UNSUBSCRIBE },
{ N_("Catchup"), OP_CATCHUP },
{ N_("Mask"), OP_ENTER_MASK },
{ N_("Help"), OP_HELP },
{ NULL, 0 },
};
#endif
typedef struct folder_t
{
struct folder_file *ff;
int num;
} FOLDER;
static char OldLastDir[_POSIX_PATH_MAX] = "";
static char LastDir[_POSIX_PATH_MAX] = "";
/* Frees up the memory allocated for the local-global variables. */
static void destroy_state(struct browser_state *state)
{
int c;
for (c = 0; c < state->entrylen; c++)
{
FREE(&((state->entry)[c].name));
FREE(&((state->entry)[c].desc));
}
#ifdef USE_IMAP
FREE(&state->folder);
#endif
FREE(&state->entry);
}
static int browser_compare_subject(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
/* inbox should be sorted ahead of its siblings */
int r = mutt_inbox_cmp(pa->name, pb->name);
if (r == 0)
r = mutt_strcoll(pa->name, pb->name);
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
static int browser_compare_desc(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
int r = mutt_strcoll(pa->desc, pb->desc);
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
static int browser_compare_date(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
int r = pa->mtime - pb->mtime;
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
static int browser_compare_size(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
int r = pa->size - pb->size;
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
static int browser_compare_count(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
int r = 0;
if (pa->has_buffy && pb->has_buffy)
r = pa->msg_count - pb->msg_count;
else if (pa->has_buffy)
r = -1;
else
r = 1;
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
static int browser_compare_count_new(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
int r = 0;
if (pa->has_buffy && pb->has_buffy)
r = pa->msg_unread - pb->msg_unread;
else if (pa->has_buffy)
r = -1;
else
r = 1;
return ((BrowserSort & SORT_REVERSE) ? -r : r);
}
/* Wild compare function that calls the others. It's useful
* because it provides a way to tell "../" is always on the
* top of the list, independently of the sort method.
*/
static int browser_compare(const void *a, const void *b)
{
struct folder_file *pa = (struct folder_file *) a;
struct folder_file *pb = (struct folder_file *) b;
if ((mutt_strcoll(pa->desc, "../") == 0) || (mutt_strcoll(pa->desc, "..") == 0))
return -1;
if ((mutt_strcoll(pb->desc, "../") == 0) || (mutt_strcoll(pb->desc, "..") == 0))
return 1;
switch (BrowserSort & SORT_MASK)
{
case SORT_DATE:
return browser_compare_date(a, b);
case SORT_SIZE:
return browser_compare_size(a, b);
case SORT_DESC:
return browser_compare_desc(a, b);
case SORT_COUNT:
return browser_compare_count(a, b);
case SORT_UNREAD:
return browser_compare_count_new(a, b);
case SORT_SUBJECT:
default:
return browser_compare_subject(a, b);
}
}
/* Call to qsort using browser_compare function. Some
* specific sort methods are not used via NNTP.
*/
static void browser_sort(struct browser_state *state)
{
switch (BrowserSort & SORT_MASK)
{
/* Also called "I don't care"-sort-method. */
case SORT_ORDER:
return;
#ifdef USE_NNTP
case SORT_SIZE:
case SORT_DATE:
if (option(OPTNEWS))
return;
#endif
default:
break;
}
qsort(state->entry, state->entrylen, sizeof(struct folder_file), browser_compare);
}
static int link_is_dir(const char *folder, const char *path)
{
struct stat st;
char fullpath[_POSIX_PATH_MAX];
mutt_concat_path(fullpath, folder, path, sizeof(fullpath));
if (stat(fullpath, &st) == 0)
return (S_ISDIR(st.st_mode));
else
return 0;
}
static const char *folder_format_str(char *dest, size_t destlen, size_t col, int cols,
char op, const char *src, const char *fmt,
const char *ifstring, const char *elsestring,
unsigned long data, format_flag flags)
{
char fn[SHORT_STRING], tmp[SHORT_STRING], permission[11];
char date[SHORT_STRING], *t_fmt = NULL;
time_t tnow;
FOLDER *folder = (FOLDER *) data;
struct passwd *pw = NULL;
struct group *gr = NULL;
int optional = (flags & MUTT_FORMAT_OPTIONAL);
switch (op)
{
case 'C':
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->num + 1);
break;
case 'd':
case 'D':
if (folder->ff->local)
{
int do_locales = true;
if (op == 'D')
{
t_fmt = NONULL(DateFmt);
if (*t_fmt == '!')
{
++t_fmt;
do_locales = false;
}
}
else
{
tnow = time(NULL);
t_fmt =
tnow - folder->ff->mtime < 31536000 ? "%b %d %H:%M" : "%b %d %Y";
}
if (!do_locales)
setlocale(LC_TIME, "C");
strftime(date, sizeof(date), t_fmt, localtime(&folder->ff->mtime));
if (!do_locales)
setlocale(LC_TIME, "");
mutt_format_s(dest, destlen, fmt, date);
}
else
mutt_format_s(dest, destlen, fmt, "");
break;
case 'f':
{
char *s = NULL;
#ifdef USE_NOTMUCH
if (mx_is_notmuch(folder->ff->name))
s = NONULL(folder->ff->desc);
else
#endif
#ifdef USE_IMAP
if (folder->ff->imap)
s = NONULL(folder->ff->desc);
else
#endif
s = NONULL(folder->ff->name);
snprintf(fn, sizeof(fn), "%s%s", s,
folder->ff->local ?
(S_ISLNK(folder->ff->mode) ?
"@" :
(S_ISDIR(folder->ff->mode) ?
"/" :
((folder->ff->mode & S_IXUSR) != 0 ? "*" : ""))) :
"");
mutt_format_s(dest, destlen, fmt, fn);
break;
}
case 'F':
if (folder->ff->local)
{
snprintf(permission, sizeof(permission), "%c%c%c%c%c%c%c%c%c%c",
S_ISDIR(folder->ff->mode) ? 'd' : (S_ISLNK(folder->ff->mode) ? 'l' : '-'),
(folder->ff->mode & S_IRUSR) != 0 ? 'r' : '-',
(folder->ff->mode & S_IWUSR) != 0 ? 'w' : '-',
(folder->ff->mode & S_ISUID) != 0 ?
's' :
(folder->ff->mode & S_IXUSR) != 0 ? 'x' : '-',
(folder->ff->mode & S_IRGRP) != 0 ? 'r' : '-',
(folder->ff->mode & S_IWGRP) != 0 ? 'w' : '-',
(folder->ff->mode & S_ISGID) != 0 ?
's' :
(folder->ff->mode & S_IXGRP) != 0 ? 'x' : '-',
(folder->ff->mode & S_IROTH) != 0 ? 'r' : '-',
(folder->ff->mode & S_IWOTH) != 0 ? 'w' : '-',
(folder->ff->mode & S_ISVTX) != 0 ?
't' :
(folder->ff->mode & S_IXOTH) != 0 ? 'x' : '-');
mutt_format_s(dest, destlen, fmt, permission);
}
#ifdef USE_IMAP
else if (folder->ff->imap)
{
/* mark folders with subfolders AND mail */
snprintf(permission, sizeof(permission), "IMAP %c",
(folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
mutt_format_s(dest, destlen, fmt, permission);
}
#endif
else
mutt_format_s(dest, destlen, fmt, "");
break;
case 'g':
if (folder->ff->local)
{
if ((gr = getgrgid(folder->ff->gid)))
mutt_format_s(dest, destlen, fmt, gr->gr_name);
else
{
snprintf(tmp, sizeof(tmp), "%%%sld", fmt);
snprintf(dest, destlen, tmp, folder->ff->gid);
}
}
else
mutt_format_s(dest, destlen, fmt, "");
break;
case 'l':
if (folder->ff->local)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->ff->nlink);
}
else
mutt_format_s(dest, destlen, fmt, "");
break;
case 'm':
if (!optional)
{
if (folder->ff->has_buffy)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->ff->msg_count);
}
else
mutt_format_s(dest, destlen, fmt, "");
}
else if (!folder->ff->msg_count)
optional = 0;
break;
case 'N':
snprintf(tmp, sizeof(tmp), "%%%sc", fmt);
snprintf(dest, destlen, tmp, folder->ff->new ? 'N' : ' ');
break;
case 'n':
if (!optional)
{
if (folder->ff->has_buffy)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->ff->msg_unread);
}
else
mutt_format_s(dest, destlen, fmt, "");
}
else if (!folder->ff->msg_unread)
optional = 0;
break;
case 's':
if (folder->ff->local)
{
mutt_pretty_size(fn, sizeof(fn), folder->ff->size);
snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
snprintf(dest, destlen, tmp, fn);
}
else
mutt_format_s(dest, destlen, fmt, "");
break;
case 't':
snprintf(tmp, sizeof(tmp), "%%%sc", fmt);
snprintf(dest, destlen, tmp, folder->ff->tagged ? '*' : ' ');
break;
case 'u':
if (folder->ff->local)
{
if ((pw = getpwuid(folder->ff->uid)))
mutt_format_s(dest, destlen, fmt, pw->pw_name);
else
{
snprintf(tmp, sizeof(tmp), "%%%sld", fmt);
snprintf(dest, destlen, tmp, folder->ff->uid);
}
}
else
mutt_format_s(dest, destlen, fmt, "");
break;
default:
snprintf(tmp, sizeof(tmp), "%%%sc", fmt);
snprintf(dest, destlen, tmp, op);
break;
}
if (optional)
mutt_FormatString(dest, destlen, col, cols, ifstring, folder_format_str, data, 0);
else if (flags & MUTT_FORMAT_OPTIONAL)
mutt_FormatString(dest, destlen, col, cols, elsestring, folder_format_str, data, 0);
return src;
}
#ifdef USE_NNTP
static const char *newsgroup_format_str(char *dest, size_t destlen, size_t col, int cols,
char op, const char *src, const char *fmt,
const char *ifstring, const char *elsestring,
unsigned long data, format_flag flags)
{
char fn[SHORT_STRING], tmp[SHORT_STRING];
FOLDER *folder = (FOLDER *) data;
switch (op)
{
case 'C':
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->num + 1);
break;
case 'f':
strncpy(fn, folder->ff->name, sizeof(fn) - 1);
snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
snprintf(dest, destlen, tmp, fn);
break;
case 'N':
snprintf(tmp, sizeof(tmp), "%%%sc", fmt);
if (folder->ff->nd->subscribed)
snprintf(dest, destlen, tmp, ' ');
else
snprintf(dest, destlen, tmp, folder->ff->new ? 'N' : 'u');
break;
case 'M':
snprintf(tmp, sizeof(tmp), "%%%sc", fmt);
if (folder->ff->nd->deleted)
snprintf(dest, destlen, tmp, 'D');
else
snprintf(dest, destlen, tmp, folder->ff->nd->allowed ? ' ' : '-');
break;
case 's':
if (flags & MUTT_FORMAT_OPTIONAL)
{
if (folder->ff->nd->unread != 0)
mutt_FormatString(dest, destlen, col, cols, ifstring,
newsgroup_format_str, data, flags);
else
mutt_FormatString(dest, destlen, col, cols, elsestring,
newsgroup_format_str, data, flags);
}
else if (Context && Context->data == folder->ff->nd)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, Context->unread);
}
else
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->ff->nd->unread);
}
break;
case 'n':
if (Context && Context->data == folder->ff->nd)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, Context->new);
}
else if (option(OPTMARKOLD) &&
folder->ff->nd->lastCached >= folder->ff->nd->firstMessage &&
folder->ff->nd->lastCached <= folder->ff->nd->lastMessage)
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp,
folder->ff->nd->lastMessage - folder->ff->nd->lastCached);
}
else
{
snprintf(tmp, sizeof(tmp), "%%%sd", fmt);
snprintf(dest, destlen, tmp, folder->ff->nd->unread);
}
break;
case 'd':
if (folder->ff->nd->desc != NULL)
{
char *buf = safe_strdup(folder->ff->nd->desc);
if (NewsgroupsCharset && *NewsgroupsCharset)
mutt_convert_string(&buf, NewsgroupsCharset, Charset, MUTT_ICONV_HOOK_FROM);
mutt_filter_unprintable(&buf);
snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
snprintf(dest, destlen, tmp, buf);
FREE(&buf);
}
else
{
snprintf(tmp, sizeof(tmp), "%%%ss", fmt);
snprintf(dest, destlen, tmp, "");
}
break;
}
return src;
}
#endif /* USE_NNTP */
static void add_folder(MUTTMENU *m, struct browser_state *state, const char *name,
const char *desc, const struct stat *s, BUFFY *b, void *data)
{
if (state->entrylen == state->entrymax)
{
/* need to allocate more space */
safe_realloc(&state->entry, sizeof(struct folder_file) * (state->entrymax += 256));
memset(&state->entry[state->entrylen], 0, sizeof(struct folder_file) * 256);
if (m)
m->data = state->entry;
}
if (s != NULL)
{
(state->entry)[state->entrylen].mode = s->st_mode;
(state->entry)[state->entrylen].mtime = s->st_mtime;
(state->entry)[state->entrylen].size = s->st_size;
(state->entry)[state->entrylen].gid = s->st_gid;
(state->entry)[state->entrylen].uid = s->st_uid;
(state->entry)[state->entrylen].nlink = s->st_nlink;
(state->entry)[state->entrylen].local = true;
}
else
(state->entry)[state->entrylen].local = false;
if (b)
{
(state->entry)[state->entrylen].has_buffy = true;
(state->entry)[state->entrylen].new = b->new;
(state->entry)[state->entrylen].msg_count = b->msg_count;
(state->entry)[state->entrylen].msg_unread = b->msg_unread;
}
(state->entry)[state->entrylen].name = safe_strdup(name);
(state->entry)[state->entrylen].desc = safe_strdup(desc ? desc : name);
#ifdef USE_IMAP
(state->entry)[state->entrylen].imap = false;
#endif
#ifdef USE_NNTP
if (option(OPTNEWS))
(state->entry)[state->entrylen].nd = (NNTP_DATA *) data;
#endif
(state->entrylen)++;
}
static void init_state(struct browser_state *state, MUTTMENU *menu)
{
state->entrylen = 0;
state->entrymax = 256;
state->entry = safe_calloc(state->entrymax, sizeof(struct folder_file));
#ifdef USE_IMAP
state->imap_browse = false;
#endif
if (menu)
menu->data = state->entry;
}
/* get list of all files/newsgroups with mask */
static int examine_directory(MUTTMENU *menu, struct browser_state *state,
char *d, const char *prefix)
{
#ifdef USE_NNTP
if (option(OPTNEWS))
{
NNTP_SERVER *nserv = CurrentNewsSrv;
unsigned int i;
init_state(state, menu);
for (i = 0; i < nserv->groups_num; i++)
{
NNTP_DATA *nntp_data = nserv->groups_list[i];
if (!nntp_data)
continue;
if (prefix && *prefix && (strncmp(prefix, nntp_data->group, strlen(prefix)) != 0))
continue;
if (!((regexec(Mask.rx, nntp_data->group, 0, NULL, 0) == 0) ^ Mask.not))
continue;
add_folder(menu, state, nntp_data->group, NULL, NULL, NULL, nntp_data);
}
}
else
#endif /* USE_NNTP */
{
struct stat s;
DIR *dp = NULL;
struct dirent *de = NULL;
char buffer[_POSIX_PATH_MAX + SHORT_STRING];
BUFFY *tmp = NULL;
while (stat(d, &s) == -1)
{
if (errno == ENOENT)
{
/* The last used directory is deleted, try to use the parent dir. */
char *c = strrchr(d, '/');
if (c && (c > d))
{
*c = 0;
continue;
}
}
mutt_perror(d);
return -1;
}
if (!S_ISDIR(s.st_mode))
{
mutt_error(_("%s is not a directory."), d);
return -1;
}
mutt_buffy_check(0);
if ((dp = opendir(d)) == NULL)
{
mutt_perror(d);
return -1;
}
init_state(state, menu);
while ((de = readdir(dp)) != NULL)
{
if (mutt_strcmp(de->d_name, ".") == 0)
continue; /* we don't need . */
if (prefix && *prefix && (mutt_strncmp(prefix, de->d_name, mutt_strlen(prefix)) != 0))
continue;
if (!((regexec(Mask.rx, de->d_name, 0, NULL, 0) == 0) ^ Mask.not))
continue;
mutt_concat_path(buffer, d, de->d_name, sizeof(buffer));
if (lstat(buffer, &s) == -1)
continue;
if ((!S_ISREG(s.st_mode)) && (!S_ISDIR(s.st_mode)) && (!S_ISLNK(s.st_mode)))
continue;
tmp = Incoming;
while (tmp && (mutt_strcmp(buffer, tmp->path) != 0))
tmp = tmp->next;
if (tmp && Context && (mutt_strcmp(tmp->realpath, Context->realpath) == 0))
{
tmp->msg_count = Context->msgcount;
tmp->msg_unread = Context->unread;
}
add_folder(menu, state, de->d_name, NULL, &s, tmp, NULL);
}
closedir(dp);
}
browser_sort(state);
return 0;
}
#ifdef USE_NOTMUCH
static int examine_vfolders(MUTTMENU *menu, struct browser_state *state)
{
BUFFY *tmp = VirtIncoming;
if (!VirtIncoming)
return -1;
mutt_buffy_check(0);
init_state(state, menu);
do
{
if (mx_is_notmuch(tmp->path))
{
nm_nonctx_get_count(tmp->path, &tmp->msg_count, &tmp->msg_unread);
add_folder(menu, state, tmp->path, tmp->desc, NULL, tmp, NULL);
continue;
}
} while ((tmp = tmp->next));
browser_sort(state);
return 0;
}
#endif
/* get list of mailboxes/subscribed newsgroups */
static int examine_mailboxes(MUTTMENU *menu, struct browser_state *state)
{
struct stat s;
char buffer[LONG_STRING];
#ifdef USE_NNTP
if (option(OPTNEWS))
{
NNTP_SERVER *nserv = CurrentNewsSrv;
unsigned int i;
init_state(state, menu);
for (i = 0; i < nserv->groups_num; i++)
{
NNTP_DATA *nntp_data = nserv->groups_list[i];
if (nntp_data &&
(nntp_data->new || (nntp_data->subscribed &&
(nntp_data->unread || !option(OPTSHOWONLYUNREAD)))))
add_folder(menu, state, nntp_data->group, NULL, NULL, NULL, nntp_data);
}
}
else
#endif
{
BUFFY *tmp = Incoming;
if (!Incoming)
return -1;
mutt_buffy_check(0);
init_state(state, menu);
do
{
if (Context && (mutt_strcmp(tmp->realpath, Context->realpath) == 0))
{
tmp->msg_count = Context->msgcount;
tmp->msg_unread = Context->unread;
}
strfcpy(buffer, NONULL(tmp->path), sizeof(buffer));
mutt_pretty_mailbox(buffer, sizeof(buffer));
#ifdef USE_IMAP
if (mx_is_imap(tmp->path))
{
add_folder(menu, state, buffer, NULL, NULL, tmp, NULL);
continue;
}
#endif
#ifdef USE_POP
if (mx_is_pop(tmp->path))
{
add_folder(menu, state, buffer, NULL, NULL, tmp, NULL);
continue;
}
#endif
#ifdef USE_NNTP
if (mx_is_nntp(tmp->path))
{
add_folder(menu, state, tmp->path, NULL, NULL, tmp, NULL);
continue;
}
#endif
if (lstat(tmp->path, &s) == -1)
continue;
if ((!S_ISREG(s.st_mode)) && (!S_ISDIR(s.st_mode)) && (!S_ISLNK(s.st_mode)))
continue;
if (mx_is_maildir(tmp->path))
{
struct stat st2;
char md[_POSIX_PATH_MAX];
snprintf(md, sizeof(md), "%s/new", tmp->path);
if (stat(md, &s) < 0)
s.st_mtime = 0;
snprintf(md, sizeof(md), "%s/cur", tmp->path);
if (stat(md, &st2) < 0)
st2.st_mtime = 0;
if (st2.st_mtime > s.st_mtime)
s.st_mtime = st2.st_mtime;
}
add_folder(menu, state, buffer, NULL, &s, tmp, NULL);
} while ((tmp = tmp->next));
}
browser_sort(state);
return 0;
}
static int select_file_search(MUTTMENU *menu, regex_t *re, int n)
{
#ifdef USE_NNTP
if (option(OPTNEWS))
return (regexec(re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
#endif
return (regexec(re, ((struct folder_file *) menu->data)[n].name, 0, NULL, 0));
}
#ifdef USE_NOTMUCH
static int select_vfolder_search(MUTTMENU *menu, regex_t *re, int n)
{
return (regexec(re, ((struct folder_file *) menu->data)[n].desc, 0, NULL, 0));
}
#endif
static void folder_entry(char *s, size_t slen, MUTTMENU *menu, int num)
{
FOLDER folder;
folder.ff = &((struct folder_file *) menu->data)[num];
folder.num = num;
#ifdef USE_NNTP
if (option(OPTNEWS))
mutt_FormatString(s, slen, 0, MuttIndexWindow->cols, NONULL(GroupFormat), newsgroup_format_str,
(unsigned long) &folder, MUTT_FORMAT_ARROWCURSOR);
else
#endif
mutt_FormatString(s, slen, 0, MuttIndexWindow->cols, NONULL(FolderFormat),
folder_format_str, (unsigned long) &folder, MUTT_FORMAT_ARROWCURSOR);
}
#ifdef USE_NOTMUCH
static void vfolder_entry(char *s, size_t slen, MUTTMENU *menu, int num)
{
FOLDER folder;
folder.ff = &((struct folder_file *) menu->data)[num];
folder.num = num;
mutt_FormatString(s, slen, 0, MuttIndexWindow->cols, NONULL(VirtFolderFormat),
folder_format_str, (unsigned long) &folder, MUTT_FORMAT_ARROWCURSOR);
}
#endif
/*
* This function takes a menu and a state and defines the current
* entry that should be highlighted.
*/
static void browser_highlight_default(struct browser_state *state, MUTTMENU *menu)
{
menu->top = 0;
/* Reset menu position to 1.
* We do not risk overflow as the init_menu function changes
* current if it is bigger than state->entrylen.
*/
if ((mutt_strcmp(state->entry[0].desc, "..") == 0) ||
(mutt_strcmp(state->entry[0].desc, "../") == 0))
/* Skip the first entry, unless there's only one entry. */
menu->current = (menu->max > 1);
else
menu->current = 0;
}
static void init_menu(struct browser_state *state, MUTTMENU *menu, char *title,
size_t titlelen, int buffy)
{
char path[_POSIX_PATH_MAX];
menu->max = state->entrylen;
if (menu->current >= menu->max)
menu->current = menu->max - 1;
if (menu->current < 0)
menu->current = 0;
if (menu->top > menu->current)
menu->top = 0;
menu->tagged = 0;
#ifdef USE_NNTP
if (option(OPTNEWS))
{
if (buffy)
snprintf(title, titlelen, _("Subscribed newsgroups"));
else
snprintf(title, titlelen, _("Newsgroups on server [%s]"),
CurrentNewsSrv->conn->account.host);
}
else
#endif
{
if (buffy)
{
menu->is_mailbox_list = 1;
snprintf(title, titlelen, _("Mailboxes [%d]"), mutt_buffy_check(0));
}
else
{
menu->is_mailbox_list = 0;
strfcpy(path, LastDir, sizeof(path));
mutt_pretty_mailbox(path, sizeof(path));
snprintf(title, titlelen, _("Directory [%s], File mask: %s"), path,
NONULL(Mask.pattern));
}
}
/* Browser tracking feature.
* The goal is to highlight the good directory if LastDir is the parent dir
* of OldLastDir (this occurs mostly when one hit "../"). It should also work
* properly when the user is in examine_mailboxes-mode.
*/
int ldlen = mutt_strlen(LastDir);
if ((ldlen > 0) && (mutt_strncmp(LastDir, OldLastDir, ldlen) == 0))
{
char TargetDir[_POSIX_PATH_MAX] = "";
#ifdef USE_IMAP
/* Use mx_is_imap to check what kind of dir is OldLastDir.
*/
if (mx_is_imap(OldLastDir))
{
strfcpy(TargetDir, OldLastDir, sizeof(TargetDir));
imap_clean_path(TargetDir, sizeof(TargetDir));
}
else
#endif
strfcpy(TargetDir, strrchr(OldLastDir, '/') + 1, sizeof(TargetDir));
/* If we get here, it means that LastDir is the parent directory of
* OldLastDir. I.e., we're returning from a subdirectory, and we want
* to position the cursor on the directory we're returning from. */
unsigned int i, matched = 0;
for (i = 0; i < state->entrylen; i++)
{
if (mutt_strcmp(state->entry[i].name, TargetDir) == 0)
{
menu->current = i;
matched = 1;
break;
}
}
if (!matched)
browser_highlight_default(state, menu);
}
else
browser_highlight_default(state, menu);
menu->redraw = REDRAW_FULL;
}
static int file_tag(MUTTMENU *menu, int n, int m)
{
struct folder_file *ff = &(((struct folder_file *) menu->data)[n]);
if (S_ISDIR(ff->mode) || (S_ISLNK(ff->mode) && link_is_dir(LastDir, ff->name)))
{
mutt_error(_("Can't attach a directory!"));
return 0;
}
bool ot = ff->tagged;
ff->tagged = (m >= 0 ? m : !ff->tagged);