add --ignore-errors arg & skip encrypting base64 emails
This commit is contained in:
parent
4670bcdafd
commit
0802cf5b3d
1 changed files with 27 additions and 6 deletions
33
gpgmymail
33
gpgmymail
|
@ -174,7 +174,8 @@ def encrypt(
|
||||||
recipients: typing.List[str],
|
recipients: typing.List[str],
|
||||||
*,
|
*,
|
||||||
unconditionally_encrypt: bool = False,
|
unconditionally_encrypt: bool = False,
|
||||||
encoding: str = DEFAULT_ENCODING
|
encoding: str = DEFAULT_ENCODING,
|
||||||
|
ignore_errors: bool = False
|
||||||
) -> str:
|
) -> str:
|
||||||
"""Encrypt given message
|
"""Encrypt given message
|
||||||
|
|
||||||
|
@ -184,22 +185,39 @@ def encrypt(
|
||||||
False (default), will NOT encrypt if any of the following conditions are
|
False (default), will NOT encrypt if any of the following conditions are
|
||||||
met:
|
met:
|
||||||
- The message is already encrypted
|
- 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"""
|
:return: The encrypted email as a string"""
|
||||||
|
|
||||||
# exclusion criteria:
|
# 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 not unconditionally_encrypt:
|
||||||
return message.as_string()
|
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
|
# make necessary changes to message
|
||||||
# this function is quite clunky and seems to throw exceptions from time to
|
# 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
|
# time; if we can't make the necessary changes we want to just continue
|
||||||
try:
|
try:
|
||||||
message = decode_email(message)
|
message = decode_email(message)
|
||||||
except:
|
except Exception as e:
|
||||||
pass
|
if ignore_errors:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
raise e
|
||||||
|
|
||||||
gpg = gnupg.GPG()
|
gpg = gnupg.GPG()
|
||||||
gpg.encoding = encoding
|
gpg.encoding = encoding
|
||||||
|
@ -257,6 +275,8 @@ def main() -> None:
|
||||||
help="Encoding to use for the gnupg.GPG object")
|
help="Encoding to use for the gnupg.GPG object")
|
||||||
parser.add_argument('--unconditional', action="store_true",
|
parser.add_argument('--unconditional', action="store_true",
|
||||||
help="Encrypt mail unconditionally. By default, mail is not encrypted if it is already encrypted.")
|
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='*',
|
parser.add_argument('recipient', nargs='*',
|
||||||
help="Key ID or email of keys to encrypt for")
|
help="Key ID or email of keys to encrypt for")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
@ -269,7 +289,8 @@ def main() -> None:
|
||||||
msg,
|
msg,
|
||||||
args.recipient,
|
args.recipient,
|
||||||
unconditionally_encrypt=args.unconditional,
|
unconditionally_encrypt=args.unconditional,
|
||||||
encoding=args.encoding
|
encoding=args.encoding,
|
||||||
|
ignore_errors=args.ignore_errors
|
||||||
))
|
))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
Loading…
Reference in a new issue