2017-10-27 12:15:33 +00:00
|
|
|
import re
|
2018-09-24 14:13:27 +00:00
|
|
|
from src import EventManager, Utils
|
2016-03-30 18:31:23 +00:00
|
|
|
|
2016-12-05 23:32:31 +00:00
|
|
|
STR_MORE = "%s (more...)" % Utils.FONT_RESET
|
2016-03-29 11:56:58 +00:00
|
|
|
STR_CONTINUED = "(...continued) "
|
|
|
|
|
|
|
|
OUT_CUTOFF = 400
|
|
|
|
|
2017-10-27 12:15:33 +00:00
|
|
|
REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF)
|
|
|
|
|
2016-03-29 15:22:22 +00:00
|
|
|
class Out(object):
|
|
|
|
def __init__(self, module_name, target):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.module_name = module_name
|
2016-03-29 15:22:22 +00:00
|
|
|
self.target = target
|
2016-03-29 11:56:58 +00:00
|
|
|
self._text = ""
|
|
|
|
self.written = False
|
|
|
|
def write(self, text):
|
|
|
|
self._text += text
|
|
|
|
self.written = True
|
|
|
|
return self
|
|
|
|
def send(self):
|
|
|
|
if self.has_text():
|
2018-07-14 08:50:12 +00:00
|
|
|
text = self._text
|
2016-03-29 11:56:58 +00:00
|
|
|
text_encoded = text.encode("utf8")
|
|
|
|
if len(text_encoded) > OUT_CUTOFF:
|
|
|
|
text = "%s%s" % (text_encoded[:OUT_CUTOFF].decode("utf8"
|
|
|
|
).rstrip(), STR_MORE)
|
|
|
|
self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF:
|
|
|
|
].decode("utf8").lstrip())
|
2018-08-05 19:43:52 +00:00
|
|
|
else:
|
|
|
|
self._text = ""
|
2018-09-22 18:10:52 +00:00
|
|
|
self.target.send_message(text, prefix=Utils.FONT_RESET + "[%s] " %
|
|
|
|
self.prefix())
|
2016-03-29 11:56:58 +00:00
|
|
|
def set_prefix(self, prefix):
|
|
|
|
self.module_name = prefix
|
|
|
|
def has_text(self):
|
|
|
|
return bool(self._text)
|
|
|
|
|
2016-03-29 15:22:22 +00:00
|
|
|
class StdOut(Out):
|
2016-03-29 11:56:58 +00:00
|
|
|
def prefix(self):
|
2018-09-21 10:38:55 +00:00
|
|
|
return Utils.color(Utils.bold(self.module_name), Utils.COLOR_GREEN)
|
2016-03-29 15:22:22 +00:00
|
|
|
class StdErr(Out):
|
2016-03-29 11:56:58 +00:00
|
|
|
def prefix(self):
|
2018-09-21 10:38:55 +00:00
|
|
|
return Utils.color(Utils.bold(self.module_name), Utils.COLOR_RED)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
class Module(object):
|
2018-09-02 18:54:45 +00:00
|
|
|
def __init__(self, bot, events, exports):
|
2016-03-29 11:56:58 +00:00
|
|
|
self.bot = bot
|
2018-08-31 11:55:52 +00:00
|
|
|
self.events = events
|
2018-09-09 16:09:04 +00:00
|
|
|
events.on("received.message.channel").hook(self.channel_message,
|
|
|
|
priority=EventManager.PRIORITY_LOW)
|
|
|
|
events.on("received.message.private").hook(self.private_message,
|
|
|
|
priority=EventManager.PRIORITY_LOW)
|
2018-09-06 15:05:14 +00:00
|
|
|
|
|
|
|
events.on("received.command.help").hook(self.help,
|
2016-04-06 11:02:44 +00:00
|
|
|
help="Show help for commands", usage="<command>")
|
2018-09-06 15:05:14 +00:00
|
|
|
events.on("received.command.usage").hook(self.usage, min_args=1,
|
|
|
|
help="Show usage help for commands", usage="<command>")
|
|
|
|
events.on("received.command.more").hook(self.more, skip_out=True,
|
|
|
|
help="Get more output from the last command")
|
|
|
|
events.on("received.command.ignore").hook(self.ignore, min_args=1,
|
|
|
|
help="Ignore commands from a given user", usage="<nickname>",
|
|
|
|
permission="ignore")
|
|
|
|
events.on("received.command.unignore").hook(self.unignore, min_args=1,
|
|
|
|
help="Unignore commands from a given user", usage="<nickname>",
|
|
|
|
permission="unignore")
|
2016-03-29 11:56:58 +00:00
|
|
|
|
2018-08-31 11:55:52 +00:00
|
|
|
events.on("new").on("user", "channel").hook(self.new)
|
2018-09-19 12:25:12 +00:00
|
|
|
events.on("send.stdout").hook(self.send_stdout)
|
|
|
|
events.on("send.stderr").hook(self.send_stderr)
|
2017-12-26 10:32:36 +00:00
|
|
|
|
2018-09-09 08:34:55 +00:00
|
|
|
exports.add("channelset", {"setting": "command-prefix",
|
|
|
|
"help": "Set the command prefix used in this channel"})
|
2018-09-09 09:17:22 +00:00
|
|
|
exports.add("serverset", {"setting": "command-prefix",
|
|
|
|
"help": "Set the command prefix used on this server"})
|
2018-09-09 08:34:55 +00:00
|
|
|
exports.add("serverset", {"setting": "identity-mechanism",
|
|
|
|
"help": "Set the identity mechanism for this server"})
|
|
|
|
|
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()
|
|
|
|
def get_hook(self, command):
|
2018-09-19 12:25:12 +00:00
|
|
|
return self.events.on("received.command").on(command
|
2016-03-29 11:56:58 +00:00
|
|
|
).get_hooks()[0]
|
|
|
|
|
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
|
|
|
|
|
|
|
def message(self, event, command, args_index=1):
|
2016-03-29 15:22:22 +00:00
|
|
|
if self.has_command(command):
|
2018-09-06 15:05:14 +00:00
|
|
|
ignore = event["user"].get_setting("ignore", False)
|
|
|
|
if ignore:
|
|
|
|
return
|
|
|
|
|
2016-03-29 15:22:22 +00:00
|
|
|
hook = self.get_hook(command)
|
|
|
|
is_channel = False
|
|
|
|
|
|
|
|
if "channel" in event:
|
|
|
|
target = event["channel"]
|
|
|
|
is_channel = True
|
|
|
|
else:
|
|
|
|
target = event["user"]
|
|
|
|
if not is_channel and hook.kwargs.get("channel_only"):
|
|
|
|
return
|
2016-04-04 22:20:55 +00:00
|
|
|
if is_channel and hook.kwargs.get("private_only"):
|
2016-04-04 17:41:07 +00:00
|
|
|
return
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2018-08-28 11:23:57 +00:00
|
|
|
buffer = target.buffer
|
2016-03-29 15:22:22 +00:00
|
|
|
|
|
|
|
module_name = hook.function.__self__._name
|
|
|
|
stdout, stderr = StdOut(module_name, target), StdErr(module_name,
|
|
|
|
target)
|
|
|
|
|
2018-09-19 12:25:12 +00:00
|
|
|
returns = self.events.on("preprocess.command"
|
2016-04-18 15:48:36 +00:00
|
|
|
).call(hook=hook, user=event["user"], server=event["server"],
|
2018-09-03 20:48:14 +00:00
|
|
|
target=target, is_channel=is_channel, tags=event["tags"])
|
2016-03-29 15:22:22 +00:00
|
|
|
for returned in returns:
|
|
|
|
if returned:
|
|
|
|
stderr.write(returned).send()
|
2018-08-28 11:23:57 +00:00
|
|
|
buffer.skip_next()
|
2016-03-29 15:22:22 +00:00
|
|
|
return
|
2018-09-06 16:25:58 +00:00
|
|
|
args_split = event["message_split"][args_index:]
|
2016-03-29 15:22:22 +00:00
|
|
|
min_args = hook.kwargs.get("min_args")
|
|
|
|
if min_args and len(args_split) < min_args:
|
2016-04-10 16:31:55 +00:00
|
|
|
if "usage" in hook.kwargs:
|
|
|
|
stderr.write("Not enough arguments, usage: %s %s" % (
|
|
|
|
command, hook.kwargs["usage"])).send()
|
|
|
|
else:
|
|
|
|
stderr.write("Not enough arguments (minimum: %d)" % min_args
|
|
|
|
).send()
|
2016-03-29 15:22:22 +00:00
|
|
|
else:
|
|
|
|
args = " ".join(args_split)
|
|
|
|
server = event["server"]
|
|
|
|
user = event["user"]
|
2018-09-19 12:25:12 +00:00
|
|
|
self.events.on("received.command").on(command
|
2018-08-28 15:35:07 +00:00
|
|
|
).call_limited(1, user=user, server=server,
|
|
|
|
target=target, buffer=buffer, args=args,
|
|
|
|
args_split=args_split, stdout=stdout, stderr=stderr,
|
2018-09-03 20:48:14 +00:00
|
|
|
command=command.lower(), is_channel=is_channel,
|
|
|
|
tags=event["tags"])
|
2018-07-14 08:50:12 +00:00
|
|
|
if not hook.kwargs.get("skip_out", False):
|
|
|
|
stdout.send()
|
|
|
|
stderr.send()
|
|
|
|
target.last_stdout = stdout
|
|
|
|
target.last_stderr = stderr
|
2018-08-28 11:23:57 +00:00
|
|
|
buffer.skip_next()
|
2018-09-09 16:09:04 +00:00
|
|
|
event.eat()
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def channel_message(self, event):
|
2016-04-14 15:48:44 +00:00
|
|
|
command_prefix = event["channel"].get_setting("command-prefix",
|
|
|
|
event["server"].get_setting("command-prefix", "!"))
|
2016-03-29 11:56:58 +00:00
|
|
|
if event["message_split"][0].startswith(command_prefix):
|
|
|
|
command = event["message_split"][0].replace(
|
|
|
|
command_prefix, "", 1).lower()
|
2016-03-29 15:22:22 +00:00
|
|
|
self.message(event, command)
|
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()
|
|
|
|
self.message(event, command, 2)
|
2016-03-29 15:22:22 +00:00
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def private_message(self, event):
|
2016-03-29 15:22:22 +00:00
|
|
|
if event["message_split"]:
|
|
|
|
command = event["message_split"][0].lower()
|
|
|
|
self.message(event, command)
|
2016-03-29 11:56:58 +00:00
|
|
|
|
|
|
|
def help(self, event):
|
|
|
|
if event["args"]:
|
|
|
|
command = event["args_split"][0].lower()
|
2018-08-31 11:55:52 +00:00
|
|
|
if command in self.events.on("received").on(
|
2016-03-29 11:56:58 +00:00
|
|
|
"command").get_children():
|
2018-09-19 12:25:12 +00:00
|
|
|
hooks = self.events.on("received.command").on(command).get_hooks()
|
2018-09-26 14:58:16 +00:00
|
|
|
kwargs = hooks[0].kwargs
|
|
|
|
help = hooks[0].kwargs.get("help", None
|
|
|
|
) or hooks[0].function.__doc__
|
|
|
|
|
|
|
|
if help:
|
|
|
|
event["stdout"].write("%s: %s" % (command, help.strip()))
|
2016-04-05 17:41:03 +00:00
|
|
|
else:
|
|
|
|
event["stderr"].write("No help available for %s" % command)
|
|
|
|
else:
|
|
|
|
event["stderr"].write("Unknown command '%s'" % command)
|
2016-03-29 11:56:58 +00:00
|
|
|
else:
|
|
|
|
help_available = []
|
2018-09-19 12:25:12 +00:00
|
|
|
for child in self.events.on("received.command").get_children():
|
|
|
|
hooks = self.events.on("received.command").on(child).get_hooks()
|
2016-03-29 11:56:58 +00:00
|
|
|
if hooks and "help" in hooks[0].kwargs:
|
|
|
|
help_available.append(child)
|
|
|
|
help_available = sorted(help_available)
|
|
|
|
event["stdout"].write("Commands: %s" % ", ".join(help_available))
|
|
|
|
|
2016-04-05 17:41:03 +00:00
|
|
|
def usage(self, event):
|
2018-09-09 08:06:17 +00:00
|
|
|
command_prefix = ""
|
2018-09-09 07:59:36 +00:00
|
|
|
if event["is_channel"]:
|
|
|
|
command_prefix = event["target"].get_setting("command-prefix",
|
2018-09-09 08:06:17 +00:00
|
|
|
event["server"].get_setting("command-prefix", "!"))
|
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()
|
2016-04-05 17:41:03 +00:00
|
|
|
if hooks and "usage" in hooks[0].kwargs:
|
2018-09-09 00:19:00 +00:00
|
|
|
event["stdout"].write("Usage: %s%s %s" % (command_prefix,
|
2018-09-09 08:06:17 +00:00
|
|
|
command, hooks[0].kwargs["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)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def more(self, event):
|
2016-03-29 15:22:22 +00:00
|
|
|
if event["target"].last_stdout and event["target"].last_stdout.has_text():
|
2016-03-29 11:56:58 +00:00
|
|
|
event["target"].last_stdout.send()
|
|
|
|
|
2018-09-06 15:05:14 +00:00
|
|
|
def ignore(self, event):
|
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
|
|
|
if user.get_setting("ignore", False):
|
|
|
|
event["stderr"].write("I'm already ignoring '%s'" %
|
|
|
|
user.nickname)
|
|
|
|
else:
|
|
|
|
user.set_setting("ignore", True)
|
|
|
|
event["stdout"].write("Now ignoring '%s'" % user.nickname)
|
|
|
|
|
|
|
|
def unignore(self, event):
|
|
|
|
user = event["server"].get_user(event["args_split"][0])
|
|
|
|
if not user.get_setting("ignore", False):
|
|
|
|
event["stderr"].write("I'm not ignoring '%s'" % user.nickname)
|
|
|
|
else:
|
|
|
|
user.set_setting("ignore", False)
|
|
|
|
event["stdout"].write("Removed ignore for '%s'" % user.nickname)
|
|
|
|
|
2016-03-29 11:56:58 +00:00
|
|
|
def send_stdout(self, event):
|
2016-03-31 12:37:08 +00:00
|
|
|
stdout = StdOut(event["module_name"], event["target"])
|
2016-03-29 11:56:58 +00:00
|
|
|
stdout.write(event["message"]).send()
|
|
|
|
if stdout.has_text():
|
|
|
|
event["target"].last_stdout = stdout
|
|
|
|
def send_stderr(self, event):
|
2016-03-31 12:37:08 +00:00
|
|
|
stderr = StdErr(event["module_name"], event["target"])
|
2016-03-29 11:56:58 +00:00
|
|
|
stderr.write(event["message"]).send()
|
|
|
|
if stderr.has_text():
|
|
|
|
event["target"].last_stderr = stderr
|