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 os
import subprocess import subprocess
import email.parser import email.parser
import email.header import email.policy
from pydbus import SessionBus from pydbus import SessionBus
from gi.repository import GLib, Gio 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() 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): def process(path):
new_watermark = None new_watermark = None
watermark = watermarks.get(path) watermark = watermarks.get(path)
@ -87,7 +78,9 @@ def process(path):
# Parse the message to extract subject, author and Message-ID # Parse the message to extract subject, author and Message-ID
message = b"\n".join(message[1:]) 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") subject = parsed.get("subject")
author = parsed.get("from") author = parsed.get("from")
messageid = parsed.get("message-id") messageid = parsed.get("message-id")
@ -97,8 +90,8 @@ def process(path):
"Thunderbird", "Thunderbird",
0, 0,
"thunderbird", "thunderbird",
f"Mail from {decode(author)}", f"Mail from {author}",
decode(subject), subject,
actions, actions,
{}, {},
10000, 10000,