[Remail] [patch v2 04/11] remail: Use email.utils.get_addresses()
Thomas Gleixner
tglx at linutronix.de
Sun Jun 18 22:13:46 CEST 2023
Replace more historical homebrewn parsing
Signed-off-by: Thomas Gleixner <tglx at linutronix.de>
---
remail/mail.py | 33 +--------------------------------
remail/maillist.py | 8 +++-----
2 files changed, 4 insertions(+), 37 deletions(-)
--- 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 @@ re_mail = re.compile('^\w+([\.-]?\w+)*@\
'''
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):
--- 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 @@ import os
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:
More information about the Remail
mailing list