temp remove base64 decoding and unconditionally quopri decode

This commit is contained in:
revsuine 2024-11-14 18:26:22 +00:00
parent 2955e990da
commit a21ee759c8
Signed by: revsuine
GPG key ID: 3F257B68F5BC9339

View file

@ -36,14 +36,12 @@ import email.mime.message
import typing
# for decode_email:
import quopri
import base64
# see: https://gnupg.readthedocs.io/en/latest/
import gnupg
# constants
DEFAULT_ENCODING='utf-8' # default is latin-1 which fails w some unicode chars
CTES_TO_BE_DECODED = ("quoted-printable", "base64")
def is_message_encrypted(message: email.message.Message) -> bool:
"""Determines whether or not an email message is encrypted.
@ -61,39 +59,19 @@ def decode_email(message: email.message.Message) -> email.message.Message:
:return: decoded email.message.Message"""
# 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
msg_ctes = message.get_all("Content-Transfer-Encoding")
# this list will be populated with any encoding that needs to be decoded,
# e.g. base64
# empty if no decoding needed
# set used to avoid dupes
decodes_needed = set()
# check if any of the parts of the message need decoding
for cte in CTES_TO_BE_DECODED:
if cte in msg_ctes:
decodes_needed.add(cte)
# no decoding needed, go ahead with message
if not decodes_needed:
return message
# decoding needed:
# as_string() gives us str, encode() gives us bytes
decoded_bytes = message.as_string().encode()
if "quoted-printable" in decodes_needed:
decoded_bytes = quopri.decodestring(decoded_bytes)
if "base64" in decodes_needed:
decoded_bytes = base64.b64decode(decoded_bytes)
decoded_bytes = quopri.decodestring(decoded_bytes)
# replace any instances of the Content-Transfer-Encoding header
decoded_bytes = decoded_bytes.replace(
b'Content-Transfer-Encoding: quoted-printable',
b'Content-Transfer-Encoding: 7bit'
)
decoded_bytes = decoded_bytes.replace(
b'Content-Transfer-Encoding: base64',
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
return email.message_from_bytes(decoded_bytes)