7or8bit-decode #5

Manually merged
revsuine merged 17 commits from 7or8bit-decode into master 2024-11-14 19:45:52 +00:00
Showing only changes of commit ed57c8f0d4 - Show all commits

View file

@ -49,6 +49,23 @@ def is_message_encrypted(message: email.message.Message) -> bool:
return message.get_content_subtype() == "encrypted" return message.get_content_subtype() == "encrypted"
def decode_email(message: email.message.Message) -> email.message.Message:
"""Turn a quoted-printable or base64 encoded email into a 7or8bit encoded
email
:param message: email.message.Message to be decoded
:return: decoded email.message.Message"""
payload = []
for part in message.walk():
if not part.is_multipart():
payload.append(part.get_payload(decode=True))
message["Content-Transfer-Encoding"] = "7bit"
message.set_payload(payload)
return message
def encrypt( def encrypt(
message: email.message.Message, message: email.message.Message,
recipients: typing.List[str], recipients: typing.List[str],
@ -67,10 +84,14 @@ def encrypt(
:return: The encrypted email as a string""" :return: The encrypted email as a string"""
# exclusion criteria:
# some mail clients like Thunderbird don't like twice-encrypted emails, # some mail clients like Thunderbird don't like twice-encrypted emails,
# so we return the message as-is if it's already encrypted # so we return the message as-is if it's already encrypted
if is_message_encrypted(message) and not unconditionally_encrypt: if is_message_encrypted(message) and not unconditionally_encrypt:
return message.as_string() return message.as_string()
# make necessary changes to message
message = decode_email(message)
gpg = gnupg.GPG() gpg = gnupg.GPG()
gpg.encoding = encoding gpg.encoding = encoding