forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdrline.c
1493 lines (1359 loc) · 45 KB
/
hdrline.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
* String processing routines to generate the mail index
*
* @authors
* Copyright (C) 1996-2000,2002,2007 Michael R. Elkins <[email protected]>
* Copyright (C) 2016 Richard Russon <[email protected]>
* Copyright (C) 2016 Ian Zimmerman <[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 hdrline String processing routines to generate the mail index
*
* String processing routines to generate the mail index
*/
#include "config.h"
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mutt/mutt.h"
#include "config/lib.h"
#include "email/lib.h"
#include "mutt.h"
#include "hdrline.h"
#include "alias.h"
#include "context.h"
#include "curs_lib.h"
#include "format_flags.h"
#include "globals.h"
#include "mailbox.h"
#include "mutt_curses.h"
#include "mutt_menu.h"
#include "mutt_parse.h"
#include "mutt_thread.h"
#include "mutt_window.h"
#include "muttlib.h"
#include "ncrypt/ncrypt.h"
#include "sort.h"
/* These Config Variables are only used in hdrline.c */
struct MbTable *C_CryptChars; ///< Config: User-configurable crypto flags: signed, encrypted etc.
struct MbTable *C_FlagChars; ///< Config: User-configurable index flags: tagged, new, etc
struct MbTable *C_FromChars; ///< Config: User-configurable index flags: to address, cc address, etc
struct MbTable *C_ToChars; ///< Config: Indicator characters for the 'To' field in the index
/**
* enum FlagChars - Index into the #C_FlagChars variable ($flag_chars)
*/
enum FlagChars
{
FLAG_CHAR_TAGGED, ///< Character denoting a tagged email
FLAG_CHAR_IMPORTANT, ///< Character denoting a important (flagged) email
FLAG_CHAR_DELETED, ///< Character denoting a deleted email
FLAG_CHAR_DELETED_ATTACH, ///< Character denoting a deleted attachment
FLAG_CHAR_REPLIED, ///< Character denoting an email that has been replied to
FLAG_CHAR_OLD, ///< Character denoting an email that has been read
FLAG_CHAR_NEW, ///< Character denoting an unread email
FLAG_CHAR_OLD_THREAD, ///< Character denoting a thread of emails that has been read
FLAG_CHAR_NEW_THREAD, ///< Character denoting a thread containing at least one new email
FLAG_CHAR_SEMPTY, ///< Character denoting a read email, $index_format %S expando
FLAG_CHAR_ZEMPTY, ///< Character denoting a read email, $index_format %Z expando
};
/**
* enum CryptChars - Index into the #C_CryptChars variable ($crypt_chars)
*/
enum CryptChars
{
FLAG_CHAR_CRYPT_GOOD_SIGN, ///< Character denoting a message signed with a verified key
FLAG_CHAR_CRYPT_ENCRYPTED, ///< Character denoting a message is PGP-encrypted
FLAG_CHAR_CRYPT_SIGNED, ///< Character denoting a message is signed
FLAG_CHAR_CRYPT_CONTAINS_KEY, ///< Character denoting a message contains a PGP key
FLAG_CHAR_CRYPT_NO_CRYPTO, ///< Character denoting a message has no cryptography information
};
/**
* mutt_is_mail_list - Is this the email address of a mailing list?
* @param addr Address to test
* @retval true If it's a mailing list
*/
bool mutt_is_mail_list(struct Address *addr)
{
if (!mutt_regexlist_match(&UnMailLists, addr->mailbox))
return mutt_regexlist_match(&MailLists, addr->mailbox);
return false;
}
/**
* mutt_is_subscribed_list - Is this the email address of a user-subscribed mailing list?
* @param addr Address to test
* @retval true If it's a subscribed mailing list
*/
bool mutt_is_subscribed_list(struct Address *addr)
{
if (!mutt_regexlist_match(&UnMailLists, addr->mailbox) &&
!mutt_regexlist_match(&UnSubscribedLists, addr->mailbox))
{
return mutt_regexlist_match(&SubscribedLists, addr->mailbox);
}
return false;
}
/**
* check_for_mailing_list - Search list of addresses for a mailing list
* @param addr List of addresses to search
* @param pfx Prefix string
* @param buf Buffer to store results
* @param buflen Buffer length
* @retval 1 Mailing list found
* @retval 0 No list found
*
* Search for a mailing list in the list of addresses pointed to by addr.
* If one is found, print pfx and the name of the list into buf.
*/
static bool check_for_mailing_list(struct Address *addr, const char *pfx, char *buf, int buflen)
{
for (; addr; addr = addr->next)
{
if (mutt_is_subscribed_list(addr))
{
if (pfx && buf && buflen)
snprintf(buf, buflen, "%s%s", pfx, mutt_get_name(addr));
return true;
}
}
return false;
}
/**
* check_for_mailing_list_addr - Check an address list for a mailing list
* @param addr Address list
* @param buf Buffer for the result
* @param buflen Length of buffer
* @retval true Mailing list found
*
* If one is found, print the address of the list into buf.
*/
static bool check_for_mailing_list_addr(struct Address *addr, char *buf, int buflen)
{
for (; addr; addr = addr->next)
{
if (mutt_is_subscribed_list(addr))
{
if (buf && buflen)
snprintf(buf, buflen, "%s", addr->mailbox);
return true;
}
}
return false;
}
/**
* first_mailing_list - Get the first mailing list in the list of addresses
* @param buf Buffer for the result
* @param buflen Length of buffer
* @param a Address list
* @retval true If a mailing list was found
*/
static bool first_mailing_list(char *buf, size_t buflen, struct Address *a)
{
for (; a; a = a->next)
{
if (mutt_is_subscribed_list(a))
{
mutt_save_path(buf, buflen, a);
return true;
}
}
return false;
}
/**
* add_index_color - Insert a color marker into a string
* @param buf Buffer to store marker
* @param buflen Buffer length
* @param flags Flags, see #MuttFormatFlags
* @param color Color, e.g. #MT_COLOR_MESSAGE
* @retval num Characters written
*
* The colors are stored as "magic" strings embedded in the text.
*/
static size_t add_index_color(char *buf, size_t buflen, MuttFormatFlags flags, char color)
{
/* only add color markers if we are operating on main index entries. */
if (!(flags & MUTT_FORMAT_INDEX))
return 0;
/* this item is going to be passed to an external filter */
if (flags & MUTT_FORMAT_NOFILTER)
return 0;
if (color == MT_COLOR_INDEX)
{ /* buf might be uninitialized other cases */
const size_t len = mutt_str_strlen(buf);
buf += len;
buflen -= len;
}
if (buflen <= 2)
return 0;
buf[0] = MUTT_SPECIAL_INDEX;
buf[1] = color;
buf[2] = '\0';
return 2;
}
/**
* enum FieldType - Header types
*
* Strings for printing headers
*/
enum FieldType
{
DISP_TO, ///< To: string
DISP_CC, ///< Cc: string
DISP_BCC, ///< Bcc: string
DISP_FROM, ///< From: string
DISP_PLAIN, ///< Empty string
DISP_MAX,
};
/**
* get_nth_wchar - Extract one char from a multi-byte table
* @param table Multi-byte table
* @param index Select this character
* @retval ptr String pointer to the character
*
* Extract one multi-byte character from a string table.
* If the index is invalid, then a space character will be returned.
* If the character selected is '\n' (Ctrl-M), then "" will be returned.
*/
static const char *get_nth_wchar(struct MbTable *table, int index)
{
if (!table || !table->chars || (index < 0) || (index >= table->len))
return " ";
if (table->chars[index][0] == '\r')
return "";
return table->chars[index];
}
/**
* make_from_prefix - Create a prefix for an author field
* @param disp Type of field
* @retval ptr Prefix string (do not free it)
*
* If $from_chars is set, pick an appropriate character from it.
* If not, use the default prefix: "To", "Cc", etc
*/
static const char *make_from_prefix(enum FieldType disp)
{
/* need 2 bytes at the end, one for the space, another for NUL */
static char padded[8];
static const char *long_prefixes[DISP_MAX] = {
[DISP_TO] = "To ", [DISP_CC] = "Cc ", [DISP_BCC] = "Bcc ",
[DISP_FROM] = "", [DISP_PLAIN] = "",
};
if (!C_FromChars || !C_FromChars->chars || (C_FromChars->len == 0))
return long_prefixes[disp];
const char *pchar = get_nth_wchar(C_FromChars, disp);
if (mutt_str_strlen(pchar) == 0)
return "";
snprintf(padded, sizeof(padded), "%s ", pchar);
return padded;
}
/**
* make_from - Generate a From: field (with optional prefix)
* @param env Envelope of the email
* @param buf Buffer to store the result
* @param buflen Size of the buffer
* @param do_lists Should we check for mailing lists?
* @param flags Format flags, see #MuttFormatFlags
*
* Generate the %F or %L field in $index_format.
* This is the author, or recipient of the email.
*
* The field can optionally be prefixed by a character from $from_chars.
* If $from_chars is not set, the prefix will be, "To", "Cc", etc
*/
static void make_from(struct Envelope *env, char *buf, size_t buflen,
bool do_lists, MuttFormatFlags flags)
{
if (!env || !buf)
return;
bool me;
enum FieldType disp;
struct Address *name = NULL;
me = mutt_addr_is_user(env->from);
if (do_lists || me)
{
if (check_for_mailing_list(env->to, make_from_prefix(DISP_TO), buf, buflen))
return;
if (check_for_mailing_list(env->cc, make_from_prefix(DISP_CC), buf, buflen))
return;
}
if (me && env->to)
{
disp = (flags & MUTT_FORMAT_PLAIN) ? DISP_PLAIN : DISP_TO;
name = env->to;
}
else if (me && env->cc)
{
disp = DISP_CC;
name = env->cc;
}
else if (me && env->bcc)
{
disp = DISP_BCC;
name = env->bcc;
}
else if (env->from)
{
disp = DISP_FROM;
name = env->from;
}
else
{
*buf = '\0';
return;
}
snprintf(buf, buflen, "%s%s", make_from_prefix(disp), mutt_get_name(name));
}
/**
* make_from_addr - Create a 'from' address for a reply email
* @param hdr Envelope of current email
* @param buf Buffer for the result
* @param buflen Length of buffer
* @param do_lists If true, check for mailing lists
*/
static void make_from_addr(struct Envelope *hdr, char *buf, size_t buflen, bool do_lists)
{
if (!hdr || !buf)
return;
bool me = mutt_addr_is_user(hdr->from);
if (do_lists || me)
{
if (check_for_mailing_list_addr(hdr->to, buf, buflen))
return;
if (check_for_mailing_list_addr(hdr->cc, buf, buflen))
return;
}
if (me && hdr->to)
snprintf(buf, buflen, "%s", hdr->to->mailbox);
else if (me && hdr->cc)
snprintf(buf, buflen, "%s", hdr->cc->mailbox);
else if (hdr->from)
mutt_str_strfcpy(buf, hdr->from->mailbox, buflen);
else
*buf = '\0';
}
/**
* user_in_addr - Do any of the addresses refer to the user?
* @param a Address list
* @retval true If any of the addresses match one of the user's addresses
*/
static bool user_in_addr(struct Address *a)
{
for (; a; a = a->next)
if (mutt_addr_is_user(a))
return true;
return false;
}
/**
* user_is_recipient - Is the user a recipient of the message
* @param e Email to test
* @retval 0 User is not in list
* @retval 1 User is unique recipient
* @retval 2 User is in the TO list
* @retval 3 User is in the CC list
* @retval 4 User is originator
* @retval 5 Sent to a subscribed mailinglist
* @retval 6 User is in the Reply-To list
*/
static int user_is_recipient(struct Email *e)
{
if (!e || !e->env)
return 0;
struct Envelope *env = e->env;
if (!e->recip_valid)
{
e->recip_valid = true;
if (mutt_addr_is_user(env->from))
e->recipient = 4;
else if (user_in_addr(env->to))
{
if (env->to->next || env->cc)
e->recipient = 2; /* non-unique recipient */
else
e->recipient = 1; /* unique recipient */
}
else if (user_in_addr(env->cc))
e->recipient = 3;
else if (check_for_mailing_list(env->to, NULL, NULL, 0))
e->recipient = 5;
else if (check_for_mailing_list(env->cc, NULL, NULL, 0))
e->recipient = 5;
else if (user_in_addr(env->reply_to))
e->recipient = 6;
else
e->recipient = 0;
}
return e->recipient;
}
/**
* apply_subject_mods - Apply regex modifications to the subject
* @param env Envelope of email
* @retval ptr Modified subject
* @retval NULL No modification made
*/
static char *apply_subject_mods(struct Envelope *env)
{
if (!env)
return NULL;
if (STAILQ_EMPTY(&SubjectRegexList))
return env->subject;
if (!env->subject || (*env->subject == '\0'))
{
env->disp_subj = NULL;
return NULL;
}
env->disp_subj = mutt_replacelist_apply(&SubjectRegexList, NULL, 0, env->subject);
return env->disp_subj;
}
/**
* thread_is_new - Does the email thread contain any new emails?
* @param ctx Mailbox
* @param e Email
* @retval true If thread contains new mail
*/
static bool thread_is_new(struct Context *ctx, struct Email *e)
{
return e->collapsed && (e->num_hidden > 1) &&
(mutt_thread_contains_unread(ctx, e) == 1);
}
/**
* thread_is_old - Does the email thread contain any unread emails?
* @param ctx Mailbox
* @param e Email
* @retval true If thread contains unread mail
*/
static bool thread_is_old(struct Context *ctx, struct Email *e)
{
return e->collapsed && (e->num_hidden > 1) &&
(mutt_thread_contains_unread(ctx, e) == 2);
}
/**
* index_format_str - Format a string for the index list - Implements ::format_t
*
* | Expando | Description
* |:--------|:-----------------------------------------------------------------
* | \%a | Address of the author
* | \%A | Reply-to address (if present; otherwise: address of author)
* | \%b | Filename of the original message folder (think mailbox)
* | \%B | The list to which the letter was sent, or else the folder name (%b).
* | \%C | Current message number
* | \%c | Number of characters (bytes) in the message
* | \%D | Date and time of message using $date_format and local timezone
* | \%d | Date and time of message using $date_format and sender's timezone
* | \%e | Current message number in thread
* | \%E | Number of messages in current thread
* | \%F | Author name, or recipient name if the message is from you
* | \%Fp | Like %F, but plain. No contextual formatting is applied to recipient name
* | \%f | Sender (address + real name), either From: or Return-Path:
* | \%g | Message tags (e.g. notmuch tags/imap flags)
* | \%Gx | Individual message tag (e.g. notmuch tags/imap flags)
* | \%H | Spam attribute(s) of this message
* | \%I | Initials of author
* | \%i | Message-id of the current message
* | \%J | Message tags (if present, tree unfolded, and != parent's tags)
* | \%K | The list to which the letter was sent (if any; otherwise: empty)
* | \%L | Like %F, except 'lists' are displayed first
* | \%l | Number of lines in the message
* | \%M | Number of hidden messages if the thread is collapsed
* | \%m | Total number of message in the mailbox
* | \%N | Message score
* | \%n | Author's real name (or address if missing)
* | \%O | Like %L, except using address instead of name
* | \%P | Progress indicator for the built-in pager (how much of the file has been displayed)
* | \%q | Newsgroup name (if compiled with NNTP support)
* | \%R | Comma separated list of Cc: recipients
* | \%r | Comma separated list of To: recipients
* | \%S | Single character status of the message (N/O/D/d/!/r/-)
* | \%s | Subject of the message
* | \%T | The appropriate character from the $$to_chars string
* | \%t | 'To:' field (recipients)
* | \%u | User (login) name of the author
* | \%v | First name of the author, or the recipient if the message is from you
* | \%W | Name of organization of author ('Organization:' field)
* | \%x | 'X-Comment-To:' field (if present and compiled with NNTP support)
* | \%X | Number of MIME attachments
* | \%Y | 'X-Label:' field (if present, tree unfolded, and != parent's x-label)
* | \%y | 'X-Label:' field (if present)
* | \%Z | Combined message flags
* | \%zc | Message crypto flags
* | \%zs | Message status flags
* | \%zt | Message tag flags
* | \%(fmt) | Date/time when the message was received
* | \%[fmt] | Message date/time converted to the local time zone
* | \%{fmt} | Message date/time converted to sender's time zone
*/
static const char *index_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)
{
struct HdrFormatInfo *hfi = (struct HdrFormatInfo *) data;
char fmt[128], tmp[1024], *p = NULL, *tags = NULL;
int optional = (flags & MUTT_FORMAT_OPTIONAL);
int threads = ((C_Sort & SORT_MASK) == SORT_THREADS);
int is_index = (flags & MUTT_FORMAT_INDEX);
size_t colorlen;
struct Email *e = hfi->email;
struct Context *ctx = hfi->ctx;
struct Mailbox *m = hfi->mailbox;
if (!e || !e->env)
return src;
buf[0] = '\0';
switch (op)
{
case 'A':
case 'I':
if (op == 'A')
{
if (e->env->reply_to && e->env->reply_to->mailbox)
{
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
mutt_format_s(buf + colorlen, buflen - colorlen, prec,
mutt_addr_for_display(e->env->reply_to));
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
}
}
else
{
if (mutt_mb_get_initials(mutt_get_name(e->env->from), tmp, sizeof(tmp)))
{
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
}
}
/* fallthrough */
case 'a':
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
if (e->env->from && e->env->from->mailbox)
{
mutt_format_s(buf + colorlen, buflen - colorlen, prec,
mutt_addr_for_display(e->env->from));
}
else
mutt_format_s(buf + colorlen, buflen - colorlen, prec, "");
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
case 'B':
case 'K':
if (!first_mailing_list(buf, buflen, e->env->to) &&
!first_mailing_list(buf, buflen, e->env->cc))
{
buf[0] = '\0';
}
if (buf[0] != '\0')
{
mutt_str_strfcpy(tmp, buf, sizeof(tmp));
mutt_format_s(buf, buflen, prec, tmp);
break;
}
if (op == 'K')
{
if (optional)
optional = 0;
/* break if 'K' returns nothing */
break;
}
/* if 'B' returns nothing */
/* fallthrough */
case 'b':
if (m)
{
p = strrchr(m->path, '/');
if (p)
mutt_str_strfcpy(buf, p + 1, buflen);
else
mutt_str_strfcpy(buf, m->path, buflen);
}
else
mutt_str_strfcpy(buf, "(null)", buflen);
mutt_str_strfcpy(tmp, buf, sizeof(tmp));
mutt_format_s(buf, buflen, prec, tmp);
break;
case 'c':
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_SIZE);
mutt_str_pretty_size(tmp, sizeof(tmp), (long) e->content->length);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
case 'C':
colorlen = add_index_color(fmt, sizeof(fmt), flags, MT_COLOR_INDEX_NUMBER);
snprintf(fmt + colorlen, sizeof(fmt) - colorlen, "%%%sd", prec);
add_index_color(fmt + colorlen, sizeof(fmt) - colorlen, flags, MT_COLOR_INDEX);
snprintf(buf, buflen, fmt, e->msgno + 1);
break;
case 'd':
case 'D':
case '{':
case '[':
case '(':
case '<':
/* preprocess $date_format to handle %Z */
{
const char *cp = NULL;
struct tm tm = { 0 };
time_t now;
int j = 0;
if (optional && ((op == '[') || (op == '(')))
{
char *is = NULL;
now = time(NULL);
tm = mutt_date_localtime(now);
now -= (op == '(') ? e->received : e->date_sent;
is = (char *) prec;
bool invert = false;
if (*is == '>')
{
invert = true;
is++;
}
while (*is && (*is != '?'))
{
int t = strtol(is, &is, 10);
/* semi-broken (assuming 30 days in all months) */
switch (*(is++))
{
case 'y':
if (t > 1)
{
t--;
t *= (60 * 60 * 24 * 365);
}
t += ((tm.tm_mon * 60 * 60 * 24 * 30) + (tm.tm_mday * 60 * 60 * 24) +
(tm.tm_hour * 60 * 60) + (tm.tm_min * 60) + tm.tm_sec);
break;
case 'm':
if (t > 1)
{
t--;
t *= (60 * 60 * 24 * 30);
}
t += ((tm.tm_mday * 60 * 60 * 24) + (tm.tm_hour * 60 * 60) +
(tm.tm_min * 60) + tm.tm_sec);
break;
case 'w':
if (t > 1)
{
t--;
t *= (60 * 60 * 24 * 7);
}
t += ((tm.tm_wday * 60 * 60 * 24) + (tm.tm_hour * 60 * 60) +
(tm.tm_min * 60) + tm.tm_sec);
break;
case 'd':
if (t > 1)
{
t--;
t *= (60 * 60 * 24);
}
t += ((tm.tm_hour * 60 * 60) + (tm.tm_min * 60) + tm.tm_sec);
break;
case 'H':
if (t > 1)
{
t--;
t *= (60 * 60);
}
t += ((tm.tm_min * 60) + tm.tm_sec);
break;
case 'M':
if (t > 1)
{
t--;
t *= (60);
}
t += (tm.tm_sec);
break;
default:
break;
}
j += t;
}
if (j < 0)
j *= -1;
if (((now > j) || (now < (-1 * j))) ^ invert)
optional = 0;
break;
}
p = buf;
cp = ((op == 'd') || (op == 'D')) ? (NONULL(C_DateFormat)) : src;
bool do_locales;
if (*cp == '!')
{
do_locales = false;
cp++;
}
else
do_locales = true;
size_t len = buflen - 1;
while ((len > 0) &&
((((op == 'd') || (op == 'D')) && *cp) ||
((op == '{') && (*cp != '}')) || ((op == '[') && (*cp != ']')) ||
((op == '(') && (*cp != ')')) || ((op == '<') && (*cp != '>'))))
{
if (*cp == '%')
{
cp++;
if (((*cp == 'Z') || (*cp == 'z')) && ((op == 'd') || (op == '{')))
{
if (len >= 5)
{
sprintf(p, "%c%02u%02u", e->zoccident ? '-' : '+', e->zhours, e->zminutes);
p += 5;
len -= 5;
}
else
break; /* not enough space left */
}
else
{
if (len >= 2)
{
*p++ = '%';
*p++ = *cp;
len -= 2;
}
else
break; /* not enough space */
}
cp++;
}
else
{
*p++ = *cp++;
len--;
}
}
*p = '\0';
if ((op == '[') || (op == 'D'))
tm = mutt_date_localtime(e->date_sent);
else if (op == '(')
tm = mutt_date_localtime(e->received);
else if (op == '<')
{
tm = mutt_date_localtime(MUTT_DATE_NOW);
}
else
{
/* restore sender's time zone */
now = e->date_sent;
if (e->zoccident)
now -= (e->zhours * 3600 + e->zminutes * 60);
else
now += (e->zhours * 3600 + e->zminutes * 60);
tm = mutt_date_gmtime(now);
}
if (!do_locales)
setlocale(LC_TIME, "C");
strftime(tmp, sizeof(tmp), buf, &tm);
if (!do_locales)
setlocale(LC_TIME, "");
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_DATE);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
if ((len > 0) && (op != 'd') && (op != 'D')) /* Skip ending op */
src = cp + 1;
break;
}
case 'e':
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, mutt_messages_in_thread(m, e, 1));
break;
case 'E':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, mutt_messages_in_thread(m, e, 0));
}
else if (mutt_messages_in_thread(m, e, 0) <= 1)
optional = 0;
break;
case 'f':
tmp[0] = '\0';
mutt_addr_write(tmp, sizeof(tmp), e->env->from, true);
mutt_format_s(buf, buflen, prec, tmp);
break;
case 'F':
if (!optional)
{
const bool is_plain = (src[0] == 'p');
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
make_from(e->env, tmp, sizeof(tmp), false,
(is_plain ? MUTT_FORMAT_PLAIN : MUTT_FORMAT_NO_FLAGS));
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
if (is_plain)
src++;
}
else if (mutt_addr_is_user(e->env->from))
{
optional = 0;
}
break;
case 'g':
tags = driver_tags_get_transformed(&e->tags);
if (!optional)
{
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_TAGS);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, NONULL(tags));
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
}
else if (!tags)
optional = 0;
FREE(&tags);
break;
case 'G':
{
char format[3];
char *tag = NULL;
if (!optional)
{
format[0] = op;
format[1] = *src;
format[2] = '\0';
tag = mutt_hash_find(TagFormats, format);
if (tag)
{
tags = driver_tags_get_transformed_for(tag, &e->tags);
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_TAG);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, NONULL(tags));
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
FREE(&tags);
}
src++;
}
else
{
format[0] = op;
format[1] = *prec;
format[2] = '\0';
tag = mutt_hash_find(TagFormats, format);
if (tag)
{
tags = driver_tags_get_transformed_for(tag, &e->tags);
if (!tags)
optional = 0;
FREE(&tags);
}
}
break;
}
case 'H':
/* (Hormel) spam score */
if (optional)
optional = e->env->spam ? 1 : 0;
if (e->env->spam)
mutt_format_s(buf, buflen, prec, NONULL(e->env->spam->data));
else
mutt_format_s(buf, buflen, prec, "");
break;
case 'i':
mutt_format_s(buf, buflen, prec, e->env->message_id ? e->env->message_id : "<no.id>");
break;
case 'J':
{
bool have_tags = true;
tags = driver_tags_get_transformed(&e->tags);
if (tags)
{
if (flags & MUTT_FORMAT_TREE)
{
char *parent_tags = NULL;
if (e->thread->prev && e->thread->prev->message)
{
parent_tags = driver_tags_get_transformed(&e->thread->prev->message->tags);
}
if (!parent_tags && e->thread->parent && e->thread->parent->message)
{
parent_tags =
driver_tags_get_transformed(&e->thread->parent->message->tags);
}
if (parent_tags && (mutt_str_strcasecmp(tags, parent_tags) == 0))
have_tags = false;
FREE(&parent_tags);
}
}
else
have_tags = false;
if (optional)
optional = have_tags;
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_TAGS);
if (have_tags)
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tags);
else
mutt_format_s(buf + colorlen, buflen - colorlen, prec, "");
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
FREE(&tags);
break;
}
case 'l':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);