forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdrline.c
1422 lines (1289 loc) · 43.3 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 <me@mutt.org>
* Copyright (C) 2016 Richard Russon <rich@flatcap.org>
* Copyright (C) 2016 Ian Zimmerman <itz@primate.net>
* Copyright (C) 2019 Pietro Cerutti <gahr@gahr.ch>
*
* @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 neo_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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/lib.h"
#include "gui/lib.h"
#include "hdrline.h"
#include "attach/lib.h"
#include "color/lib.h"
#include "ncrypt/lib.h"
#include "format_flags.h"
#include "hook.h"
#include "maillist.h"
#include "mutt_globals.h"
#include "mutt_thread.h"
#include "muttlib.h"
#include "mx.h"
#include "sort.h"
#include "subjectrx.h"
#ifdef USE_NOTMUCH
#include "notmuch/lib.h"
#endif
/**
* struct HdrFormatInfo - Data passed to index_format_str()
*/
struct HdrFormatInfo
{
struct Mailbox *mailbox; ///< Current Mailbox
int msg_in_pager; ///< Index of Email displayed in the Pager
struct Email *email; ///< Current Email
const char *pager_progress; ///< String representing Pager postiion through Email
};
/**
* enum FlagChars - Index into the `$flag_chars` 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 `$crypt_chars` 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
};
/**
* 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,
};
/**
* 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_len(buf);
buf += len;
buflen -= len;
}
if (buflen <= 2)
return 0;
buf[0] = MUTT_SPECIAL_INDEX;
buf[1] = color;
buf[2] = '\0';
return 2;
}
/**
* 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(const 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] = "",
};
const struct MbTable *c_from_chars =
cs_subset_mbtable(NeoMutt->sub, "from_chars");
if (!c_from_chars || !c_from_chars->chars || (c_from_chars->len == 0))
return long_prefixes[disp];
const char *pchar = get_nth_wchar(c_from_chars, disp);
if (mutt_str_len(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 AddressList *name = NULL;
me = mutt_addr_is_user(TAILQ_FIRST(&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 && !TAILQ_EMPTY(&env->to))
{
disp = (flags & MUTT_FORMAT_PLAIN) ? DISP_PLAIN : DISP_TO;
name = &env->to;
}
else if (me && !TAILQ_EMPTY(&env->cc))
{
disp = DISP_CC;
name = &env->cc;
}
else if (me && !TAILQ_EMPTY(&env->bcc))
{
disp = DISP_BCC;
name = &env->bcc;
}
else if (!TAILQ_EMPTY(&env->from))
{
disp = DISP_FROM;
name = &env->from;
}
else
{
*buf = '\0';
return;
}
snprintf(buf, buflen, "%s%s", make_from_prefix(disp), mutt_get_name(TAILQ_FIRST(name)));
}
/**
* make_from_addr - Create a 'from' address for a reply email
* @param env 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 *env, char *buf, size_t buflen, bool do_lists)
{
if (!env || !buf)
return;
bool me = mutt_addr_is_user(TAILQ_FIRST(&env->from));
if (do_lists || me)
{
if (check_for_mailing_list_addr(&env->to, buf, buflen))
return;
if (check_for_mailing_list_addr(&env->cc, buf, buflen))
return;
}
if (me && !TAILQ_EMPTY(&env->to))
snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->to)->mailbox);
else if (me && !TAILQ_EMPTY(&env->cc))
snprintf(buf, buflen, "%s", TAILQ_FIRST(&env->cc)->mailbox);
else if (!TAILQ_EMPTY(&env->from))
mutt_str_copy(buf, TAILQ_FIRST(&env->from)->mailbox, buflen);
else
*buf = '\0';
}
/**
* user_in_addr - Do any of the addresses refer to the user?
* @param al AddressList
* @retval true Any of the addresses match one of the user's addresses
*/
static bool user_in_addr(struct AddressList *al)
{
struct Address *a = NULL;
TAILQ_FOREACH(a, al, entries)
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(TAILQ_FIRST(&env->from)))
e->recipient = 4;
else if (user_in_addr(&env->to))
{
if (TAILQ_NEXT(TAILQ_FIRST(&env->to), entries) || !TAILQ_EMPTY(&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;
}
/**
* thread_is_new - Does the email thread contain any new emails?
* @param e Email
* @retval true Thread contains new mail
*/
static bool thread_is_new(struct Email *e)
{
return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 1);
}
/**
* thread_is_old - Does the email thread contain any unread emails?
* @param e Email
* @retval true Thread contains unread mail
*/
static bool thread_is_old(struct Email *e)
{
return e->collapsed && (e->num_hidden > 1) && (mutt_thread_contains_unread(e) == 2);
}
/**
* index_format_str - Format a string for the index list - Implements ::format_t - @ingroup expando_api
*
* | 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 email was sent, or else the folder name (%b)
* | \%C | Current message number
* | \%c | Number of characters (bytes) in the body of the message
* | \%cr | Number of characters (bytes) in the message, including header
* | \%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
* | \%Fp | Like %F, but plain. No contextual formatting is applied to recipient name
* | \%F | Author name, or recipient name if the message is from you
* | \%f | Sender (address + real name), either From: or Return-Path:
* | \%Gx | Individual message tag (e.g. notmuch tags/imap flags)
* | \%g | Message tags (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 email 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 | Author's real name (or address if missing)
* | \%N | Message score
* | \%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 | 'To:' field (recipients)
* | \%T | The appropriate character from the `$to_chars` string
* | \%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)
* | \%Y | 'X-Label:' field (if present, tree unfolded, and != parent's x-label)
* | \%zc | Message crypto flags
* | \%zs | Message status flags
* | \%zt | Message tag flags
* | \%Z | Combined message flags
* | \%\@name\@ | Insert and evaluate format-string from the matching "$index-format-hook" command
* | \%(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,
intptr_t data, MuttFormatFlags flags)
{
struct HdrFormatInfo *hfi = (struct HdrFormatInfo *) data;
char fmt[128], tmp[1024];
char *p = NULL, *tags = NULL;
bool optional = (flags & MUTT_FORMAT_OPTIONAL);
const bool threads = mutt_using_threads();
int is_index = (flags & MUTT_FORMAT_INDEX);
size_t colorlen;
struct Email *e = hfi->email;
size_t msg_in_pager = hfi->msg_in_pager;
struct Mailbox *m = hfi->mailbox;
if (!e || !e->env)
return src;
const struct Address *reply_to = TAILQ_FIRST(&e->env->reply_to);
const struct Address *from = TAILQ_FIRST(&e->env->from);
const struct Address *to = TAILQ_FIRST(&e->env->to);
const struct Address *cc = TAILQ_FIRST(&e->env->cc);
const struct MbTable *c_crypt_chars =
cs_subset_mbtable(NeoMutt->sub, "crypt_chars");
const struct MbTable *c_flag_chars =
cs_subset_mbtable(NeoMutt->sub, "flag_chars");
const struct MbTable *c_to_chars =
cs_subset_mbtable(NeoMutt->sub, "to_chars");
const char *const c_date_format =
cs_subset_string(NeoMutt->sub, "date_format");
buf[0] = '\0';
switch (op)
{
case 'A':
case 'I':
if (op == 'A')
{
if (reply_to && 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(reply_to));
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
}
}
else
{
if (mutt_mb_get_initials(mutt_get_name(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 (from && from->mailbox)
{
mutt_format_s(buf + colorlen, buflen - colorlen, prec, mutt_addr_for_display(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))
{
mutt_str_copy(tmp, buf, sizeof(tmp));
mutt_format_s(buf, buflen, prec, tmp);
}
else if (optional)
{
optional = false;
}
break;
case 'b':
if (m)
{
p = strrchr(mailbox_path(m), '/');
#ifdef USE_NOTMUCH
if (m->type == MUTT_NOTMUCH)
{
char *rel_path = nm_email_get_folder_rel_db(m, e);
if (rel_path)
p = rel_path;
}
#endif
if (p)
mutt_str_copy(buf, p + 1, buflen);
else
mutt_str_copy(buf, mailbox_path(m), buflen);
}
else
mutt_str_copy(buf, "(null)", buflen);
mutt_str_copy(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);
if (src[0] == 'r')
{
mutt_str_pretty_size(tmp, sizeof(tmp), email_size(e));
src++;
}
else
{
mutt_str_pretty_size(tmp, sizeof(tmp), e->body->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;
time_t now;
int j = 0;
if (optional && ((op == '[') || (op == '(')))
{
now = mutt_date_epoch();
struct tm tm = mutt_date_localtime(now);
now -= (op == '(') ? e->received : e->date_sent;
char *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 = false;
break;
}
p = buf;
cp = ((op == 'd') || (op == 'D')) ? (NONULL(c_date_format)) : 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';
struct tm tm;
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, MIT_POSITION));
break;
case 'E':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, mutt_messages_in_thread(m, e, MIT_NUM_MESSAGES));
}
else if (mutt_messages_in_thread(m, e, MIT_NUM_MESSAGES) <= 1)
optional = false;
break;
case 'f':
tmp[0] = '\0';
mutt_addrlist_write(&e->env->from, tmp, sizeof(tmp), 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(from))
{
optional = false;
}
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 = false;
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(&e->tags, tag);
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(&e->tags, tag);
if (!tags)
optional = false;
FREE(&tags);
}
}
break;
}
case 'H':
/* (Hormel) spam score */
if (optional)
optional = !mutt_buffer_is_empty(&e->env->spam);
mutt_format_s(buf, buflen, prec, mutt_buffer_string(&e->env->spam));
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_istr_equal(tags, parent_tags))
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);
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_SIZE);
snprintf(buf + colorlen, buflen - colorlen, fmt, (int) e->lines);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
}
else if (e->lines <= 0)
optional = false;
break;
case 'L':
if (!optional)
{
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
make_from(e->env, tmp, sizeof(tmp), true, flags);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, tmp);
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
}
else if (!check_for_mailing_list(&e->env->to, NULL, NULL, 0) &&
!check_for_mailing_list(&e->env->cc, NULL, NULL, 0))
{
optional = false;
}
break;
case 'm':
if (m)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, m->msg_count);
}
else
mutt_str_copy(buf, "(null)", buflen);
break;
case 'n':
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_AUTHOR);
mutt_format_s(buf + colorlen, buflen - colorlen, prec, mutt_get_name(from));
add_index_color(buf + colorlen, buflen - colorlen, flags, MT_COLOR_INDEX);
break;
case 'M':
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
if (!optional)
{
colorlen = add_index_color(buf, buflen, flags, MT_COLOR_INDEX_COLLAPSED);
if (threads && is_index && e->collapsed && (e->num_hidden > 1))
{
snprintf(buf + colorlen, buflen - colorlen, fmt, e->num_hidden);
add_index_color(buf, buflen - colorlen, flags, MT_COLOR_INDEX);
}
else if (is_index && threads)
{
mutt_format_s(buf + colorlen, buflen - colorlen, prec, " ");
add_index_color(buf, buflen - colorlen, flags, MT_COLOR_INDEX);
}
else
*buf = '\0';
}
else
{
if (!(threads && is_index && e->collapsed && (e->num_hidden > 1)))
optional = false;
}
break;
case 'N':
if (!optional)
{
snprintf(fmt, sizeof(fmt), "%%%sd", prec);
snprintf(buf, buflen, fmt, e->score);
}
else
{
if (e->score == 0)
optional = false;
}
break;
case 'O':
if (!optional)
{
make_from_addr(e->env, tmp, sizeof(tmp), true);
const bool c_save_address =
cs_subset_bool(NeoMutt->sub, "save_address");
if (!c_save_address && (p = strpbrk(tmp, "%@")))
*p = '\0';
mutt_format_s(buf, buflen, prec, tmp);
}
else if (!check_for_mailing_list_addr(&e->env->to, NULL, 0) &&