Compare commits
2 commits
f1a07cb1e0
...
633a54e2b1
Author | SHA1 | Date | |
---|---|---|---|
633a54e2b1 | |||
218ee51ac2 |
1 changed files with 35 additions and 17 deletions
52
gpgmymail
52
gpgmymail
|
@ -77,6 +77,31 @@ def decode_email(message: email.message.Message) -> email.message.Message:
|
|||
# then find and replaces in decoded_bytes the b64 payload
|
||||
# with the decoded payload
|
||||
# lol
|
||||
|
||||
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
|
||||
if most_recent_boundary:
|
||||
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
|
||||
)
|
||||
|
||||
quopri_decoded_message = email.message_from_bytes(decoded_bytes)
|
||||
if quopri_decoded_message.is_multipart():
|
||||
most_recent_boundary = None
|
||||
|
@ -85,24 +110,17 @@ def decode_email(message: email.message.Message) -> email.message.Message:
|
|||
if part.is_multipart() and part.get_boundary():
|
||||
most_recent_boundary = part.get_boundary()
|
||||
else:
|
||||
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
|
||||
decoded_b64_str = part.get_payload(decode=True)
|
||||
decoded_bytes = decoded_bytes.replace(
|
||||
b64_str,
|
||||
decoded_b64_str
|
||||
)
|
||||
decoded_bytes = decode_b64_part(
|
||||
part,
|
||||
decoded_bytes,
|
||||
most_recent_boundary
|
||||
)
|
||||
else:
|
||||
# TODO
|
||||
pass
|
||||
decoded_bytes = decode_b64_part(
|
||||
quopri_decoded_message,
|
||||
decoded_bytes,
|
||||
None
|
||||
)
|
||||
|
||||
decoded_bytes = decoded_bytes.replace(
|
||||
b'Content-Transfer-Encoding: base64',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue