From 88f8e48b13810001ca021a396caae6d965a8676a Mon Sep 17 00:00:00 2001 From: Thomas Gleixner Date: Fri, 16 Jun 2023 23:31:07 +0200 Subject: [PATCH] remail: Use email.utils.get_addresses() Replace more historical homebrewn parsing Signed-off-by: Thomas Gleixner --- remail/mail.py | 33 +-------------------------------- remail/maillist.py | 8 +++----- 2 files changed, 4 insertions(+), 37 deletions(-) diff --git a/remail/mail.py b/remail/mail.py index 7d2dc3f..440eecc 100644 --- a/remail/mail.py +++ b/remail/mail.py @@ -5,7 +5,7 @@ # Mail message related code from email.utils import make_msgid, formatdate, parseaddr -from email.header import Header, decode_header +from email.header import Header from email import message_from_string, message_from_bytes from email.generator import Generator from email.message import Message, EmailMessage @@ -152,37 +152,6 @@ def get_raw_email_addr(addr): ''' return parseaddr(addr)[1] -re_compress_space = re.compile('\s+') - -def decode_hdr(hdr): - ''' - Decode a mail header with encoding - ''' - elm = decode_header(hdr.strip()) - res = '' - for txt, enc in elm: - # Groan .... - if enc: - res += ' ' + txt.decode(enc) - elif isinstance(txt, str): - res += ' ' + txt - else: - res += ' ' + txt.decode('ascii') - return re_compress_space.sub(' ', res).strip() - -def decode_addrs(hdr): - ''' - Decode mail addresses from a header and handle encondings - ''' - addrs = [] - if not hdr: - return addrs - parts = re_compress_space.sub(' ', hdr).split(',') - for p in parts: - addr = decode_hdr(p) - addrs.append(addr) - return addrs - re_noquote = re.compile('[a-zA-Z0-9_\- ]+') def encode_addr(fulladdr): diff --git a/remail/maillist.py b/remail/maillist.py index 048dbef..3f7e4ab 100644 --- a/remail/maillist.py +++ b/remail/maillist.py @@ -6,15 +6,15 @@ from remail.mail import msg_set_header, msg_force_msg_id, send_mail from remail.mail import msg_sanitize_incoming, msg_is_autoreply -from remail.mail import get_raw_email_addr, decode_addrs from remail.mail import msg_from_string, sender_info +from remail.mail import get_raw_email_addr from remail.smime import smime_crypt, RemailSmimeException from remail.gpg import gpg_crypt, RemailGPGException from remail.tracking import account_tracking from remail.config import accounts_config, gpg_config, smime_config -from email.utils import make_msgid, formatdate +from email.utils import make_msgid, formatdate, getaddresses from email.policy import EmailPolicy from flufl.bounce import all_failures @@ -303,10 +303,8 @@ class maillist(object): def get_destination(self, msg): # Handle the case where someone put several addresses on To: - addrs = decode_addrs(msg['To']) - for addr in addrs: - to = get_raw_email_addr(addr) + for name, to in getaddresses(msg.get_all('To', [])): dest = self.config.listaddrs.get_destination(to, self.config.admins, self.config.subscribers) if dest: