[core] V0.1: parse, compose, reply/forward, safe names, threading, rules + Sieve, sanitiser, search (55 tests green)

This commit is contained in:
2026-11-08 14:11:27 +01:00
parent c59480a0c0
commit e98e626c76
14 changed files with 1276 additions and 75 deletions
+60 -7
View File
@@ -50,13 +50,26 @@ pub enum RuleError {
impl Rule {
/// A rule with no condition, an empty text or no folder would match everything or nothing.
pub fn validate(&self) -> Result<(), RuleError> {
todo!()
if self.conditions.is_empty() {
return Err(RuleError::NoConditions);
}
if self.conditions.iter().any(|c| c.contains.trim().is_empty()) {
return Err(RuleError::EmptyText);
}
if self.folder.trim().is_empty() {
return Err(RuleError::NoFolder);
}
Ok(())
}
/// The rule "Rule from this mail": sender address -> folder.
pub fn from_sender(sender: &Address, folder: &str) -> Rule {
let _ = (sender, folder);
todo!()
let email = sender.email.trim().to_lowercase();
Rule {
name: format!("From {email}"),
conditions: vec![Condition { field: Field::From, contains: email }],
folder: folder.trim().to_string(),
}
}
}
@@ -70,12 +83,52 @@ pub struct RuleMessage<'a> {
/// The first valid rule whose conditions all hold, or `None` (the message stays where it is).
pub fn first_match<'r>(rules: &'r [Rule], message: &RuleMessage<'_>) -> Option<&'r Rule> {
let _ = (rules, message);
todo!()
rules.iter().find(|rule| rule.validate().is_ok() && rule.conditions.iter().all(|c| holds(c, message)))
}
fn holds(condition: &Condition, message: &RuleMessage<'_>) -> bool {
let needle = condition.contains.trim().to_lowercase();
let any = |list: &[Address]| list.iter().any(|a| a.display().to_lowercase().contains(&needle));
match condition.field {
Field::From => any(message.from),
Field::To => any(message.to) || any(message.cc),
Field::Subject => message.subject.to_lowercase().contains(&needle),
}
}
fn sieve_string(s: &str) -> String {
let mut out = String::with_capacity(s.len() + 2);
out.push('"');
for c in s.chars().filter(|c| !c.is_control()) {
if c == '"' || c == '\\' {
out.push('\\');
}
out.push(c);
}
out.push('"');
out
}
fn sieve_test(condition: &Condition) -> String {
let header = match condition.field {
Field::From => "\"from\"",
Field::To => "[\"to\", \"cc\"]",
Field::Subject => "\"subject\"",
};
format!("header :contains :comparator \"i;unicode-casemap\" {header} {}", sieve_string(condition.contains.trim()))
}
/// The same rules as a Sieve script (RFC 5228 + fileinto + RFC 5051 unicode-casemap).
pub fn to_sieve(rules: &[Rule]) -> String {
let _ = rules;
todo!()
let mut out = String::from("require [\"fileinto\", \"comparator-i;unicode-casemap\"];\n");
for rule in rules.iter().filter(|r| r.validate().is_ok()) {
let name: String = rule.name.chars().filter(|c| !c.is_control()).collect();
let tests: Vec<String> = rule.conditions.iter().map(sieve_test).collect();
let test = if tests.len() == 1 { tests[0].clone() } else { format!("allof({})", tests.join(", ")) };
out.push_str(&format!(
"\n# {name}\nif {test} {{\n fileinto {};\n stop;\n}}\n",
sieve_string(rule.folder.trim())
));
}
out
}