From 9118af5e9c9c0e80507fd2bc8bc6cd840fad2c2f Mon Sep 17 00:00:00 2001 From: jesopo Date: Tue, 2 Oct 2018 21:40:34 +0100 Subject: [PATCH] Support changing command responses from PRIVMSG to NOTICE --- modules/commands.py | 40 +++++++++++++++++++++++++++++++--------- modules/line_handler.py | 5 +++-- src/IRCBuffer.py | 13 ++++++++++--- src/IRCChannel.py | 2 ++ src/IRCServer.py | 23 +++++++++++++++++------ 5 files changed, 63 insertions(+), 20 deletions(-) diff --git a/modules/commands.py b/modules/commands.py index 836377c9..7ccd19cf 100644 --- a/modules/commands.py +++ b/modules/commands.py @@ -4,12 +4,16 @@ from src import EventManager, ModuleManager, Utils STR_MORE = "%s (more...)" % Utils.FONT_RESET STR_CONTINUED = "(...continued) " +COMMAND_METHOD = "command-method" +COMMAND_METHODS = ["PRIVMSG", "NOTICE"] + OUT_CUTOFF = 400 REGEX_CUTOFF = re.compile("^.{1,%d}(?:\s|$)" % OUT_CUTOFF) class Out(object): - def __init__(self, module_name, target, msgid): + def __init__(self, server, module_name, target, msgid): + self.server = server self.module_name = module_name self.target = target self._text = "" @@ -37,8 +41,16 @@ class Out(object): if self._msgid: tags["+draft/reply"] = self._msgid - self.target.send_message(text, - prefix=Utils.FONT_RESET + "[%s] " % self.prefix(), tags=tags) + prefix = Utils.FONT_RESET + "[%s] " % self.prefix() + method = self._get_method() + if method == "PRIVMSG": + self.target.send_message(text, prefix=prefix, tags=tags) + elif method == "NOTICE": + self.target.send_notice(text, prefix=prefix, tags=tags) + + def _get_method(self): + return self.target.get_setting(COMMAND_METHOD, + self.server.get_setting(COMMAND_METHOD, "PRIVMSG")).upper() def set_prefix(self, prefix): self.module_name = prefix @@ -53,12 +65,22 @@ class StdErr(Out): def prefix(self): return Utils.color(Utils.bold("!"+self.module_name), Utils.COLOR_RED) +def _command_method_validate(s): + if s.upper() in COMMAND_METHODS: + return s.upper() + @Utils.export("channelset", {"setting": "command-prefix", "help": "Set the command prefix used in this channel"}) @Utils.export("serverset", {"setting": "command-prefix", "help": "Set the command prefix used on this server"}) @Utils.export("serverset", {"setting": "identity-mechanism", "help": "Set the identity mechanism for this server"}) +@Utils.export("serverset", {"setting": "command-method", + "help": "Set the method used to respond to commands", + "validate": _command_method_validate}) +@Utils.export("channelset", {"setting": "command-method", + "help": "Set the method used to respond to commands", + "validate": _command_method_validate}) class Module(ModuleManager.BaseModule): @Utils.hook("new.user|channel") def new(self, event): @@ -105,8 +127,8 @@ class Module(ModuleManager.BaseModule): module_name = hook.function.__self__._name msgid = event["tags"].get("draft/msgid", None) - stdout = StdOut(module_name, target, msgid) - stderr = StdErr(module_name, target, msgid) + stdout = StdOut(event["server"], module_name, target, msgid) + stderr = StdErr(event["server"], module_name, target, msgid) returns = self.events.on("preprocess.command").call_unsafe( hook=hook, user=event["user"], server=event["server"], @@ -260,15 +282,15 @@ class Module(ModuleManager.BaseModule): @Utils.hook("send.stdout") def send_stdout(self, event): - stdout = StdOut(event["module_name"], event["target"], - event.get("msgid", None)) + stdout = StdOut(event["server"], event["module_name"], + event["target"], event.get("msgid", None)) stdout.write(event["message"]).send() if stdout.has_text(): event["target"].last_stdout = stdout @Utils.hook("send.stderr") def send_stderr(self, event): - stderr = StdErr(event["module_name"], event["target"], - event.get("msgid", None)) + stderr = StdErr(event["server"], event["module_name"], + event["target"], event.get("msgid", None)) stderr.write(event["message"]).send() if stderr.has_text(): event["target"].last_stderr = stderr diff --git a/modules/line_handler.py b/modules/line_handler.py index 68a717ee..0885e002 100644 --- a/modules/line_handler.py +++ b/modules/line_handler.py @@ -400,12 +400,13 @@ class Module(ModuleManager.BaseModule): channel = event["server"].get_channel(event["args"][0]) self.events.on("received.message.channel").call( user=user, channel=channel, **kwargs) - channel.buffer.add_line(user.nickname, message, action, + channel.buffer.add_message(user.nickname, message, action, event["tags"]) elif event["server"].is_own_nickname(target): self.events.on("received.message.private").call( user=user, **kwargs) - user.buffer.add_line(user.nickname, message, action, event["tags"]) + user.buffer.add_message(user.nickname, message, action, + event["tags"]) # we've received a notice @Utils.hook("raw.notice") diff --git a/src/IRCBuffer.py b/src/IRCBuffer.py index b3825178..f5e6fa70 100644 --- a/src/IRCBuffer.py +++ b/src/IRCBuffer.py @@ -2,12 +2,13 @@ import re from . import Utils class BufferLine(object): - def __init__(self, sender, message, action, tags, from_self): + def __init__(self, sender, message, action, tags, from_self, method): self.sender = sender self.message = message self.action = action self.tags = tags self.from_self = from_self + self.method = method class Buffer(object): def __init__(self, bot, server): @@ -16,13 +17,19 @@ class Buffer(object): self.lines = [] self.max_lines = 64 self._skip_next = False - def add_line(self, sender, message, action, tags, from_self=False): + + def _add_message(self, sender, message, action, tags, from_self, method): if not self._skip_next: - line = BufferLine(sender, message, action, tags, from_self) + line = BufferLine(sender, message, action, tags, from_self, method) self.lines.insert(0, line) if len(self.lines) > self.max_lines: self.lines.pop() self._skip_next = False + def add_message(self, sender, message, action, tags, from_self=False): + self._add_message(sender, message, action, tags, from_self, "PRIVMSG") + def add_notice(self, sender, message, tags, from_self=False): + self._add_message(sender, message, False, tags, from_self, "NOTICE") + def get(self, index=0, **kwargs): from_self = kwargs.get("from_self", True) for line in self.lines: diff --git a/src/IRCChannel.py b/src/IRCChannel.py index ed7064d7..02622380 100644 --- a/src/IRCChannel.py +++ b/src/IRCChannel.py @@ -107,6 +107,8 @@ class Channel(IRCObject.Object): def send_message(self, text, prefix=None, tags={}): self.server.send_message(self.name, text, prefix=prefix, tags=tags) + def send_notice(self, text, prefix=None, tags={}): + self.server.send_notice(self.name, text, prefix=prefix, tags=tags) def send_mode(self, mode=None, target=None): self.server.send_mode(self.name, mode, target) def send_kick(self, target, reason=None): diff --git a/src/IRCServer.py b/src/IRCServer.py index d6f79b31..55e1e3ca 100644 --- a/src/IRCServer.py +++ b/src/IRCServer.py @@ -351,7 +351,7 @@ class Server(IRCObject.Object): def send_quit(self, reason="Leaving"): self.send("QUIT :%s" % reason) - def send_message(self, target, message, prefix=None, tags={}): + def _tag_str(self, tags): tag_str = "" for tag, value in tags.items(): if tag_str: @@ -361,9 +361,12 @@ class Server(IRCObject.Object): tag_str += "=%s" % value if tag_str: tag_str = "@%s " % tag_str + return tag_str + def send_message(self, target, message, prefix=None, tags={}): full_message = message if not prefix else prefix+message - self.send("%sPRIVMSG %s :%s" % (tag_str, target, full_message)) + self.send("%sPRIVMSG %s :%s" % (self._tag_str(tags), target, + full_message)) action = full_message.startswith("\01ACTION " ) and full_message.endswith("\01") @@ -374,19 +377,27 @@ class Server(IRCObject.Object): full_message_split = full_message.split() if self.has_channel(target): channel = self.get_channel(target) - channel.buffer.add_line(None, message, action, tags, True) + channel.buffer.add_message(None, message, action, tags, True) self.events.on("self.message.channel").call( message=full_message, message_split=full_message_split, channel=channel, action=action, server=self) else: user = self.get_user(target) - user.buffer.add_line(None, message, action, tags, True) + user.buffer.add_message(None, message, action, tags, True) self.events.on("self.message.private").call( message=full_message, message_split=full_message_split, user=user, action=action, server=self) - def send_notice(self, target, message): - self.send("NOTICE %s :%s" % (target, message)) + def send_notice(self, target, message, prefix=None, tags={}): + full_message = message if not prefix else prefix+message + self.send("%sNOTICE %s :%s" % (self._tag_str(tags), target, + full_message)) + if self.has_channel(target): + self.get_channel(target).buffer.add_notice(None, message, tags, + True) + else: + self.get_user(target).buffer.add_notice(None, message, tags, + True) def send_mode(self, target, mode=None, args=None): self.send("MODE %s%s%s" % (target, "" if mode == None else " %s" % mode,