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 82c5144e58 - Show all commits

View file

@ -65,13 +65,36 @@ def decode_email(message: email.message.Message) -> email.message.Message:
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
# quopri version, we do base64 version down there
decoded_bytes = decoded_bytes.replace( decoded_bytes = decoded_bytes.replace(
b'Content-Transfer-Encoding: quoted-printable', b'Content-Transfer-Encoding: quoted-printable',
b'Content-Transfer-Encoding: 7bit' b'Content-Transfer-Encoding: 7bit'
) )
# TODO: base64 decoding, which is more difficult due to the need to not # REALLY hacky but i had issues with the more sensible ways to do this.
# treat the whole email like it's base64 # iterates through a Message object to find CTEs of base64
# gets the b64 payload and the decoded payload
# then find and replaces in decoded_bytes the b64 payload
# with the decoded payload
# lol
quopri_decoded_message = email.message_from_bytes(decoded_bytes)
if quopri_decoded_message.is_multipart():
for part in quopri_decoded_message.walk():
if not part.is_multipart():
if part.get("Content-Transfer-Encoding") == "base64":
b64_str = part.get_payload()
decoded_b64_str = part.get_payload(decode=True)
decoded_bytes = decoded_bytes.replace(
b64_str,
decoded_b64_str
)
else:
# TODO
decoded_bytes = decoded_bytes.replace(
b'Content-Transfer-Encoding: base64',
b'Content-Transfer-Encoding: 7bit'
)
return email.message_from_bytes(decoded_bytes) return email.message_from_bytes(decoded_bytes)