finally get a grip on utf-8
All checks were successful
/ deploy (push) Successful in 4m24s
/ test (push) Successful in 2m26s

This commit is contained in:
j3d1 2025-01-20 18:30:42 +01:00
parent f133ae9e60
commit 4ea74637a3

View file

@ -53,6 +53,12 @@ def unescape_simplified_quoted_printable(s, encoding='utf-8'):
return quopri.decodestring(s).decode(encoding) return quopri.decodestring(s).decode(encoding)
def decode_inline_encodings(s):
s = unescape_and_decode_quoted_printable(s)
s = unescape_and_decode_base64(s)
return s
def ascii_strip(s): def ascii_strip(s):
if not s: if not s:
return None return None
@ -134,8 +140,7 @@ def decode_email_segment(segment, charset, transfer_encoding):
import base64 import base64
segment = base64.b64decode(segment).decode('utf-8') segment = base64.b64decode(segment).decode('utf-8')
else: else:
segment = unescape_and_decode_quoted_printable(segment) segment = decode_inline_encodings(segment.decode('utf-8'))
segment = unescape_and_decode_base64(segment)
return segment return segment
@ -160,7 +165,7 @@ def parse_email_body(raw, log=None):
segment = part.get_payload() segment = part.get_payload()
if not segment: if not segment:
continue continue
segment = decode_email_segment(segment, charset, part.get('Content-Transfer-Encoding')) segment = decode_email_segment(segment.encode('utf-8'), charset, part.get('Content-Transfer-Encoding'))
log.debug(segment) log.debug(segment)
body = body + segment body = body + segment
elif 'attachment' in cdispo or 'inline' in cdispo: elif 'attachment' in cdispo or 'inline' in cdispo:
@ -193,7 +198,8 @@ def parse_email_body(raw, log=None):
else: else:
log.warning("Unknown content type %s", parsed.get_content_type()) log.warning("Unknown content type %s", parsed.get_content_type())
body = "Unknown content type" body = "Unknown content type"
body = decode_email_segment(body, parsed.get_content_charset(), parsed.get('Content-Transfer-Encoding')) body = decode_email_segment(body.encode('utf-8'), parsed.get_content_charset(),
parsed.get('Content-Transfer-Encoding'))
log.debug(body) log.debug(body)
return parsed, body, attachments return parsed, body, attachments
@ -221,8 +227,9 @@ def receive_email(envelope, log=None):
subject = ascii_strip(parsed.get('Subject')) subject = ascii_strip(parsed.get('Subject'))
if not subject: if not subject:
subject = "No subject" subject = "No subject"
subject = unescape_and_decode_quoted_printable(subject) subject = decode_inline_encodings(subject)
subject = unescape_and_decode_base64(subject) recipient = decode_inline_encodings(recipient)
sender = decode_inline_encodings(sender)
target_event = find_target_event(recipient) target_event = find_target_event(recipient)
active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject, target_event) active_issue_thread, new = find_active_issue_thread(header_in_reply_to, recipient, subject, target_event)
@ -256,7 +263,7 @@ do not create a new request.
Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid()) Your c3lf (Cloakroom + Lost&Found) Team'''.format(active_issue_thread.short_uuid())
reply_email = Email.objects.create( reply_email = Email.objects.create(
sender=recipient, recipient=sender, body=body, subject=ascii_strip(subject), sender=recipient, recipient=sender, body=body, subject=subject,
in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread) in_reply_to=header_message_id, event=target_event, issue_thread=active_issue_thread)
reply = make_reply(reply_email, references, event=target_event.slug if target_event else None) reply = make_reply(reply_email, references, event=target_event.slug if target_event else None)
else: else: