Support continuous alias arg replacements (e.g. "index 1 onwards" using "$1-")

This commit is contained in:
jesopo 2019-04-14 21:54:02 +01:00
parent 9e79320d50
commit cf9898d5bd

View file

@ -5,7 +5,7 @@ from . import outs
COMMAND_METHOD = "command-method" COMMAND_METHOD = "command-method"
COMMAND_METHODS = ["PRIVMSG", "NOTICE"] COMMAND_METHODS = ["PRIVMSG", "NOTICE"]
REGEX_ARG_NUMBER = re.compile(r"\$(\d+)") REGEX_ARG_NUMBER = re.compile(r"\$(\d+)(-?)")
def _command_method_validate(s): def _command_method_validate(s):
if s.upper() in COMMAND_METHODS: if s.upper() in COMMAND_METHODS:
@ -57,6 +57,21 @@ class Module(ModuleManager.BaseModule):
def _set_aliases(self, server, aliases): def _set_aliases(self, server, aliases):
server.set_setting("command-aliases", aliases) server.set_setting("command-aliases", aliases)
def _alias_arg_replace(self, s, args_split):
for match in REGEX_ARG_NUMBER.finditer(s):
index = int(match.group(1))
continuous = match.group(2) == "-"
if index >= len(args_split):
raise IndexError("Unknown alias arg index")
if continuous:
replace = " ".join(args_split[index:])
else:
replace = args_split[index]
s = s.replace(match.group(0), replace)
return s.split(" ")
def _command_method(self, target, server): def _command_method(self, target, server):
return target.get_setting(COMMAND_METHOD, return target.get_setting(COMMAND_METHOD,
server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper() server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper()
@ -67,13 +82,11 @@ class Module(ModuleManager.BaseModule):
aliases = self._get_aliases(event["server"]) aliases = self._get_aliases(event["server"])
if command.lower() in aliases: if command.lower() in aliases:
command, _, new_args = aliases[command.lower()].partition(" ") command, _, new_args = aliases[command.lower()].partition(" ")
for match in REGEX_ARG_NUMBER.finditer(new_args):
index = int(match.group(1)) try:
if index >= len(args_split): args_split = self._alias_arg_replace(new_args, args_split)
except IndexError:
return return
new_args = new_args.replace(match.group(0),
args_split[index])
args_split = new_args.split(" ")
if self.has_command(command): if self.has_command(command):
ignore = event["user"].get_setting("ignore", False) ignore = event["user"].get_setting("ignore", False)