[PATCH 1/2] Save to/cc headers as-is for tracking

Konstantin Ryabitsev <[email protected]> Mon, 3 May 2021 17:14:57 -0400
Newsgroups org.kernel.lore.signatures
Message-ID <[email protected]>
If we clean the to/cc headers to get rid of all unicode escaping, we run=0D
into a Python bug that is unable to properly parse addresses, e.g.:=0D
=0D
In [5]: from email import utils=0D
=0D
In [6]: utils.getaddresses(['foo <[email protected]>'])=0D
Out[6]: [('foo', '[email protected]')]=0D
=0D
In [7]: utils.getaddresses(['Shuming [=E8=8C=83=E6=9B=B8=E9=8A=98] <shuming=
[email protected]>'])=0D
Out[7]:=0D
[('', 'Shuming'),=0D
 ('', ''),=0D
 ('', '=E8=8C=83=E6=9B=B8=E9=8A=98'),=0D
 ('', ''),=0D
 ('', '[email protected]')]=0D
=0D
If we store the headers as-is from the original message, we are less=0D
likely to run into this bug, as all non-ascii sequences should be=0D
qp-escaped in the original headers:=0D
=0D
=3D?big5?B?U2h1bWluZyBbrVOu0bvKXQ=3D=3D?=3D <[email protected]>=0D
=0D
This doesn't fix the underlying bug in Python, but works around it.=0D
=0D
Reported-by: Mark Brown <[email protected]>=0D
Signed-off-by: Konstantin Ryabitsev <[email protected]>=0D
---=0D
 b4/__init__.py | 11 ++++++++---=0D
 b4/mbox.py     |  4 ++--=0D
 b4/pr.py       |  4 ++--=0D
 3 files changed, 12 insertions(+), 7 deletions(-)=0D
=0D
diff --git a/b4/__init__.py b/b4/__init__.py=0D
index ee07f16..32b5c02 100644=0D
--- a/b4/__init__.py=0D
+++ b/b4/__init__.py=0D
@@ -2375,11 +2375,16 @@ def git_get_toplevel(path=3DNone):=0D
     return topdir=0D
 =0D
 =0D
-def format_addrs(pairs):=0D
+def format_addrs(pairs, clean=3DTrue):=0D
     addrs =3D set()=0D
     for pair in pairs:=0D
-        # Remove any quoted-printable header junk from the name=0D
-        addrs.add(email.utils.formataddr((LoreMessage.clean_header(pair[0]=
), LoreMessage.clean_header(pair[1]))))=0D
+        pair =3D list(pair)=0D
+        if pair[0] =3D=3D pair[1]:=0D
+            pair[0] =3D ''=0D
+        if clean:=0D
+            # Remove any quoted-printable header junk from the name=0D
+            pair[0] =3D LoreMessage.clean_header(pair[0])=0D
+        addrs.add(email.utils.formataddr(pair))  # noqa=0D
     return ', '.join(addrs)=0D
 =0D
 =0D
diff --git a/b4/mbox.py b/b4/mbox.py=0D
index d84d390..d3bde25 100644=0D
--- a/b4/mbox.py=0D
+++ b/b4/mbox.py=0D
@@ -294,8 +294,8 @@ def thanks_record_am(lser, cherrypick=3DNone):=0D
         'subject': lmsg.full_subject,=0D
         'fromname': lmsg.fromname,=0D
         'fromemail': lmsg.fromemail,=0D
-        'to': b4.format_addrs(allto),=0D
-        'cc': b4.format_addrs(allcc),=0D
+        'to': b4.format_addrs(allto, clean=3DFalse),=0D
+        'cc': b4.format_addrs(allcc, clean=3DFalse),=0D
         'references': b4.LoreMessage.clean_header(lmsg.msg['References']),=
=0D
         'sentdate': b4.LoreMessage.clean_header(lmsg.msg['Date']),=0D
         'quote': b4.make_quote(lmsg.body, maxlines=3D5),=0D
diff --git a/b4/pr.py b/b4/pr.py=0D
index 0ff68f8..5e6c7a1 100644=0D
--- a/b4/pr.py=0D
+++ b/b4/pr.py=0D
@@ -225,8 +225,8 @@ def thanks_record_pr(lmsg):=0D
         'subject': lmsg.full_subject,=0D
         'fromname': lmsg.fromname,=0D
         'fromemail': lmsg.fromemail,=0D
-        'to': b4.format_addrs(allto),=0D
-        'cc': b4.format_addrs(allcc),=0D
+        'to': b4.format_addrs(allto, clean=3DFalse),=0D
+        'cc': b4.format_addrs(allcc, clean=3DFalse),=0D
         'references': b4.LoreMessage.clean_header(lmsg.msg['References']),=
=0D
         'remote': lmsg.pr_repo,=0D
         'ref': lmsg.pr_ref,=0D
-- =0D
2.30.2=0D
=0D