add --ignore-errors arg & skip encrypting base64 emails

This commit is contained in:
revsuine 2024-11-15 17:02:42 +00:00
parent 4670bcdafd
commit 0802cf5b3d
Signed by: revsuine
GPG key ID: 3F257B68F5BC9339

View file

@ -174,7 +174,8 @@ def encrypt(
recipients: typing.List[str],
*,
unconditionally_encrypt: bool = False,
encoding: str = DEFAULT_ENCODING
encoding: str = DEFAULT_ENCODING,
ignore_errors: bool = False
) -> str:
"""Encrypt given message
@ -184,22 +185,39 @@ def encrypt(
False (default), will NOT encrypt if any of the following conditions are
met:
- The message is already encrypted
- The message is encoded with base64
:param encoding: string for encoding to use for the gnupg.GPG object
:param ignore_errors: bool, puts some parts of the function in
try:
do_stuff()
except:
pass
blocks. find ignore_errors to see instances where this occurs
:return: The encrypted email as a string"""
# exclusion criteria:
# 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) and not unconditionally_encrypt:
return message.as_string()
if not unconditionally_encrypt:
if is_message_encrypted(message):
return message.as_string()
# bc i just have a bunch of issues w b64
if "Content-Transfer-Encoding: base64" in message.as_string():
return message.as_string()
# make necessary changes to message
# this function is quite clunky and seems to throw exceptions from time to
# time; if we can't make the necessary changes we want to just continue
try:
message = decode_email(message)
except:
pass
except Exception as e:
if ignore_errors:
pass
else:
raise e
gpg = gnupg.GPG()
gpg.encoding = encoding
@ -257,6 +275,8 @@ def main() -> None:
help="Encoding to use for the gnupg.GPG object")
parser.add_argument('--unconditional', action="store_true",
help="Encrypt mail unconditionally. By default, mail is not encrypted if it is already encrypted.")
parser.add_argument('--ignore-errors', action="store_true",
help="Ignore errors at certain error-prone points of the script.")
parser.add_argument('recipient', nargs='*',
help="Key ID or email of keys to encrypt for")
args = parser.parse_args()
@ -269,7 +289,8 @@ def main() -> None:
msg,
args.recipient,
unconditionally_encrypt=args.unconditional,
encoding=args.encoding
encoding=args.encoding,
ignore_errors=args.ignore_errors
))
if __name__ == '__main__':