[alias] use subparse method

This commit is contained in:
nemunaire 2016-01-16 14:50:43 +01:00
parent bd2eff83b7
commit d028afd09e

View File

@ -215,22 +215,24 @@ def cmd_alias(msg):
if not len(msg.args): if not len(msg.args):
raise IMException("!alias takes as argument an alias to extend.") raise IMException("!alias takes as argument an alias to extend.")
elif len(msg.args) == 1: alias = context.subparse(msg, msg.args[0])
alias = msg.args[0] if alias is None or not isinstance(alias, Command):
if alias[0] == "!": raise IMException("%s is not a valid alias" % msg.args[0])
alias = alias[1:]
if alias in context.data.getNode("aliases").index:
return Response("!%s correspond to %s" % (alias, context.data.getNode("aliases").index[alias]["origin"]), channel=msg.channel, nick=msg.nick)
else:
return Response("!%s is not an alias" % alias, channel=msg.channel, nick=msg.nick)
else: if alias.cmd in context.data.getNode("aliases").index:
create_alias(msg.args[0], return Response("%s corresponds to %s" % (alias.cmd, context.data.getNode("aliases").index[alias.cmd]["origin"]),
channel=msg.channel, nick=msg.nick)
elif len(msg.args) > 1:
create_alias(alias.cmd,
" ".join(msg.args[1:]), " ".join(msg.args[1:]),
channel=msg.channel, channel=msg.channel,
creator=msg.nick) creator=msg.nick)
return Response("New alias %s successfully registered." % return Response("New alias %s successfully registered." % alias.cmd,
msg.args[0], channel=msg.channel) channel=msg.channel)
else:
raise IMException("%s is not an alias" % msg.args[0])
@hook.command("unalias", @hook.command("unalias",
@ -257,19 +259,15 @@ def cmd_unalias(msg):
@hook.add(["pre","Command"]) @hook.add(["pre","Command"])
def treat_alias(msg): def treat_alias(msg):
if msg.cmd in context.data.getNode("aliases").index: if msg.cmd in context.data.getNode("aliases").index:
txt = context.data.getNode("aliases").index[msg.cmd]["origin"] origin = context.data.getNode("aliases").index[msg.cmd]["origin"]
# TODO: for legacy compatibility rpl_cmd = context.subparse(msg, origin)
if txt[0] == "!": rpl_cmd.args = replace_variables(rpl_cmd.args, msg)
txt = txt[1:] rpl_cmd.args += msg.args
try: rpl_cmd.kwargs.update(msg.kwargs)
args = shlex.split(txt)
except ValueError:
args = txt.split(' ')
nmsg = Command(args[0], args=replace_variables(args[1:], msg) + msg.args, kwargs=msg.kwargs, **msg.export_args())
# Avoid infinite recursion # Avoid infinite recursion
if msg.cmd != nmsg.cmd: if msg.cmd != rpl_cmd.cmd:
# Also return origin message, if it can be treated as well # Also return origin message, if it can be treated as well
return [msg, nmsg] return [msg, rpl_cmd]
return msg return msg