thunderbird: simplify headers decoding

This commit is contained in:
Vincent Bernat 2022-06-12 18:53:01 +02:00
parent 3985ffa085
commit fa4bbc3f92

View file

@ -14,7 +14,7 @@ import argparse
import os
import subprocess
import email.parser
import email.header
import email.policy
from pydbus import SessionBus
from gi.repository import GLib, Gio
@ -24,15 +24,6 @@ parser.add_argument("folder", metavar="FOLDER", help="folder to monitor", nargs=
options = parser.parse_args()
def decode(header):
return " ".join(
(
decoded.decode(charset or "ascii") if type(decoded) is bytes else decoded
for decoded, charset in email.header.decode_header(header)
)
)
def process(path):
new_watermark = None
watermark = watermarks.get(path)
@ -87,7 +78,9 @@ def process(path):
# Parse the message to extract subject, author and Message-ID
message = b"\n".join(message[1:])
parsed = email.parser.BytesParser().parsebytes(message, headersonly=True)
parsed = email.parser.BytesParser(policy=email.policy.default).parsebytes(
message, headersonly=True
)
subject = parsed.get("subject")
author = parsed.get("from")
messageid = parsed.get("message-id")
@ -97,8 +90,8 @@ def process(path):
"Thunderbird",
0,
"thunderbird",
f"Mail from {decode(author)}",
decode(subject),
f"Mail from {author}",
subject,
actions,
{},
10000,