2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
import enum, re, shlex, string, traceback, typing
|
2019-10-28 10:56:33 +00:00
|
|
|
from src import EventManager, IRCLine, ModuleManager, utils
|
2019-02-09 17:49:01 +00:00
|
|
|
from . import outs
|
2016-03-30 18:31:23 +00:00
|
|
|
|
2018-10-02 20:40:34 +00:00
|
|
|
COMMAND_METHOD = "command-method"
|
|
|
|
COMMAND_METHODS = ["PRIVMSG", "NOTICE"]
|
|
|
|
|
2019-11-22 16:23:30 +00:00
|
|
|
STR_MORE = " (more...)"
|
|
|
|
STR_MORE_LEN = len(STR_MORE.encode("utf8"))
|
|
|
|
STR_CONTINUED = "(...continued)"
|
|
|
|
WORD_BOUNDARIES = [" "]
|
2019-05-14 12:02:45 +00:00
|
|
|
|
2019-05-23 15:05:41 +00:00
|
|
|
NON_ALPHANUMERIC = [char for char in string.printable if not char.isalnum()]
|
2019-05-23 15:03:49 +00:00
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
class OutType(enum.Enum):
|
|
|
|
OUT = 1
|
|
|
|
ERR = 2
|
|
|
|
|
2019-07-11 12:31:35 +00:00
|
|
|
class BadContextException(Exception):
|
|
|
|
def __init__(self, required_context):
|
|
|
|
self.required_context = required_context
|
|
|
|
Exception.__init__(self)
|
|
|
|
|
2019-11-20 11:34:36 +00:00
|
|
|
class CommandEvent(object):
|
|
|
|
def __init__(self, command, args):
|
|
|
|
self.command = command
|
|
|
|
self.args = args
|
|
|
|
|
2019-09-04 13:27:10 +00:00
|
|
|
SETTING_COMMANDMETHOD = utils.OptionsSetting(COMMAND_METHODS, COMMAND_METHOD,
|
|
|
|
"Set the method used to respond to commands")
|
2019-06-28 22:16:05 +00:00
|
|
|
|
|
|
|
@utils.export("channelset", utils.Setting("command-prefix",
|
|
|
|
"Set the command prefix used in this channel", example="!"))
|
|
|
|
@utils.export("serverset", utils.Setting("command-prefix",
|
|
|
|
"Set the command prefix used on this server", example="!"))
|
2019-09-04 13:27:10 +00:00
|
|
|
@utils.export("serverset", SETTING_COMMANDMETHOD)
|
|
|
|
@utils.export("channelset", SETTING_COMMANDMETHOD)
|
|
|
|
@utils.export("botset", SETTING_COMMANDMETHOD)
|
2019-06-28 22:16:05 +00:00
|
|
|
@utils.export("channelset", utils.BoolSetting("hide-prefix",
|
|
|
|
"Disable/enable hiding prefix in command reponses"))
|
|
|
|
@utils.export("channelset", utils.BoolSetting("commands",
|
|
|
|
"Disable/enable responding to commands in-channel"))
|
|
|
|
@utils.export("channelset", utils.BoolSetting("prefixed-commands",
|
|
|
|
"Disable/enable responding to prefixed commands in-channel"))
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-06-26 09:59:03 +00:00
|
|
|
@utils.hook("new.user")
|
|
|
|
@utils.hook("new.channel")
|
2016-03-29 11:56:58 +00:00
|
|
|
def new(self, event):
|
|
|
|
if "user" in event:
|
|
|
|
target = event["user"]
|
|
|
|
else:
|
|
|
|
target = event["channel"]
|
|
|
|
|
|
|
|
def has_command(self, command):
|
2018-08-31 11:55:52 +00:00
|
|
|
return command.lower() in self.events.on("received").on(
|
2016-03-29 11:56:58 +00:00
|
|
|
"command").get_children()
|
2019-01-30 11:23:17 +00:00
|
|
|
def get_hooks(self, command):
|
2018-09-19 12:25:12 +00:00
|
|
|
return self.events.on("received.command").on(command
|
2019-01-30 11:23:17 +00:00
|
|
|
).get_hooks()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2016-03-30 11:49:46 +00:00
|
|
|
def is_highlight(self, server, s):
|
2018-09-18 23:44:04 +00:00
|
|
|
if s and s[-1] in [":", ","]:
|
2019-06-28 22:33:14 +00:00
|
|
|
return server.is_own_nickname(s[:-1])
|
2016-03-30 11:49:46 +00:00
|
|
|
|
2019-11-22 16:23:30 +00:00
|
|
|
def _command_method(self, server, target):
|
2019-01-23 20:35:28 +00:00
|
|
|
return target.get_setting(COMMAND_METHOD,
|
2019-07-04 13:06:40 +00:00
|
|
|
server.get_setting(COMMAND_METHOD,
|
2019-11-22 16:23:30 +00:00
|
|
|
self.bot.get_setting(COMMAND_METHOD, "PRIVMSG"))).upper()
|
2019-01-23 20:35:28 +00:00
|
|
|
|
2019-11-20 11:34:36 +00:00
|
|
|
def _find_command_hook(self, server, target, is_channel, command, args):
|
2018-12-22 01:00:38 +00:00
|
|
|
if not self.has_command(command):
|
2019-11-20 11:34:36 +00:00
|
|
|
command_event = CommandEvent(command, args)
|
|
|
|
self.events.on("get.command").call(command=command_event,
|
|
|
|
server=server, target=target, is_channel=is_channel)
|
2019-04-14 20:54:02 +00:00
|
|
|
|
2019-11-20 11:34:36 +00:00
|
|
|
command = command_event.command
|
|
|
|
args = command_event.args
|
2018-12-22 01:00:38 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
hook = None
|
2019-11-15 13:26:11 +00:00
|
|
|
args_split = []
|
2019-07-11 12:31:35 +00:00
|
|
|
channel_skip = False
|
|
|
|
private_skip = False
|
2016-03-29 15:22:22 +00:00
|
|
|
if self.has_command(command):
|
2019-01-30 11:23:17 +00:00
|
|
|
for potential_hook in self.get_hooks(command):
|
2019-01-30 11:26:54 +00:00
|
|
|
alias_of = self._get_alias_of(potential_hook)
|
2019-01-30 11:20:14 +00:00
|
|
|
if alias_of:
|
|
|
|
if self.has_command(alias_of):
|
2019-01-30 11:26:54 +00:00
|
|
|
potential_hook = self.get_hooks(alias_of)[0]
|
2019-01-30 11:20:14 +00:00
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"'%s' is an alias of unknown command '%s'"
|
|
|
|
% (command.lower(), alias_of.lower()))
|
|
|
|
|
2019-06-26 09:59:52 +00:00
|
|
|
if not is_channel and potential_hook.get_kwarg("channel_only",
|
|
|
|
False):
|
2019-07-11 12:31:35 +00:00
|
|
|
channel_skip = True
|
2019-01-30 11:20:14 +00:00
|
|
|
continue
|
2019-06-26 09:59:52 +00:00
|
|
|
if is_channel and potential_hook.get_kwarg("private_only",
|
|
|
|
False):
|
2019-07-11 12:31:35 +00:00
|
|
|
private_skip = True
|
2019-01-30 11:20:14 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
hook = potential_hook
|
2019-11-15 13:26:11 +00:00
|
|
|
|
2019-11-20 12:03:02 +00:00
|
|
|
if args:
|
|
|
|
argparse = hook.get_kwarg("argparse", "plain")
|
|
|
|
if argparse == "shlex":
|
|
|
|
args_split = shlex.split(args)
|
|
|
|
elif argparse == "plain":
|
|
|
|
args_split = args.split(" ")
|
2019-11-15 13:26:11 +00:00
|
|
|
|
2019-01-30 19:15:11 +00:00
|
|
|
break
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-07-11 12:31:35 +00:00
|
|
|
if not hook and (private_skip or channel_skip):
|
|
|
|
raise BadContextException("channel" if channel_skip else "private")
|
|
|
|
|
2019-09-12 20:53:35 +00:00
|
|
|
return hook, command, args_split
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-06-14 11:01:55 +00:00
|
|
|
def _check(self, context, kwargs, requests=[]):
|
2019-06-14 10:42:12 +00:00
|
|
|
event_hook = self.events.on(context).on("command")
|
|
|
|
|
2019-06-14 11:01:55 +00:00
|
|
|
returns = []
|
|
|
|
if requests:
|
|
|
|
for request, request_args in requests:
|
2019-06-26 10:04:41 +00:00
|
|
|
returns.append(event_hook.on(request).call_for_result_unsafe(
|
2019-06-14 11:01:55 +00:00
|
|
|
**kwargs, request_args=request_args))
|
|
|
|
else:
|
|
|
|
returns = event_hook.call_unsafe(**kwargs)
|
2019-06-14 10:42:12 +00:00
|
|
|
|
|
|
|
hard_fail = False
|
|
|
|
force_success = False
|
|
|
|
error = None
|
|
|
|
for returned in returns:
|
2019-09-26 11:14:55 +00:00
|
|
|
if returned:
|
|
|
|
type, message = returned
|
|
|
|
if type == utils.consts.PERMISSION_HARD_FAIL:
|
|
|
|
error = message
|
|
|
|
hard_fail = True
|
|
|
|
break
|
|
|
|
elif type == utils.consts.PERMISSION_FORCE_SUCCESS:
|
|
|
|
force_success = True
|
|
|
|
break
|
|
|
|
elif type == utils.consts.PERMISSION_ERROR:
|
2019-09-26 11:19:50 +00:00
|
|
|
error = message
|
2019-06-14 10:42:12 +00:00
|
|
|
|
2019-06-20 10:51:04 +00:00
|
|
|
if hard_fail:
|
2019-09-26 11:14:55 +00:00
|
|
|
return False, error
|
2019-06-20 10:51:04 +00:00
|
|
|
elif not force_success and error:
|
|
|
|
return False, error
|
|
|
|
else:
|
|
|
|
return True, None
|
2019-06-14 10:42:12 +00:00
|
|
|
|
2019-06-15 17:42:14 +00:00
|
|
|
|
2019-11-11 13:26:14 +00:00
|
|
|
def _check_assert(self, check_kwargs, user,
|
2019-06-15 17:42:14 +00:00
|
|
|
check: typing.Union[utils.Check, utils.MultiCheck]):
|
2019-06-20 10:51:04 +00:00
|
|
|
checks = check.to_multi() # both Check and MultiCheck has this func
|
2019-06-15 17:42:14 +00:00
|
|
|
is_success, message = self._check("check", check_kwargs,
|
|
|
|
checks.requests())
|
|
|
|
if not is_success:
|
2019-11-11 13:26:14 +00:00
|
|
|
raise utils.EventError("%s: %s" % (user.nickname, message))
|
2019-06-14 10:42:12 +00:00
|
|
|
|
2019-05-21 22:14:56 +00:00
|
|
|
def command(self, server, target, target_str, is_channel, user, command,
|
2019-11-22 16:23:30 +00:00
|
|
|
args_split, line, hook, **kwargs):
|
|
|
|
module_name = (self._get_prefix(hook) or
|
|
|
|
self.bot.modules.from_context(hook.context).title)
|
2019-05-21 11:56:05 +00:00
|
|
|
|
2019-11-22 16:23:30 +00:00
|
|
|
stdout = outs.StdOut(module_name)
|
|
|
|
stderr = outs.StdOut(module_name)
|
2019-01-30 19:35:43 +00:00
|
|
|
|
2019-06-07 14:30:53 +00:00
|
|
|
ret = False
|
2019-07-07 10:29:22 +00:00
|
|
|
has_out = False
|
2019-06-07 14:30:53 +00:00
|
|
|
|
2019-06-26 09:59:52 +00:00
|
|
|
if hook.get_kwarg("remove_empty", True):
|
2019-05-18 17:35:47 +00:00
|
|
|
args_split = list(filter(None, args_split))
|
|
|
|
|
2019-09-26 10:56:14 +00:00
|
|
|
event_kwargs = {"hook": hook, "user": user, "server": server,
|
2019-11-22 16:23:30 +00:00
|
|
|
"target": target, "target_str": target_str,
|
|
|
|
"is_channel": is_channel, "line": line, "args_split": args_split,
|
|
|
|
"command": command, "args": " ".join(args_split), "stdout": stdout,
|
2019-11-25 16:28:03 +00:00
|
|
|
"stderr": stderr, "tags": {}}
|
2019-09-26 10:56:14 +00:00
|
|
|
event_kwargs.update(kwargs)
|
|
|
|
|
2019-11-11 13:26:14 +00:00
|
|
|
check_assert = lambda check: self._check_assert(event_kwargs, user,
|
|
|
|
check)
|
2019-09-26 10:56:14 +00:00
|
|
|
event_kwargs["check_assert"] = check_assert
|
|
|
|
|
2019-11-25 10:43:30 +00:00
|
|
|
eaten = False
|
|
|
|
|
2019-09-26 10:56:14 +00:00
|
|
|
check_success, check_message = self._check("preprocess", event_kwargs)
|
2019-11-22 16:23:30 +00:00
|
|
|
if check_success:
|
|
|
|
new_event = self.events.on(hook.event_name).make_event(**event_kwargs)
|
|
|
|
self.log.trace("calling command '%s': %s", [command, new_event.kwargs])
|
2019-09-26 10:56:14 +00:00
|
|
|
|
2019-11-22 16:23:30 +00:00
|
|
|
try:
|
|
|
|
hook.call(new_event)
|
|
|
|
except utils.EventError as e:
|
|
|
|
stderr.write(str(e))
|
2019-11-25 10:43:30 +00:00
|
|
|
eaten = new_event.eaten
|
2019-11-22 16:23:30 +00:00
|
|
|
else:
|
|
|
|
if check_message:
|
|
|
|
stderr.write("%s: %s" % (user.nickname, check_message))
|
|
|
|
|
|
|
|
self._check("postprocess", event_kwargs)
|
|
|
|
# postprocess - send stdout/stderr and typing tag
|
|
|
|
|
2019-11-25 10:43:30 +00:00
|
|
|
return eaten
|
2019-11-22 16:23:30 +00:00
|
|
|
|
|
|
|
@utils.hook("postprocess.command")
|
|
|
|
@utils.kwarg("priority", EventManager.PRIORITY_LOW)
|
|
|
|
def postprocess(self, event):
|
2019-11-22 17:29:49 +00:00
|
|
|
type = None
|
2019-11-22 16:23:30 +00:00
|
|
|
obj = None
|
|
|
|
if event["stdout"].has_text():
|
2019-11-22 17:29:49 +00:00
|
|
|
type = OutType.OUT
|
2019-11-22 16:23:30 +00:00
|
|
|
obj = event["stdout"]
|
|
|
|
elif event["stderr"].has_text():
|
2019-11-22 17:29:49 +00:00
|
|
|
type = OutType.ERR
|
2019-11-22 16:23:30 +00:00
|
|
|
obj = event["stderr"]
|
|
|
|
else:
|
|
|
|
return
|
2019-11-22 17:29:49 +00:00
|
|
|
self._out(event["server"], event["target"], event["target_str"], obj,
|
2019-11-25 16:28:03 +00:00
|
|
|
type, event["tags"])
|
2019-11-22 17:29:49 +00:00
|
|
|
|
2019-11-25 16:28:03 +00:00
|
|
|
def _out(self, server, target, target_str, obj, type, tags):
|
2019-11-22 17:29:49 +00:00
|
|
|
if type == OutType.OUT:
|
|
|
|
color = utils.consts.GREEN
|
|
|
|
else:
|
|
|
|
color = utils.consts.RED
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
line_str = obj.pop()
|
|
|
|
if obj.prefix:
|
|
|
|
line_str = "[%s] %s" % (
|
|
|
|
utils.irc.color(obj.prefix, color), line_str)
|
|
|
|
method = self._command_method(server, target)
|
2019-11-22 16:23:30 +00:00
|
|
|
|
|
|
|
if not method in ["PRIVMSG", "NOTICE"]:
|
|
|
|
raise ValueError("Unknown command-method '%s'" % method)
|
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
line = IRCLine.ParsedLine(method, [target_str, line_str],
|
2019-11-25 16:28:03 +00:00
|
|
|
tags=tags)
|
2019-11-22 17:29:49 +00:00
|
|
|
valid, trunc = line.truncate(server.hostmask(),
|
2019-11-22 16:23:30 +00:00
|
|
|
margin=STR_MORE_LEN)
|
|
|
|
|
|
|
|
if trunc:
|
|
|
|
if not trunc[0] in WORD_BOUNDARIES:
|
|
|
|
for boundary in WORD_BOUNDARIES:
|
|
|
|
left, *right = valid.rsplit(boundary, 1)
|
|
|
|
if right:
|
|
|
|
valid = left
|
|
|
|
trunc = right[0]+trunc
|
|
|
|
obj.insert("%s %s" % (STR_CONTINUED, trunc))
|
|
|
|
valid = valid+STR_MORE
|
|
|
|
line = IRCLine.parse_line(valid)
|
2019-11-22 17:29:49 +00:00
|
|
|
server.send(line)
|
2019-06-07 15:21:11 +00:00
|
|
|
|
2019-09-26 10:56:14 +00:00
|
|
|
@utils.hook("preprocess.command")
|
|
|
|
def _check_min_args(self, event):
|
|
|
|
min_args = event["hook"].get_kwarg("min_args")
|
|
|
|
if min_args and len(event["args_split"]) < min_args:
|
|
|
|
usage = self._get_usage(event["hook"], event["command"],
|
|
|
|
event["command_prefix"])
|
2019-09-26 11:14:55 +00:00
|
|
|
error = None
|
2019-09-26 10:56:14 +00:00
|
|
|
if usage:
|
2019-09-26 11:14:55 +00:00
|
|
|
error = "Not enough arguments, usage: %s" % usage
|
2019-09-26 10:56:14 +00:00
|
|
|
else:
|
2019-09-26 11:14:55 +00:00
|
|
|
error = "Not enough arguments (minimum: %d)" % min_args
|
|
|
|
return utils.consts.PERMISSION_HARD_FAIL, error
|
2019-09-26 10:56:14 +00:00
|
|
|
|
2019-01-29 08:32:22 +00:00
|
|
|
def _command_prefix(self, server, channel):
|
|
|
|
return channel.get_setting("command-prefix",
|
|
|
|
server.get_setting("command-prefix", "!"))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.message.channel", priority=EventManager.PRIORITY_LOW)
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
2018-11-12 22:55:40 +00:00
|
|
|
commands_enabled = event["channel"].get_setting("commands", True)
|
|
|
|
if not commands_enabled:
|
|
|
|
return
|
|
|
|
|
2019-01-29 08:32:22 +00:00
|
|
|
command_prefix = self._command_prefix(event["server"], event["channel"])
|
2019-05-18 17:35:47 +00:00
|
|
|
command = None
|
2019-11-15 11:33:52 +00:00
|
|
|
args = ""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["message_split"][0].startswith(command_prefix):
|
2019-06-16 17:40:25 +00:00
|
|
|
if not event["channel"].get_setting("prefixed-commands",True):
|
2018-11-12 22:59:21 +00:00
|
|
|
return
|
2016-03-29 11:56:58 +00:00
|
|
|
command = event["message_split"][0].replace(
|
|
|
|
command_prefix, "", 1).lower()
|
2019-11-15 11:33:52 +00:00
|
|
|
if " " in event["message"]:
|
|
|
|
args = event["message"].split(" ", 1)[1]
|
2016-03-30 11:49:46 +00:00
|
|
|
elif len(event["message_split"]) > 1 and self.is_highlight(
|
|
|
|
event["server"], event["message_split"][0]):
|
|
|
|
command = event["message_split"][1].lower()
|
2019-11-15 11:33:52 +00:00
|
|
|
if event["message"].count(" ") > 1:
|
|
|
|
args = event["message"].split(" ", 2)[2]
|
2019-06-16 17:40:25 +00:00
|
|
|
|
2019-11-06 13:05:41 +00:00
|
|
|
hook = None
|
2019-11-15 11:33:52 +00:00
|
|
|
args_split = []
|
2019-11-06 13:05:41 +00:00
|
|
|
if command:
|
2019-07-11 12:31:35 +00:00
|
|
|
try:
|
2019-09-12 20:53:35 +00:00
|
|
|
hook, command, args_split = self._find_command_hook(
|
2019-11-20 11:34:36 +00:00
|
|
|
event["server"], event["channel"], True, command, args)
|
2019-07-11 12:31:35 +00:00
|
|
|
except BadContextException:
|
|
|
|
event["channel"].send_message(
|
|
|
|
"%s: That command is not valid in a channel" %
|
|
|
|
event["user"].nickname)
|
|
|
|
return
|
|
|
|
|
2019-11-06 13:05:41 +00:00
|
|
|
if hook:
|
|
|
|
if event["action"]:
|
|
|
|
return
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if hook:
|
2019-05-21 22:14:56 +00:00
|
|
|
self.command(event["server"], event["channel"],
|
|
|
|
event["target_str"], True, event["user"], command,
|
2019-11-22 16:23:30 +00:00
|
|
|
args_split, event["line"], hook,
|
2019-11-27 12:06:57 +00:00
|
|
|
command_prefix=command_prefix,
|
|
|
|
buffer_line=event["buffer_line"])
|
2019-06-24 21:53:28 +00:00
|
|
|
else:
|
|
|
|
self.events.on("unknown.command").call(server=event["server"],
|
|
|
|
target=event["channel"], user=event["user"],
|
2019-06-24 21:54:34 +00:00
|
|
|
command=command, command_prefix=command_prefix,
|
|
|
|
is_channel=True)
|
2019-05-18 17:35:47 +00:00
|
|
|
else:
|
2019-06-16 17:40:25 +00:00
|
|
|
regex_hooks = self.events.on("command.regex").get_hooks()
|
|
|
|
for hook in regex_hooks:
|
|
|
|
if event["action"] and hook.get_kwarg("ignore_action", True):
|
|
|
|
continue
|
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
pattern = hook.get_kwarg("pattern", None)
|
|
|
|
if pattern:
|
2019-05-19 07:41:20 +00:00
|
|
|
match = re.search(pattern, event["message"])
|
2019-05-18 17:35:47 +00:00
|
|
|
if match:
|
|
|
|
command = hook.get_kwarg("command", "")
|
|
|
|
res = self.command(event["server"], event["channel"],
|
2019-05-21 22:14:56 +00:00
|
|
|
event["target_str"], True, event["user"], command,
|
2019-11-22 16:23:30 +00:00
|
|
|
"", event["line"], hook, match=match,
|
2019-10-03 15:42:11 +00:00
|
|
|
message=event["message"], command_prefix="",
|
2019-11-27 12:06:57 +00:00
|
|
|
action=event["action"],
|
|
|
|
buffer_line=event["buffer_line"])
|
2019-05-18 17:35:47 +00:00
|
|
|
|
|
|
|
if res:
|
|
|
|
break
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.message.private", priority=EventManager.PRIORITY_LOW)
|
2016-03-29 11:56:58 +00:00
|
|
|
def private_message(self, event):
|
2019-05-06 22:26:13 +00:00
|
|
|
if event["message_split"] and not event["action"]:
|
2016-03-29 15:22:22 +00:00
|
|
|
command = event["message_split"][0].lower()
|
2019-05-23 15:10:31 +00:00
|
|
|
|
|
|
|
# this should help catch commands when people try to do prefixed
|
|
|
|
# commands ('!help' rather than 'help') in PM
|
2019-05-23 15:03:49 +00:00
|
|
|
command = command.lstrip("".join(NON_ALPHANUMERIC))
|
2019-05-23 15:10:31 +00:00
|
|
|
|
2019-11-15 11:33:52 +00:00
|
|
|
args = ""
|
|
|
|
if " " in event["message"]:
|
|
|
|
args = event["message"].split(" ", 1)[1]
|
2019-05-20 05:39:58 +00:00
|
|
|
|
2019-07-11 12:31:35 +00:00
|
|
|
try:
|
2019-09-12 20:53:35 +00:00
|
|
|
hook, command, args_split = self._find_command_hook(
|
2019-11-20 11:34:36 +00:00
|
|
|
event["server"], event["user"], False, command, args)
|
2019-07-11 12:31:35 +00:00
|
|
|
except BadContextException:
|
|
|
|
event["user"].send_message(
|
|
|
|
"That command is not valid in a PM")
|
|
|
|
return
|
2019-05-20 05:39:58 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if hook:
|
2019-05-21 22:14:56 +00:00
|
|
|
self.command(event["server"], event["user"],
|
2019-05-22 13:26:22 +00:00
|
|
|
event["user"].nickname, False, event["user"], command,
|
2019-11-27 12:06:57 +00:00
|
|
|
args_split, event["line"], hook, command_prefix="",
|
|
|
|
buffer_line=event["buffer_line"])
|
2019-06-24 21:53:28 +00:00
|
|
|
else:
|
|
|
|
self.events.on("unknown.command").call(server=event["server"],
|
|
|
|
target=event["user"], user=event["user"], command=command,
|
2019-06-24 21:54:34 +00:00
|
|
|
command_prefix="", is_channel=False)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2019-01-29 08:32:22 +00:00
|
|
|
def _get_usage(self, hook, command, command_prefix=""):
|
2019-01-29 08:33:32 +00:00
|
|
|
command = "%s%s" % (command_prefix, command)
|
2019-07-28 11:35:04 +00:00
|
|
|
usages = hook.get_kwargs("usage")
|
2018-12-29 21:35:06 +00:00
|
|
|
|
|
|
|
if usages:
|
|
|
|
return " | ".join(
|
|
|
|
"%s %s" % (command, usage) for usage in usages)
|
2019-09-01 07:34:55 +00:00
|
|
|
return None
|
2018-10-28 11:50:56 +00:00
|
|
|
|
2018-10-04 10:04:24 +00:00
|
|
|
def _get_prefix(self, hook):
|
|
|
|
return hook.get_kwarg("prefix", None)
|
2018-10-10 09:42:41 +00:00
|
|
|
def _get_alias_of(self, hook):
|
|
|
|
return hook.get_kwarg("alias_of", None)
|
2018-09-26 17:26:10 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("send.stdout")
|
2019-11-22 17:29:49 +00:00
|
|
|
def _stdout(self, event):
|
|
|
|
self._send_out(event, OutType.OUT)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("send.stderr")
|
2019-11-22 17:29:49 +00:00
|
|
|
def _stderr(self, event):
|
|
|
|
self._send_out(event, OutType.ERR)
|
2018-11-07 11:27:10 +00:00
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
def _send_out(self, event, type):
|
|
|
|
target = event["target"]
|
|
|
|
stdout = outs.StdOut(event["module_name"])
|
|
|
|
stdout.write(event["message"])
|
2018-11-07 11:27:10 +00:00
|
|
|
if event.get("hide_prefix", False):
|
2019-11-22 17:29:49 +00:00
|
|
|
stdout.prefix = None
|
2018-11-07 11:27:10 +00:00
|
|
|
|
2019-11-22 17:29:49 +00:00
|
|
|
target_str = event.get("target_str", target.name)
|
|
|
|
self._out(event["server"], target, target_str, stdout,
|
2019-11-26 13:42:16 +00:00
|
|
|
type, {})
|
2018-12-22 01:00:38 +00:00
|
|
|
|
2019-06-14 11:09:42 +00:00
|
|
|
@utils.hook("check.command.self")
|
|
|
|
def check_command_self(self, event):
|
|
|
|
if event["server"].irc_lower(event["request_args"][0]
|
|
|
|
) == event["user"].name:
|
2019-09-26 11:14:55 +00:00
|
|
|
return utils.consts.PERMISSION_FORCE_SUCCESS, None
|
2019-06-14 11:09:42 +00:00
|
|
|
else:
|
2019-09-26 11:14:55 +00:00
|
|
|
return (utils.consts.PERMISSION_ERROR,
|
|
|
|
"You do not have permission to do this")
|
2019-07-01 12:51:13 +00:00
|
|
|
|
|
|
|
@utils.hook("check.command.is-channel")
|
2019-07-01 18:20:36 +00:00
|
|
|
def check_command_is_channel(self, event):
|
2019-07-01 12:51:13 +00:00
|
|
|
if event["is_channel"]:
|
2019-09-26 11:14:55 +00:00
|
|
|
return utils.consts.PERMISSION_FORCE_SUCCESS, None
|
2019-07-01 12:51:13 +00:00
|
|
|
else:
|
2019-09-26 11:14:55 +00:00
|
|
|
return (utils.consts.PERMISSION_ERROR,
|
|
|
|
"This command can only be used in-channel")
|