1#![doc = include_str!("../README.md")]
8
9use std::{borrow::Cow, sync::Arc, vec::IntoIter};
10
11use ahash::{AHashMap, AHashSet};
12use compiler::grammar::{
13 actions::action_redirect::{ByTime, Notify, Ret},
14 instruction::Instruction,
15 Capability,
16};
17use mail_parser::{HeaderName, Message};
18use runtime::{context::ScriptStack, Variable};
19
20pub mod compiler;
21pub mod runtime;
22
23pub(crate) const MAX_MATCH_VARIABLES: u32 = 63;
24pub(crate) const MAX_LOCAL_VARIABLES: u32 = 256;
25
26#[derive(Debug, Clone, Eq, PartialEq)]
27#[cfg_attr(
28 any(test, feature = "serde"),
29 derive(serde::Serialize, serde::Deserialize)
30)]
31#[cfg_attr(
32 feature = "rkyv",
33 derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
34)]
35pub struct Sieve {
36 instructions: Vec<Instruction>,
37 num_vars: u32,
38 num_match_vars: u32,
39}
40
41#[derive(Clone)]
42pub struct Compiler {
43 pub(crate) max_script_size: usize,
45 pub(crate) max_string_size: usize,
46 pub(crate) max_variable_name_size: usize,
47 pub(crate) max_nested_blocks: usize,
48 pub(crate) max_nested_tests: usize,
49 pub(crate) max_nested_foreverypart: usize,
50 pub(crate) max_match_variables: usize,
51 pub(crate) max_local_variables: usize,
52 pub(crate) max_header_size: usize,
53 pub(crate) max_includes: usize,
54 pub(crate) no_capability_check: bool,
55
56 pub(crate) functions: AHashMap<String, (u32, u32)>,
58}
59
60pub type Function = for<'x> fn(&'x Context<'x>, Vec<Variable>) -> Variable;
61
62#[derive(Default, Clone)]
63pub struct FunctionMap {
64 pub(crate) map: AHashMap<String, (u32, u32)>,
65 pub(crate) functions: Vec<Function>,
66}
67
68#[derive(Debug, Clone)]
69pub struct Runtime {
70 pub(crate) allowed_capabilities: AHashSet<Capability>,
71 pub(crate) valid_notification_uris: AHashSet<Cow<'static, str>>,
72 pub(crate) valid_ext_lists: AHashSet<Cow<'static, str>>,
73 pub(crate) protected_headers: Vec<HeaderName<'static>>,
74 pub(crate) environment: AHashMap<Cow<'static, str>, Variable>,
75 pub(crate) metadata: Vec<(Metadata<String>, Cow<'static, str>)>,
76 pub(crate) include_scripts: AHashMap<String, Arc<Sieve>>,
77 pub(crate) local_hostname: Cow<'static, str>,
78 pub(crate) functions: Vec<Function>,
79
80 pub(crate) max_nested_includes: usize,
81 pub(crate) cpu_limit: usize,
82 pub(crate) max_variable_size: usize,
83 pub(crate) max_redirects: usize,
84 pub(crate) max_received_headers: usize,
85 pub(crate) max_header_size: usize,
86 pub(crate) max_out_messages: usize,
87
88 pub(crate) default_vacation_expiry: u64,
89 pub(crate) default_duplicate_expiry: u64,
90
91 pub(crate) vacation_use_orig_rcpt: bool,
92 pub(crate) vacation_default_subject: Cow<'static, str>,
93 pub(crate) vacation_subject_prefix: Cow<'static, str>,
94}
95
96#[derive(Clone, Debug)]
97pub struct Context<'x> {
98 #[cfg(test)]
99 pub(crate) runtime: Runtime,
100 #[cfg(not(test))]
101 pub(crate) runtime: &'x Runtime,
102 pub(crate) user_address: Cow<'x, str>,
103 pub(crate) user_full_name: Cow<'x, str>,
104 pub(crate) current_time: i64,
105
106 pub(crate) message: Message<'x>,
107 pub(crate) message_size: usize,
108 pub(crate) envelope: Vec<(Envelope, Variable)>,
109 pub(crate) metadata: Vec<(Metadata<String>, Cow<'x, str>)>,
110
111 pub(crate) part: u32,
112 pub(crate) part_iter: IntoIter<u32>,
113 pub(crate) part_iter_stack: Vec<(u32, IntoIter<u32>)>,
114
115 pub(crate) spam_status: SpamStatus,
116 pub(crate) virus_status: VirusStatus,
117
118 pub(crate) pos: usize,
119 pub(crate) test_result: bool,
120 pub(crate) script_cache: AHashMap<Script, Arc<Sieve>>,
121 pub(crate) script_stack: Vec<ScriptStack>,
122 pub(crate) vars_global: AHashMap<Cow<'static, str>, Variable>,
123 pub(crate) vars_env: AHashMap<Cow<'static, str>, Variable>,
124 pub(crate) vars_local: Vec<Variable>,
125 pub(crate) vars_match: Vec<Variable>,
126 pub(crate) expr_stack: Vec<Variable>,
127 pub(crate) expr_pos: usize,
128
129 pub(crate) queued_events: IntoIter<Event>,
130 pub(crate) final_event: Option<Event>,
131 pub(crate) last_message_id: usize,
132 pub(crate) main_message_id: usize,
133
134 pub(crate) has_changes: bool,
135 pub(crate) num_redirects: usize,
136 pub(crate) num_instructions: usize,
137 pub(crate) num_out_messages: usize,
138}
139
140#[derive(Debug, Clone, Eq, PartialEq, Hash)]
141pub enum Script {
142 Personal(String),
143 Global(String),
144}
145
146#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
147#[cfg_attr(
148 any(test, feature = "serde"),
149 derive(serde::Serialize, serde::Deserialize)
150)]
151#[cfg_attr(
152 feature = "rkyv",
153 derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
154)]
155pub enum Envelope {
156 From,
157 To,
158 ByTimeAbsolute,
159 ByTimeRelative,
160 ByMode,
161 ByTrace,
162 Notify,
163 Orcpt,
164 Ret,
165 Envid,
166}
167
168#[derive(Debug, Clone, Eq, PartialEq, Hash)]
169#[cfg_attr(
170 any(test, feature = "serde"),
171 derive(serde::Serialize, serde::Deserialize)
172)]
173#[cfg_attr(
174 feature = "rkyv",
175 derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
176)]
177pub enum Metadata<T> {
178 Server { annotation: T },
179 Mailbox { name: T, annotation: T },
180}
181
182#[derive(Debug, Clone, Eq, PartialEq)]
183pub enum Event {
184 IncludeScript {
185 name: Script,
186 optional: bool,
187 },
188 MailboxExists {
189 mailboxes: Vec<Mailbox>,
190 special_use: Vec<String>,
191 },
192 ListContains {
193 lists: Vec<String>,
194 values: Vec<String>,
195 match_as: MatchAs,
196 },
197 DuplicateId {
198 id: String,
199 expiry: u64,
200 last: bool,
201 },
202 SetEnvelope {
203 envelope: Envelope,
204 value: String,
205 },
206 Function {
207 id: ExternalId,
208 arguments: Vec<Variable>,
209 },
210
211 Keep {
213 flags: Vec<String>,
214 message_id: usize,
215 },
216 Discard,
217 Reject {
218 extended: bool,
219 reason: String,
220 },
221 FileInto {
222 folder: String,
223 flags: Vec<String>,
224 mailbox_id: Option<String>,
225 special_use: Option<String>,
226 create: bool,
227 message_id: usize,
228 },
229 SendMessage {
230 recipient: Recipient,
231 notify: Notify,
232 return_of_content: Ret,
233 by_time: ByTime<i64>,
234 message_id: usize,
235 },
236 Notify {
237 from: Option<String>,
238 importance: Importance,
239 options: Vec<String>,
240 message: String,
241 method: String,
242 },
243 CreatedMessage {
244 message_id: usize,
245 message: Vec<u8>,
246 },
247}
248
249pub type ExternalId = u32;
250
251#[derive(Debug, Clone, PartialEq, Eq, Hash)]
252#[cfg_attr(
253 any(test, feature = "serde"),
254 derive(serde::Serialize, serde::Deserialize)
255)]
256#[cfg_attr(
257 feature = "rkyv",
258 derive(rkyv::Serialize, rkyv::Deserialize, rkyv::Archive)
259)]
260pub(crate) struct FileCarbonCopy<T> {
261 pub mailbox: T,
262 pub mailbox_id: Option<T>,
263 pub create: bool,
264 pub flags: Vec<T>,
265 pub special_use: Option<T>,
266}
267
268#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
269pub enum Importance {
270 High,
271 Normal,
272 Low,
273}
274
275#[derive(Debug, Clone, Copy, Eq, PartialEq)]
276pub enum MatchAs {
277 Octet,
278 Lowercase,
279 Number,
280}
281
282#[derive(Debug, Clone, Eq, PartialEq, Hash)]
283pub enum Recipient {
284 Address(String),
285 List(String),
286 Group(Vec<String>),
287}
288
289#[derive(Debug, Clone, Eq, PartialEq)]
290pub enum Input {
291 True,
292 False,
293 FncResult(Variable),
294 Script { name: Script, script: Arc<Sieve> },
295}
296
297#[derive(Debug, Clone, Eq, PartialEq, Hash)]
298pub enum Mailbox {
299 Name(String),
300 Id(String),
301}
302
303#[derive(Debug, Clone, Copy, PartialEq)]
304pub enum SpamStatus {
305 Unknown,
306 Ham,
307 MaybeSpam(f64),
308 Spam,
309}
310
311#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
312pub enum VirusStatus {
313 Unknown,
314 Clean,
315 Replaced,
316 Cured,
317 MaybeVirus,
318 Virus,
319}
320
321#[cfg(test)]
322mod tests {
323 use std::{
324 fs,
325 path::{Path, PathBuf},
326 };
327
328 use ahash::{AHashMap, AHashSet};
329 use mail_parser::{
330 parsers::MessageStream, Encoding, HeaderValue, Message, MessageParser, MessagePart,
331 PartType,
332 };
333
334 use crate::{
335 compiler::grammar::Capability,
336 runtime::{actions::action_mime::reset_test_boundary, Variable},
337 Compiler, Context, Envelope, Event, FunctionMap, Input, Mailbox, Recipient, Runtime,
338 SpamStatus, VirusStatus,
339 };
340
341 impl Variable {
342 pub fn unwrap_string(self) -> String {
343 self.to_string().into_owned()
344 }
345 }
346
347 #[test]
348 fn test_suite() {
349 let mut tests = Vec::new();
350 let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
351 path.push("tests");
352
353 read_dir(path, &mut tests);
354
355 for test in tests {
356 println!("===== {} =====", test.display());
366 run_test(&test);
367 }
368 }
369
370 fn read_dir(path: PathBuf, files: &mut Vec<PathBuf>) {
371 for entry in fs::read_dir(path).unwrap() {
372 let entry = entry.unwrap().path();
373 if entry.is_dir() {
374 read_dir(entry, files);
375 } else if entry
376 .extension()
377 .and_then(|e| e.to_str())
378 .unwrap_or("")
379 .eq("svtest")
380 {
381 files.push(entry);
382 }
383 }
384 }
385
386 fn run_test(script_path: &Path) {
387 let mut fnc_map = FunctionMap::new()
388 .with_function("trim", |_, v| match v.into_iter().next().unwrap() {
389 crate::runtime::Variable::String(s) => s.trim().to_string().into(),
390 v => v.to_string().into(),
391 })
392 .with_function("len", |_, v| v[0].to_string().len().into())
393 .with_function("count", |_, v| {
394 v[0].as_array().map_or(0, |arr| arr.len()).into()
395 })
396 .with_function("to_lowercase", |_, v| {
397 v[0].to_string().to_lowercase().to_string().into()
398 })
399 .with_function("to_uppercase", |_, v| {
400 v[0].to_string().to_uppercase().to_string().into()
401 })
402 .with_function("is_uppercase", |_, v| {
403 v[0].to_string()
404 .as_ref()
405 .chars()
406 .filter(|c| c.is_alphabetic())
407 .all(|c| c.is_uppercase())
408 .into()
409 })
410 .with_function("is_ascii", |_, v| {
411 v[0].to_string().as_ref().is_ascii().into()
412 })
413 .with_function("char_count", |_, v| {
414 v[0].to_string().as_ref().chars().count().into()
415 })
416 .with_function("lines", |_, v| {
417 v[0].to_string()
418 .lines()
419 .map(|line| Variable::from(line.to_string()))
420 .collect::<Vec<_>>()
421 .into()
422 })
423 .with_function_args(
424 "contains",
425 |_, v| v[0].to_string().contains(v[1].to_string().as_ref()).into(),
426 2,
427 )
428 .with_function_args(
429 "eq_lowercase",
430 |_, v| {
431 v[0].to_string()
432 .as_ref()
433 .eq_ignore_ascii_case(v[1].to_string().as_ref())
434 .into()
435 },
436 2,
437 )
438 .with_function_args(
439 "concat_three",
440 |_, v| format!("{}-{}-{}", v[0], v[1], v[2]).into(),
441 3,
442 )
443 .with_function_args(
444 "in_array",
445 |_, v| {
446 v[0].as_array()
447 .is_some_and(|arr| arr.contains(&v[1]))
448 .into()
449 },
450 2,
451 )
452 .with_external_function("ext_zero", 0, 0)
453 .with_external_function("ext_one", 1, 1)
454 .with_external_function("ext_two", 2, 2)
455 .with_external_function("ext_three", 3, 3)
456 .with_external_function("ext_true", 4, 0)
457 .with_external_function("ext_false", 5, 0);
458 let mut compiler = Compiler::new()
459 .with_max_string_size(10240)
460 .register_functions(&mut fnc_map);
461
462 let mut ancestors = script_path.ancestors();
463 ancestors.next();
464 let base_path = ancestors.next().unwrap();
465 let script = compiler
466 .compile(&add_crlf(&fs::read(script_path).unwrap()))
467 .unwrap();
468
469 let mut input = Input::script("", script);
470 let mut current_test = String::new();
471 let mut raw_message_: Option<Vec<u8>> = None;
472 let mut prev_state = None;
473 let mut mailboxes = Vec::new();
474 let mut lists: AHashMap<String, AHashSet<String>> = AHashMap::new();
475 let mut duplicated_ids = AHashSet::new();
476 let mut actions = Vec::new();
477
478 'outer: loop {
479 let runtime = Runtime::new()
480 .with_protected_header("Auto-Submitted")
481 .with_protected_header("Received")
482 .with_valid_notification_uri("mailto")
483 .with_max_out_messages(100)
484 .with_capability(Capability::While)
485 .with_capability(Capability::Expressions)
486 .with_functions(&mut fnc_map.clone());
487 let mut instance = Context::new(
488 &runtime,
489 Message {
490 parts: vec![MessagePart {
491 headers: vec![],
492 is_encoding_problem: false,
493 body: PartType::Text("".into()),
494 encoding: Encoding::None,
495 offset_header: 0,
496 offset_body: 0,
497 offset_end: 0,
498 }],
499 raw_message: b""[..].into(),
500 ..Default::default()
501 },
502 );
503 let raw_message = raw_message_.take().unwrap_or_default();
504 instance.message =
505 MessageParser::new()
506 .parse(&raw_message)
507 .unwrap_or_else(|| Message {
508 html_body: vec![],
509 text_body: vec![],
510 attachments: vec![],
511 parts: vec![MessagePart {
512 headers: vec![],
513 is_encoding_problem: false,
514 body: PartType::Text("".into()),
515 encoding: Encoding::None,
516 offset_header: 0,
517 offset_body: 0,
518 offset_end: 0,
519 }],
520 raw_message: b""[..].into(),
521 });
522 instance.message_size = raw_message.len();
523 if let Some((pos, script_cache, script_stack, vars_global, vars_local, vars_match)) =
524 prev_state.take()
525 {
526 instance.pos = pos;
527 instance.script_cache = script_cache;
528 instance.script_stack = script_stack;
529 instance.vars_global = vars_global;
530 instance.vars_local = vars_local;
531 instance.vars_match = vars_match;
532 }
533 instance.set_env_variable("vnd.stalwart.default_mailbox", "INBOX");
534 instance.set_env_variable("vnd.stalwart.username", "john.doe");
535 instance.set_user_address("MAILER-DAEMON");
536 if let Some(addr) = instance
537 .message
538 .from()
539 .and_then(|a| a.first())
540 .and_then(|a| a.address.as_ref())
541 {
542 instance.set_envelope(Envelope::From, addr.to_string());
543 }
544 if let Some(addr) = instance
545 .message
546 .to()
547 .and_then(|a| a.first())
548 .and_then(|a| a.address.as_ref())
549 {
550 instance.set_envelope(Envelope::To, addr.to_string());
551 }
552
553 while let Some(event) = instance.run(input) {
554 match event.unwrap() {
555 Event::IncludeScript { name, optional } => {
556 let mut include_path = PathBuf::from(base_path);
557 include_path.push(if matches!(name, crate::Script::Personal(_)) {
558 "included"
559 } else {
560 "included-global"
561 });
562 include_path.push(format!("{name}.sieve"));
563
564 if let Ok(bytes) = fs::read(include_path.as_path()) {
565 let script = compiler.compile(&add_crlf(&bytes)).unwrap();
566 input = Input::script(name, script);
567 } else if optional {
568 input = Input::False;
569 } else {
570 panic!("Script {} not found.", include_path.display());
571 }
572 }
573 Event::MailboxExists {
574 mailboxes: mailboxes_,
575 special_use,
576 } => {
577 for action in &actions {
578 if let Event::FileInto { folder, create, .. } = action
579 && *create && !mailboxes.contains(folder) {
580 mailboxes.push(folder.to_string());
581 }
582 }
583 input = (special_use.is_empty()
584 && mailboxes_.iter().all(|n| {
585 if let Mailbox::Name(n) = n {
586 mailboxes.contains(n)
587 } else {
588 false
589 }
590 }))
591 .into();
592 }
593 Event::ListContains {
594 lists: lists_,
595 values,
596 ..
597 } => {
598 let mut result = false;
599 'list: for list in &lists_ {
600 if let Some(list) = lists.get(list) {
601 for value in &values {
602 if list.contains(value) {
603 result = true;
604 break 'list;
605 }
606 }
607 }
608 }
609
610 input = result.into();
611 }
612 Event::DuplicateId { id, .. } => {
613 input = duplicated_ids.contains(&id).into();
614 }
615 Event::Function { id, arguments } => {
616 if id == u32::MAX {
617 input = Input::True;
619 let mut arguments = arguments.into_iter();
620 let command = arguments.next().unwrap().unwrap_string();
621 let mut params =
622 arguments.map(|arg| arg.unwrap_string()).collect::<Vec<_>>();
623
624 match command.as_str() {
625 "test" => {
626 current_test = params.pop().unwrap();
627 println!("Running test '{current_test}'...");
628 }
629 "test_set" => {
630 let mut params = params.into_iter();
631 let target = params.next().expect("test_set parameter");
632 if target == "message" {
633 let value = params.next().unwrap();
634 raw_message_ = if value.eq_ignore_ascii_case(":smtp") {
635 let mut message = None;
636 for action in actions.iter().rev() {
637 if let Event::SendMessage { message_id, .. } =
638 action
639 {
640 let message_ = actions
641 .iter()
642 .find_map(|item| {
643 if let Event::CreatedMessage {
644 message_id: message_id_,
645 message,
646 } = item
647 && message_id == message_id_ {
648 return Some(message);
649 }
650 None
651 })
652 .unwrap();
653 message = message_.into();
658 break;
659 }
660 }
661 message.expect("No SMTP message found").to_vec().into()
662 } else {
663 value.into_bytes().into()
664 };
665 prev_state = (
666 instance.pos,
667 instance.script_cache,
668 instance.script_stack,
669 instance.vars_global,
670 instance.vars_local,
671 instance.vars_match,
672 )
673 .into();
674
675 continue 'outer;
676 } else if let Some(envelope) = target.strip_prefix("envelope.")
677 {
678 let envelope =
679 Envelope::try_from(envelope.to_string()).unwrap();
680 instance.envelope.retain(|(e, _)| e != &envelope);
681 instance.set_envelope(envelope, params.next().unwrap());
682 } else if target == "currentdate" {
683 let bytes = params.next().unwrap().into_bytes();
684 if let HeaderValue::DateTime(dt) =
685 MessageStream::new(&bytes).parse_date()
686 {
687 instance.current_time = dt.to_timestamp();
688 } else {
689 panic!("Invalid currentdate");
690 }
691 } else {
692 panic!("test_set {target} not implemented.");
693 }
694 }
695 "test_message" => {
696 let mut params = params.into_iter();
697 input = match params.next().unwrap().as_str() {
698 ":folder" => {
699 let folder_name = params.next().expect("test_message folder name");
700 matches!(&instance.final_event, Some(Event::Keep { .. })) ||
701 actions.iter().any(|a| if !folder_name.eq_ignore_ascii_case("INBOX") {
702 matches!(a, Event::FileInto { folder, .. } if folder == &folder_name )
703 } else {
704 matches!(a, Event::Keep { .. })
705 })
706 }
707 ":smtp" => {
708 actions.iter().any(|a| matches!(a, Event::SendMessage { .. } ))
709 }
710 param => panic!("Invalid test_message param '{param}'" ),
711 }.into();
712 }
713 "test_assert_message" => {
714 let expected_message =
715 params.first().expect("test_set parameter");
716 let built_message = instance.build_message();
717 if expected_message.as_bytes() != built_message {
718 print!("<[");
720 print!("{}", String::from_utf8(built_message).unwrap());
721 println!("]>");
722 panic!("Message built incorrectly at '{current_test}'");
723 }
724 }
725 "test_config_set" => {
726 let mut params = params.into_iter();
727 let name = params.next().unwrap();
728 let value = params.next().expect("test_config_set value");
729
730 match name.as_str() {
731 "sieve_editheader_protected"
732 | "sieve_editheader_forbid_add"
733 | "sieve_editheader_forbid_delete" => {
734 if !value.is_empty() {
735 for header_name in value.split(' ') {
736 instance.runtime.set_protected_header(
737 header_name.to_string(),
738 );
739 }
740 } else {
741 instance.runtime.protected_headers.clear();
742 }
743 }
744 "sieve_variables_max_variable_size" => {
745 instance
746 .runtime
747 .set_max_variable_size(value.parse().unwrap());
748 }
749 "sieve_valid_ext_list" => {
750 instance.runtime.set_valid_ext_list(value);
751 }
752 "sieve_ext_list_item" => {
753 lists
754 .entry(value)
755 .or_default()
756 .insert(params.next().expect("list item value"));
757 }
758 "sieve_duplicated_id" => {
759 duplicated_ids.insert(value);
760 }
761 "sieve_user_email" => {
762 instance.set_user_address(value);
763 }
764 "sieve_vacation_use_original_recipient" => {
765 instance.runtime.set_vacation_use_orig_rcpt(
766 value.eq_ignore_ascii_case("yes"),
767 );
768 }
769 "sieve_vacation_default_subject" => {
770 instance.runtime.set_vacation_default_subject(value);
771 }
772 "sieve_vacation_default_subject_template" => {
773 instance.runtime.set_vacation_subject_prefix(value);
774 }
775 "sieve_spam_status" => {
776 instance.set_spam_status(SpamStatus::from_number(
777 value.parse().unwrap(),
778 ));
779 }
780 "sieve_spam_status_plus" => {
781 instance.set_spam_status(
782 match value.parse::<u32>().unwrap() {
783 0 => SpamStatus::Unknown,
784 100.. => SpamStatus::Spam,
785 n => SpamStatus::MaybeSpam((n as f64) / 100.0),
786 },
787 );
788 }
789 "sieve_virus_status" => {
790 instance.set_virus_status(VirusStatus::from_number(
791 value.parse().unwrap(),
792 ));
793 }
794 "sieve_editheader_max_header_size" => {
795 let mhs = if !value.is_empty() {
796 value.parse::<usize>().unwrap()
797 } else {
798 1024
799 };
800 instance.runtime.set_max_header_size(mhs);
801 compiler.set_max_header_size(mhs);
802 }
803 "sieve_include_max_includes" => {
804 compiler.set_max_includes(if !value.is_empty() {
805 value.parse::<usize>().unwrap()
806 } else {
807 3
808 });
809 }
810 "sieve_include_max_nesting_depth" => {
811 compiler.set_max_nested_blocks(if !value.is_empty() {
812 value.parse::<usize>().unwrap()
813 } else {
814 3
815 });
816 }
817 param => panic!("Invalid test_config_set param '{param}'"),
818 }
819 }
820 "test_result_execute" => {
821 input =
822 (matches!(&instance.final_event, Some(Event::Keep { .. }))
823 || actions.iter().any(|a| {
824 matches!(
825 a,
826 Event::Keep { .. }
827 | Event::FileInto { .. }
828 | Event::SendMessage { .. }
829 )
830 }))
831 .into();
832 }
833 "test_result_action" => {
834 let param =
835 params.first().expect("test_result_action parameter");
836 input = if param == "reject" {
837 (actions.iter().any(|a| matches!(a, Event::Reject { .. })))
838 .into()
839 } else if param == "redirect" {
840 let param = params
841 .last()
842 .expect("test_result_action redirect address");
843 (actions
844 .iter()
845 .any(|a| matches!(a, Event::SendMessage { recipient: Recipient::Address(address), .. } if address == param)))
846 .into()
847 } else if param == "keep" {
848 (matches!(&instance.final_event, Some(Event::Keep { .. }))
849 || actions
850 .iter()
851 .any(|a| matches!(a, Event::Keep { .. })))
852 .into()
853 } else if param == "send_message" {
854 (actions
855 .iter()
856 .any(|a| matches!(a, Event::SendMessage { .. })))
857 .into()
858 } else {
859 panic!("test_result_action {param} not implemented");
860 };
861 }
862 "test_result_action_count" => {
863 input = (actions.len()
864 == params.first().unwrap().parse::<usize>().unwrap())
865 .into();
866 }
867 "test_imap_metadata_set" => {
868 let mut params = params.into_iter();
869 let first = params.next().expect("metadata parameter");
870 let (mailbox, annotation) = if first == ":mailbox" {
871 (
872 params.next().expect("metadata mailbox name").into(),
873 params.next().expect("metadata annotation name"),
874 )
875 } else {
876 (None, first)
877 };
878 let value = params.next().expect("metadata value");
879 if let Some(mailbox) = mailbox {
880 instance.set_medatata((mailbox, annotation), value);
881 } else {
882 instance.set_medatata(annotation, value);
883 }
884 }
885 "test_mailbox_create" => {
886 mailboxes.push(params.pop().expect("mailbox to create"));
887 }
888 "test_result_reset" => {
889 actions.clear();
890 instance.final_event = Event::Keep {
891 flags: vec![],
892 message_id: 0,
893 }
894 .into();
895 instance.metadata.clear();
896 instance.has_changes = false;
897 instance.num_redirects = 0;
898 instance.runtime.vacation_use_orig_rcpt = false;
899 mailboxes.clear();
900 lists.clear();
901 reset_test_boundary();
902 }
903 "test_script_compile" => {
904 let mut include_path = PathBuf::from(base_path);
905 include_path.push(params.first().unwrap());
906
907 if let Ok(bytes) = fs::read(include_path.as_path()) {
908 let result = compiler.compile(&add_crlf(&bytes));
909 input = result.is_ok().into();
913 } else {
914 panic!("Script {} not found.", include_path.display());
915 }
916 }
917 "test_config_reload" => (),
918 "test_fail" => {
919 panic!(
920 "Test '{}' failed: {}",
921 current_test,
922 params.pop().unwrap()
923 );
924 }
925 _ => panic!("Test command {command} not implemented."),
926 }
927 } else {
928 let result = match id {
929 0 => Variable::from("my_value"),
930 1 => Variable::from(arguments[0].to_string().to_uppercase()),
931 2 => Variable::from(format!(
932 "{}-{}",
933 arguments[0].to_string(),
934 arguments[1].to_string()
935 )),
936 3 => Variable::from(format!(
937 "{}-{}-{}",
938 arguments[0].to_string(),
939 arguments[1].to_string(),
940 arguments[2].to_string()
941 )),
942 4 => true.into(),
943 5 => false.into(),
944 _ => {
945 panic!("Unknown external function {id}");
946 }
947 };
948
949 input = result.into();
950 }
951 }
952
953 action => {
954 actions.push(action);
955 input = true.into();
956 }
957 }
958 }
959
960 return;
961 }
962 }
963
964 fn add_crlf(bytes: &[u8]) -> Vec<u8> {
965 let mut result = Vec::with_capacity(bytes.len());
966 let mut last_ch = 0;
967 for &ch in bytes {
968 if ch == b'\n' && last_ch != b'\r' {
969 result.push(b'\r');
970 }
971 result.push(ch);
972 last_ch = ch;
973 }
974 result
975 }
976}