Pass default domain via commandline argument

This commit is contained in:
Gonne 2024-11-03 18:11:39 +01:00
parent 203f4e12d5
commit cd674370ca

View file

@ -1,8 +1,17 @@
use std::io::{self}; use std::io::{self};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::env;
fn main() { fn main() {
let redirects = parse_alias_to_map(); let args: Vec<String> = 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); let sieve_script = generate_sieve_script(redirects);
println!("{}", sieve_script); println!("{}", sieve_script);
} }
@ -24,7 +33,7 @@ fn generate_sieve_script(redirects: BTreeMap<String, Vec<String>>) -> String {
script script
} }
fn parse_alias_to_map() -> BTreeMap<String, Vec<String>> { fn parse_alias_to_map(default_domain : &str) -> BTreeMap<String, Vec<String>> {
// File must exist in the current path // File must exist in the current path
let mut redirect_map : BTreeMap<String, Vec<String>> = BTreeMap::new(); let mut redirect_map : BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut destinations : Vec<String> = Vec::new(); let mut destinations : Vec<String> = Vec::new();
@ -36,12 +45,12 @@ fn parse_alias_to_map() -> BTreeMap<String, Vec<String>> {
continue; continue;
} }
let redirects: Vec<String> = line.split_at(line.find(char::is_whitespace).unwrap_or(0)).1.split(" ") let redirects: Vec<String> = 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() { if redirects.is_empty() {
continue; continue;
} }
destinations.push(to_mailaddress(destination)); destinations.push(to_mailaddress(destination, default_domain));
redirect_map.insert(to_mailaddress(destination), redirects); redirect_map.insert(to_mailaddress(destination, default_domain), redirects);
} }
let mut changed = true; let mut changed = true;
while changed { while changed {
@ -64,11 +73,16 @@ fn parse_alias_to_map() -> BTreeMap<String, Vec<String>> {
redirect_map 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(); let mut addr = local_part.trim().to_string();
addr = addr.replace(",", ""); addr = addr.replace(",", "");
if addr.contains("@") { if addr.contains("@") {
return addr; 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");
}