summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2020-03-05 12:17:50 +0100
committerAnton Khirnov <anton@khirnov.net>2020-03-05 13:51:46 +0100
commit54cac0fc755f79a6f5ab848233517e8f6afc52cb (patch)
treefa0ccd77efaa77d4a5f452d182f6229d7cb0a19c
parentd4b8a1ccacda08c29ad93cdb344c6fc1debcac30 (diff)
db/message: remove unnecessary get_params()
-rw-r--r--alot/db/message.py43
-rw-r--r--tests/db/test_utils.py48
2 files changed, 12 insertions, 79 deletions
diff --git a/alot/db/message.py b/alot/db/message.py
index 7263c6fe..3cf89d7a 100644
--- a/alot/db/message.py
+++ b/alot/db/message.py
@@ -4,7 +4,9 @@
import email
import email.charset as charset
import email.policy
+import logging
import mailcap
+import os
import tempfile
from datetime import datetime
@@ -71,22 +73,7 @@ def _add_signature_headers(mail, sigs, error_msg):
'False' if (error_msg or not sig_known) else 'True')
mail.add_header(X_SIGNATURE_MESSAGE_HEADER, msg)
-def _get_params(mail, failobj=None, header='content-type', unquote=True):
- '''Get Content-Type parameters as dict.
-
- RFC 2045 specifies that parameter names are case-insensitive, so
- we normalize them here.
-
- :param mail: :class:`email.message.Message`
- :param failobj: object to return if no such header is found
- :param header: the header to search for parameters, default
- :param unquote: unquote the values
- :returns: a `dict` containing the parameters
- '''
- failobj = failobj or []
- return {k.lower(): v for k, v in mail.get_params(failobj, header, unquote)}
-
-def _handle_signatures(original, message, params):
+def _handle_signatures(original, message):
"""Shared code for handling message signatures.
RFC 3156 is quite strict:
@@ -99,8 +86,6 @@ def _handle_signatures(original, message, params):
:type original: :class:`email.message.Message`
:param message: The multipart/signed payload to verify
:type message: :class:`email.message.Message`
- :param params: the message parameters as returned by :func:`get_params`
- :type params: dict[str, str]
"""
malformed = None
if len(message.get_payload()) != 2:
@@ -115,9 +100,9 @@ def _handle_signatures(original, message, params):
# TODO: RFC 3156 says the alg has to be lower case, but I've seen a message
# with 'PGP-'. maybe we should be more permissive here, or maybe not, this
# is crypto stuff...
- if not params.get('micalg', 'nothing').startswith('pgp-'):
- malformed = 'expected micalg=pgp-..., got: {0}'.format(
- params.get('micalg', 'nothing'))
+ micalg = message.get_param('micalg', '')
+ if not micalg.startswith('pgp-'):
+ malformed = 'expected micalg=pgp-..., got: {0}'.format(micalg)
sigs = []
if not malformed:
@@ -224,16 +209,14 @@ def _decrypted_message_from_bytes(bytestring, session_keys = None):
del enc[X_SIGNATURE_MESSAGE_HEADER]
if enc.is_multipart():
- p = _get_params(enc)
-
# handle OpenPGP signed data
if (enc.get_content_subtype() == 'signed' and
- p.get('protocol') == _APP_PGP_SIG):
- _handle_signatures(enc, enc, p)
+ enc.get_param('protocol') == _APP_PGP_SIG):
+ _handle_signatures(enc, enc)
# handle OpenPGP encrypted data
elif (enc.get_content_subtype() == 'encrypted' and
- p.get('protocol') == _APP_PGP_ENC and
+ enc.get_param('protocol') == _APP_PGP_ENC and
'Version: 1' in enc.get_payload(0).get_payload()):
_handle_encrypted(enc, enc, session_keys)
@@ -243,13 +226,11 @@ def _decrypted_message_from_bytes(bytestring, session_keys = None):
sub = enc.get_payload(0)
if sub.is_multipart():
- p = _get_params(sub)
-
if (sub.get_content_subtype() == 'signed' and
- p.get('protocol') == _APP_PGP_SIG):
- _handle_signatures(enc, sub, p)
+ sub.get_param('protocol') == _APP_PGP_SIG):
+ _handle_signatures(enc, sub)
elif (sub.get_content_subtype() == 'encrypted' and
- p.get('protocol') == _APP_PGP_ENC):
+ sub.get_param('protocol') == _APP_PGP_ENC):
_handle_encrypted(enc, sub, session_keys)
return enc
diff --git a/tests/db/test_utils.py b/tests/db/test_utils.py
index 5622ff4b..ee5f4c25 100644
--- a/tests/db/test_utils.py
+++ b/tests/db/test_utils.py
@@ -28,54 +28,6 @@ from alot.account import Account
from ..utilities import make_key, make_uid, TestCaseClassCleanup
-class TestGetParams(unittest.TestCase):
-
- mailstring = '\n'.join([
- 'From: me',
- 'To: you',
- 'Subject: header field capitalisation',
- 'Content-type: text/plain; charset=utf-8',
- 'X-Header: param=one; and=two; or=three',
- "X-Quoted: param=utf-8''%C3%9Cmlaut; second=plain%C3%9C",
- 'X-UPPERCASE: PARAM1=ONE; PARAM2=TWO'
- '\n',
- 'content'
- ])
- mail = email.message_from_string(mailstring)
-
- def test_returns_content_type_parameters_by_default(self):
- actual = utils.get_params(self.mail)
- expected = {'text/plain': '', 'charset': 'utf-8'}
- self.assertDictEqual(actual, expected)
-
- def test_can_return_params_of_any_header_field(self):
- actual = utils.get_params(self.mail, header='x-header')
- expected = {'param': 'one', 'and': 'two', 'or': 'three'}
- self.assertDictEqual(actual, expected)
-
- @unittest.expectedFailure
- def test_parameters_are_decoded(self):
- actual = utils.get_params(self.mail, header='x-quoted')
- expected = {'param': 'Ümlaut', 'second': 'plain%C3%9C'}
- self.assertDictEqual(actual, expected)
-
- def test_parameters_names_are_converted_to_lowercase(self):
- actual = utils.get_params(self.mail, header='x-uppercase')
- expected = {'param1': 'ONE', 'param2': 'TWO'}
- self.assertDictEqual(actual, expected)
-
- def test_returns_empty_dict_if_header_not_present(self):
- actual = utils.get_params(self.mail, header='x-header-not-present')
- self.assertDictEqual(actual, dict())
-
- def test_returns_failobj_if_header_not_present(self):
- failobj = [('my special failobj for the test', 'needs to be a pair!')]
- actual = utils.get_params(self.mail, header='x-header-not-present',
- failobj=failobj)
- expected = dict(failobj)
- self.assertEqual(actual, expected)
-
-
class TestIsSubdirOf(unittest.TestCase):
def test_both_paths_absolute_matching(self):