From a2eaa913be8bc381b185fc6ff2bbddca9ab314c2 Mon Sep 17 00:00:00 2001 From: revsuine Date: Tue, 12 Nov 2024 14:06:19 +0000 Subject: [PATCH] allow user to control encoding --- README.md | 10 ++++++++-- gpgmymail | 23 +++++++++++++---------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e7d870f..a8dfe15 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,14 @@ with the up-to-date versions of Python and python-gnupg * Python 3 * [python-gnupg](https://gnupg.readthedocs.io/en/latest/) +# Notes on Scope + +Since this script is written to be used as a Sieve filter, no exclusion +behaviour which is much better achieved with Sieve will be implemented, e.g. +"do not encrypt emails from kate@gmail.com". + # Credits * Julian Klode for the [original code](https://github.com/julian-klode/ansible.jak-linux.org/blob/dovecot/roles/mailserver/files/usr/local/lib/dovecot-sieve-filters/gpgmymail) -* revsuine for some modifications: - * Not encrypting already encrypted mail +* revsuine for modifications to gpgmymail + diff --git a/gpgmymail b/gpgmymail index 721cc6a..3b40fad 100755 --- a/gpgmymail +++ b/gpgmymail @@ -39,7 +39,7 @@ import typing import gnupg # constants -ENCODING='utf-8' # default is latin-1 which fails w some unicode chars +DEFAULT_ENCODING='utf-8' # default is latin-1 which fails w some unicode chars def is_message_encrypted(message: email.message.Message) -> bool: """Determines whether or not an email message is encrypted. @@ -53,7 +53,8 @@ def encrypt( message: email.message.Message, recipients: typing.List[str], *, - unconditionally_encrypt: bool = False + unconditionally_encrypt: bool = False, + encoding: str = DEFAULT_ENCODING ) -> str: """Encrypt given message @@ -72,7 +73,7 @@ def encrypt( return message.as_string() gpg = gnupg.GPG() - gpg.encoding = ENCODING + gpg.encoding = encoding encrypted_content = gpg.encrypt(message.as_string(), recipients, armor=True) if not encrypted_content: raise ValueError(encrypted_content.status) @@ -108,11 +109,10 @@ def encrypt( return encmsg.as_string() -def decrypt(message: email.message.Message) -> str: - """Decrypt the given message - Likely won't work on this server as I don't store private keys""" +def decrypt(message: email.message.Message, *, encoding: str = DEFAULT_ENCODING) -> str: + """Decrypt the given message""" gpg = gnupg.GPG() - gpg.encoding = ENCODING + gpg.encoding = encoding return str(gpg.decrypt(message.as_string())) def main() -> None: @@ -123,15 +123,18 @@ def main() -> None: ) parser.add_argument('-d', '--decrypt', action="store_true", help="Decrypt rather than encrypt") + parser.add_argument('--encoding', action="store", default=DEFAULT_ENCODING, + required=False, + help="Encoding to use for the gnupg.GPG object") 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")A args = parser.parse_args() msg = email.message_from_file(sys.stdin) if args.decrypt: - sys.stdout.write(decrypt(msg)) + sys.stdout.write(decrypt(msg), encoding=args.encoding) else: - sys.stdout.write(encrypt(msg, args.recipient)) + sys.stdout.write(encrypt(msg, args.recipient, encoding=args.encoding)) if __name__ == '__main__': main()