update !apikey to use command spec

This commit is contained in:
jesopo 2020-02-05 16:40:15 +00:00
parent 460cda87bb
commit e4f85ce6b3

View file

@ -198,45 +198,39 @@ class Module(ModuleManager.BaseModule):
@utils.hook("received.command.apikey") @utils.hook("received.command.apikey")
@utils.kwarg("private_only", True) @utils.kwarg("private_only", True)
@utils.kwarg("min_args", 1) @utils.spec("!'list ?<alias>wordlower")
@utils.kwarg("usage", "list") @utils.spec("!'add !<alias>wordlower ?<endpoints>words")
@utils.kwarg("usage", "add <alias> [endpoint [endpoint ...]]") @utils.spec("!'remove !<alias>wordlower")
@utils.kwarg("usage", "remove <alias>")
@utils.kwarg("usage", "info <alias>")
@utils.kwarg("permission", "apikey") @utils.kwarg("permission", "apikey")
def apikey(self, event): def apikey(self, event):
subcommand = event["args_split"][0].lower() subcommand = event["spec"][0]
alias = None alias = event["spec"][1]
alias_lower = None
found = None found = None
if len(event["args_split"]) > 1:
alias = event["args_split"][1]
alias_lower = alias.lower()
api_keys = {} api_keys = {}
for key, value in self.bot.find_settings(prefix="api-key-"): for key, value in self.bot.find_settings(prefix="api-key-"):
api_keys[key] = value api_keys[key] = value
if alias and value["comment"].lower() == alias_lower: if alias and value["comment"].lower() == alias:
alias = value["comment"]
found = key found = key
if subcommand == "list": if subcommand == "list":
aliases = [v["comment"] for v in api_keys.values()] aliases = {v["comment"]: v for v in api_keys.values()}
aliases.sort() if alias:
event["stdout"].write("API keys: %s" % ", ".join(aliases)) 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": elif subcommand == "add":
if not len(event["args_split"]) > 1:
raise utils.EventError(
"Please provide an alias for the API key")
if found == None: if found == None:
comment = event["args_split"][1]
new_key = binascii.hexlify(os.urandom(16)).decode("ascii") new_key = binascii.hexlify(os.urandom(16)).decode("ascii")
self.bot.set_setting("api-key-%s" % new_key, { 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" % event["stdout"].write("New API key '%s': %s" %
(comment, new_key)) (alias, new_key))
else: else:
event["stderr"].write("API key alias '%s' already exists" % event["stderr"].write("API key alias '%s' already exists" %
alias) alias)
@ -251,16 +245,6 @@ class Module(ModuleManager.BaseModule):
(key, alias)) (key, alias))
else: else:
event["stderr"].write("Count not find API key '%s'" % alias) 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={}, def _url_for(self, route, endpoint, args=[], get_params={},
host_override=None): host_override=None):