From bc066f0d446583ea0e681cf25254b9e32ca29220 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Thu, 6 Nov 2014 22:59:47 -0800 Subject: [PATCH] ssoma-mda: Decode RFC-2047-encoded Subject: headers For example, get [1]: 'If you can read this you understand the example.' from a message with: Subject: =?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= =?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?= [1]: http://tools.ietf.org/html/rfc2047.html#section-8 --- ssoma-mda | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/ssoma-mda b/ssoma-mda index 39bed3e..abae180 100755 --- a/ssoma-mda +++ b/ssoma-mda @@ -44,6 +44,7 @@ from __future__ import unicode_literals import argparse as _argparse import email as _email +import email.header as _email_header import email.message as _email_message import email.policy as _email_policy import email.utils as _email_utils @@ -218,6 +219,16 @@ def message_id_path(message_id): return _os_path.join(hash[:2], hash[2:]) +def get_commit_message(message): + """Unwrap and decode the message subject for use as a commit message.""" + commit_message = message.get('Subject', '') + commit_message = _FOLDING_WHITESPACE_REGEX.sub(' ', commit_message) + commit_message = ''.join( + str(decoded, charset) for decoded, charset + in _email_header.decode_header(commit_message)) + return commit_message + + def get_author(message): """Create a pygit2.Signature for the message author.""" author_name, author_email = _email_utils.parseaddr( @@ -259,9 +270,7 @@ def deliver(message=None, message_bytes=None, **kwargs): message_id = message.get('Message-ID', '') path = message_id_path(message_id=message_id) _LOG.info('deliver {} to {}'.format(message_id, path)) - commit_message = message.get('Subject', '') - if '\n' in commit_message: - commit_message = _FOLDING_WHITESPACE_REGEX.sub(' ', commit_message) + commit_message = get_commit_message(message=message) author = get_author(message=message) repository = _pygit2.Repository(_os_path.curdir) append( -- 2.26.2