refactor perform.py

This commit is contained in:
jesopo 2020-03-02 14:54:19 +00:00
parent 9dfbe3723c
commit ea87013249

View file

@ -24,53 +24,39 @@ class Module(ModuleManager.BaseModule):
self._execute(event["server"], commands, NICK=event["server"].nickname, self._execute(event["server"], commands, NICK=event["server"].nickname,
CHAN=event["channel"].name) CHAN=event["channel"].name)
def _perform(self, target, args_split): def _perform(self, target, spec):
subcommand = args_split[0].lower() subcommand = spec[0]
current_perform = target.get_setting("perform", []) current_perform = target.get_setting("perform", [])
if subcommand == "list": if subcommand == "list":
return "Configured commands: %s" % ", ".join(current_perform) return "Configured commands: %s" % ", ".join(current_perform)
message = None message = None
if subcommand == "add": if subcommand == "add":
if not len(args_split) > 1: current_perform.append(spec[1])
raise utils.EventError("Please provide a raw command to add")
current_perform.append(" ".join(args_split[1:]))
message = "Added command" message = "Added command"
elif subcommand == "remove": elif subcommand == "remove":
if not len(args_split) > 1: index = spec[1]
raise utils.EventError("Please provide an index to remove")
if not args_split[1].isdigit():
raise utils.EventError("Please provide a number")
index = int(args_split[1])
if not index < len(current_perform): if not index < len(current_perform):
raise utils.EventError("Index out of bounds") raise utils.EventError("Index out of bounds")
current_perform.pop(index) command = current_perform.pop(index)
message = "Removed command" message = "Removed command %d (%s)" % (index, command)
else:
raise utils.EventError("Unknown subcommand '%s'" % subcommand)
target.set_setting("perform", current_perform) target.set_setting("perform", current_perform)
return message return message
@utils.hook("received.command.perform", min_args=1) @utils.hook("received.command.perform", permission="perform",
@utils.kwarg("min_args", 1) help="Edit on-connect command configuration")
@utils.hook("received.command.cperform", permission="perform",
help="Edit channel on-join command configuration", channel_only=True)
@utils.kwarg("help", "Edit on-connect command configuration") @utils.kwarg("help", "Edit on-connect command configuration")
@utils.kwarg("usage", "list") @utils.spec("!'list")
@utils.kwarg("usage", "add <raw command>") @utils.spec("!'add !<command>string")
@utils.kwarg("usage", "remove <index>") @utils.spec("!'remove !<index>int")
@utils.kwarg("permission", "perform")
def perform(self, event): def perform(self, event):
event["stdout"].write(self._perform(event["server"], if event["command"] == "perform":
event["args_split"])) target = event["server"]
elif event["command"] == "cperform":
target = event["target"]
@utils.hook("received.command.cperform", min_args=1) out = self._perform(target, event["spec"])
@utils.kwarg("min_args", 1) event["stdout"].write("%s: %s" % (event["user"].nickname, out))
@utils.kwarg("channel_only", True)
@utils.kwarg("help", "Edit channel on-join command configuration")
@utils.kwarg("usage", "list")
@utils.kwarg("usage", "add <raw command>")
@utils.kwarg("usage", "remove <index>")
@utils.kwarg("permission", "cperform")
def cperform(self, event):
event["stdout"].write(self._perform(event["target"],
event["args_split"]))