forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexec.c
1159 lines (1083 loc) · 32.8 KB
/
exec.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
* Execute a Pattern
*
* @authors
* Copyright (C) 2019 Pietro Cerutti <[email protected]>
* Copyright (C) 2020 Richard Russon <[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 pattern_exec Execute a Pattern
*
* Execute a Pattern
*/
#include "config.h"
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "private.h"
#include "mutt/lib.h"
#include "address/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "alias/alias.h" // IWYU pragma: keep
#include "alias/gui.h" // IWYU pragma: keep
#include "alias/lib.h"
#include "mutt.h"
#include "lib.h"
#include "attach/lib.h"
#include "ncrypt/lib.h"
#include "send/lib.h"
#include "copy.h"
#include "handler.h"
#include "maillist.h"
#include "muttlib.h"
#include "mx.h"
#ifndef USE_FMEMOPEN
#include <sys/stat.h>
#endif
static bool pattern_exec(struct Pattern *pat, PatternExecFlags flags,
struct Mailbox *m, struct Email *e,
struct Message *msg, struct PatternCache *cache);
/**
* patmatch - Compare a string to a Pattern
* @param pat Pattern to use
* @param buf String to compare
* @retval true Match
* @retval false No match
*/
static bool patmatch(const struct Pattern *pat, const char *buf)
{
if (pat->is_multi)
return (mutt_list_find(&pat->p.multi_cases, buf) != NULL);
if (pat->string_match)
return pat->ign_case ? mutt_istr_find(buf, pat->p.str) : strstr(buf, pat->p.str);
if (pat->group_match)
return mutt_group_match(pat->p.group, buf);
return (regexec(pat->p.regex, buf, 0, NULL, 0) == 0);
}
/**
* print_crypt_pattern_op_error - Print an error for a disabled crypto pattern
* @param op Operation, e.g. #MUTT_PAT_CRYPT_SIGN
*/
static void print_crypt_pattern_op_error(int op)
{
const struct PatternFlags *entry = lookup_op(op);
if (entry)
{
/* L10N: One of the crypt pattern operators: ~g, ~G, ~k, ~V
was invoked when NeoMutt was compiled without crypto support.
%c is the pattern character, i.e. "g". */
mutt_error(_("Pattern operator '~%c' is disabled"), entry->tag);
}
else
{
/* L10N: An unknown pattern operator was somehow invoked.
This shouldn't be possible unless there is a bug. */
mutt_error(_("error: unknown op %d (report this error)"), op);
}
}
/**
* msg_search - Search an email
* @param pat Pattern to find
* @param e Email
* @param msg Message
* @retval true Pattern found
* @retval false Error or pattern not found
*/
static bool msg_search(struct Pattern *pat, struct Email *e, struct Message *msg)
{
assert(msg);
bool match = false;
FILE *fp = NULL;
long len = 0;
#ifdef USE_FMEMOPEN
char *temp = NULL;
size_t tempsize = 0;
#else
struct stat st = { 0 };
#endif
const bool needs_head = (pat->op == MUTT_PAT_HEADER) || (pat->op == MUTT_PAT_WHOLE_MSG);
const bool needs_body = (pat->op == MUTT_PAT_BODY) || (pat->op == MUTT_PAT_WHOLE_MSG);
const bool c_thorough_search = cs_subset_bool(NeoMutt->sub, "thorough_search");
if (c_thorough_search)
{
/* decode the header / body */
struct State s = { 0 };
s.fp_in = msg->fp;
s.flags = MUTT_CHARCONV;
#ifdef USE_FMEMOPEN
s.fp_out = open_memstream(&temp, &tempsize);
if (!s.fp_out)
{
mutt_perror(_("Error opening 'memory stream'"));
return false;
}
#else
s.fp_out = mutt_file_mkstemp();
if (!s.fp_out)
{
mutt_perror(_("Can't create temporary file"));
return false;
}
#endif
if (needs_head)
{
mutt_copy_header(msg->fp, e, s.fp_out, CH_FROM | CH_DECODE, NULL, 0);
}
if (needs_body)
{
mutt_parse_mime_message(e, msg->fp);
if ((WithCrypto != 0) && (e->security & SEC_ENCRYPT) &&
!crypt_valid_passphrase(e->security))
{
if (s.fp_out)
{
mutt_file_fclose(&s.fp_out);
#ifdef USE_FMEMOPEN
FREE(&temp);
#endif
}
return false;
}
if (!mutt_file_seek(msg->fp, e->offset, SEEK_SET))
{
return false;
}
mutt_body_handler(e->body, &s);
}
#ifdef USE_FMEMOPEN
mutt_file_fclose(&s.fp_out);
len = tempsize;
if (tempsize != 0)
{
fp = fmemopen(temp, tempsize, "r");
if (!fp)
{
mutt_perror(_("Error re-opening 'memory stream'"));
FREE(&temp);
return false;
}
}
else
{ /* fmemopen can't handle empty buffers */
fp = mutt_file_fopen("/dev/null", "r");
if (!fp)
{
mutt_perror(_("Error opening /dev/null"));
return false;
}
}
#else
fp = s.fp_out;
fflush(fp);
if (!mutt_file_seek(fp, 0, SEEK_SET) || fstat(fileno(fp), &st))
{
mutt_perror(_("Error checking length of temporary file"));
mutt_file_fclose(&fp);
return false;
}
len = (long) st.st_size;
#endif
}
else
{
/* raw header / body */
fp = msg->fp;
if (needs_head)
{
if (!mutt_file_seek(fp, e->offset, SEEK_SET))
{
return false;
}
len = e->body->offset - e->offset;
}
if (needs_body)
{
if (pat->op == MUTT_PAT_BODY)
{
if (!mutt_file_seek(fp, e->body->offset, SEEK_SET))
{
return false;
}
}
len += e->body->length;
}
}
size_t blen = 256;
char *buf = mutt_mem_malloc(blen);
/* search the file "fp" */
while (len > 0)
{
if (pat->op == MUTT_PAT_HEADER)
{
buf = mutt_rfc822_read_line(fp, buf, &blen);
if (*buf == '\0')
break;
}
else if (!fgets(buf, blen - 1, fp))
break; /* don't loop forever */
if (patmatch(pat, buf))
{
match = true;
break;
}
len -= mutt_str_len(buf);
}
FREE(&buf);
if (c_thorough_search)
mutt_file_fclose(&fp);
#ifdef USE_FMEMOPEN
FREE(&temp);
#endif
return match;
}
/**
* perform_and - Perform a logical AND on a set of Patterns
* @param pat Patterns to test
* @param flags Optional flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param e Email
* @param msg Message
* @param cache Cached Patterns
* @retval true ALL of the Patterns evaluates to true
*/
static bool perform_and(struct PatternList *pat, PatternExecFlags flags,
struct Mailbox *m, struct Email *e, struct Message *msg,
struct PatternCache *cache)
{
struct Pattern *p = NULL;
SLIST_FOREACH(p, pat, entries)
{
if (!pattern_exec(p, flags, m, e, msg, cache))
{
return false;
}
}
return true;
}
/**
* perform_alias_and - Perform a logical AND on a set of Patterns
* @param pat Patterns to test
* @param flags Optional flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param av AliasView
* @param cache Cached Patterns
* @retval true ALL of the Patterns evaluate to true
*/
static bool perform_alias_and(struct PatternList *pat, PatternExecFlags flags,
struct AliasView *av, struct PatternCache *cache)
{
struct Pattern *p = NULL;
SLIST_FOREACH(p, pat, entries)
{
if (!mutt_pattern_alias_exec(p, flags, av, cache))
{
return false;
}
}
return true;
}
/**
* perform_or - Perform a logical OR on a set of Patterns
* @param pat Patterns to test
* @param flags Optional flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param e Email
* @param msg Message
* @param cache Cached Patterns
* @retval true ONE (or more) of the Patterns evaluates to true
*/
static int perform_or(struct PatternList *pat, PatternExecFlags flags,
struct Mailbox *m, struct Email *e, struct Message *msg,
struct PatternCache *cache)
{
struct Pattern *p = NULL;
SLIST_FOREACH(p, pat, entries)
{
if (pattern_exec(p, flags, m, e, msg, cache))
{
return true;
}
}
return false;
}
/**
* perform_alias_or - Perform a logical OR on a set of Patterns
* @param pat Patterns to test
* @param flags Optional flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param av AliasView
* @param cache Cached Patterns
* @retval true ONE (or more) of the Patterns evaluates to true
*/
static int perform_alias_or(struct PatternList *pat, PatternExecFlags flags,
struct AliasView *av, struct PatternCache *cache)
{
struct Pattern *p = NULL;
SLIST_FOREACH(p, pat, entries)
{
if (mutt_pattern_alias_exec(p, flags, av, cache))
{
return true;
}
}
return false;
}
/**
* match_addrlist - Match a Pattern against an Address list
* @param pat Pattern to find
* @param match_personal If true, also match the pattern against the real name
* @param n Number of Addresses supplied
* @param ... Variable number of Addresses
* @retval true
* - One Address matches (all_addr is false)
* - All the Addresses match (all_addr is true)
*/
static int match_addrlist(struct Pattern *pat, bool match_personal, int n, ...)
{
va_list ap;
va_start(ap, n);
for (; n; n--)
{
struct AddressList *al = va_arg(ap, struct AddressList *);
struct Address *a = NULL;
TAILQ_FOREACH(a, al, entries)
{
if (pat->all_addr ^ ((!pat->is_alias || alias_reverse_lookup(a)) &&
((a->mailbox && patmatch(pat, a->mailbox)) ||
(match_personal && a->personal && patmatch(pat, a->personal)))))
{
va_end(ap);
return !pat->all_addr; /* Found match, or non-match if all_addr */
}
}
}
va_end(ap);
return pat->all_addr; /* No matches, or all matches if all_addr */
}
/**
* match_reference - Match references against a Pattern
* @param pat Pattern to match
* @param refs List of References
* @retval true One of the references matches
*/
static bool match_reference(struct Pattern *pat, struct ListHead *refs)
{
struct ListNode *np = NULL;
STAILQ_FOREACH(np, refs, entries)
{
if (patmatch(pat, np->data))
return true;
}
return false;
}
/**
* mutt_is_predicate_recipient - Test an Envelopes Addresses using a predicate function
* @param all_addr If true, ALL Addresses must match
* @param env Envelope
* @param p Predicate function, e.g. mutt_is_subscribed_list()
* @retval true
* - One Address matches (all_addr is false)
* - All the Addresses match (all_addr is true)
*
* Test the 'To' and 'Cc' fields of an Address using a test function (the predicate).
*/
static int mutt_is_predicate_recipient(bool all_addr, struct Envelope *env, addr_predicate_t p)
{
struct AddressList *als[] = { &env->to, &env->cc };
for (size_t i = 0; i < mutt_array_size(als); ++i)
{
struct AddressList *al = als[i];
struct Address *a = NULL;
TAILQ_FOREACH(a, al, entries)
{
if (all_addr ^ p(a))
return !all_addr;
}
}
return all_addr;
}
/**
* mutt_is_subscribed_list_recipient - Matches subscribed mailing lists
* @param all_addr If true, ALL Addresses must be on the subscribed list
* @param env Envelope
* @retval true
* - One Address is subscribed (all_addr is false)
* - All the Addresses are subscribed (all_addr is true)
*/
int mutt_is_subscribed_list_recipient(bool all_addr, struct Envelope *env)
{
return mutt_is_predicate_recipient(all_addr, env, &mutt_is_subscribed_list);
}
/**
* mutt_is_list_recipient - Matches known mailing lists
* @param all_addr If true, ALL Addresses must be mailing lists
* @param env Envelope
* @retval true
* - One Address is a mailing list (all_addr is false)
* - All the Addresses are mailing lists (all_addr is true)
*/
int mutt_is_list_recipient(bool all_addr, struct Envelope *env)
{
return mutt_is_predicate_recipient(all_addr, env, &mutt_is_mail_list);
}
/**
* match_user - Matches the user's email Address
* @param all_addr If true, ALL Addresses must refer to the user
* @param al1 First AddressList
* @param al2 Second AddressList
* @retval true
* - One Address refers to the user (all_addr is false)
* - All the Addresses refer to the user (all_addr is true)
*/
static int match_user(int all_addr, struct AddressList *al1, struct AddressList *al2)
{
struct Address *a = NULL;
if (al1)
{
TAILQ_FOREACH(a, al1, entries)
{
if (all_addr ^ mutt_addr_is_user(a))
return !all_addr;
}
}
if (al2)
{
TAILQ_FOREACH(a, al2, entries)
{
if (all_addr ^ mutt_addr_is_user(a))
return !all_addr;
}
}
return all_addr;
}
/**
* match_threadcomplete - Match a Pattern against an email thread
* @param pat Pattern to match
* @param flags Flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param t Email thread
* @param left Navigate to the previous email
* @param up Navigate to the email's parent
* @param right Navigate to the next email
* @param down Navigate to the email's children
* @retval 1 Success, match found
* @retval 0 No match
*/
static int match_threadcomplete(struct PatternList *pat, PatternExecFlags flags,
struct Mailbox *m, struct MuttThread *t,
int left, int up, int right, int down)
{
if (!t)
return 0;
int a;
struct Email *e = t->message;
if (e)
if (mutt_pattern_exec(SLIST_FIRST(pat), flags, m, e, NULL))
return 1;
if (up && (a = match_threadcomplete(pat, flags, m, t->parent, 1, 1, 1, 0)))
return a;
if (right && t->parent && (a = match_threadcomplete(pat, flags, m, t->next, 0, 0, 1, 1)))
{
return a;
}
if (left && t->parent && (a = match_threadcomplete(pat, flags, m, t->prev, 1, 0, 0, 1)))
{
return a;
}
if (down && (a = match_threadcomplete(pat, flags, m, t->child, 1, 0, 1, 1)))
return a;
return 0;
}
/**
* match_threadparent - Match Pattern against an email's parent
* @param pat Pattern to match
* @param flags Flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param t Thread of email
* @retval 1 Success, pattern matched
* @retval 0 Pattern did not match
* @retval -1 Error
*/
static int match_threadparent(struct PatternList *pat, PatternExecFlags flags,
struct Mailbox *m, struct MuttThread *t)
{
if (!t || !t->parent || !t->parent->message)
return 0;
return mutt_pattern_exec(SLIST_FIRST(pat), flags, m, t->parent->message, NULL);
}
/**
* match_threadchildren - Match Pattern against an email's children
* @param pat Pattern to match
* @param flags Flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param t Thread of email
* @retval 1 Success, pattern matched
* @retval 0 Pattern did not match
* @retval -1 Error
*/
static int match_threadchildren(struct PatternList *pat, PatternExecFlags flags,
struct Mailbox *m, struct MuttThread *t)
{
if (!t || !t->child)
return 0;
for (t = t->child; t; t = t->next)
if (t->message && mutt_pattern_exec(SLIST_FIRST(pat), flags, m, t->message, NULL))
return 1;
return 0;
}
/**
* match_content_type - Match a Pattern against an Attachment's Content-Type
* @param pat Pattern to match
* @param b Attachment
* @retval true Success, pattern matched
* @retval false Pattern did not match
*/
static bool match_content_type(const struct Pattern *pat, struct Body *b)
{
if (!b)
return false;
char buf[256];
snprintf(buf, sizeof(buf), "%s/%s", TYPE(b), b->subtype);
if (patmatch(pat, buf))
return true;
if (match_content_type(pat, b->parts))
return true;
if (match_content_type(pat, b->next))
return true;
return false;
}
/**
* match_mime_content_type - Match a Pattern against an email's Content-Type
* @param pat Pattern to match
* @param e Email
* @param fp Message file
* @retval true Success, pattern matched
* @retval false Pattern did not match
*/
static bool match_mime_content_type(const struct Pattern *pat, struct Email *e, FILE *fp)
{
mutt_parse_mime_message(e, fp);
return match_content_type(pat, e->body);
}
/**
* match_update_dynamic_date - Update a dynamic date pattern
* @param pat Pattern to modify
* @retval true Pattern valid and updated
* @retval false Pattern invalid
*/
static bool match_update_dynamic_date(struct Pattern *pat)
{
struct Buffer *err = mutt_buffer_pool_get();
bool rc = eval_date_minmax(pat, pat->p.str, err);
mutt_buffer_pool_release(&err);
return rc;
}
/**
* set_pattern_cache_value - Sets a value in the PatternCache cache entry
* @param cache_entry Cache entry to update
* @param value Value to set
*
* Normalizes the "true" value to 2.
*/
static void set_pattern_cache_value(int *cache_entry, int value)
{
*cache_entry = (value != 0) ? 2 : 1;
}
/**
* get_pattern_cache_value - Get pattern cache value
* @param cache_entry Cache entry to get
* @retval 1 The cache value is set and has a true value
* @retval 0 otherwise (even if unset!)
*/
static int get_pattern_cache_value(int cache_entry)
{
return cache_entry == 2;
}
/**
* is_pattern_cache_set - Is a given Pattern cached?
* @param cache_entry Cache entry to check
* @retval true Pattern is cached
*/
static int is_pattern_cache_set(int cache_entry)
{
return cache_entry != 0;
}
/**
* msg_search_sendmode - Search in send-mode
* @param e Email to search
* @param pat Pattern to find
* @retval 1 Success, pattern matched
* @retval 0 Pattern did not match
* @retval -1 Error
*/
static int msg_search_sendmode(struct Email *e, struct Pattern *pat)
{
bool match = false;
char *buf = NULL;
size_t blen = 0;
FILE *fp = NULL;
if ((pat->op == MUTT_PAT_HEADER) || (pat->op == MUTT_PAT_WHOLE_MSG))
{
struct Buffer *tempfile = mutt_buffer_pool_get();
mutt_buffer_mktemp(tempfile);
fp = mutt_file_fopen(mutt_buffer_string(tempfile), "w+");
if (!fp)
{
mutt_perror(mutt_buffer_string(tempfile));
mutt_buffer_pool_release(&tempfile);
return 0;
}
mutt_rfc822_write_header(fp, e->env, e->body, MUTT_WRITE_HEADER_POSTPONE,
false, false, NeoMutt->sub);
fflush(fp);
if (mutt_file_seek(fp, 0, SEEK_SET))
{
while ((buf = mutt_file_read_line(buf, &blen, fp, NULL, MUTT_RL_NO_FLAGS)) != NULL)
{
if (patmatch(pat, buf) == 0)
{
match = true;
break;
}
}
}
FREE(&buf);
mutt_file_fclose(&fp);
unlink(mutt_buffer_string(tempfile));
mutt_buffer_pool_release(&tempfile);
if (match)
return match;
}
if ((pat->op == MUTT_PAT_BODY) || (pat->op == MUTT_PAT_WHOLE_MSG))
{
fp = mutt_file_fopen(e->body->filename, "r");
if (!fp)
{
mutt_perror(e->body->filename);
return 0;
}
while ((buf = mutt_file_read_line(buf, &blen, fp, NULL, MUTT_RL_NO_FLAGS)) != NULL)
{
if (patmatch(pat, buf) == 0)
{
match = true;
break;
}
}
FREE(&buf);
mutt_file_fclose(&fp);
}
return match;
}
/**
* pattern_needs_msg - Check whether a pattern needs a full message
* @param m Mailbox
* @param pat Pattern
* @retval true The pattern needs a full message
* @retval false The pattern does not need a full message
*/
static bool pattern_needs_msg(const struct Mailbox *m, const struct Pattern *pat)
{
if ((pat->op == MUTT_PAT_MIMETYPE) || (pat->op == MUTT_PAT_MIMEATTACH))
{
return true;
}
if ((pat->op == MUTT_PAT_WHOLE_MSG) || (pat->op == MUTT_PAT_BODY) || (pat->op == MUTT_PAT_HEADER))
{
#ifdef USE_IMAP
return !((m->type == MUTT_IMAP) && pat->string_match);
#else
return true;
#endif
}
if ((pat->op == MUTT_PAT_AND) || (pat->op == MUTT_PAT_OR))
{
struct Pattern *p = NULL;
SLIST_FOREACH(p, pat->child, entries)
{
if (pattern_needs_msg(m, p))
{
return true;
}
}
}
return false;
}
/**
* pattern_exec - Match a pattern against an email header
* @param pat Pattern to match
* @param flags Flags, e.g. #MUTT_MATCH_FULL_ADDRESS
* @param m Mailbox
* @param e Email
* @param msg MEssage
* @param cache Cache for common Patterns
* @retval true Success, pattern matched
* @retval false Pattern did not match
*
* flags: MUTT_MATCH_FULL_ADDRESS: match both personal and machine address
* cache: For repeated matches against the same Header, passing in non-NULL will
* store some of the cacheable pattern matches in this structure.
*/
static bool pattern_exec(struct Pattern *pat, PatternExecFlags flags,
struct Mailbox *m, struct Email *e,
struct Message *msg, struct PatternCache *cache)
{
switch (pat->op)
{
case MUTT_PAT_AND:
return pat->pat_not ^ (perform_and(pat->child, flags, m, e, msg, cache) > 0);
case MUTT_PAT_OR:
return pat->pat_not ^ (perform_or(pat->child, flags, m, e, msg, cache) > 0);
case MUTT_PAT_THREAD:
return pat->pat_not ^
match_threadcomplete(pat->child, flags, m, e->thread, 1, 1, 1, 1);
case MUTT_PAT_PARENT:
return pat->pat_not ^ match_threadparent(pat->child, flags, m, e->thread);
case MUTT_PAT_CHILDREN:
return pat->pat_not ^ match_threadchildren(pat->child, flags, m, e->thread);
case MUTT_ALL:
return !pat->pat_not;
case MUTT_EXPIRED:
return pat->pat_not ^ e->expired;
case MUTT_SUPERSEDED:
return pat->pat_not ^ e->superseded;
case MUTT_FLAG:
return pat->pat_not ^ e->flagged;
case MUTT_TAG:
return pat->pat_not ^ e->tagged;
case MUTT_NEW:
return pat->pat_not ? e->old || e->read : !(e->old || e->read);
case MUTT_UNREAD:
return pat->pat_not ? e->read : !e->read;
case MUTT_REPLIED:
return pat->pat_not ^ e->replied;
case MUTT_OLD:
return pat->pat_not ? (!e->old || e->read) : (e->old && !e->read);
case MUTT_READ:
return pat->pat_not ^ e->read;
case MUTT_DELETED:
return pat->pat_not ^ e->deleted;
case MUTT_PAT_MESSAGE:
return pat->pat_not ^ ((EMSG(e) >= pat->min) && (EMSG(e) <= pat->max));
case MUTT_PAT_DATE:
if (pat->dynamic)
match_update_dynamic_date(pat);
return pat->pat_not ^ (e->date_sent >= pat->min && e->date_sent <= pat->max);
case MUTT_PAT_DATE_RECEIVED:
if (pat->dynamic)
match_update_dynamic_date(pat);
return pat->pat_not ^ (e->received >= pat->min && e->received <= pat->max);
case MUTT_PAT_BODY:
case MUTT_PAT_HEADER:
case MUTT_PAT_WHOLE_MSG:
if (pat->sendmode)
{
if (!e->body || !e->body->filename)
return false;
return pat->pat_not ^ msg_search_sendmode(e, pat);
}
/* m can be NULL in certain cases, such as when replying to a message
* from the attachment menu and the user has a reply-hook using "~e".
* This is also the case when message scoring. */
if (!m)
return false;
#ifdef USE_IMAP
/* IMAP search sets e->matched at search compile time */
if ((m->type == MUTT_IMAP) && pat->string_match)
return e->matched;
#endif
return pat->pat_not ^ msg_search(pat, e, msg);
case MUTT_PAT_SERVERSEARCH:
#ifdef USE_IMAP
if (!m)
return false;
if (m->type == MUTT_IMAP)
{
return (pat->string_match) ? e->matched : false;
}
#endif
mutt_error(_("error: server custom search only supported with IMAP"));
return false;
case MUTT_PAT_SENDER:
if (!e->env)
return false;
return pat->pat_not ^ match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS),
1, &e->env->sender);
case MUTT_PAT_FROM:
if (!e->env)
return false;
return pat->pat_not ^
match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS), 1, &e->env->from);
case MUTT_PAT_TO:
if (!e->env)
return false;
return pat->pat_not ^
match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS), 1, &e->env->to);
case MUTT_PAT_CC:
if (!e->env)
return false;
return pat->pat_not ^
match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS), 1, &e->env->cc);
case MUTT_PAT_SUBJECT:
if (!e->env)
return false;
return pat->pat_not ^ (e->env->subject && patmatch(pat, e->env->subject));
case MUTT_PAT_ID:
case MUTT_PAT_ID_EXTERNAL:
if (!e->env)
return false;
return pat->pat_not ^ (e->env->message_id && patmatch(pat, e->env->message_id));
case MUTT_PAT_SCORE:
return pat->pat_not ^ (e->score >= pat->min &&
(pat->max == MUTT_MAXRANGE || e->score <= pat->max));
case MUTT_PAT_SIZE:
return pat->pat_not ^ (e->body->length >= pat->min &&
(pat->max == MUTT_MAXRANGE || e->body->length <= pat->max));
case MUTT_PAT_REFERENCE:
if (!e->env)
return false;
return pat->pat_not ^ (match_reference(pat, &e->env->references) ||
match_reference(pat, &e->env->in_reply_to));
case MUTT_PAT_ADDRESS:
if (!e->env)
return false;
return pat->pat_not ^ match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS),
4, &e->env->from, &e->env->sender,
&e->env->to, &e->env->cc);
case MUTT_PAT_RECIPIENT:
if (!e->env)
return false;
return pat->pat_not ^ match_addrlist(pat, (flags & MUTT_MATCH_FULL_ADDRESS),
2, &e->env->to, &e->env->cc);
case MUTT_PAT_LIST: /* known list, subscribed or not */
{
if (!e->env)
return false;
int result;
if (cache)
{
int *cache_entry = pat->all_addr ? &cache->list_all : &cache->list_one;
if (!is_pattern_cache_set(*cache_entry))
{
set_pattern_cache_value(cache_entry,
mutt_is_list_recipient(pat->all_addr, e->env));
}
result = get_pattern_cache_value(*cache_entry);
}
else
result = mutt_is_list_recipient(pat->all_addr, e->env);
return pat->pat_not ^ result;
}
case MUTT_PAT_SUBSCRIBED_LIST:
{
if (!e->env)
return false;
int result;
if (cache)
{
int *cache_entry = pat->all_addr ? &cache->sub_all : &cache->sub_one;
if (!is_pattern_cache_set(*cache_entry))
{
set_pattern_cache_value(cache_entry,
mutt_is_subscribed_list_recipient(pat->all_addr, e->env));
}
result = get_pattern_cache_value(*cache_entry);
}
else
result = mutt_is_subscribed_list_recipient(pat->all_addr, e->env);
return pat->pat_not ^ result;
}
case MUTT_PAT_PERSONAL_RECIP:
{
if (!e->env)
return false;
int result;
if (cache)
{
int *cache_entry = pat->all_addr ? &cache->pers_recip_all : &cache->pers_recip_one;
if (!is_pattern_cache_set(*cache_entry))
{
set_pattern_cache_value(cache_entry,
match_user(pat->all_addr, &e->env->to, &e->env->cc));
}
result = get_pattern_cache_value(*cache_entry);
}
else
result = match_user(pat->all_addr, &e->env->to, &e->env->cc);
return pat->pat_not ^ result;
}
case MUTT_PAT_PERSONAL_FROM:
{