sieve-kit 0.2.1

Structured mail filter rules engine with RFC 5228 Sieve semantics: conditions, actions, vacation/notify evaluation, IMAP flags, bounded regex
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
//! Rule evaluation engine. Synchronous, bounded, no I/O.
//!
//! Regex evaluation is checked against a 100 ms budget after matching.
//! Invalid regex patterns are treated as non-matching (never panic).

use std::sync::Arc;

use regex::Regex;

use crate::types::{
    AddressPart, Condition, ConditionField, EnvelopePart, FilterRule, Filterable, LogicOp, Operator,
};

/// Maximum time allowed for a single regex match (milliseconds).
const REGEX_TIMEOUT_MS: u128 = 100;

/// Cache of compiled regex patterns keyed by the raw pattern string.
/// Prevents re-compilation on every evaluation pass.
#[derive(Clone, Default)]
pub struct RegexCache {
    inner: Arc<std::sync::RwLock<std::collections::HashMap<String, Option<Regex>>>>,
}

impl RegexCache {
    /// Returns a compiled regex, or `None` if the pattern is invalid.
    #[must_use]
    pub fn get_or_compile(&self, pattern: &str) -> Option<Regex> {
        // Fast path: already compiled.
        if let Ok(cache) = self.inner.read() {
            if let Some(entry) = cache.get(pattern) {
                return entry.clone();
            }
        }
        // Slow path: compile and insert.
        let compiled = Regex::new(pattern).ok();
        if let Ok(mut cache) = self.inner.write() {
            cache.insert(pattern.to_string(), compiled.clone());
        }
        compiled
    }
}

/// Resolve the string value of a condition field on a message.
///
/// `HasAttachment` resolves to `"true"`/`"false"`; missing fields resolve to
/// the empty string. For [`ConditionField::Envelope`] this is the *full*
/// envelope address (`:all` semantics); use
/// [`evaluate_condition`] to apply `:localpart`/`:domain` extraction.
#[must_use]
pub fn field_value<'a, F: Filterable + ?Sized>(msg: &'a F, field: &ConditionField) -> &'a str {
    match field {
        ConditionField::From => msg.from(),
        ConditionField::To => msg.to(),
        ConditionField::Cc => msg.cc(),
        ConditionField::Subject => msg.subject(),
        ConditionField::Body => msg.body(),
        ConditionField::Header(name) => msg.header(name).unwrap_or_default(),
        ConditionField::HasAttachment => {
            if msg.has_attachment() {
                "true"
            } else {
                "false"
            }
        }
        ConditionField::Envelope { part, .. } => match part {
            EnvelopePart::From => msg.envelope_from().unwrap_or_default(),
            EnvelopePart::To => msg.envelope_to().unwrap_or_default(),
        },
    }
}

/// Evaluate a filter rule against a message.
///
/// Returns `true` when the rule matches (all/any conditions satisfied).
/// Disabled rules and rules without conditions always return `false`.
///
/// # Panics
///
/// Never panics. Invalid regex patterns are treated as non-matching.
#[must_use]
pub fn evaluate_rule<F: Filterable + ?Sized>(
    rule: &FilterRule,
    msg: &F,
    regex_cache: &RegexCache,
) -> bool {
    if !rule.enabled {
        return false;
    }
    if rule.conditions.is_empty() {
        return false;
    }

    let results: Vec<bool> = rule
        .conditions
        .iter()
        .map(|c| evaluate_condition(c, msg, regex_cache))
        .collect();

    match rule.condition_logic {
        LogicOp::And => results.iter().all(|&r| r),
        LogicOp::Or => results.iter().any(|&r| r),
    }
}

/// Evaluate a single condition against a message.
///
/// Envelope conditions (RFC 5228 §5.1) evaluate as non-matching when the
/// message supplies no envelope data; `:localpart`/`:domain` select the
/// portion of the address compared.
///
/// # Panics
///
/// Never panics.
#[must_use]
pub fn evaluate_condition<F: Filterable + ?Sized>(
    condition: &Condition,
    msg: &F,
    regex_cache: &RegexCache,
) -> bool {
    let result = if let ConditionField::Envelope { part, address_part } = &condition.field {
        let raw = match part {
            EnvelopePart::From => msg.envelope_from(),
            EnvelopePart::To => msg.envelope_to(),
        };
        // Missing envelope data: the test does not match (a negated test
        // then matches, per `not` semantics).
        match raw {
            Some(value) => apply_operator(
                &condition.operator,
                extract_address_part(value, *address_part),
                condition,
                regex_cache,
            ),
            None => false,
        }
    } else {
        let field_value = field_value(msg, &condition.field);
        apply_operator(&condition.operator, field_value, condition, regex_cache)
    };
    if condition.negate { !result } else { result }
}

/// Extract the portion of an address selected by an
/// [`AddressPart`]. `:localpart` is everything before the last `@` (the
/// whole value when there is no `@`); `:domain` is everything after the
/// last `@` (empty when there is no `@`).
#[must_use]
pub fn extract_address_part(address: &str, part: AddressPart) -> &str {
    match part {
        AddressPart::All => address,
        AddressPart::Localpart => address.rsplit_once('@').map_or(address, |(lp, _)| lp),
        AddressPart::Domain => address.rsplit_once('@').map_or("", |(_, d)| d),
    }
}

/// Apply a comparison operator to a resolved field value. Shared by the
/// plain and envelope code paths so operator semantics (the `:comparator`
/// plumbing) stay identical across `address`/`header`/`envelope` tests.
fn apply_operator(
    operator: &Operator,
    value: &str,
    condition: &Condition,
    regex_cache: &RegexCache,
) -> bool {
    match operator {
        Operator::Contains => value
            .to_lowercase()
            .contains(&condition.value.to_lowercase()),
        Operator::Equals => value.eq_ignore_ascii_case(&condition.value),
        Operator::Matches => glob_match(&condition.value, value),
        Operator::Regex => evaluate_regex(&condition.value, value, regex_cache),
        Operator::Exists => !value.is_empty(),
        Operator::NumericEquals => ascii_numeric_eq(&condition.value, value),
    }
}

/// `i;ascii-numeric` equality (RFC 4790): both strings must consist solely
/// of ASCII digits and be numerically equal (leading zeros ignored). The
/// empty string compares equal only to the empty string; any non-numeric
/// operand never matches.
#[must_use]
pub fn ascii_numeric_eq(a: &str, b: &str) -> bool {
    let valid = |s: &str| s.bytes().all(|c| c.is_ascii_digit());
    if !valid(a) || !valid(b) {
        return false;
    }
    if a.is_empty() || b.is_empty() {
        return a.is_empty() && b.is_empty();
    }
    strip_leading_zeros(a) == strip_leading_zeros(b)
}

fn strip_leading_zeros(s: &str) -> &str {
    let trimmed = s.trim_start_matches('0');
    if trimmed.is_empty() { "0" } else { trimmed }
}

/// Evaluate a regex condition with a bounded budget.
///
/// Returns `false` on invalid patterns or when the match exceeds the time
/// budget (never panics).
fn evaluate_regex(pattern: &str, input: &str, cache: &RegexCache) -> bool {
    let Some(re) = cache.get_or_compile(pattern) else {
        return false;
    };

    // The regex crate is linear-time (no catastrophic backtracking) and has
    // no built-in timeout. Wall-clock measurement is a post-hoc safety check:
    // a match that ran over budget is reported as non-matching so callers can
    // treat pathological rules conservatively.
    let start = std::time::Instant::now();
    let matched = re.is_match(input);
    if start.elapsed().as_millis() > REGEX_TIMEOUT_MS {
        return false;
    }
    matched
}

/// Simple glob matching supporting `*` (any chars) and `?` (single char).
///
/// `*` and `?` are the only special characters; backslash escapes them.
/// Matching is case-insensitive.
#[must_use]
pub fn glob_match(pattern: &str, input: &str) -> bool {
    let pattern_lower = pattern.to_lowercase();
    let input_lower = input.to_lowercase();
    glob_match_inner(pattern_lower.as_bytes(), input_lower.as_bytes())
}

#[allow(clippy::similar_names)]
fn glob_match_inner(pattern: &[u8], input: &[u8]) -> bool {
    let mut pi = 0;
    let mut ii = 0;
    let mut star_pi = usize::MAX;
    let mut star_ii = 0;

    while ii < input.len() {
        if pi < pattern.len() && pattern[pi] == b'*' {
            star_pi = pi;
            star_ii = ii;
            pi += 1;
        } else if pi < pattern.len() && (pattern[pi] == b'?' || pattern[pi] == input[ii]) {
            pi += 1;
            ii += 1;
        } else if star_pi != usize::MAX {
            pi = star_pi + 1;
            star_ii += 1;
            ii = star_ii;
        } else {
            return false;
        }
    }

    while pi < pattern.len() && pattern[pi] == b'*' {
        pi += 1;
    }

    pi == pattern.len()
}

/// Sort rules by priority (ascending = highest priority first).
pub fn sort_rules_by_priority(rules: &mut [FilterRule]) {
    rules.sort_by_key(|r| r.priority);
}

/// Caller-supplied runtime context for [`evaluate_plan`].
///
/// Evaluation stays synchronous and I/O-free: instead of the engine touching
/// storage, callers hand in an optional `seen_before` predicate that decides
/// whether an automated reply was already sent to a sender within its
/// respond period.
#[derive(Default)]
pub struct EvalContext<'a> {
    /// Respond-once hook for the
    /// [`vacation`](crate::types::Action::Vacation) action (RFC 5230):
    /// return `true` when a reply was already sent to this sender within
    /// the configured period. When `None`, no dedup is applied and the
    /// outcome always carries the reply (hosts can dedup themselves using
    /// the `to`/`days` fields of
    /// [`VacationReply`](crate::actions::VacationReply), or use the
    /// ready-made [`VacationTracker`](crate::actions::VacationTracker)).
    pub seen_before: Option<&'a dyn Fn(&str) -> bool>,
}

impl<'a> EvalContext<'a> {
    /// Attach a respond-once dedup predicate.
    #[must_use]
    pub fn with_seen_before(mut self, seen_before: &'a dyn Fn(&str) -> bool) -> Self {
        self.seen_before = Some(seen_before);
        self
    }
}

/// A non-fatal observation made during [`evaluate_plan`]. Evaluation never
/// fails: misconfigured or exotic actions are reported as warnings while
/// the rest of the plan still executes.
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum EvalWarning {
    /// A [`notify`](crate::types::Action::Notify) method URI uses a scheme
    /// outside the recognized set
    /// ([`KNOWN_NOTIFY_SCHEMES`](crate::types::KNOWN_NOTIFY_SCHEMES)). The
    /// notification outcome is still produced — the host decides whether to
    /// attempt delivery.
    #[error("notify method {method:?} has an unrecognized URI scheme")]
    UnknownNotifyMethod {
        /// The offending method URI.
        method: String,
    },
    /// A [`vacation`](crate::types::Action::Vacation) reply could not be
    /// routed: neither envelope sender nor header From yielded a usable
    /// address. The reply is omitted from the plan.
    #[error("vacation action could not determine a reply-to sender")]
    VacationNoSender,
}

/// The result of evaluating a rule set with [`evaluate_plan`]: the matched
/// rule (if any), the planned outcomes, and any warnings.
#[derive(Clone, Debug, Default)]
pub struct EvalOutcome {
    /// Id of the first matching rule, or `None` when no rule matched.
    pub rule_id: Option<String>,
    /// Outcomes to execute, in rule action order.
    pub plan: Vec<crate::actions::PlannedAction>,
    /// Non-fatal observations (unknown notify schemes, unroutable replies).
    pub warnings: Vec<EvalWarning>,
}

impl EvalOutcome {
    /// Whether the outcome set is empty (no rule matched and no warnings).
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.plan.is_empty() && self.warnings.is_empty()
    }
}

/// Evaluate rules against a message and resolve them into execution-ready
/// outcomes.
///
/// Like [`collect_matches`](crate::actions::collect_matches) this is
/// first-match-wins, but the returned plan additionally resolves the
/// actions that need runtime context:
///
/// - [`vacation`](crate::types::Action::Vacation): the reply-to sender is
///   taken from envelope data (falling back to the header From address);
///   the subject defaults to `Re: <original subject>`; a reply already sent
///   (per [`EvalContext::seen_before`]) suppresses the outcome, and an
///   unroutable reply emits [`EvalWarning::VacationNoSender`].
/// - [`notify`](crate::types::Action::Notify): unrecognized method schemes
///   emit [`EvalWarning::UnknownNotifyMethod`].
///
/// Pure and synchronous: no I/O, never panics.
#[must_use]
pub fn evaluate_plan<F: Filterable + ?Sized>(
    rules: &[FilterRule],
    msg: &F,
    ctx: &EvalContext<'_>,
    regex_cache: &RegexCache,
) -> EvalOutcome {
    let mut outcome = EvalOutcome::default();
    let Some(rule) = rules.iter().find(|r| evaluate_rule(r, msg, regex_cache)) else {
        return outcome;
    };
    outcome.rule_id = Some(rule.id.clone());
    for action in &rule.actions {
        match action {
            crate::types::Action::Vacation(vacation) => {
                plan_vacation(vacation, msg, ctx, &mut outcome);
            }
            crate::types::Action::Notify(notify) => {
                if !notify.has_known_scheme() {
                    outcome.warnings.push(EvalWarning::UnknownNotifyMethod {
                        method: notify.method.clone(),
                    });
                }
                outcome
                    .plan
                    .push(crate::actions::PlannedAction::from(action));
            }
            other => outcome
                .plan
                .push(crate::actions::PlannedAction::from(other)),
        }
    }
    outcome
}

/// Resolve a vacation action into a (possibly suppressed) reply outcome.
fn plan_vacation<F: Filterable + ?Sized>(
    vacation: &crate::types::Vacation,
    msg: &F,
    ctx: &EvalContext<'_>,
    outcome: &mut EvalOutcome,
) {
    let sender = msg
        .envelope_from()
        .filter(|s| !s.is_empty())
        .map_or_else(|| msg.from().to_string(), str::to_string);
    if sender.is_empty() {
        outcome.warnings.push(EvalWarning::VacationNoSender);
        return;
    }
    if ctx.seen_before.is_some_and(|seen| seen(&sender)) {
        // Respond-once-per-sender-per-period: suppress silently.
        return;
    }
    let subject = vacation.subject.clone().unwrap_or_else(|| {
        let original = msg.subject();
        if original.is_empty() {
            "Automated reply".to_string()
        } else {
            format!("Re: {original}")
        }
    });
    outcome.plan.push(crate::actions::PlannedAction::Vacation(
        crate::actions::VacationReply {
            to: sender,
            days: vacation.days,
            subject,
            from: vacation.from.clone(),
            message: vacation.message.clone(),
        },
    ));
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used, clippy::expect_used)]

    use super::*;
    use crate::types::MailEnvelope;

    fn make_rule(conditions: Vec<Condition>, logic: LogicOp) -> FilterRule {
        FilterRule {
            id: "test-rule-1".to_string(),
            name: "Test Rule".to_string(),
            enabled: true,
            priority: 0,
            conditions,
            condition_logic: logic,
            actions: vec![],
        }
    }

    fn make_envelope() -> MailEnvelope {
        MailEnvelope {
            from: "alice@example.com".to_string(),
            to: "bob@example.com".to_string(),
            cc: String::new(),
            subject: "Hello World".to_string(),
            body: String::new(),
            has_attachment: false,
            envelope_from: None,
            envelope_to: None,
            headers: vec![],
        }
    }

    #[test]
    fn contains_operator_matches_case_insensitive() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "hello".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn contains_operator_no_match() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "nomatch".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn negate_inverts_result() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Contains,
                value: "nomatch".to_string(),
                negate: true,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn and_logic_requires_all() {
        let rule = make_rule(
            vec![
                Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "hello".to_string(),
                    negate: false,
                },
                Condition {
                    field: ConditionField::From,
                    operator: Operator::Contains,
                    value: "nomatch".to_string(),
                    negate: false,
                },
            ],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn or_logic_requires_any() {
        let rule = make_rule(
            vec![
                Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "nomatch".to_string(),
                    negate: false,
                },
                Condition {
                    field: ConditionField::From,
                    operator: Operator::Contains,
                    value: "alice".to_string(),
                    negate: false,
                },
            ],
            LogicOp::Or,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_condition_matches() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: r"(?i)hello\s+world".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_invalid_pattern_returns_false() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: "[invalid".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn glob_match_basic() {
        assert!(glob_match("hello*", "hello world"));
        assert!(glob_match("*world", "hello world"));
        assert!(glob_match("hello*world", "hello beautiful world"));
        assert!(glob_match("h?llo", "hello"));
        assert!(!glob_match("h?llo", "hllo"));
        assert!(glob_match("*", "anything"));
    }

    #[test]
    fn disabled_rule_never_matches() {
        let mut rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Exists,
                value: String::new(),
                negate: false,
            }],
            LogicOp::And,
        );
        rule.enabled = false;
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn empty_conditions_never_match() {
        let rule = make_rule(vec![], LogicOp::And);
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(!evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn exists_operator() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Exists,
                value: String::new(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = make_envelope();
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn body_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Body,
                operator: Operator::Contains,
                value: "test".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            body: "this is a test body".to_string(),
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn has_attachment_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::HasAttachment,
                operator: Operator::Equals,
                value: "true".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            has_attachment: true,
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn header_condition() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Header("X-Priority".to_string()),
                operator: Operator::Equals,
                value: "high".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            headers: vec![("x-priority".to_string(), "high".to_string())],
            ..make_envelope()
        };
        let cache = RegexCache::default();
        assert!(evaluate_rule(&rule, &msg, &cache));
    }

    #[test]
    fn regex_cache_reuses_compiled_pattern() {
        let cache = RegexCache::default();
        let r1 = cache.get_or_compile(r"\d+");
        let r2 = cache.get_or_compile(r"\d+");
        // Both calls should succeed and return equivalent patterns.
        assert!(r1.is_some());
        assert!(r2.is_some());
        // Verify both patterns match the same input.
        assert!(r1.unwrap().is_match("123"));
        assert!(r2.unwrap().is_match("123"));
    }

    #[test]
    fn rule_validation_rejects_invalid_regex() {
        let mut rule = make_rule(
            vec![Condition {
                field: ConditionField::Subject,
                operator: Operator::Regex,
                value: "[invalid".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::InvalidRegex { .. })
        ));
        rule.conditions[0].operator = Operator::Contains;
        rule.conditions[0].value = "ok".to_string();
        assert!(rule.validate().is_ok());
        rule.id = String::new();
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::EmptyRuleId)
        ));
        rule.id = "x".to_string();
        rule.name = String::new();
        assert!(matches!(
            rule.validate(),
            Err(crate::error::FilterError::EmptyRuleName)
        ));
    }

    // ---- envelope test (RFC 5228 §5.1) ---------------------------------

    fn envelope_condition(part: EnvelopePart, part_kind: AddressPart, value: &str) -> Condition {
        Condition {
            field: ConditionField::Envelope {
                part,
                address_part: part_kind,
            },
            operator: Operator::Equals,
            value: value.to_string(),
            negate: false,
        }
    }

    fn envelope_msg() -> MailEnvelope {
        MailEnvelope {
            from: "newsletter@lists.example.com".to_string(),
            envelope_from: Some("bounce@sender.example.net".to_string()),
            envelope_to: Some("you@corp.example".to_string()),
            ..MailEnvelope::default()
        }
    }

    #[test]
    fn envelope_all_matches_full_address() {
        let msg = envelope_msg();
        let cache = RegexCache::default();
        for (part, value) in [
            (EnvelopePart::From, "bounce@sender.example.net"),
            (EnvelopePart::To, "you@corp.example"),
        ] {
            let cond = envelope_condition(part, AddressPart::All, value);
            assert!(evaluate_condition(&cond, &msg, &cache));
        }
    }

    #[test]
    fn envelope_localpart_and_domain() {
        let msg = envelope_msg();
        let cache = RegexCache::default();
        let cond = envelope_condition(EnvelopePart::From, AddressPart::Localpart, "bounce");
        assert!(evaluate_condition(&cond, &msg, &cache));
        let cond = envelope_condition(
            EnvelopePart::From,
            AddressPart::Domain,
            "sender.example.net",
        );
        assert!(evaluate_condition(&cond, &msg, &cache));
        let cond = envelope_condition(EnvelopePart::From, AddressPart::Domain, "elsewhere");
        assert!(!evaluate_condition(&cond, &msg, &cache));
    }

    #[test]
    fn envelope_matching_is_case_insensitive() {
        let msg = envelope_msg();
        let cache = RegexCache::default();
        let cond = envelope_condition(EnvelopePart::To, AddressPart::All, "YOU@CORP.EXAMPLE");
        assert!(evaluate_condition(&cond, &msg, &cache));
    }

    #[test]
    fn envelope_without_at_domain_is_empty() {
        let msg = MailEnvelope {
            envelope_from: Some("bare-address".to_string()),
            ..MailEnvelope::default()
        };
        let cache = RegexCache::default();
        let localpart =
            envelope_condition(EnvelopePart::From, AddressPart::Localpart, "bare-address");
        assert!(evaluate_condition(&localpart, &msg, &cache));
        let domain = envelope_condition(EnvelopePart::From, AddressPart::Domain, "");
        assert!(evaluate_condition(&domain, &msg, &cache));
        let domain = envelope_condition(EnvelopePart::From, AddressPart::Domain, "example.com");
        assert!(!evaluate_condition(&domain, &msg, &cache));
    }

    #[test]
    fn envelope_missing_data_never_matches() {
        let msg = MailEnvelope::default();
        let cache = RegexCache::default();
        let cond = envelope_condition(EnvelopePart::From, AddressPart::All, "anything");
        assert!(!evaluate_condition(&cond, &msg, &cache));
        // Negation inverts: `not envelope` matches when data is missing.
        let cond = Condition {
            negate: true,
            ..cond
        };
        assert!(evaluate_condition(&cond, &msg, &cache));
    }

    #[test]
    fn envelope_supports_all_operators() {
        let msg = envelope_msg();
        let cache = RegexCache::default();
        let base_field = ConditionField::Envelope {
            part: EnvelopePart::To,
            address_part: AddressPart::Domain,
        };
        let contains = Condition {
            field: base_field.clone(),
            operator: Operator::Contains,
            value: "corp".to_string(),
            negate: false,
        };
        assert!(evaluate_condition(&contains, &msg, &cache));
        let glob = Condition {
            field: base_field.clone(),
            operator: Operator::Matches,
            value: "*.example".to_string(),
            negate: false,
        };
        assert!(evaluate_condition(&glob, &msg, &cache));
        let exists = Condition {
            field: base_field.clone(),
            operator: Operator::Exists,
            value: String::new(),
            negate: false,
        };
        assert!(evaluate_condition(&exists, &msg, &cache));
        let numeric = Condition {
            field: base_field,
            operator: Operator::NumericEquals,
            value: "7".to_string(),
            negate: false,
        };
        assert!(!evaluate_condition(&numeric, &msg, &cache));
    }

    #[test]
    fn field_value_envelope_resolves_full_address() {
        let msg = envelope_msg();
        assert_eq!(
            field_value(
                &msg,
                &ConditionField::Envelope {
                    part: EnvelopePart::From,
                    address_part: AddressPart::All,
                }
            ),
            "bounce@sender.example.net"
        );
    }

    // ---- i;ascii-numeric comparator ------------------------------------

    #[test]
    fn ascii_numeric_equality() {
        assert!(ascii_numeric_eq("42", "42"));
        assert!(ascii_numeric_eq("007", "7"));
        assert!(ascii_numeric_eq("", ""));
        assert!(!ascii_numeric_eq("42", "0421"));
        assert!(!ascii_numeric_eq("", "0"));
        assert!(!ascii_numeric_eq("12a", "12"));
        assert!(!ascii_numeric_eq("12", "1 2"));
        assert!(!ascii_numeric_eq("-1", "1"));
    }

    #[test]
    fn numeric_equals_operator_on_header() {
        let rule = make_rule(
            vec![Condition {
                field: ConditionField::Header("X-Spam-Score".to_string()),
                operator: Operator::NumericEquals,
                value: "10".to_string(),
                negate: false,
            }],
            LogicOp::And,
        );
        let msg = MailEnvelope {
            headers: vec![("X-Spam-Score".to_string(), "010".to_string())],
            ..MailEnvelope::default()
        };
        assert!(evaluate_rule(&rule, &msg, &RegexCache::default()));
    }

    // ---- evaluate_plan: vacation, notify, warnings ----------------------

    use crate::actions::{VacationReply, VacationTracker};
    use crate::types::{Action, Notify, Vacation};

    /// A rule that always matches [`eval_envelope`] (subject "Hello").
    fn matching_rule(actions: Vec<Action>) -> FilterRule {
        FilterRule {
            actions,
            ..make_rule(
                vec![Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "hello".to_string(),
                    negate: false,
                }],
                LogicOp::And,
            )
        }
    }

    fn eval_envelope() -> MailEnvelope {
        MailEnvelope {
            from: "alice@example.com".to_string(),
            envelope_from: Some("alice@bounce.example.com".to_string()),
            to: "you@example.com".to_string(),
            subject: "Hello".to_string(),
            ..MailEnvelope::default()
        }
    }

    #[test]
    fn evaluate_plan_resolves_vacation_reply() {
        let rule = matching_rule(vec![Action::Vacation(
            Vacation::new("I am away until Monday.")
                .with_subject("Away")
                .with_days(3),
        )]);
        let msg = eval_envelope();
        let outcome = evaluate_plan(
            &[rule],
            &msg,
            &EvalContext::default(),
            &RegexCache::default(),
        );
        assert_eq!(outcome.plan.len(), 1);
        assert!(outcome.warnings.is_empty());
        let crate::actions::PlannedAction::Vacation(reply) = &outcome.plan[0] else {
            panic!("expected vacation outcome");
        };
        assert_eq!(
            reply,
            &VacationReply {
                to: "alice@bounce.example.com".to_string(),
                days: 3,
                subject: "Away".to_string(),
                from: None,
                message: "I am away until Monday.".to_string(),
            }
        );
    }

    #[test]
    fn vacation_subject_defaults_to_re_original() {
        let rule = FilterRule {
            actions: vec![Action::Vacation(Vacation::new("away"))],
            ..matching_rule(vec![])
        };
        let outcome = evaluate_plan(
            &[rule],
            &eval_envelope(),
            &EvalContext::default(),
            &RegexCache::default(),
        );
        let crate::actions::PlannedAction::Vacation(reply) = &outcome.plan[0] else {
            panic!("expected vacation outcome");
        };
        assert_eq!(reply.subject, "Re: Hello");
        assert_eq!(reply.days, 7);
    }

    #[test]
    fn vacation_prefers_envelope_sender_over_header_from() {
        let rule = FilterRule {
            actions: vec![Action::Vacation(Vacation::new("away"))],
            ..matching_rule(vec![])
        };
        let msg = MailEnvelope {
            from: "header-from@example.com".to_string(),
            envelope_from: Some("envelope-from@example.net".to_string()),
            ..eval_envelope()
        };
        let outcome = evaluate_plan(
            &[rule],
            &msg,
            &EvalContext::default(),
            &RegexCache::default(),
        );
        let crate::actions::PlannedAction::Vacation(reply) = &outcome.plan[0] else {
            panic!("expected vacation outcome");
        };
        assert_eq!(reply.to, "envelope-from@example.net");
    }

    #[test]
    fn vacation_seen_before_hook_suppresses_reply() {
        let rule = FilterRule {
            actions: vec![Action::Vacation(Vacation::new("away"))],
            ..matching_rule(vec![])
        };
        let msg = eval_envelope();
        let seen = |sender: &str| sender == "alice@bounce.example.com";
        let ctx = EvalContext::default().with_seen_before(&seen);
        let outcome = evaluate_plan(&[rule], &msg, &ctx, &RegexCache::default());
        assert!(outcome.plan.is_empty());
        assert!(outcome.warnings.is_empty());
    }

    #[test]
    fn vacation_tracker_dedups_across_evaluations() {
        let rule = FilterRule {
            actions: vec![Action::Vacation(Vacation::new("away"))],
            ..matching_rule(vec![])
        };
        let tracker = VacationTracker::new();
        let seen = |sender: &str| tracker.seen_before(sender, 7);
        let ctx = EvalContext::default().with_seen_before(&seen);

        let first = evaluate_plan(
            std::slice::from_ref(&rule),
            &eval_envelope(),
            &ctx,
            &RegexCache::default(),
        );
        assert_eq!(first.plan.len(), 1);
        if let crate::actions::PlannedAction::Vacation(reply) = &first.plan[0] {
            tracker.record(&reply.to);
        }

        let second = evaluate_plan(
            std::slice::from_ref(&rule),
            &eval_envelope(),
            &ctx,
            &RegexCache::default(),
        );
        assert!(second.plan.is_empty());
    }

    #[test]
    fn vacation_without_sender_warns_and_is_omitted() {
        let rule = FilterRule {
            actions: vec![Action::Vacation(Vacation::new("away"))],
            ..matching_rule(vec![])
        };
        // Matches on subject but supplies neither envelope sender nor a
        // header From address to reply to.
        let msg = MailEnvelope {
            subject: "Hello".to_string(),
            ..MailEnvelope::default()
        };
        let outcome = evaluate_plan(
            &[rule],
            &msg,
            &EvalContext::default(),
            &RegexCache::default(),
        );
        assert!(outcome.plan.is_empty());
        assert_eq!(outcome.warnings, vec![EvalWarning::VacationNoSender]);
    }

    #[test]
    fn notify_plans_method_and_message() {
        let rule = FilterRule {
            actions: vec![Action::Notify(Notify::new(
                "mailto:ops@example.com",
                "Payment received",
            ))],
            ..matching_rule(vec![])
        };
        let outcome = evaluate_plan(
            &[rule],
            &eval_envelope(),
            &EvalContext::default(),
            &RegexCache::default(),
        );
        assert!(outcome.warnings.is_empty());
        assert_eq!(
            outcome.plan,
            vec![crate::actions::PlannedAction::Notify {
                method: "mailto:ops@example.com".to_string(),
                message: "Payment received".to_string(),
            }]
        );
    }

    #[test]
    fn unknown_notify_method_parses_but_warns() {
        let rule = FilterRule {
            actions: vec![
                Action::Notify(Notify::new("carrier-pigeon:perth", "hello")),
                Action::MarkRead,
            ],
            ..matching_rule(vec![])
        };
        let outcome = evaluate_plan(
            &[rule],
            &eval_envelope(),
            &EvalContext::default(),
            &RegexCache::default(),
        );
        // The notification outcome is still produced...
        assert_eq!(outcome.plan.len(), 2);
        // ...alongside a warning about the unrecognized scheme.
        assert_eq!(
            outcome.warnings,
            vec![EvalWarning::UnknownNotifyMethod {
                method: "carrier-pigeon:perth".to_string(),
            }]
        );
    }

    #[test]
    fn notify_method_without_scheme_warns() {
        let rule = FilterRule {
            actions: vec![Action::Notify(Notify::new("no-scheme-here", ""))],
            ..matching_rule(vec![])
        };
        let outcome = evaluate_plan(
            &[rule],
            &eval_envelope(),
            &EvalContext::default(),
            &RegexCache::default(),
        );
        assert_eq!(outcome.plan.len(), 1);
        assert_eq!(
            outcome.warnings,
            vec![EvalWarning::UnknownNotifyMethod {
                method: "no-scheme-here".to_string(),
            }]
        );
    }

    #[test]
    fn no_matching_rule_yields_empty_outcome() {
        let rule = FilterRule {
            actions: vec![Action::MarkRead],
            ..make_rule(
                vec![Condition {
                    field: ConditionField::Subject,
                    operator: Operator::Contains,
                    value: "never".to_string(),
                    negate: false,
                }],
                LogicOp::And,
            )
        };
        let outcome = evaluate_plan(
            &[rule],
            &eval_envelope(),
            &EvalContext::default(),
            &RegexCache::default(),
        );
        assert!(outcome.is_empty());
        assert_eq!(outcome.rule_id, None);
    }
}