From 47483603807c3258ac63696be204931e5313e68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 26 Mar 2023 17:52:15 +0200 Subject: [PATCH] Add header to the CLI --- Cargo.toml | 2 +- src/config.rs | 41 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 ++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 45e6340..6adb35f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0" publish = false [dependencies] -clap = { version = "4.1.13", default-features = false, features = ["std", "derive", "help", "usage"] } +clap = { version = "4.1.13", default-features = false, features = ["std", "derive"] } env_logger = "^0.10" log = "^0.4" mailparse = "^0.14" diff --git a/src/config.rs b/src/config.rs index aa766fb..706f24e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -18,6 +18,10 @@ pub struct Config { domain: Vec, #[arg(short = 'D', long, value_name = "FILE")] domain_file: Option, + #[arg(short, long)] + header: Vec, + #[arg(short = 'o', long)] + header_optional: Vec, #[arg(short, long, value_name = "FILE")] revocation_list: Option, #[arg(short = 'x', long, default_value_t = 1296000)] @@ -27,6 +31,8 @@ pub struct Config { impl Config { pub fn init() -> Result { let mut cnf = Self::parse(); + + // domains let mut domain_set: HashSet = cnf.domain.into_iter().collect(); if let Some(path) = &cnf.domain_file { let f = File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; @@ -39,6 +45,11 @@ impl Config { } } cnf.domain = domain_set.into_iter().collect::>(); + + // headers + cnf.header = process_headers(&cnf.header, crate::DEFAULT_HEADERS); + cnf.header_optional = process_headers(&cnf.header_optional, crate::DEFAULT_HEADERS_OPT); + Ok(cnf) } @@ -61,4 +72,34 @@ impl Config { None } } + + pub fn headers(&self) -> &[String] { + &self.header + } + + pub fn headers_optional(&self) -> &[String] { + &self.header_optional + } +} + +fn process_headers(lst: &[String], default: &str) -> Vec { + let ret = if lst.is_empty() { + let default_lst = vec![default.to_string()]; + do_process_headers(&default_lst) + } else { + do_process_headers(lst) + }; + ret.into_iter().collect::>() +} + +fn do_process_headers(lst: &[String]) -> HashSet { + let mut ret = HashSet::with_capacity(128); + for input in lst { + for h in input.split(':') { + ret.insert(h.to_string().to_lowercase()); + } + //let mut input_headers: Vec = input.split(':').map(|e| e.to_string()).collect(); + //ret.append(&mut input_headers); + } + ret } diff --git a/src/main.rs b/src/main.rs index 7418565..4bd107c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ use std::collections::HashMap; use stdin_reader::StdinReader; const DEFAULT_BUFF_SIZE: usize = 1024; +const DEFAULT_HEADERS: &str = "from:reply-to:subject:date:to:cc"; +const DEFAULT_HEADERS_OPT: &str = "resent-date:resent-from:resent-to:resent-cc:in-reply-to:references:list-id:list-help:list-unsubscribe:list-subscribe:list-post:list-owner:list-archive"; const DEFAULT_MSG_SIZE: usize = 1024 * 1024; const LOG_LEVEL_ENV_VAR: &str = "OPENSMTPD_FILTER_DKIMOUT_LOG_LEVEL";