From 57a0e2f10fa1a3dc879a9f9a2bc000e13c54826a Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 29 Mar 2016 16:22:22 +0100 Subject: [PATCH] made commands work in channel AND in private message. --- IRCLineHandler.py | 1 + IRCServer.py | 10 +++-- IRCUser.py | 2 + modules/commands.py | 104 +++++++++++++++++++++++++------------------ modules/translate.py | 2 +- 5 files changed, 70 insertions(+), 49 deletions(-) diff --git a/IRCLineHandler.py b/IRCLineHandler.py index 2c6423e7..95c2b6b7 100644 --- a/IRCLineHandler.py +++ b/IRCLineHandler.py @@ -247,6 +247,7 @@ def handle_PRIVMSG(line, line_split, bot, server): line=line, line_split=line_split, server=server, user=user, message=message, message_split=message_split, action=action) + user.log.add_line(user.nickname, message, action) @handler(description="response to a WHO command for user information") def handle_352(line, line_split, bot, server): user = server.get_user(line_split[7]) diff --git a/IRCServer.py b/IRCServer.py index 1f291c23..aad88f41 100644 --- a/IRCServer.py +++ b/IRCServer.py @@ -183,12 +183,14 @@ class Server(object): self.send("QUIT :%s" % reason) def send_message(self, target, message): self.send("PRIVMSG %s :%s" % (target, message)) + action = message.startswith("\01ACTION ") and message.endswith( + "\01") + if action: + message = message.split("\01ACTION ", 1)[1][:-1] if self.has_channel(target): - action = message.startswith("\01ACTION ") and message.endswith( - "\01") - if action: - message = message.split("\01ACTION ", 1)[1][:-1] self.get_channel(target).log.add_line(None, message, action, True) + else: + self.get_user(target).log.add_line(None, message, action, True) def send_notice(self, target, message): self.send("NOTICE %s :%s" % (target, message)) def send_mode(self, target, mode=None, args=None): diff --git a/IRCUser.py b/IRCUser.py index 204ca85a..db7aa4d9 100644 --- a/IRCUser.py +++ b/IRCUser.py @@ -1,4 +1,5 @@ import uuid +import IRCLog class User(object): def __init__(self, nickname, server, bot): @@ -12,6 +13,7 @@ class User(object): self.id = None while self.id == None or self.id in server.users: self.id = uuid.uuid1().hex + self.log = IRCLog.Log(bot) def set_nickname(self, nickname): self.nickname = nickname self.nickname_lower = nickname.lower() diff --git a/modules/commands.py b/modules/commands.py index 3815e5e8..60ab511f 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -4,10 +4,10 @@ STR_CONTINUED = "(...continued) " OUT_CUTOFF = 400 -class ChannelOut(object): - def __init__(self, module_name, channel): +class Out(object): + def __init__(self, module_name, target): self.module_name = module_name - self.channel = channel + self.target = target self._text = "" self.written = False def write(self, text): @@ -23,16 +23,16 @@ class ChannelOut(object): ).rstrip(), STR_MORE) self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF: ].decode("utf8").lstrip()) - self.channel.send_message(text) + self.target.send_message(text) def set_prefix(self, prefix): self.module_name = prefix def has_text(self): return bool(self._text) -class ChannelStdOut(ChannelOut): +class StdOut(Out): def prefix(self): return self.module_name -class ChannelStdErr(ChannelOut): +class StdErr(Out): def prefix(self): return "!%s" % self.module_name @@ -43,9 +43,11 @@ class Module(object): self.channel_message) bot.events.on("received").on("message").on("private").hook( self.private_message) - bot.events.on("received").on("command").on("help").hook(self.help) + bot.events.on("received").on("command").on("help").hook(self.help, + help="Show help on commands") + bot.events.on("received").on("command").on("more").hook(self.more, + help="Get more output from the last command") bot.events.on("new").on("user", "channel").hook(self.new) - bot.events.on("received").on("command").on("more").hook(self.more) bot.events.on("send").on("stdout").hook(self.send_stdout) bot.events.on("send").on("stderr").hook(self.send_stderr) @@ -64,48 +66,62 @@ class Module(object): return self.bot.events.on("received").on("command").on(command ).get_hooks()[0] + def message(self, event, command): + if self.has_command(command): + 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 + + log = target.log + log.skip_next() + + module_name = hook.function.__self__._name + stdout, stderr = StdOut(module_name, target), StdErr(module_name, + target) + + returns = self.bot.events.on("preprocess").on("command" + ).call(hook=hook, user=event["user"], server=event["server"]) + for returned in returns: + if returned: + stderr.write(returned).send() + return + args_split = list(filter(None, event["message_split"][1:])) + min_args = hook.kwargs.get("min_args") + if min_args and len(args_split) < min_args: + stderr.write("Not enough arguments (minimum: %d)" % min_args + ).send() + else: + args = " ".join(args_split) + server = event["server"] + user = event["user"] + self.bot.events.on("received").on("command").on(command).call( + 1, user=user, server=server, target=target, log=log, + args=args, args_split=args_split, stdout=stdout, stderr=stderr, + command=command.lower()) + stdout.send() + target.last_stdout = stdout + stderr.send() + target.last_stderr = stderr + def channel_message(self, event): command_prefix = event["channel"].get_setting("command_prefix", event["server"].get_setting("command_prefix", "!")) if event["message_split"][0].startswith(command_prefix): command = event["message_split"][0].replace( command_prefix, "", 1).lower() - if self.has_command(command): - hook = self.get_hook(command) - module_name = hook.function.__self__._name - stdout = ChannelStdOut(module_name, event["channel"]) - stderr = ChannelStdErr(module_name, event["channel"]) - returns = self.bot.events.on("preprocess").on("command" - ).call(hook=hook, user=event["user"], server=event[ - "server"]) - for returned in returns: - if returned: - event["stderr"].write(returned).send() - return - min_args = hook.kwargs.get("min_args") - # get rid of all the empty strings - args_split = list(filter(None, event["message_split"][1:])) - if min_args and len(args_split) < min_args: - ChannelStdErr("Error", event["channel"] - ).write("Not enough arguments (" - "minimum: %d)" % min_args).send() - else: - args = " ".join(args_split) - self.bot.events.on("received").on("command").on( - command).call(1, user=event["user"], - channel=event["channel"], args=args, - args_split=args_split, server=event["server" - ], stdout=stdout, stderr=stderr, command=command, - log=event["channel"].log, target=event["channel"]) - stdout.send() - stderr.send() - if stdout.has_text(): - event["channel"].last_stdout = stdout - if stderr.has_text(): - event["channel"].last_stderr = stderr - event["channel"].log.skip_next() + self.message(event, command) + def private_message(self, event): - pass + if event["message_split"]: + command = event["message_split"][0].lower() + self.message(event, command) def help(self, event): if event["args"]: @@ -125,7 +141,7 @@ class Module(object): event["stdout"].write("Commands: %s" % ", ".join(help_available)) def more(self, event): - if event["target"].last_stdout: + if event["target"].last_stdout and event["target"].last_stdout.has_text(): event["target"].last_stdout.send() def send_stdout(self, event): diff --git a/modules/translate.py b/modules/translate.py index 5e0cc15a..2886330d 100644 --- a/modules/translate.py +++ b/modules/translate.py @@ -13,7 +13,7 @@ class Module(object): def translate(self, event): phrase = event["args"] if not phrase: - phrase = event["channel"].log.get() + phrase = event["log"].get() if phrase: phrase = phrase.message if not phrase: