diff --git a/gpgmymail b/gpgmymail index 3bf1705..c75b4f2 100755 --- a/gpgmymail +++ b/gpgmymail @@ -57,11 +57,27 @@ def decode_email(message: email.message.Message) -> email.message.Message: :param message: email.message.Message to be decoded :return: decoded email.message.Message""" + def decoded_bytes_to_return_value(decoded_bytes: bytes) -> email.message.Message: + """ + if at any point you want to return, return a call of this function and + pass decoded_bytes + + :param decoded_bytes: an email as an ASCII byte array; this should be + stored in decoded_bytes + :return: the expected return value of decode_email + """ + # if i do message_from_bytes it bizarrely changes it back to base64? + # utf-8 has encoding issues so do latin1 + return email.message_from_string(decoded_bytes.decode("latin1")) + # this is a kinda hacky way to do this by manipulating the message as a # string but i couldn't get it to work any other way - # decoding needed: - # as_string() gives us str, encode() gives us bytes decoded_bytes = message.as_bytes() + # if email doesn't need decoding + has_quopri = b'Content-Transfer-Encoding: quoted-printable' in decoded_bytes + has_base64 = b'Content-Transfer-Encoding: base64' in decoded_bytes + if not (has_quopri or has_base64): + return message decoded_bytes = quopri.decodestring(decoded_bytes) # replace any instances of the Content-Transfer-Encoding header @@ -71,6 +87,10 @@ def decode_email(message: email.message.Message) -> email.message.Message: b'Content-Transfer-Encoding: 7bit' ) + # now exit if there's no base64 as i think that's the most fucky + if not has_base64: + return decoded_bytes_to_return_value(decoded_bytes) + # 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 @@ -142,9 +162,7 @@ def decode_email(message: email.message.Message) -> email.message.Message: b'Content-Transfer-Encoding: 7bit' ) - # if i do message_from_bytes it bizarrely changes it back to base64? - # utf-8 has encoding issues so do latin1 - return email.message_from_string(decoded_bytes.decode("latin1")) + return decoded_bytes_to_return_value(decoded_bytes) def encrypt( message: email.message.Message,