documentation + add unconditionally_encrypt optional kwarg to encrypt()

This commit is contained in:
revsuine 2024-11-04 17:17:02 +00:00
parent 3f0aa89f08
commit fe0caa446a
Signed by: revsuine
GPG key ID: 3F257B68F5BC9339
2 changed files with 16 additions and 4 deletions

2
.gitignore vendored
View file

@ -1,2 +1,2 @@
.venv/ .venv/
.idea/

View file

@ -46,12 +46,24 @@ def is_message_encrypted(message: email.message.Message) -> bool:
return message.get_content_type() == "multipart/encrypted" return message.get_content_type() == "multipart/encrypted"
def encrypt(message: email.message.Message, recipients: typing.List[str]) -> str: def encrypt(
"""Encrypt given message""" 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, # 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): if is_message_encrypted(message) and not unconditionally_encrypt:
return message.as_string() return message.as_string()
encrypted_content = gnupg.GPG().encrypt(message.as_string(), recipients, armor=True) encrypted_content = gnupg.GPG().encrypt(message.as_string(), recipients, armor=True)