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 633a54e2b1 - Show all commits

View file

@ -71,6 +71,13 @@ def decode_email(message: email.message.Message) -> email.message.Message:
b'Content-Transfer-Encoding: 7bit' b'Content-Transfer-Encoding: 7bit'
) )
# REALLY hacky but i had issues with the more sensible ways to do this.
# 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
def decode_b64_part( def decode_b64_part(
part: email.message.Message, part: email.message.Message,
decoded_bytes: bytes, decoded_bytes: bytes,
@ -79,7 +86,8 @@ def decode_email(message: email.message.Message) -> email.message.Message:
if part.get("Content-Transfer-Encoding") == "base64": if part.get("Content-Transfer-Encoding") == "base64":
b64_str = part.get_payload() b64_str = part.get_payload()
# remove the boundary as we don't want to change this # remove the boundary as we don't want to change this
b64_str = b64_str.replace(most_recent_boundary, "") if most_recent_boundary:
b64_str = b64_str.replace(most_recent_boundary, "")
# sometimes we have leftover hyphens from a boundary, so strip: # sometimes we have leftover hyphens from a boundary, so strip:
# hyphens not in base64 so we know not to use them # hyphens not in base64 so we know not to use them
# strip whitespace first # strip whitespace first
@ -94,12 +102,6 @@ def decode_email(message: email.message.Message) -> email.message.Message:
decoded_b64_str decoded_b64_str
) )
# REALLY hacky but i had issues with the more sensible ways to do this.
# 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) 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 most_recent_boundary = None
@ -114,8 +116,11 @@ def decode_email(message: email.message.Message) -> email.message.Message:
most_recent_boundary most_recent_boundary
) )
else: else:
# TODO decoded_bytes = decode_b64_part(
pass quopri_decoded_message,
decoded_bytes,
None
)
decoded_bytes = decoded_bytes.replace( decoded_bytes = decoded_bytes.replace(
b'Content-Transfer-Encoding: base64', b'Content-Transfer-Encoding: base64',