From fe0caa446a131d80cd62440b71f1939e6e12ec77 Mon Sep 17 00:00:00 2001 From: revsuine Date: Mon, 4 Nov 2024 17:17:02 +0000 Subject: [PATCH] documentation + add unconditionally_encrypt optional kwarg to encrypt() --- .gitignore | 2 +- gpgmymail | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index def4cc9..dfcd050 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .venv/ - +.idea/ diff --git a/gpgmymail b/gpgmymail index 0e2233f..1e48c92 100755 --- a/gpgmymail +++ b/gpgmymail @@ -46,12 +46,24 @@ def is_message_encrypted(message: email.message.Message) -> bool: return message.get_content_type() == "multipart/encrypted" -def encrypt(message: email.message.Message, recipients: typing.List[str]) -> str: - """Encrypt given message""" +def encrypt( + message: email.message.Message, + recipients: typing.List[str], + *, + unconditionally_encrypt: bool = False + ) -> str: + """Encrypt given message + + :param message: an email.message.Message object to be encrypted + :param recipients: a List of recipients + :param unconditionally_encrypt: if True, will encrypt no matter what. If + False (default), will NOT encrypt if any of the following conditions are + met: + - The message is already encrypted""" # some mail clients like Thunderbird don't like twice-encrypted emails, # so we return the message as-is if it's already encrypted - if is_message_encrypted(message): + if is_message_encrypted(message) and not unconditionally_encrypt: return message.as_string() encrypted_content = gnupg.GPG().encrypt(message.as_string(), recipients, armor=True)