From 41f8b16a421b60c79b935fec071da286ded58263 Mon Sep 17 00:00:00 2001 From: Thomas Preisner Date: Sun, 23 Mar 2025 15:38:07 +0100 Subject: [PATCH] pkgs: mailnix: Refactor domain-check and address-parsing into own helper functions --- pkgs/mailnix/src/config.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/pkgs/mailnix/src/config.rs b/pkgs/mailnix/src/config.rs index ce019c3..09d8c9a 100644 --- a/pkgs/mailnix/src/config.rs +++ b/pkgs/mailnix/src/config.rs @@ -76,6 +76,19 @@ fn sanitize_map_keys(map: &mut HashMap) -> Result<(), Box bool { + !address.contains("@") +} + +fn parse_address(address: &str) -> Result<(String, String), Box> { + let address_pattern = Regex::new(r"^(?P.*)@(?P.*)$").unwrap(); + let caps = address_pattern + .captures(address) + .ok_or("Mail address regex does not match for \"{address}\"") + .unwrap(); + Ok((caps["local_part"].to_string(), caps["domain"].to_string())) +} + impl Config { pub fn load>(path: P) -> Result> { let file = File::open(path)?; @@ -121,28 +134,22 @@ impl Config { pub fn check(&self) -> Result<(), Box> { // check whether all account domains exist - let re = Regex::new(r"^(?P.*)@(?P.*)$").unwrap(); for name in self.accounts.keys() { - let caps = re - .captures(name) - .ok_or("Mail address regex does not match") - .unwrap(); - if !self.domains.contains(&caps["domain"].to_string()) { + let (_, domain) = parse_address(name).unwrap(); + if !self.domains.contains(&domain) { panic!("Domain of account \"{name}\" does not exist"); } } // check whether aliases have corresponding accounts/domains for (from, dests) in self.aliases.iter() { - let is_domain = !from.contains("@"); - if is_domain && !self.domains.contains(from) { + if is_domain(from) && !self.domains.contains(from) { panic!("Aliased from-domain \"{from}\" does not exist"); } for dest in dests.iter() { - let is_domain = !dest.contains("@"); - if is_domain && !self.domains.contains(dest) { + if is_domain(dest) && !self.domains.contains(dest) { panic!("Aliased dest-domain \"{dest}\" does not exist"); - } else if !is_domain && !self.accounts.contains_key(dest) { + } else if !is_domain(dest) && !self.accounts.contains_key(dest) { panic!("Aliased dest-account \"{dest}\" does not exist"); } }