summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-11-24 10:53:49 +0100
committerAnton Khirnov <anton@khirnov.net>2021-11-24 10:56:38 +0100
commit2f9907f24755b59ceb9ae075e26b1e4f606adeec (patch)
treecdfbed35bb9a6688134cb4680dfee724081ac04d
parentaa2f8fe5215521437325b25ef89837657b081b7a (diff)
mail: add a custom email policy
Uses structured parsing for more headers.
-rw-r--r--alot/db/message.py8
-rw-r--r--alot/mail/policy.py19
2 files changed, 23 insertions, 4 deletions
diff --git a/alot/db/message.py b/alot/db/message.py
index 19a0f043..97ab991f 100644
--- a/alot/db/message.py
+++ b/alot/db/message.py
@@ -5,7 +5,6 @@
import codecs
import email
import email.charset as charset
-import email.policy
from functools import cached_property
import logging
import os
@@ -16,6 +15,7 @@ from datetime import datetime
from urwid.util import detected_encoding
from ..mail.attachment import Attachment
+from ..mail.policy import p as policy
from .. import crypto
from .. import helper
from ..errors import GPGProblem
@@ -384,7 +384,7 @@ class _MimeTree:
d = None
if d:
- child = email.message_from_bytes(d, policy = email.policy.SMTP)
+ child = email.message_from_bytes(d, policy = policy)
self.children = [_MimeTree(child, session_keys)]
if sigs:
@@ -503,11 +503,11 @@ class Message:
def _email(self):
try:
with open(self.filename, 'rb') as f:
- mail = email.message_from_bytes(f.read(), policy = email.policy.SMTP)
+ mail = email.message_from_bytes(f.read(), policy = policy)
except IOError:
warning = b"Subject: Caution!\n"\
b"Message file is no longer accessible:\n%s" % self.filename
- mail = email.message_from_bytes(warning, policy = email.policy.SMTP)
+ mail = email.message_from_bytes(warning, policy = policy)
return mail
diff --git a/alot/mail/policy.py b/alot/mail/policy.py
new file mode 100644
index 00000000..1b9f07c3
--- /dev/null
+++ b/alot/mail/policy.py
@@ -0,0 +1,19 @@
+# This file is released under the GNU GPL, version 3 or a later revision.
+# For further details see the COPYING file
+
+import email.policy as _p
+import email.headerregistry as _hdr
+
+from . import headers as HDR
+
+_headers = {
+ HDR.X_BEEN_THERE : _hdr.UniqueAddressHeader,
+ HDR.X_MAILING_LIST : _hdr.UniqueAddressHeader,
+}
+
+# here we define our custom policy that handles additional headers
+# derive from SMTP
+p = _p.EmailPolicy() + _p.SMTP
+
+for h, t in _headers.items():
+ p.header_factory.map_to_type(h, t)