add kwarg name replacements to aliases.py, use it for $NICK$

This commit is contained in:
jesopo 2019-12-17 14:30:14 +00:00
parent 00a8129634
commit ddcf54e751

View file

@ -3,13 +3,14 @@ import re
from src import EventManager, ModuleManager, utils from src import EventManager, ModuleManager, utils
REGEX_ARG_NUMBER = re.compile(r"\$(?:(\d+)(-?)|(-))") REGEX_ARG_NUMBER = re.compile(r"\$(?:(\d+)(-?)|(-))")
REGEX_ARG_NAME = re.compile(r"\$([^\$]+)\$")
SETTING_PREFIX = "command-alias-" SETTING_PREFIX = "command-alias-"
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
def _arg_replace(self, s, args_split): def _arg_replace(self, s, args_split, kwargs):
parts = s.split("$$") parts = s.split("$$")
for i, part in enumerate(parts): for i, part in enumerate(parts):
for match in REGEX_ARG_NUMBER.finditer(s): for match in REGEX_ARG_NUMBER.finditer(part):
if match.group(1): if match.group(1):
index = int(match.group(1)) index = int(match.group(1))
continuous = match.group(2) == "-" continuous = match.group(2) == "-"
@ -24,6 +25,10 @@ class Module(ModuleManager.BaseModule):
else: else:
replace = args_split[index] replace = args_split[index]
parts[i] = part.replace(match.group(0), replace) parts[i] = part.replace(match.group(0), replace)
for match in REGEX_ARG_NAME.finditer(part):
key = match.group(1).upper()
if key in kwargs:
parts[i] = part.replace(match.group(0), kwargs[key])
return "$".join(parts) return "$".join(parts)
def _get_alias(self, server, target, command): def _get_alias(self, server, target, command):
@ -56,7 +61,8 @@ class Module(ModuleManager.BaseModule):
alias, alias_args = alias alias, alias_args = alias
event["command"].command = alias event["command"].command = alias
event["command"].args = self._arg_replace(alias_args, event["command"].args = self._arg_replace(alias_args,
event["command"].args.split(" ")) event["command"].args.split(" "),
{"NICK": event["user"].nickname})
@utils.hook("received.command.alias") @utils.hook("received.command.alias")
@utils.hook("received.command.balias") @utils.hook("received.command.balias")