Skip to content

Commit

Permalink
Fixed address matching
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Sep 22, 2023
1 parent 4f4d446 commit 44f5961
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/runtime/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,16 @@ impl HeaderVariable {
match self.index_part.cmp(&0) {
Ordering::Greater => list
.nth((self.index_part - 1) as usize)
.and_then(|a| addr.eval(a))
.and_then(|a| addr.eval_strict(a))
.map(Variable::from),
Ordering::Less => list
.rev()
.nth((self.index_part.unsigned_abs() - 1) as usize)
.and_then(|a| addr.eval(a))
.and_then(|a| addr.eval_strict(a))
.map(Variable::from),
Ordering::Equal => {
for item in list {
if let Some(part) = addr.eval(item) {
if let Some(part) = addr.eval_strict(item) {
result.push(Variable::from(part));
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/runtime/tests/test_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,22 @@ impl AddressPart {
}
}

pub(crate) fn eval_strict<'x>(&self, addr: &'x Addr<'x>) -> Option<&'x str> {
match (self, addr.address.as_deref()) {
(AddressPart::All, Some(email)) => Some(email),
(AddressPart::LocalPart, Some(email)) if !email.is_empty() => {
parse_address_local_part(email)
}
(AddressPart::Domain, Some(email)) if !email.is_empty() => parse_address_domain(email),
(AddressPart::User, Some(email)) if !email.is_empty() => parse_address_user_part(email),
(AddressPart::Detail, Some(email)) if !email.is_empty() => {
parse_address_detail_part(email)
}
(AddressPart::Name, _) => addr.name.as_deref(),
(_, email) => email,
}
}

pub(crate) fn eval_string<'x>(&self, addr: &'x str) -> Option<&'x str> {
if !addr.is_empty() {
match self {
Expand Down
22 changes: 21 additions & 1 deletion tests/stalwart/extensions.svtest
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,24 @@ test "Variable scopes" {
test_fail "match group failed = '${ADDR}'";
}

}
}

test_set "message" text:
From: Giovanni
To: bill.doe@example.net
Subject: Pranzo d'acqua fa volti sghembi

V kožuščku hudobnega fanta stopiclja mizar

.
;

test "Header addresses" {
if not eval "header.from.addr == '' && header.from.name == 'Giovanni'" {
test_fail "failed header.from.addr == '' && header.from.name == 'Giovanni'";
}

if not eval "header.to.addr == 'bill.doe@example.net' && header.to.name == ''" {
test_fail "header.to.addr == 'bill.doe@example.net' && header.to.name == ''";
}
}

0 comments on commit 44f5961

Please sign in to comment.