rename contact_from_header -> from_header

This commit is contained in:
Son NK 2020-12-05 18:15:53 +01:00
parent 8c5f311367
commit d150dfacdb
1 changed files with 9 additions and 13 deletions

View File

@ -157,19 +157,17 @@ def new_app():
return app return app
def get_or_create_contact( def get_or_create_contact(from_header: str, mail_from: str, alias: Alias) -> Contact:
contact_from_header: str, mail_from: str, alias: Alias
) -> Contact:
""" """
contact_from_header is the RFC 2047 format FROM header contact_from_header is the RFC 2047 format FROM header
""" """
contact_name, contact_email = parseaddr_unicode(contact_from_header) contact_name, contact_email = parseaddr_unicode(from_header)
if not is_valid_email(contact_email): if not is_valid_email(contact_email):
# From header is wrongly formatted, try with mail_from # From header is wrongly formatted, try with mail_from
if mail_from and mail_from != "<>": if mail_from and mail_from != "<>":
LOG.warning( LOG.warning(
"Cannot parse email from from_header %s, parse from mail_from %s", "Cannot parse email from from_header %s, parse from mail_from %s",
contact_from_header, from_header,
mail_from, mail_from,
) )
_, contact_email = parseaddr_unicode(mail_from) _, contact_email = parseaddr_unicode(mail_from)
@ -178,7 +176,7 @@ def get_or_create_contact(
LOG.warning( LOG.warning(
"invalid contact email %s. Parse from %s %s", "invalid contact email %s. Parse from %s %s",
contact_email, contact_email,
contact_from_header, from_header,
mail_from, mail_from,
) )
# either reuse a contact with empty email or create a new contact with empty email # either reuse a contact with empty email or create a new contact with empty email
@ -207,14 +205,14 @@ def get_or_create_contact(
contact.mail_from = mail_from contact.mail_from = mail_from
db.session.commit() db.session.commit()
if not contact.from_header and contact_from_header: if not contact.from_header and from_header:
LOG.d( LOG.d(
"Set contact from_header %s: %s to %s", "Set contact from_header %s: %s to %s",
contact, contact,
contact.from_header, contact.from_header,
contact_from_header, from_header,
) )
contact.from_header = contact_from_header contact.from_header = from_header
db.session.commit() db.session.commit()
else: else:
LOG.d( LOG.d(
@ -230,7 +228,7 @@ def get_or_create_contact(
website_email=contact_email, website_email=contact_email,
name=contact_name, name=contact_name,
mail_from=mail_from, mail_from=mail_from,
from_header=contact_from_header, from_header=from_header,
reply_email=generate_reply_email(contact_email) reply_email=generate_reply_email(contact_email)
if is_valid_email(contact_email) if is_valid_email(contact_email)
else NOREPLY, else NOREPLY,
@ -523,9 +521,7 @@ def handle_forward(envelope, msg: Message, rcpt_to: str) -> List[Tuple[bool, str
else: else:
from_header = str(msg["From"]) from_header = str(msg["From"])
contact = get_or_create_contact( contact = get_or_create_contact(from_header, envelope.mail_from, alias)
from_header, envelope.mail_from, alias
)
if not alias.enabled: if not alias.enabled:
LOG.d("%s is disabled, do not forward", alias) LOG.d("%s is disabled, do not forward", alias)