7or8bit-decode #5
1 changed files with 28 additions and 15 deletions
43
gpgmymail
43
gpgmymail
|
@ -71,6 +71,29 @@ def decode_email(message: email.message.Message) -> email.message.Message:
|
||||||
b'Content-Transfer-Encoding: 7bit'
|
b'Content-Transfer-Encoding: 7bit'
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def decode_b64_part(
|
||||||
|
part: email.message.Message,
|
||||||
|
decoded_bytes: bytes,
|
||||||
|
most_recent_boundary: str = None
|
||||||
|
) -> bytes:
|
||||||
|
if part.get("Content-Transfer-Encoding") == "base64":
|
||||||
|
b64_str = part.get_payload()
|
||||||
|
# remove the boundary as we don't want to change this
|
||||||
|
b64_str = b64_str.replace(most_recent_boundary, "")
|
||||||
|
# sometimes we have leftover hyphens from a boundary, so strip:
|
||||||
|
# hyphens not in base64 so we know not to use them
|
||||||
|
# strip whitespace first
|
||||||
|
b64_str = b64_str.strip()
|
||||||
|
b64_str = b64_str.strip('-')
|
||||||
|
b64_str = b64_str.encode() # turn into bytes-like object
|
||||||
|
# this will also decode the boundary so there'll be some nonsese
|
||||||
|
# chars at end of email but it's nbd
|
||||||
|
decoded_b64_str = part.get_payload(decode=True)
|
||||||
|
return decoded_bytes.replace(
|
||||||
|
b64_str,
|
||||||
|
decoded_b64_str
|
||||||
|
)
|
||||||
|
|
||||||
# REALLY hacky but i had issues with the more sensible ways to do this.
|
# REALLY hacky but i had issues with the more sensible ways to do this.
|
||||||
# iterates through a Message object to find CTEs of base64
|
# iterates through a Message object to find CTEs of base64
|
||||||
# gets the b64 payload and the decoded payload
|
# gets the b64 payload and the decoded payload
|
||||||
|
@ -85,21 +108,11 @@ def decode_email(message: email.message.Message) -> email.message.Message:
|
||||||
if part.is_multipart() and part.get_boundary():
|
if part.is_multipart() and part.get_boundary():
|
||||||
most_recent_boundary = part.get_boundary()
|
most_recent_boundary = part.get_boundary()
|
||||||
else:
|
else:
|
||||||
if part.get("Content-Transfer-Encoding") == "base64":
|
decoded_bytes = decode_b64_part(
|
||||||
b64_str = part.get_payload()
|
part,
|
||||||
# remove the boundary as we don't want to change this
|
decoded_bytes,
|
||||||
b64_str = b64_str.replace(most_recent_boundary, "")
|
most_recent_boundary
|
||||||
# sometimes we have leftover hyphens from a boundary, so strip:
|
)
|
||||||
# hyphens not in base64 so we know not to use them
|
|
||||||
# strip whitespace first
|
|
||||||
b64_str = b64_str.strip()
|
|
||||||
b64_str = b64_str.strip('-')
|
|
||||||
b64_str = b64_str.encode() # turn into bytes-like object
|
|
||||||
decoded_b64_str = part.get_payload(decode=True)
|
|
||||||
decoded_bytes = decoded_bytes.replace(
|
|
||||||
b64_str,
|
|
||||||
decoded_b64_str
|
|
||||||
)
|
|
||||||
else:
|
else:
|
||||||
# TODO
|
# TODO
|
||||||
pass
|
pass
|
||||||
|
|
Loading…
Reference in a new issue