Commit ae288cb2 authored by Raphaël Hertzog's avatar Raphaël Hertzog

mail: properly unfold headers before creating replies

Folded lines get rejected by Python's email API and throw undesired
exceptions.
parent 2536399f
......@@ -19,6 +19,7 @@ from distro_tracker.core.utils import distro_tracker_render_to_string
from distro_tracker.core.utils import extract_email_address_from_header
from distro_tracker.core.utils import get_decoded_message_payload
from distro_tracker.core.utils.email_messages import decode_header
from distro_tracker.core.utils.email_messages import unfold_header
from distro_tracker.mail.control.commands import CommandFactory
from distro_tracker.mail.control.commands import CommandProcessor
......@@ -48,24 +49,24 @@ def send_response(original_message, message_text, recipient_email, cc=None):
response.
:param cc: A list of emails which should receive a CC of the response.
"""
subject = decode_header(original_message.get('Subject'))
subject = unfold_header(decode_header(original_message.get('Subject')))
if not subject:
subject = 'Your mail'
references = original_message.get('References', '')
references = re.sub(r"\r?\n\s", " ", references, 0, re.MULTILINE)
message_id = unfold_header(original_message.get('Message-ID', ''))
references = unfold_header(original_message.get('References', ''))
if references:
references += ' '
references += original_message.get('Message-ID', '')
references += message_id
message = EmailMessage(
subject='Re: ' + subject,
to=[original_message['From']],
to=[unfold_header(original_message['From'])],
cc=cc,
from_email=DISTRO_TRACKER_BOUNCES_EMAIL,
headers={
'From': DISTRO_TRACKER_CONTACT_EMAIL,
'X-Loop': DISTRO_TRACKER_CONTROL_EMAIL,
'References': references,
'In-Reply-To': original_message.get('Message-ID', ''),
'In-Reply-To': message_id,
},
body=message_text,
)
......
......@@ -350,13 +350,15 @@ class ControlBotBasic(EmailControlTest):
references = " " + self.message['References'] + references
self.assert_header_equal('References', references)
def test_response_when_references_has_two_lines(self):
def test_response_when_headers_have_two_lines(self):
"""
Tests that we don't fail when the References field contain
multiple lines.
Tests that we don't fail when header fields (References, From, Subject)
contain multiple lines.
"""
for sep in ["\n\t", "\n ", "\r\n "]:
self.set_header('References', "<foo@bar>" + sep + "<foo2@bar2>")
self.set_header('From', "This is long" + sep + "<foo@bar.baz>")
self.set_header('Subject', "This is a long" + sep + " subject")
self.set_input_lines(['help'])
self.control_process()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment