diff options
author | Richard van der Hoff <richard@matrix.org> | 2016-05-17 18:55:39 +0100 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2016-05-24 13:39:34 +0100 |
commit | e545ad7eaf55ac8b7dc7d37c046c541e35cef542 (patch) | |
tree | 2d80aa3d3a420a21478d54d8b51ed69d79eb7115 /python/olm/__main__.py | |
parent | c058554132a0f97e8e8ae3a402605220f8fdaed4 (diff) |
Outbound group session support in the python wrappers
Diffstat (limited to 'python/olm/__main__.py')
-rwxr-xr-x | python/olm/__main__.py | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/python/olm/__main__.py b/python/olm/__main__.py index d2b0d38..8bb2419 100755 --- a/python/olm/__main__.py +++ b/python/olm/__main__.py @@ -8,7 +8,7 @@ import yaml from . import * -if __name__ == '__main__': +def build_arg_parser(): parser = argparse.ArgumentParser() parser.add_argument("--key", help="Account encryption key", default="") commands = parser.add_subparsers() @@ -206,5 +206,41 @@ if __name__ == '__main__': decrypt.set_defaults(func=do_decrypt) + outbound_group = commands.add_parser("outbound_group", help="Create an outbound group session") + outbound_group.add_argument("session_file", help="Local group session file") + outbound_group.set_defaults(func=do_outbound_group) + + group_encrypt = commands.add_parser("group_encrypt", help="Encrypt a group message") + group_encrypt.add_argument("session_file", help="Local group session file") + group_encrypt.add_argument("plaintext_file", help="Plaintext", + type=argparse.FileType('rb'), default=sys.stdin) + group_encrypt.add_argument("message_file", help="Message", + type=argparse.FileType('wb'), default=sys.stdout) + group_encrypt.set_defaults(func=do_group_encrypt) + + return parser + +def do_outbound_group(args): + if os.path.exists(args.session_file): + sys.stderr.write("Session %r file already exists" % ( + args.session_file, + )) + sys.exit(1) + session = OutboundGroupSession() + with open(args.session_file, "wb") as f: + f.write(session.pickle(args.key)) + +def do_group_encrypt(args): + session = OutboundGroupSession() + with open(args.session_file, "rb") as f: + session.unpickle(args.key, f.read()) + plaintext = args.plaintext_file.read() + message = session.encrypt(plaintext) + with open(args.session_file, "wb") as f: + f.write(session.pickle(args.key)) + args.message_file.write(message) + +if __name__ == '__main__': + parser = build_arg_parser() args = parser.parse_args() args.func(args) |