diff --git a/gpgmymail b/gpgmymail index e7c6073..b2f539f 100755 --- a/gpgmymail +++ b/gpgmymail @@ -71,6 +71,29 @@ def decode_email(message: email.message.Message) -> email.message.Message: 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. # iterates through a Message object to find CTEs of base64 # 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(): 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