From cd674370ca908d64466def72d0e4df4d736649dd Mon Sep 17 00:00:00 2001 From: Gonne Date: Sun, 3 Nov 2024 18:11:39 +0100 Subject: [PATCH] Pass default domain via commandline argument --- src/main.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 42d55f1..763f97e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,17 @@ use std::io::{self}; use std::collections::BTreeMap; +use std::env; fn main() { - let redirects = parse_alias_to_map(); + let args: Vec = env::args().collect(); + if args.len() < 2 { + print_help(); + return; + } + + let default_domain = &args[1]; + + let redirects = parse_alias_to_map(default_domain); let sieve_script = generate_sieve_script(redirects); println!("{}", sieve_script); } @@ -24,7 +33,7 @@ fn generate_sieve_script(redirects: BTreeMap>) -> String { script } -fn parse_alias_to_map() -> BTreeMap> { +fn parse_alias_to_map(default_domain : &str) -> BTreeMap> { // File must exist in the current path let mut redirect_map : BTreeMap> = BTreeMap::new(); let mut destinations : Vec = Vec::new(); @@ -36,12 +45,12 @@ fn parse_alias_to_map() -> BTreeMap> { continue; } let redirects: Vec = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).1.split(" ") - .filter(|address| address.trim().to_string().replace(",","") != "").map(to_mailaddress).collect(); + .filter(|address| address.trim().to_string().replace(",","") != "").map(|addr| to_mailaddress(addr, default_domain)).collect(); if redirects.is_empty() { continue; } - destinations.push(to_mailaddress(destination)); - redirect_map.insert(to_mailaddress(destination), redirects); + destinations.push(to_mailaddress(destination, default_domain)); + redirect_map.insert(to_mailaddress(destination, default_domain), redirects); } let mut changed = true; while changed { @@ -64,11 +73,16 @@ fn parse_alias_to_map() -> BTreeMap> { redirect_map } -fn to_mailaddress(local_part: &str) -> String { +fn to_mailaddress(local_part: &str, default_domain : &str) -> String { let mut addr = local_part.trim().to_string(); addr = addr.replace(",", ""); if addr.contains("@") { return addr; } - addr + "@mathebau.de" + addr + "@" + default_domain } + +fn print_help(){ + print!("Reads a virtual alias file from STDIN and needs a default domain to append to local paths, e.g. + cat virt_aliases | ./alias_to_sieve example.com"); +} \ No newline at end of file