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::collections::BTreeMap;
use std::env;
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);
println!("{}", sieve_script);
}
@ -24,7 +33,7 @@ fn generate_sieve_script(redirects: BTreeMap<String, Vec<String>>) -> String {
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
let mut redirect_map : BTreeMap<String, Vec<String>> = BTreeMap::new();
let mut destinations : Vec<String> = Vec::new();
@ -36,12 +45,12 @@ fn parse_alias_to_map() -> BTreeMap<String, Vec<String>> {
continue;
}
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() {
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<String, Vec<String>> {
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");
}