implement opt-in shlex for command args

closes #202
This commit is contained in:
jesopo 2019-11-15 11:33:52 +00:00
parent 718a026beb
commit 35cdc5321f
2 changed files with 27 additions and 10 deletions

View file

@ -1,7 +1,7 @@
#--depends-on config #--depends-on config
#--depends-on permissions #--depends-on permissions
import re, string, traceback, typing import re, shlex, string, traceback, typing
from src import EventManager, IRCLine, ModuleManager, utils from src import EventManager, IRCLine, ModuleManager, utils
from . import outs from . import outs
@ -80,21 +80,21 @@ class Module(ModuleManager.BaseModule):
else: else:
replace = args_split[index] replace = args_split[index]
s = s.replace(match.group(0), replace) s = s.replace(match.group(0), replace)
return s.split(" ") return s
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, server.get_setting(COMMAND_METHOD,
self.bot.get_setting(COMMAND_METHOD, "PRIVMSG"))) self.bot.get_setting(COMMAND_METHOD, "PRIVMSG")))
def _find_command_hook(self, server, command, is_channel, args_split): def _find_command_hook(self, server, command, is_channel, args):
if not self.has_command(command): if not self.has_command(command):
aliases = self._get_aliases(server) aliases = self._get_aliases(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(" ")
try: try:
args_split = self._alias_arg_replace(new_args, args_split) args = self._alias_arg_replace(new_args, shlex.split(args))
except IndexError: except IndexError:
return None, None, None return None, None, None
@ -127,6 +127,12 @@ class Module(ModuleManager.BaseModule):
if not hook and (private_skip or channel_skip): if not hook and (private_skip or channel_skip):
raise BadContextException("channel" if channel_skip else "private") raise BadContextException("channel" if channel_skip else "private")
argparse = hook.get_kwarg("argparse", "plain")
if argparse == "shlex":
args_split = shlex.split(args)
elif argparse == "plain":
args_split = args.split(" ")
return hook, command, args_split return hook, command, args_split
def _check(self, context, kwargs, requests=[]): def _check(self, context, kwargs, requests=[]):
@ -273,24 +279,26 @@ class Module(ModuleManager.BaseModule):
command_prefix = self._command_prefix(event["server"], event["channel"]) command_prefix = self._command_prefix(event["server"], event["channel"])
command = None command = None
args_split = None args = ""
if event["message_split"][0].startswith(command_prefix): if event["message_split"][0].startswith(command_prefix):
if not event["channel"].get_setting("prefixed-commands",True): if not event["channel"].get_setting("prefixed-commands",True):
return return
command = event["message_split"][0].replace( command = event["message_split"][0].replace(
command_prefix, "", 1).lower() command_prefix, "", 1).lower()
args_split = event["message_split"][1:] if " " in event["message"]:
args = event["message"].split(" ", 1)[1]
elif len(event["message_split"]) > 1 and self.is_highlight( elif len(event["message_split"]) > 1 and self.is_highlight(
event["server"], event["message_split"][0]): event["server"], event["message_split"][0]):
command = event["message_split"][1].lower() command = event["message_split"][1].lower()
args_split = event["message_split"][2:] if event["message"].count(" ") > 1:
args = event["message"].split(" ", 2)[2]
hook = None hook = None
args_split = []
if command: if command:
try: try:
hook, command, args_split = self._find_command_hook( hook, command, args_split = self._find_command_hook(
event["server"], command, True, args_split) event["server"], command, True, args)
except BadContextException: except BadContextException:
event["channel"].send_message( event["channel"].send_message(
"%s: That command is not valid in a channel" % "%s: That command is not valid in a channel" %
@ -343,7 +351,9 @@ class Module(ModuleManager.BaseModule):
# commands ('!help' rather than 'help') in PM # commands ('!help' rather than 'help') in PM
command = command.lstrip("".join(NON_ALPHANUMERIC)) command = command.lstrip("".join(NON_ALPHANUMERIC))
args_split = event["message_split"][1:] args = ""
if " " in event["message"]:
args = event["message"].split(" ", 1)[1]
try: try:
hook, command, args_split = self._find_command_hook( hook, command, args_split = self._find_command_hook(

View file

@ -5,12 +5,18 @@ from src import ModuleManager, utils
class Module(ModuleManager.BaseModule): class Module(ModuleManager.BaseModule):
@utils.hook("received.command.echo") @utils.hook("received.command.echo")
@utils.kwarg("min_args", 1) @utils.kwarg("min_args", 1)
@utils.kwarg("argparse", "shlex")
@utils.kwarg("remove_empty", False)
@utils.kwarg("help", "Echo a string back")
def echo(self, event): def echo(self, event):
event["stdout"].write(event["args"]) event["stdout"].write(event["args"])
@utils.hook("received.command.action") @utils.hook("received.command.action")
@utils.kwarg("min_args", 1) @utils.kwarg("min_args", 1)
@utils.kwarg("expect_output", False) @utils.kwarg("expect_output", False)
@utils.kwarg("remove_empty", False)
@utils.kwarg("argparse", "shlex")
@utils.kwarg("help", "Make the bot send a /me")
def action(self, event): def action(self, event):
event["target"].send_message("\x01ACTION %s\x01" % event["args"]) event["target"].send_message("\x01ACTION %s\x01" % event["args"])
@ -18,6 +24,7 @@ class Module(ModuleManager.BaseModule):
@utils.kwarg("min_args", 2) @utils.kwarg("min_args", 2)
@utils.kwarg("permission", "say") @utils.kwarg("permission", "say")
@utils.kwarg("remove_empty", False) @utils.kwarg("remove_empty", False)
@utils.kwarg("argparse", "shlex")
@utils.kwarg("help", "Send a message to a target") @utils.kwarg("help", "Send a message to a target")
def msg(self, event): def msg(self, event):
event["server"].send_message(event["args_split"][0], event["server"].send_message(event["args_split"][0],