commands: Add support for encryption key imports/exports.
This commit is contained in:
parent
1f67491325
commit
b1d1871d11
1 changed files with 44 additions and 3 deletions
|
@ -17,11 +17,13 @@
|
||||||
|
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
from builtins import str
|
from builtins import str
|
||||||
from future.moves.itertools import zip_longest
|
from future.moves.itertools import zip_longest
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
from nio import EncryptionError
|
||||||
|
|
||||||
from . import globals as G
|
from . import globals as G
|
||||||
from .colors import Formatted
|
from .colors import Formatted
|
||||||
|
@ -142,6 +144,14 @@ class WeechatCommandParser(object):
|
||||||
unblacklist_parser.add_argument("user_filter")
|
unblacklist_parser.add_argument("user_filter")
|
||||||
unblacklist_parser.add_argument("device_filter", nargs="?")
|
unblacklist_parser.add_argument("device_filter", nargs="?")
|
||||||
|
|
||||||
|
export_parser = subparsers.add_parser("export")
|
||||||
|
export_parser.add_argument("file")
|
||||||
|
export_parser.add_argument("passphrase")
|
||||||
|
|
||||||
|
import_parser = subparsers.add_parser("import")
|
||||||
|
import_parser.add_argument("file")
|
||||||
|
import_parser.add_argument("passphrase")
|
||||||
|
|
||||||
return WeechatCommandParser._run_parser(parser, args)
|
return WeechatCommandParser._run_parser(parser, args)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -374,13 +384,18 @@ def hook_commands():
|
||||||
("info all|blacklisted|private|unverified|verified <filter>||"
|
("info all|blacklisted|private|unverified|verified <filter>||"
|
||||||
"blacklist <user-id> <device-id> ||"
|
"blacklist <user-id> <device-id> ||"
|
||||||
"unverify <user-id> <device-id> ||"
|
"unverify <user-id> <device-id> ||"
|
||||||
"verify <user-id> <device-id>"),
|
"verify <user-id> <device-id> ||"
|
||||||
|
"export <file-name> <passphrase> ||"
|
||||||
|
"import <file-name> <passphrase>"
|
||||||
|
),
|
||||||
# Description
|
# Description
|
||||||
(" info: show info about known devices and their keys\n"
|
(" info: show info about known devices and their keys\n"
|
||||||
" blacklist: blacklist a device\n"
|
" blacklist: blacklist a device\n"
|
||||||
"unblacklist: unblacklist a device\n"
|
"unblacklist: unblacklist a device\n"
|
||||||
" unverify: unverify a device\n"
|
" unverify: unverify a device\n"
|
||||||
" verify: verify a device\n\n"
|
" verify: verify a device\n"
|
||||||
|
" export: export encryption keys\n"
|
||||||
|
" import: import encryption keys\n\n"
|
||||||
"Examples:"
|
"Examples:"
|
||||||
"\n /olm verify @example:example.com *"
|
"\n /olm verify @example:example.com *"
|
||||||
"\n /olm info all example*"
|
"\n /olm info all example*"
|
||||||
|
@ -390,7 +405,10 @@ def hook_commands():
|
||||||
'blacklist %(olm_user_ids) %(olm_devices) ||'
|
'blacklist %(olm_user_ids) %(olm_devices) ||'
|
||||||
'unblacklist %(olm_user_ids) %(olm_devices) ||'
|
'unblacklist %(olm_user_ids) %(olm_devices) ||'
|
||||||
'unverify %(olm_user_ids) %(olm_devices) ||'
|
'unverify %(olm_user_ids) %(olm_devices) ||'
|
||||||
'verify %(olm_user_ids) %(olm_devices)'),
|
'verify %(olm_user_ids) %(olm_devices) ||'
|
||||||
|
'export %(filename) ||'
|
||||||
|
'import %(filename)'
|
||||||
|
),
|
||||||
# Function name
|
# Function name
|
||||||
'matrix_olm_command_cb',
|
'matrix_olm_command_cb',
|
||||||
'')
|
'')
|
||||||
|
@ -673,6 +691,25 @@ def olm_unblacklist_command(server, args):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def olm_export_command(server, args):
|
||||||
|
file_path = os.path.expanduser(args.file)
|
||||||
|
try:
|
||||||
|
server.client.export_keys(file_path, args.passphrase)
|
||||||
|
except IOError as e:
|
||||||
|
server.error("Error exporting keys: {}".format(str(e)))
|
||||||
|
|
||||||
|
server.info("Succesfully exported keys")
|
||||||
|
|
||||||
|
def olm_import_command(server, args):
|
||||||
|
file_path = os.path.expanduser(args.file)
|
||||||
|
try:
|
||||||
|
server.client.import_keys(file_path, args.passphrase)
|
||||||
|
except (IOError, EncryptionError) as e:
|
||||||
|
server.error("Error importing keys: {}".format(str(e)))
|
||||||
|
|
||||||
|
server.info("Succesfully imported keys")
|
||||||
|
|
||||||
|
|
||||||
@utf8_decode
|
@utf8_decode
|
||||||
def matrix_olm_command_cb(data, buffer, args):
|
def matrix_olm_command_cb(data, buffer, args):
|
||||||
def command(server, data, buffer, args):
|
def command(server, data, buffer, args):
|
||||||
|
@ -687,6 +724,10 @@ def matrix_olm_command_cb(data, buffer, args):
|
||||||
|
|
||||||
if not parsed_args.subcommand or parsed_args.subcommand == "info":
|
if not parsed_args.subcommand or parsed_args.subcommand == "info":
|
||||||
olm_info_command(server, parsed_args)
|
olm_info_command(server, parsed_args)
|
||||||
|
elif parsed_args.subcommand == "export":
|
||||||
|
olm_export_command(server, parsed_args)
|
||||||
|
elif parsed_args.subcommand == "import":
|
||||||
|
olm_import_command(server, parsed_args)
|
||||||
elif parsed_args.subcommand == "verify":
|
elif parsed_args.subcommand == "verify":
|
||||||
olm_verify_command(server, parsed_args)
|
olm_verify_command(server, parsed_args)
|
||||||
elif parsed_args.subcommand == "unverify":
|
elif parsed_args.subcommand == "unverify":
|
||||||
|
|
Loading…
Reference in a new issue