7or8bit-decode #5

Manually merged
revsuine merged 17 commits from 7or8bit-decode into master 2024-11-14 19:45:52 +00:00
Showing only changes of commit 10f25158bf - Show all commits

View file

@ -36,6 +36,7 @@ import email.mime.message
import typing import typing
# for decode_email: # for decode_email:
import quopri import quopri
import base64
# see: https://gnupg.readthedocs.io/en/latest/ # see: https://gnupg.readthedocs.io/en/latest/
import gnupg import gnupg
@ -60,7 +61,8 @@ def decode_email(message: email.message.Message) -> email.message.Message:
# this is a kinda hacky way to do this by manipulating the message as a # this is a kinda hacky way to do this by manipulating the message as a
# string but i couldn't get it to work any other way # string but i couldn't get it to work any other way
# decoding needed: # decoding needed:
decoded_bytes = message.as_bytes() # as_string() gives us str, encode() gives us bytes
decoded_bytes = message.as_string().encode()
decoded_bytes = quopri.decodestring(decoded_bytes) decoded_bytes = quopri.decodestring(decoded_bytes)
# replace any instances of the Content-Transfer-Encoding header # replace any instances of the Content-Transfer-Encoding header
@ -78,15 +80,20 @@ def decode_email(message: email.message.Message) -> email.message.Message:
# lol # lol
quopri_decoded_message = email.message_from_bytes(decoded_bytes) quopri_decoded_message = email.message_from_bytes(decoded_bytes)
if quopri_decoded_message.is_multipart(): if quopri_decoded_message.is_multipart():
most_recent_boundary = None
for part in quopri_decoded_message.walk(): for part in quopri_decoded_message.walk():
if not part.is_multipart(): # multipart and has boundary (not None)
if part.is_multipart() and part.get_boundary():
most_recent_boundary = part.get_boundary()
else:
if part.get("Content-Transfer-Encoding") == "base64": if part.get("Content-Transfer-Encoding") == "base64":
new_part = part b64_str = part.get_payload()
new_part.replace_header("Content-Transfer-Encoding", "7bit") # remove the boundary as we don't want to change this
new_part.set_payload(part.get_payload(decode=True)) b64_str = b64_str.replace(most_recent_boundary, "")
decoded_b64_str = base64.b64decode(b64_str)
decoded_bytes = decoded_bytes.replace( decoded_bytes = decoded_bytes.replace(
part.as_bytes(), b64_str,
new_part.as_bytes() decoded_b64_str
) )
else: else:
# TODO # TODO