forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.c
2283 lines (2062 loc) · 65.8 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
/**
* @file
* GUI component for displaying/selecting items from a list
*
* @authors
* Copyright (C) 1996-2000,2007,2010,2013 Michael R. Elkins <[email protected]>
*
* @copyright
* 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/>.
*/
/**
* @page browser GUI component for displaying/selecting items from a list
*
* GUI component for displaying/selecting items from a list
*/
#include "config.h"
#include <dirent.h>
#include <errno.h>
#include <grp.h>
#include <limits.h>
#include <locale.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "conn/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "browser.h"
#include "context.h"
#include "format_flags.h"
#include "globals.h"
#include "keymap.h"
#include "mutt_attach.h"
#include "mutt_mailbox.h"
#include "mutt_menu.h"
#include "muttlib.h"
#include "mx.h"
#include "opcodes.h"
#include "options.h"
#include "sendlib.h"
#ifdef USE_IMAP
#include "imap/lib.h"
#endif
#ifdef USE_NNTP
#include "nntp/lib.h"
#endif
/* These Config Variables are only used in browser.c */
bool C_BrowserAbbreviateMailboxes; ///< Config: Abbreviate mailboxes using '~' and '=' in the browser
char *C_FolderFormat; ///< Config: printf-like format string for the browser's display of folders
char *C_GroupIndexFormat; ///< Config: (nntp) printf-like format string for the browser's display of newsgroups
char *C_NewsgroupsCharset; ///< Config: (nntp) Character set of newsgroups' descriptions
bool C_ShowOnlyUnread; ///< Config: (nntp) Only show subscribed newsgroups with unread articles
short C_SortBrowser; ///< Config: Sort method for the browser
char *C_VfolderFormat; ///< Config: (notmuch) printf-like format string for the browser's display of virtual folders
static const struct Mapping 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 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
static struct Buffer LastDir = { 0 };
static struct Buffer LastDirBackup = { 0 };
/**
* init_lastdir - Initialise the browser directories
*
* These keep track of where the browser used to be looking.
*/
static void init_lastdir(void)
{
static bool done = false;
if (!done)
{
mutt_buffer_alloc(&LastDir, PATH_MAX);
mutt_buffer_alloc(&LastDirBackup, PATH_MAX);
done = true;
}
}
/**
* mutt_browser_cleanup - Clean up working Buffers
*/
void mutt_browser_cleanup(void)
{
mutt_buffer_dealloc(&LastDir);
mutt_buffer_dealloc(&LastDirBackup);
}
/**
* destroy_state - Free the BrowserState
* @param state State to free
*
* Frees up the memory allocated for the local-global variables.
*/
static void destroy_state(struct BrowserState *state)
{
for (size_t 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);
}
/**
* browser_compare_subject - Compare the subject of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_subject(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
/* inbox should be sorted ahead of its siblings */
int r = mutt_inbox_cmp(pa->name, pb->name);
if (r == 0)
r = mutt_str_strcoll(pa->name, pb->name);
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare_desc - Compare the descriptions of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_desc(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
int r = mutt_str_strcoll(pa->desc, pb->desc);
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare_date - Compare the date of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_date(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
int r = pa->mtime - pb->mtime;
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare_size - Compare the size of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_size(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
int r = pa->size - pb->size;
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare_count - Compare the message count of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_count(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
int r = 0;
if (pa->has_mailbox && pb->has_mailbox)
r = pa->msg_count - pb->msg_count;
else if (pa->has_mailbox)
r = -1;
else
r = 1;
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare_count_new - Compare the new count of two browser entries
* @param a First browser entry
* @param b Second browser entry
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*/
static int browser_compare_count_new(const void *a, const void *b)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
int r = 0;
if (pa->has_mailbox && pb->has_mailbox)
r = pa->msg_unread - pb->msg_unread;
else if (pa->has_mailbox)
r = -1;
else
r = 1;
return (C_SortBrowser & SORT_REVERSE) ? -r : r;
}
/**
* browser_compare - Sort the items in the browser
* @param a First item
* @param b Second item
* @retval -1 a precedes b
* @retval 0 a and b are identical
* @retval 1 b precedes a
*
* 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)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
if ((mutt_str_strcoll(pa->desc, "../") == 0) || (mutt_str_strcoll(pa->desc, "..") == 0))
return -1;
if ((mutt_str_strcoll(pb->desc, "../") == 0) || (mutt_str_strcoll(pb->desc, "..") == 0))
return 1;
switch (C_SortBrowser & SORT_MASK)
{
case SORT_COUNT:
return browser_compare_count(a, b);
case SORT_DATE:
return browser_compare_date(a, b);
case SORT_DESC:
return browser_compare_desc(a, b);
case SORT_SIZE:
return browser_compare_size(a, b);
case SORT_UNREAD:
return browser_compare_count_new(a, b);
case SORT_SUBJECT:
default:
return browser_compare_subject(a, b);
}
}
/**
* browser_sort - Sort the entries in the browser
* @param state Browser state
*
* Call to qsort using browser_compare function.
* Some specific sort methods are not used via NNTP.
*/
static void browser_sort(struct BrowserState *state)
{
switch (C_SortBrowser & SORT_MASK)
{
/* Also called "I don't care"-sort-method. */
case SORT_ORDER:
return;
#ifdef USE_NNTP
case SORT_SIZE:
case SORT_DATE:
if (OptNews)
return;
#endif
default:
break;
}
qsort(state->entry, state->entrylen, sizeof(struct FolderFile), browser_compare);
}
/**
* link_is_dir - Does this symlink point to a directory?
* @param folder Folder
* @param path Link name
* @retval true Links to a directory
* @retval false Otherwise
*/
static bool link_is_dir(const char *folder, const char *path)
{
struct stat st;
bool retval = false;
struct Buffer *fullpath = mutt_buffer_pool_get();
mutt_buffer_concat_path(fullpath, folder, path);
if (stat(mutt_b2s(fullpath), &st) == 0)
retval = S_ISDIR(st.st_mode);
mutt_buffer_pool_release(&fullpath);
return retval;
}
/**
* folder_format_str - Format a string for the folder browser - Implements ::format_t
*
* | Expando | Description
* |:--------|:--------------------------------------------------------
* | \%C | Current file number
* | \%d | Date/time folder was last modified
* | \%D | Date/time folder was last modified using `$date_format.`
* | \%F | File permissions
* | \%f | Filename (with suffix `/`, `@` or `*`)
* | \%g | Group name (or numeric gid, if missing)
* | \%i | Description of the folder
* | \%l | Number of hard links
* | \%m | Number of messages in the mailbox
* | \%N | N if mailbox has new mail, blank otherwise
* | \%n | Number of unread messages in the mailbox
* | \%s | Size in bytes
* | \%t | `*` if the file is tagged, blank otherwise
* | \%u | Owner name (or numeric uid, if missing)
*/
static const char *folder_format_str(char *buf, size_t buflen, size_t col, int cols,
char op, const char *src, const char *prec,
const char *if_str, const char *else_str,
unsigned long data, MuttFormatFlags flags)
{
char fn[128], fmt[128];
struct Folder *folder = (struct Folder *) data;
bool optional = (flags & MUTT_FORMAT_OPTIONAL);
switch (op)
{
case 'C':
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, folder->num + 1);
break;
case 'd':
case 'D':
if (folder->ff->local)
{
bool do_locales = true;
char *t_fmt = NULL;
if (op == 'D')
{
t_fmt = NONULL(C_DateFormat);
if (*t_fmt == '!')
{
t_fmt++;
do_locales = false;
}
}
else
{
static const time_t one_year = 31536000;
t_fmt = ((mutt_date_epoch() - folder->ff->mtime) < one_year) ?
"%b %d %H:%M" :
"%b %d %Y";
}
if (!do_locales)
setlocale(LC_TIME, "C");
char date[128];
mutt_date_localtime_format(date, sizeof(date), t_fmt, folder->ff->mtime);
if (!do_locales)
setlocale(LC_TIME, "");
mutt_format_s(buf, buflen, prec, date);
}
else
mutt_format_s(buf, buflen, prec, "");
break;
case 'f':
{
char *s = NULL;
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(buf, buflen, prec, fn);
break;
}
case 'F':
{
if (folder->ff->local)
{
char permission[11];
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(buf, buflen, prec, permission);
}
#ifdef USE_IMAP
else if (folder->ff->imap)
{
char permission[11];
/* mark folders with subfolders AND mail */
snprintf(permission, sizeof(permission), "IMAP %c",
(folder->ff->inferiors && folder->ff->selectable) ? '+' : ' ');
mutt_format_s(buf, buflen, prec, permission);
}
#endif
else
mutt_format_s(buf, buflen, prec, "");
break;
}
case 'g':
if (folder->ff->local)
{
struct group *gr = getgrgid(folder->ff->gid);
if (gr)
mutt_format_s(buf, buflen, prec, gr->gr_name);
else
{
snprintf(fmt, sizeof(fmt), "%%%sld", prec);
snprintf(buf, buflen, fmt, folder->ff->gid);
}
}
else
mutt_format_s(buf, buflen, prec, "");
break;
case 'i':
{
char *s = NULL;
if (folder->ff->desc)
s = folder->ff->desc;
else
s = 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(buf, buflen, prec, fn);
break;
}
case 'l':
if (folder->ff->local)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, folder->ff->nlink);
}
else
mutt_format_s(buf, buflen, prec, "");
break;
case 'm':
if (!optional)
{
if (folder->ff->has_mailbox)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, folder->ff->msg_count);
}
else
mutt_format_s(buf, buflen, prec, "");
}
else if (folder->ff->msg_count == 0)
optional = false;
break;
case 'N':
snprintf(fmt, sizeof(fmt), "%%%sc", prec);
snprintf(buf, buflen, fmt, folder->ff->has_new_mail ? 'N' : ' ');
break;
case 'n':
if (!optional)
{
if (folder->ff->has_mailbox)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, folder->ff->msg_unread);
}
else
mutt_format_s(buf, buflen, prec, "");
}
else if (folder->ff->msg_unread == 0)
optional = false;
break;
case 's':
if (folder->ff->local)
{
mutt_str_pretty_size(fn, sizeof(fn), folder->ff->size);
snprintf(fmt, sizeof(fmt), "%%%ss", prec);
snprintf(buf, buflen, fmt, fn);
}
else
mutt_format_s(buf, buflen, prec, "");
break;
case 't':
snprintf(fmt, sizeof(fmt), "%%%sc", prec);
snprintf(buf, buflen, fmt, folder->ff->tagged ? '*' : ' ');
break;
case 'u':
if (folder->ff->local)
{
struct passwd *pw = getpwuid(folder->ff->uid);
if (pw)
mutt_format_s(buf, buflen, prec, pw->pw_name);
else
{
snprintf(fmt, sizeof(fmt), "%%%sld", prec);
snprintf(buf, buflen, fmt, folder->ff->uid);
}
}
else
mutt_format_s(buf, buflen, prec, "");
break;
default:
snprintf(fmt, sizeof(fmt), "%%%sc", prec);
snprintf(buf, buflen, fmt, op);
break;
}
if (optional)
{
mutt_expando_format(buf, buflen, col, cols, if_str, folder_format_str, data,
MUTT_FORMAT_NO_FLAGS);
}
else if (flags & MUTT_FORMAT_OPTIONAL)
{
mutt_expando_format(buf, buflen, col, cols, else_str, folder_format_str,
data, MUTT_FORMAT_NO_FLAGS);
}
return src;
}
/**
* add_folder - Add a folder to the browser list
* @param menu Menu to use
* @param state Browser state
* @param name Name of folder
* @param desc Description of folder
* @param s stat info for the folder
* @param m Mailbox
* @param data Data to associate with the folder
*/
static void add_folder(struct Menu *menu, struct BrowserState *state,
const char *name, const char *desc, const struct stat *s,
struct Mailbox *m, void *data)
{
if ((!menu || menu->is_mailbox_list) && m && (m->flags & MB_HIDDEN))
{
return;
}
if (state->entrylen == state->entrymax)
{
/* need to allocate more space */
mutt_mem_realloc(&state->entry, sizeof(struct FolderFile) * (state->entrymax += 256));
memset(&state->entry[state->entrylen], 0, sizeof(struct FolderFile) * 256);
if (menu)
menu->data = state->entry;
}
if (s)
{
(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 (m)
{
(state->entry)[state->entrylen].has_mailbox = true;
(state->entry)[state->entrylen].has_new_mail = m->has_new;
(state->entry)[state->entrylen].msg_count = m->msg_count;
(state->entry)[state->entrylen].msg_unread = m->msg_unread;
}
(state->entry)[state->entrylen].name = mutt_str_strdup(name);
(state->entry)[state->entrylen].desc = mutt_str_strdup(desc ? desc : name);
#ifdef USE_IMAP
(state->entry)[state->entrylen].imap = false;
#endif
#ifdef USE_NNTP
if (OptNews)
(state->entry)[state->entrylen].nd = data;
#endif
(state->entrylen)++;
}
/**
* init_state - Initialise a browser state
* @param state BrowserState to initialise
* @param menu Current menu
*/
static void init_state(struct BrowserState *state, struct Menu *menu)
{
state->entrylen = 0;
state->entrymax = 256;
state->entry = mutt_mem_calloc(state->entrymax, sizeof(struct FolderFile));
#ifdef USE_IMAP
state->imap_browse = false;
#endif
if (menu)
menu->data = state->entry;
}
/**
* examine_directory - Get list of all files/newsgroups with mask
* @param menu Current Menu
* @param state State of browser
* @param d Directory
* @param prefix Files/newsgroups must match this prefix
* @retval 0 Success
* @retval -1 Error
*/
static int examine_directory(struct Menu *menu, struct BrowserState *state,
const char *d, const char *prefix)
{
int rc = -1;
struct Buffer *buf = mutt_buffer_pool_get();
#ifdef USE_NNTP
if (OptNews)
{
struct NntpAccountData *adata = CurrentNewsSrv;
init_state(state, menu);
for (unsigned int i = 0; i < adata->groups_num; i++)
{
struct NntpMboxData *mdata = adata->groups_list[i];
if (!mdata)
continue;
if (prefix && *prefix && !mutt_str_startswith(mdata->group, prefix, CASE_MATCH))
continue;
if (!mutt_regex_match(C_Mask, mdata->group))
{
continue;
}
add_folder(menu, state, mdata->group, NULL, NULL, NULL, mdata);
}
}
else
#endif /* USE_NNTP */
{
struct stat s;
DIR *dp = NULL;
struct dirent *de = 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);
goto ed_out;
}
if (!S_ISDIR(s.st_mode))
{
mutt_error(_("%s is not a directory"), d);
goto ed_out;
}
if (Context && Context->mailbox)
mutt_mailbox_check(Context->mailbox, 0);
dp = opendir(d);
if (!dp)
{
mutt_perror(d);
goto ed_out;
}
init_state(state, menu);
struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY);
while ((de = readdir(dp)))
{
if (mutt_str_strcmp(de->d_name, ".") == 0)
continue; /* we don't need . */
if (prefix && *prefix && !mutt_str_startswith(de->d_name, prefix, CASE_MATCH))
{
continue;
}
if (!mutt_regex_match(C_Mask, de->d_name))
{
continue;
}
mutt_buffer_concat_path(buf, d, de->d_name);
if (lstat(mutt_b2s(buf), &s) == -1)
continue;
/* No size for directories or symlinks */
if (S_ISDIR(s.st_mode) || S_ISLNK(s.st_mode))
s.st_size = 0;
else if (!S_ISREG(s.st_mode))
continue;
struct MailboxNode *np = NULL;
STAILQ_FOREACH(np, &ml, entries)
{
if (mutt_str_strcmp(mutt_b2s(buf), mailbox_path(np->mailbox)) == 0)
break;
}
if (np && Context && Context->mailbox &&
(mutt_str_strcmp(np->mailbox->realpath, Context->mailbox->realpath) == 0))
{
np->mailbox->msg_count = Context->mailbox->msg_count;
np->mailbox->msg_unread = Context->mailbox->msg_unread;
}
add_folder(menu, state, de->d_name, NULL, &s, np ? np->mailbox : NULL, NULL);
}
neomutt_mailboxlist_clear(&ml);
closedir(dp);
}
browser_sort(state);
rc = 0;
ed_out:
mutt_buffer_pool_release(&buf);
return rc;
}
/**
* examine_mailboxes - Get list of mailboxes/subscribed newsgroups
* @param menu Current menu
* @param state State of browser
* @retval 0 Success
* @retval -1 Error
*/
static int examine_mailboxes(struct Menu *menu, struct BrowserState *state)
{
struct stat s;
struct Buffer *md = NULL;
struct Buffer *mailbox = NULL;
#ifdef USE_NNTP
if (OptNews)
{
struct NntpAccountData *adata = CurrentNewsSrv;
init_state(state, menu);
for (unsigned int i = 0; i < adata->groups_num; i++)
{
struct NntpMboxData *mdata = adata->groups_list[i];
if (mdata && (mdata->has_new_mail ||
(mdata->subscribed && (mdata->unread || !C_ShowOnlyUnread))))
{
add_folder(menu, state, mdata->group, NULL, NULL, NULL, mdata);
}
}
}
else
#endif
{
init_state(state, menu);
if (TAILQ_EMPTY(&NeoMutt->accounts))
return -1;
mailbox = mutt_buffer_pool_get();
md = mutt_buffer_pool_get();
mutt_mailbox_check(Context ? Context->mailbox : NULL, 0);
struct MailboxList ml = neomutt_mailboxlist_get_all(NeoMutt, MUTT_MAILBOX_ANY);
struct MailboxNode *np = NULL;
STAILQ_FOREACH(np, &ml, entries)
{
if (!np->mailbox)
continue;
if (Context && Context->mailbox &&
(mutt_str_strcmp(np->mailbox->realpath, Context->mailbox->realpath) == 0))
{
np->mailbox->msg_count = Context->mailbox->msg_count;
np->mailbox->msg_unread = Context->mailbox->msg_unread;
}
mutt_buffer_strcpy(mailbox, mailbox_path(np->mailbox));
if (C_BrowserAbbreviateMailboxes)
mutt_buffer_pretty_mailbox(mailbox);
switch (np->mailbox->type)
{
case MUTT_IMAP:
case MUTT_POP:
add_folder(menu, state, mutt_b2s(mailbox), np->mailbox->name, NULL,
np->mailbox, NULL);
continue;
case MUTT_NOTMUCH:
case MUTT_NNTP:
add_folder(menu, state, mailbox_path(np->mailbox), np->mailbox->name,
NULL, np->mailbox, NULL);
continue;
default: /* Continue */
break;
}
if (lstat(mailbox_path(np->mailbox), &s) == -1)
continue;
if ((!S_ISREG(s.st_mode)) && (!S_ISDIR(s.st_mode)) && (!S_ISLNK(s.st_mode)))
continue;
if (np->mailbox->type == MUTT_MAILDIR)
{
struct stat st2;
mutt_buffer_printf(md, "%s/new", mailbox_path(np->mailbox));
if (stat(mutt_b2s(md), &s) < 0)
s.st_mtime = 0;
mutt_buffer_printf(md, "%s/cur", mailbox_path(np->mailbox));
if (stat(mutt_b2s(md), &st2) < 0)
st2.st_mtime = 0;
if (st2.st_mtime > s.st_mtime)
s.st_mtime = st2.st_mtime;
}
add_folder(menu, state, mutt_b2s(mailbox), np->mailbox->name, &s, np->mailbox, NULL);
}
neomutt_mailboxlist_clear(&ml);
}
browser_sort(state);
mutt_buffer_pool_release(&mailbox);
mutt_buffer_pool_release(&md);
return 0;
}
/**
* select_file_search - Menu search callback for matching files - Implements Menu::search()
*/
static int select_file_search(struct Menu *menu, regex_t *rx, int line)
{
#ifdef USE_NNTP
if (OptNews)
return regexec(rx, ((struct FolderFile *) menu->data)[line].desc, 0, NULL, 0);
#endif
struct FolderFile current_ff = ((struct FolderFile *) menu->data)[line];
char *search_on = current_ff.desc ? current_ff.desc : current_ff.name;
return regexec(rx, search_on, 0, NULL, 0);
}
/**
* folder_make_entry - Format a menu item for the folder browser - Implements Menu::make_entry()
*/
static void folder_make_entry(char *buf, size_t buflen, struct Menu *menu, int line)
{
struct Folder folder;
folder.ff = &((struct FolderFile *) menu->data)[line];
folder.num = line;
#ifdef USE_NNTP
if (OptNews)
{
mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols,
NONULL(C_GroupIndexFormat), group_index_format_str,
(unsigned long) &folder, MUTT_FORMAT_ARROWCURSOR);
}
else
#endif
{
mutt_expando_format(buf, buflen, 0, menu->win_index->state.cols,
NONULL(C_FolderFormat), folder_format_str,
(unsigned long) &folder, MUTT_FORMAT_ARROWCURSOR);
}
}
/**
* browser_highlight_default - Decide which browser item should be highlighted
* @param state Browser state
* @param menu Current Menu
*
* This function takes a menu and a state and defines the current entry that
* should be highlighted.
*/
static void browser_highlight_default(struct BrowserState *state, struct Menu *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_str_strcmp(state->entry[0].desc, "..") == 0) ||
(mutt_str_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;
}
}