Fix localpart splitting and make code more readable.

master
Alexander Graf 2 years ago
parent e50f6c58c0
commit 5179cf0618
No known key found for this signature in database
GPG Key ID: B8A9DC143E075629

@ -439,10 +439,15 @@ class Email(object):
localpart_stripped = None localpart_stripped = None
stripped_alias = None stripped_alias = None
delim = os.environ.get('RECIPIENT_DELIMITER') if delims := os.environ.get('RECIPIENT_DELIMITER'):
if delim in localpart: try:
localpart_stripped = localpart.rsplit(delim, 1)[0] pos = next(i for i, c in enumerate(localpart) if c in delims)
except StopIteration:
pass
else:
localpart_stripped = localpart[:pos]
# is localpart@domain_name or localpart_stripped@domain_name an user?
user = User.query.get(f'{localpart}@{domain_name}') user = User.query.get(f'{localpart}@{domain_name}')
if not user and localpart_stripped: if not user and localpart_stripped:
user = User.query.get(f'{localpart_stripped}@{domain_name}') user = User.query.get(f'{localpart_stripped}@{domain_name}')
@ -450,18 +455,17 @@ class Email(object):
if user: if user:
email = f'{localpart}@{domain_name}' email = f'{localpart}@{domain_name}'
if user.forward_enabled: if not user.forward_enabled:
return [email]
destination = user.forward_destination destination = user.forward_destination
if user.forward_keep or ignore_forward_keep: if user.forward_keep or ignore_forward_keep:
destination.append(email) destination.append(email)
else:
destination = [email]
return destination return destination
pure_alias = Alias.resolve(localpart, domain_name) # is localpart, domain_name or localpart_stripped@domain_name an alias?
if pure_alias := Alias.resolve(localpart, domain_name):
if pure_alias and not pure_alias.wildcard: if not pure_alias.wildcard:
return pure_alias.destination return pure_alias.destination
if stripped_alias := Alias.resolve(localpart_stripped, domain_name): if stripped_alias := Alias.resolve(localpart_stripped, domain_name):

Loading…
Cancel
Save