From e4f85ce6b33f77d8757464d79eb176948d8b7921 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 5 Feb 2020 16:40:15 +0000 Subject: [PATCH] update !apikey to use command spec --- modules/rest_api.py | 50 +++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/modules/rest_api.py b/modules/rest_api.py index 0f3a1d2e..735e366d 100644 --- a/modules/rest_api.py +++ b/modules/rest_api.py @@ -198,45 +198,39 @@ class Module(ModuleManager.BaseModule): @utils.hook("received.command.apikey") @utils.kwarg("private_only", True) - @utils.kwarg("min_args", 1) - @utils.kwarg("usage", "list") - @utils.kwarg("usage", "add [endpoint [endpoint ...]]") - @utils.kwarg("usage", "remove ") - @utils.kwarg("usage", "info ") + @utils.spec("!'list ?wordlower") + @utils.spec("!'add !wordlower ?words") + @utils.spec("!'remove !wordlower") @utils.kwarg("permission", "apikey") def apikey(self, event): - subcommand = event["args_split"][0].lower() - alias = None - alias_lower = None + subcommand = event["spec"][0] + alias = event["spec"][1] found = None - if len(event["args_split"]) > 1: - alias = event["args_split"][1] - alias_lower = alias.lower() api_keys = {} for key, value in self.bot.find_settings(prefix="api-key-"): api_keys[key] = value - if alias and value["comment"].lower() == alias_lower: - alias = value["comment"] + if alias and value["comment"].lower() == alias: found = key if subcommand == "list": - aliases = [v["comment"] for v in api_keys.values()] - aliases.sort() - event["stdout"].write("API keys: %s" % ", ".join(aliases)) + aliases = {v["comment"]: v for v in api_keys.values()} + if alias: + if not alias in aliases: + event["stderr"].write("API key '%s' not found" % alias) + event["stdout"].write("API key %s ('%s') can access: %s" % + (key, alias, " ".join(aliases[alias]["permissions"]))) + else: + event["stdout"].write("API keys: %s" + % ", ".join(sorted(aliases.keys()))) elif subcommand == "add": - if not len(event["args_split"]) > 1: - raise utils.EventError( - "Please provide an alias for the API key") - if found == None: - comment = event["args_split"][1] new_key = binascii.hexlify(os.urandom(16)).decode("ascii") self.bot.set_setting("api-key-%s" % new_key, { - "comment": comment, "permissions": event["args_split"][2:] + "comment": alias, "permissions": event["spec"][2] or [] }) event["stdout"].write("New API key '%s': %s" % - (comment, new_key)) + (alias, new_key)) else: event["stderr"].write("API key alias '%s' already exists" % alias) @@ -251,16 +245,6 @@ class Module(ModuleManager.BaseModule): (key, alias)) else: event["stderr"].write("Count not find API key '%s'" % alias) - elif subcommand == "info": - if not len(event["args_split"]) > 1: - raise utils.EventError("Please provide a key alias to remove") - - if not found == None: - key = found.replace("api-key-", "", 1) - event["stdout"].write("API key %s ('%s') can access: %s" % - (key, alias, " ".join(api_keys[found]["permissions"]))) - else: - event["stderr"].write("Count not find API key '%s'" % alias) def _url_for(self, route, endpoint, args=[], get_params={}, host_override=None):