2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on config
|
|
|
|
#--depends-on permissions
|
|
|
|
|
2019-05-23 15:03:49 +00:00
|
|
|
import re, string
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import EventManager, 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-04-14 20:54:02 +00:00
|
|
|
REGEX_ARG_NUMBER = re.compile(r"\$(\d+)(-?)")
|
2017-10-27 12:15:33 +00:00
|
|
|
|
2019-06-07 14:30:53 +00:00
|
|
|
MESSAGE_TAGS_CAP = utils.irc.Capability("message-tags",
|
|
|
|
"draft/message-tags-0.2")
|
2019-05-15 07:45:59 +00:00
|
|
|
MSGID_TAG = utils.irc.MessageTag("msgid", "draft/msgid")
|
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
|
|
|
|
2018-10-02 20:40:34 +00:00
|
|
|
def _command_method_validate(s):
|
|
|
|
if s.upper() in COMMAND_METHODS:
|
|
|
|
return s.upper()
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "command-prefix",
|
2019-05-23 09:28:14 +00:00
|
|
|
"help": "Set the command prefix used in this channel", "example": "!"})
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("serverset", {"setting": "command-prefix",
|
2019-05-23 09:28:14 +00:00
|
|
|
"help": "Set the command prefix used on this server", "example": "!"})
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("serverset", {"setting": "command-method",
|
2018-10-02 20:40:34 +00:00
|
|
|
"help": "Set the method used to respond to commands",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": _command_method_validate, "example": "NOTICE"})
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.export("channelset", {"setting": "command-method",
|
2018-10-02 20:40:34 +00:00
|
|
|
"help": "Set the method used to respond to commands",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": _command_method_validate, "example": "NOTICE"})
|
2018-11-07 11:27:10 +00:00
|
|
|
@utils.export("channelset", {"setting": "hide-prefix",
|
|
|
|
"help": "Disable/enable hiding prefix in command reponses",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2018-11-12 22:55:40 +00:00
|
|
|
@utils.export("channelset", {"setting": "commands",
|
|
|
|
"help": "Disable/enable responding to commands in-channel",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2018-11-12 23:00:51 +00:00
|
|
|
@utils.export("channelset", {"setting": "prefixed-commands",
|
|
|
|
"help": "Disable/enable responding to prefixed commands in-channel",
|
2019-05-23 09:28:14 +00:00
|
|
|
"validate": utils.bool_or_none, "example": "on"})
|
2018-09-27 11:08:07 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-04-27 13:49:45 +00:00
|
|
|
def on_load(self):
|
|
|
|
self.exports.add("is-ignored", self._is_ignored)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("new.user|channel")
|
2016-03-29 11:56:58 +00:00
|
|
|
def new(self, event):
|
|
|
|
if "user" in event:
|
|
|
|
target = event["user"]
|
|
|
|
else:
|
|
|
|
target = event["channel"]
|
|
|
|
target.last_stdout = None
|
|
|
|
target.last_stderr = None
|
|
|
|
|
|
|
|
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 [":", ","]:
|
2018-09-17 10:31:29 +00:00
|
|
|
s = s[:-1]
|
|
|
|
return server.is_own_nickname(s)
|
2016-03-30 11:49:46 +00:00
|
|
|
|
2018-12-22 01:00:38 +00:00
|
|
|
def _get_aliases(self, server):
|
|
|
|
return server.get_setting("command-aliases", {})
|
|
|
|
def _set_aliases(self, server, aliases):
|
|
|
|
server.set_setting("command-aliases", aliases)
|
|
|
|
|
2019-04-14 20:54:02 +00:00
|
|
|
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(" ")
|
|
|
|
|
2019-01-23 20:35:28 +00:00
|
|
|
def _command_method(self, target, server):
|
|
|
|
return target.get_setting(COMMAND_METHOD,
|
|
|
|
server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper()
|
|
|
|
|
2019-05-06 16:08:56 +00:00
|
|
|
def _is_ignored(self, server, user, command):
|
2019-04-27 13:49:45 +00:00
|
|
|
if user.get_setting("ignore", False):
|
|
|
|
return True
|
|
|
|
elif user.get_setting("ignore-%s" % command, False):
|
|
|
|
return True
|
2019-05-06 16:08:56 +00:00
|
|
|
elif server.get_setting("ignore-%s" % command, False):
|
|
|
|
return True
|
2019-04-27 13:49:45 +00:00
|
|
|
return False
|
|
|
|
|
2019-05-20 05:39:58 +00:00
|
|
|
def _find_command_hook(self, server, command, is_channel, args_split):
|
2018-12-22 01:00:38 +00:00
|
|
|
if not self.has_command(command):
|
2019-05-18 17:35:47 +00:00
|
|
|
aliases = self._get_aliases(server)
|
2018-12-22 01:00:38 +00:00
|
|
|
if command.lower() in aliases:
|
2018-12-22 01:01:25 +00:00
|
|
|
command, _, new_args = aliases[command.lower()].partition(" ")
|
2019-04-14 20:54:02 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
args_split = self._alias_arg_replace(new_args, args_split)
|
|
|
|
except IndexError:
|
2019-05-20 05:39:58 +00:00
|
|
|
return None, None
|
2018-12-22 01:00:38 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
hook = None
|
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-01-30 11:26:54 +00:00
|
|
|
if not is_channel and potential_hook.kwargs.get("channel_only"):
|
2019-01-30 11:20:14 +00:00
|
|
|
continue
|
2019-01-30 11:26:54 +00:00
|
|
|
if is_channel and potential_hook.kwargs.get("private_only"):
|
2019-01-30 11:20:14 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
hook = potential_hook
|
2019-01-30 19:15:11 +00:00
|
|
|
break
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-05-20 05:39:58 +00:00
|
|
|
return hook, args_split
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-05-21 22:14:56 +00:00
|
|
|
def command(self, server, target, target_str, is_channel, user, command,
|
|
|
|
args_split, tags, hook, **kwargs):
|
2019-05-18 17:35:47 +00:00
|
|
|
if self._is_ignored(server, user, command):
|
|
|
|
return False
|
2019-01-30 11:37:29 +00:00
|
|
|
|
2019-06-07 14:30:53 +00:00
|
|
|
message_tags = server.has_capability(MESSAGE_TAGS_CAP)
|
2019-06-07 14:57:15 +00:00
|
|
|
expect_output = hook.kwargs.get("expect_output", True)
|
2019-06-07 14:30:53 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
module_name = self._get_prefix(hook) or ""
|
|
|
|
if not module_name and hasattr(hook.function, "__self__"):
|
|
|
|
module_name = hook.function.__self__._name
|
2019-01-30 19:35:43 +00:00
|
|
|
|
2019-05-21 12:02:40 +00:00
|
|
|
send_tags = {}
|
2019-06-07 14:30:53 +00:00
|
|
|
if message_tags:
|
|
|
|
msgid = MSGID_TAG.get_value(tags)
|
|
|
|
if msgid:
|
|
|
|
send_tags["+draft/reply"] = msgid
|
|
|
|
|
2019-06-07 14:57:15 +00:00
|
|
|
if expect_output:
|
|
|
|
server.send(utils.irc.protocol.tagmsg(target_str,
|
|
|
|
{"+draft/typing": "active"}), immediate=True)
|
2019-05-21 11:56:05 +00:00
|
|
|
|
2019-05-21 22:09:32 +00:00
|
|
|
stdout = outs.StdOut(server, module_name, target, target_str, send_tags)
|
|
|
|
stderr = outs.StdErr(server, module_name, target, target_str, send_tags)
|
2019-05-18 17:35:47 +00:00
|
|
|
command_method = self._command_method(target, server)
|
2019-01-30 19:35:43 +00:00
|
|
|
|
2019-06-07 14:30:53 +00:00
|
|
|
ret = False
|
2019-06-07 15:21:19 +00:00
|
|
|
had_out = False
|
2019-06-07 14:30:53 +00:00
|
|
|
|
2019-05-18 17:35:47 +00:00
|
|
|
if hook.kwargs.get("remove_empty", True):
|
|
|
|
args_split = list(filter(None, args_split))
|
|
|
|
|
|
|
|
min_args = hook.kwargs.get("min_args")
|
|
|
|
if min_args and len(args_split) < min_args:
|
|
|
|
command_prefix = ""
|
|
|
|
if is_channel:
|
|
|
|
command_prefix = self._command_prefix(server, target)
|
|
|
|
usage = self._get_usage(hook, command, command_prefix)
|
|
|
|
if usage:
|
|
|
|
stderr.write("Not enough arguments, usage: %s" %
|
|
|
|
usage).send(command_method)
|
|
|
|
else:
|
|
|
|
stderr.write("Not enough arguments (minimum: %d)" %
|
|
|
|
min_args).send(command_method)
|
|
|
|
else:
|
|
|
|
returns = self.events.on("preprocess.command").call_unsafe(
|
|
|
|
hook=hook, user=user, server=server, target=target,
|
|
|
|
is_channel=is_channel, tags=tags, args_split=args_split,
|
2019-05-23 10:44:52 +00:00
|
|
|
command=command, **kwargs)
|
2019-05-18 17:35:47 +00:00
|
|
|
|
|
|
|
hard_fail = False
|
|
|
|
force_success = False
|
|
|
|
error = None
|
|
|
|
for returned in returns:
|
|
|
|
if returned == utils.consts.PERMISSION_HARD_FAIL:
|
|
|
|
hard_fail = True
|
|
|
|
break
|
|
|
|
elif returned == utils.consts.PERMISSION_FORCE_SUCCESS:
|
|
|
|
force_success = True
|
|
|
|
elif returned:
|
|
|
|
error = returned
|
|
|
|
|
|
|
|
if hard_fail or (not force_success and error):
|
|
|
|
if error:
|
|
|
|
stderr.write(error).send(command_method)
|
|
|
|
return True
|
|
|
|
|
|
|
|
args = " ".join(args_split)
|
|
|
|
|
|
|
|
new_event = self.events.on(hook.event_name).make_event(user=user,
|
|
|
|
server=server, target=target, args=args, tags=tags,
|
|
|
|
args_split=args_split, stdout=stdout, stderr=stderr,
|
|
|
|
is_channel=is_channel, command=command, **kwargs)
|
|
|
|
|
|
|
|
self.log.trace("calling command '%s': %s",
|
|
|
|
[command, new_event.kwargs])
|
|
|
|
try:
|
|
|
|
hook.call(new_event)
|
|
|
|
except utils.EventError as e:
|
|
|
|
stderr.write(str(e))
|
|
|
|
|
|
|
|
if not hook.kwargs.get("skip_out", False):
|
2019-06-07 15:21:19 +00:00
|
|
|
had_out = stdout.has_text() or stderr.has_text()
|
2019-05-18 17:35:47 +00:00
|
|
|
command_method = self._command_method(target, server)
|
|
|
|
stdout.send(command_method)
|
|
|
|
stderr.send(command_method)
|
|
|
|
target.last_stdout = stdout
|
|
|
|
target.last_stderr = stderr
|
2019-06-07 14:30:53 +00:00
|
|
|
ret = new_event.eaten
|
|
|
|
|
2019-06-07 15:21:19 +00:00
|
|
|
if expect_output and message_tags and not had_out:
|
2019-06-07 14:30:53 +00:00
|
|
|
server.send(utils.irc.protocol.tagmsg(target_str,
|
|
|
|
{"+draft/typing": "done"}), immediate=True)
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2019-06-07 15:21:11 +00:00
|
|
|
return ret
|
|
|
|
|
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):
|
2019-05-06 22:26:13 +00:00
|
|
|
if event["action"]:
|
|
|
|
return
|
|
|
|
|
2018-11-12 22:55:40 +00:00
|
|
|
commands_enabled = event["channel"].get_setting("commands", True)
|
|
|
|
if not commands_enabled:
|
|
|
|
return
|
2018-11-12 22:59:21 +00:00
|
|
|
prefixed_commands = event["channel"].get_setting("prefixed-commands", True)
|
2018-11-12 22:55:40 +00:00
|
|
|
|
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
|
|
|
|
args_split = None
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["message_split"][0].startswith(command_prefix):
|
2018-11-12 22:59:21 +00:00
|
|
|
if not prefixed_commands:
|
|
|
|
return
|
2016-03-29 11:56:58 +00:00
|
|
|
command = event["message_split"][0].replace(
|
|
|
|
command_prefix, "", 1).lower()
|
2019-05-18 17:35:47 +00:00
|
|
|
args_split = event["message_split"][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-05-18 17:35:47 +00:00
|
|
|
args_split = event["message_split"][2:]
|
|
|
|
|
|
|
|
if command:
|
2019-05-20 05:39:58 +00:00
|
|
|
hook, args_split = self._find_command_hook(event["server"], command,
|
2019-05-20 05:42:17 +00:00
|
|
|
True, args_split)
|
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-05-23 10:44:52 +00:00
|
|
|
args_split, event["tags"], hook,
|
|
|
|
command_prefix=command_prefix)
|
2019-05-19 16:36:31 +00:00
|
|
|
event["channel"].buffer.skip_next()
|
2019-05-18 17:35:47 +00:00
|
|
|
else:
|
|
|
|
regex_hook = self.events.on("command.regex").get_hooks()
|
|
|
|
for hook in regex_hook:
|
|
|
|
pattern = hook.get_kwarg("pattern", None)
|
|
|
|
if not pattern and hook.get_kwarg("pattern-url", None) == "1":
|
|
|
|
pattern = utils.http.REGEX_URL
|
|
|
|
|
|
|
|
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,
|
|
|
|
"", event["tags"], hook, match=match,
|
2019-05-23 10:44:52 +00:00
|
|
|
message=event["message"], command_prefix="")
|
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-05-20 05:39:58 +00:00
|
|
|
args_split = event["message_split"][1:]
|
|
|
|
|
|
|
|
hook, args_split = self._find_command_hook(event["server"], command,
|
2019-05-20 05:42:17 +00:00
|
|
|
False, args_split)
|
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-05-23 10:44:52 +00:00
|
|
|
args_split, event["tags"], hook, command_prefix="")
|
2019-05-19 16:36:31 +00:00
|
|
|
event["user"].buffer.skip_next()
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-09-26 17:26:10 +00:00
|
|
|
def _get_help(self, hook):
|
2018-09-30 16:29:09 +00:00
|
|
|
return hook.get_kwarg("help", None) or hook.docstring.description
|
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)
|
2018-10-28 11:50:56 +00:00
|
|
|
usage = hook.get_kwarg("usage", None)
|
2018-12-29 21:38:03 +00:00
|
|
|
if usage:
|
|
|
|
usages = [usage]
|
|
|
|
else:
|
2018-10-28 11:50:56 +00:00
|
|
|
usages = hook.docstring.var_items.get("usage", None)
|
2018-12-29 21:35:06 +00:00
|
|
|
|
|
|
|
if usages:
|
|
|
|
return " | ".join(
|
|
|
|
"%s %s" % (command, usage) for usage in usages)
|
2018-10-28 11:50:56 +00:00
|
|
|
return usage
|
|
|
|
|
2019-05-10 15:11:53 +00:00
|
|
|
def _all_command_hooks(self):
|
|
|
|
all_hooks = {}
|
|
|
|
for child_name in self.events.on("received.command").get_children():
|
|
|
|
hooks = self.events.on("received.command").on(child_name
|
|
|
|
).get_hooks()
|
|
|
|
if hooks:
|
|
|
|
all_hooks[child_name.lower()] = hooks[0]
|
|
|
|
return all_hooks
|
|
|
|
|
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("received.command.help")
|
2016-03-29 11:56:58 +00:00
|
|
|
def help(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show help for a given command
|
2019-05-11 16:07:24 +00:00
|
|
|
:usage: [module [command]]
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["args"]:
|
2019-05-10 15:11:53 +00:00
|
|
|
module_name = event["args_split"][0]
|
|
|
|
module = self.bot.modules.from_name(module_name)
|
|
|
|
if module == None:
|
|
|
|
raise utils.EventError("No such module '%s'" % module_name)
|
|
|
|
|
|
|
|
if len(event["args_split"]) == 1:
|
|
|
|
commands = []
|
|
|
|
for command, command_hook in self._all_command_hooks().items():
|
|
|
|
if (command_hook.context == module.context and
|
|
|
|
not self._get_alias_of(command_hook)):
|
|
|
|
commands.append(command)
|
|
|
|
|
|
|
|
event["stdout"].write("Commands for %s module: %s" % (
|
|
|
|
module.name, ", ".join(commands)))
|
2016-04-05 17:41:03 +00:00
|
|
|
else:
|
2019-05-10 15:11:53 +00:00
|
|
|
requested_command = event["args_split"][1].lower()
|
|
|
|
available_commands = self._all_command_hooks()
|
|
|
|
if requested_command in available_commands:
|
|
|
|
command_hook = available_commands[requested_command]
|
|
|
|
help = self._get_help(command_hook)
|
|
|
|
|
|
|
|
if help:
|
|
|
|
event["stdout"].write("%s: %s" % (
|
|
|
|
requested_command, help))
|
|
|
|
else:
|
|
|
|
event["stderr"].write("No help available for %s" %
|
|
|
|
requested_command)
|
2018-10-10 09:42:41 +00:00
|
|
|
|
2019-05-10 15:11:53 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown command '%s'" %
|
|
|
|
requested_command)
|
|
|
|
else:
|
|
|
|
contexts = {}
|
|
|
|
for command, command_hook in self._all_command_hooks().items():
|
|
|
|
if not command_hook.context in contexts:
|
|
|
|
module = self.bot.modules.from_context(command_hook.context)
|
|
|
|
contexts[module.context] = module.name
|
|
|
|
|
|
|
|
modules_available = sorted(contexts.values())
|
|
|
|
event["stdout"].write("Modules: %s" % ", ".join(modules_available))
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.usage", min_args=1)
|
2016-04-05 17:41:03 +00:00
|
|
|
def usage(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show the usage for a given command
|
|
|
|
:usage: <command>
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-09 08:06:17 +00:00
|
|
|
command_prefix = ""
|
2018-09-09 07:59:36 +00:00
|
|
|
if event["is_channel"]:
|
2019-01-29 08:32:22 +00:00
|
|
|
command_prefix = self._command_prefix(event["server"],
|
|
|
|
event["target"])
|
2018-09-09 00:19:00 +00:00
|
|
|
|
2016-04-05 17:41:03 +00:00
|
|
|
command = event["args_split"][0].lower()
|
2018-08-31 11:55:52 +00:00
|
|
|
if command in self.events.on("received").on(
|
2016-04-05 17:41:03 +00:00
|
|
|
"command").get_children():
|
2018-09-19 12:25:12 +00:00
|
|
|
hooks = self.events.on("received.command").on(command).get_hooks()
|
2019-01-29 08:36:47 +00:00
|
|
|
usage = self._get_usage(hooks[0], command, command_prefix)
|
2018-09-30 16:29:09 +00:00
|
|
|
|
|
|
|
if usage:
|
2018-10-28 11:54:43 +00:00
|
|
|
event["stdout"].write("Usage: %s" % usage)
|
2016-04-05 17:41:03 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No usage help available for %s" % command)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown command '%s'" % command)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.more", skip_out=True)
|
2016-03-29 11:56:58 +00:00
|
|
|
def more(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Show more output from the last command
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2016-03-29 15:22:22 +00:00
|
|
|
if event["target"].last_stdout and event["target"].last_stdout.has_text():
|
2019-01-25 07:11:45 +00:00
|
|
|
event["target"].last_stdout.send(
|
|
|
|
self._command_method(event["target"], event["server"]))
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.ignore", min_args=1)
|
2018-09-06 15:05:14 +00:00
|
|
|
def ignore(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Ignore commands from a given user
|
2019-04-27 13:49:45 +00:00
|
|
|
:usage: <nickname> [command]
|
2018-09-30 16:29:09 +00:00
|
|
|
:permission: ignore
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-04-27 13:49:45 +00:00
|
|
|
setting = "ignore"
|
|
|
|
for_str = ""
|
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
command = event["args_split"][1].lower()
|
|
|
|
setting = "ignore-%s" % command
|
|
|
|
for_str = " for '%s'" % command
|
|
|
|
|
2018-09-06 15:05:14 +00:00
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
2019-04-27 13:49:45 +00:00
|
|
|
if user.get_setting(setting, False):
|
|
|
|
event["stderr"].write("I'm already ignoring '%s'%s" %
|
|
|
|
(user.nickname, for_str))
|
2018-09-06 15:05:14 +00:00
|
|
|
else:
|
2019-04-27 13:49:45 +00:00
|
|
|
user.set_setting(setting, True)
|
|
|
|
event["stdout"].write("Now ignoring '%s'%s" %
|
|
|
|
(user.nickname, for_str))
|
2018-09-06 15:05:14 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.command.unignore", min_args=1)
|
2018-09-06 15:05:14 +00:00
|
|
|
def unignore(self, event):
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2018-09-30 16:29:09 +00:00
|
|
|
:help: Unignore commands from a given user
|
2019-04-27 13:49:45 +00:00
|
|
|
:usage: <nickname> [command]
|
2018-09-30 16:29:09 +00:00
|
|
|
:permission: unignore
|
2018-09-26 17:27:17 +00:00
|
|
|
"""
|
2019-04-27 13:49:45 +00:00
|
|
|
setting = "ignore"
|
|
|
|
for_str = ""
|
|
|
|
if len(event["args_split"]) > 1:
|
|
|
|
command = event["args_split"][1].lower()
|
|
|
|
setting = "ignore-%s" % command
|
|
|
|
for_str = " for '%s'" % command
|
|
|
|
|
2018-09-06 15:05:14 +00:00
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
2019-04-27 13:49:45 +00:00
|
|
|
if not user.get_setting(setting, False):
|
|
|
|
event["stderr"].write("I'm not ignoring '%s'%s" %
|
|
|
|
(user.nickname, for_str))
|
2018-09-06 15:05:14 +00:00
|
|
|
else:
|
2019-04-27 13:49:45 +00:00
|
|
|
user.del_setting(setting)
|
|
|
|
event["stdout"].write("Removed ignore for '%s'%s" %
|
|
|
|
(user.nickname, for_str))
|
2018-09-06 15:05:14 +00:00
|
|
|
|
2019-05-06 16:08:56 +00:00
|
|
|
@utils.hook("received.command.serverignore", in_args=1)
|
|
|
|
def server_ignore(self, event):
|
2019-05-06 16:16:48 +00:00
|
|
|
"""
|
|
|
|
:permission: server-ignore
|
|
|
|
"""
|
2019-05-06 16:08:56 +00:00
|
|
|
command = event["args_split"][0].lower()
|
|
|
|
setting = "ignore-%s" % command
|
|
|
|
|
2019-05-06 16:16:48 +00:00
|
|
|
if event["server"].get_setting(setting, False):
|
2019-05-06 16:08:56 +00:00
|
|
|
event["stderr"].write("I'm already ignoring '%s' for %s" %
|
|
|
|
(command, str(event["server"])))
|
|
|
|
else:
|
2019-05-06 16:13:09 +00:00
|
|
|
event["server"].set_setting(setting, True)
|
2019-05-06 16:08:56 +00:00
|
|
|
event["stdout"].write("Now ignoring '%s' for %s" %
|
|
|
|
(command, str(event["server"])))
|
|
|
|
|
2019-05-06 16:17:06 +00:00
|
|
|
@utils.hook("received.command.serverunignore", in_args=1)
|
|
|
|
def server_unignore(self, event):
|
|
|
|
"""
|
|
|
|
:permission: server-unignore
|
|
|
|
"""
|
|
|
|
command = event["args_split"][0].lower()
|
|
|
|
setting = "ignore-%s" % command
|
|
|
|
|
|
|
|
if not event["server"].get_setting(setting, False):
|
|
|
|
event["stderr"].write("I'm not ignoring '%s' for %s" %
|
|
|
|
(command, str(event["server"])))
|
|
|
|
else:
|
|
|
|
event["server"].del_setting(setting)
|
|
|
|
event["stdout"].write("No longer ignoring '%s' for %s" %
|
|
|
|
(command, str(event["server"])))
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("send.stdout")
|
2016-03-29 11:56:58 +00:00
|
|
|
def send_stdout(self, event):
|
2019-05-22 07:24:59 +00:00
|
|
|
target = event["target"]
|
2019-02-09 17:49:01 +00:00
|
|
|
stdout = outs.StdOut(event["server"], event["module_name"],
|
2019-05-22 07:24:59 +00:00
|
|
|
target, event.get("target_str", target.name), {})
|
2018-11-07 11:27:10 +00:00
|
|
|
|
|
|
|
if event.get("hide_prefix", False):
|
|
|
|
stdout.hide_prefix()
|
|
|
|
|
2019-01-23 21:39:05 +00:00
|
|
|
stdout.write(event["message"]).send(
|
|
|
|
self._command_method(event["target"], event["server"]))
|
2016-03-29 11:56:58 +00:00
|
|
|
if stdout.has_text():
|
|
|
|
event["target"].last_stdout = stdout
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("send.stderr")
|
2016-03-29 11:56:58 +00:00
|
|
|
def send_stderr(self, event):
|
2019-05-22 07:24:59 +00:00
|
|
|
target = event["target"]
|
2019-02-09 17:49:01 +00:00
|
|
|
stderr = outs.StdErr(event["server"], event["module_name"],
|
2019-05-22 07:24:59 +00:00
|
|
|
target, event.get("target_str", target.name), {})
|
2018-11-07 11:27:10 +00:00
|
|
|
|
|
|
|
if event.get("hide_prefix", False):
|
|
|
|
stderr.hide_prefix()
|
|
|
|
|
2019-01-23 21:39:05 +00:00
|
|
|
stderr.write(event["message"]).send(
|
|
|
|
self._command_method(event["target"], event["server"]))
|
2016-03-29 11:56:58 +00:00
|
|
|
if stderr.has_text():
|
|
|
|
event["target"].last_stderr = stderr
|
2018-12-22 01:00:38 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.alias", min_args=2)
|
|
|
|
def add_alias(self, event):
|
|
|
|
"""
|
|
|
|
:help: Add a command alias
|
|
|
|
:usage: <alias> <command> <args...>
|
|
|
|
:permission: command-alias
|
|
|
|
"""
|
|
|
|
alias = event["args_split"][0].lower()
|
|
|
|
command = " ".join(event["args_split"][1:])
|
|
|
|
aliases = self._get_aliases(event["server"])
|
|
|
|
aliases[alias] = command
|
|
|
|
self._set_aliases(event["server"], aliases)
|
|
|
|
event["stdout"].write("Added '%s' alias" % alias)
|
2018-12-22 01:05:47 +00:00
|
|
|
|
|
|
|
@utils.hook("received.command.removealias", min_args=1)
|
|
|
|
def remove_alias(self, event):
|
|
|
|
"""
|
|
|
|
:help: Remove a command alias
|
|
|
|
:usage: <alias>
|
|
|
|
:permission: command-alias
|
|
|
|
"""
|
|
|
|
alias = event["args_split"][0].lower()
|
|
|
|
aliases = self._get_aliases(event["server"])
|
|
|
|
|
|
|
|
if not alias in aliases:
|
|
|
|
raise utils.EventError("No '%s' alias" % alias)
|
|
|
|
|
|
|
|
del aliases[alias]
|
2018-12-22 01:06:42 +00:00
|
|
|
self._set_aliases(event["server"], aliases)
|
2018-12-22 01:05:47 +00:00
|
|
|
event["stdout"].write("Removed '%s' alias" % alias)
|