pkgs: mailnix: Refactor domain-check and address-parsing into own helper functions
This commit is contained in:
parent
01e63f4bab
commit
41f8b16a42
1 changed files with 18 additions and 11 deletions
|
|
@ -76,6 +76,19 @@ fn sanitize_map_keys<T: Clone>(map: &mut HashMap<String, T>) -> Result<(), Box<d
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_domain(address: &str) -> bool {
|
||||||
|
!address.contains("@")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_address(address: &str) -> Result<(String, String), Box<dyn Error>> {
|
||||||
|
let address_pattern = Regex::new(r"^(?P<local_part>.*)@(?P<domain>.*)$").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 {
|
impl Config {
|
||||||
pub fn load<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
|
pub fn load<P: AsRef<Path>>(path: P) -> Result<Config, Box<dyn Error>> {
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
|
|
@ -121,28 +134,22 @@ impl Config {
|
||||||
|
|
||||||
pub fn check(&self) -> Result<(), Box<dyn Error>> {
|
pub fn check(&self) -> Result<(), Box<dyn Error>> {
|
||||||
// check whether all account domains exist
|
// check whether all account domains exist
|
||||||
let re = Regex::new(r"^(?P<local_part>.*)@(?P<domain>.*)$").unwrap();
|
|
||||||
for name in self.accounts.keys() {
|
for name in self.accounts.keys() {
|
||||||
let caps = re
|
let (_, domain) = parse_address(name).unwrap();
|
||||||
.captures(name)
|
if !self.domains.contains(&domain) {
|
||||||
.ok_or("Mail address regex does not match")
|
|
||||||
.unwrap();
|
|
||||||
if !self.domains.contains(&caps["domain"].to_string()) {
|
|
||||||
panic!("Domain of account \"{name}\" does not exist");
|
panic!("Domain of account \"{name}\" does not exist");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check whether aliases have corresponding accounts/domains
|
// check whether aliases have corresponding accounts/domains
|
||||||
for (from, dests) in self.aliases.iter() {
|
for (from, dests) in self.aliases.iter() {
|
||||||
let is_domain = !from.contains("@");
|
if is_domain(from) && !self.domains.contains(from) {
|
||||||
if is_domain && !self.domains.contains(from) {
|
|
||||||
panic!("Aliased from-domain \"{from}\" does not exist");
|
panic!("Aliased from-domain \"{from}\" does not exist");
|
||||||
}
|
}
|
||||||
for dest in dests.iter() {
|
for dest in dests.iter() {
|
||||||
let is_domain = !dest.contains("@");
|
if is_domain(dest) && !self.domains.contains(dest) {
|
||||||
if is_domain && !self.domains.contains(dest) {
|
|
||||||
panic!("Aliased dest-domain \"{dest}\" does not exist");
|
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");
|
panic!("Aliased dest-account \"{dest}\" does not exist");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue