diff --git a/gpgmymail b/gpgmymail index d7575d9..708d9d2 100755 --- a/gpgmymail +++ b/gpgmymail @@ -65,13 +65,36 @@ def decode_email(message: email.message.Message) -> email.message.Message: decoded_bytes = quopri.decodestring(decoded_bytes) # replace any instances of the Content-Transfer-Encoding header + # quopri version, we do base64 version down there decoded_bytes = decoded_bytes.replace( b'Content-Transfer-Encoding: quoted-printable', b'Content-Transfer-Encoding: 7bit' ) - # TODO: base64 decoding, which is more difficult due to the need to not - # treat the whole email like it's base64 + # 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) + 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)