From a0070fca38aa8097f46d9d087fcf5b3059af6b71 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Mon, 30 May 2022 19:43:39 -0700 Subject: [PATCH] Small fix for plugins that accept non-channel messages There's a long explanation in the code of this commit that says this: > TL;DR OF THE BELOW: if the first parameter looks like a channel in > addition to message type, then filter by channel. Otherwise, don't > filter by channel. > > Here's the issue: plugins are *usually* multiplexed by channel. But > that's only for messages that target channels, such as PRIVMSG and JOIN. > For non-channel messages, such as server status messages (such as 001 on > connect, or 372 for MOTD, etc) we want to ignore the channel aspect of > plugin multiplexing. In order to accomplish this, we just check if the > first parameter looks like a channel - i.e., starts with an octothorpe #. Signed-off-by: Alek Ratzloff --- omnibot/bot.py | 21 +++++++++++++++++++-- omnibot/plugin.py | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/omnibot/bot.py b/omnibot/bot.py index 675b4d3..49a1490 100644 --- a/omnibot/bot.py +++ b/omnibot/bot.py @@ -135,10 +135,27 @@ class Bot: # Don't raise on_message events for ourselves. return line = message.parameters[1] - # Filter plugins by get_message_types() and channel + + # TL;DR OF THE BELOW: if the first parameter looks like a channel in + # addition to message type, then filter by channel. Otherwise, don't + # filter by channel. + # + # Here's the issue: plugins are *usually* multiplexed by channel. But + # that's only for messages that target channels, such as PRIVMSG and + # JOIN. For non-channel messages, such as server status messages (such + # as 001 on connect, or 372 for MOTD, etc) we want to ignore the channel + # aspect of plugin multiplexing. + # In order to accomplish this, we just check if the first parameter + # looks like a channel - i.e., starts with an octothorpe #. + if channel and channel[0] == "#": + plugin_pool = self.channel_plugins(channel) + else: + plugin_pool = self.plugins + + # Filter plugins by get_message_types() plugins = [ plugin - for plugin in self.channel_plugins(channel) + for plugin in plugin_pool if message.command in plugin.get_message_types() ] if plugins: diff --git a/omnibot/plugin.py b/omnibot/plugin.py index b515bd0..aa2fe78 100644 --- a/omnibot/plugin.py +++ b/omnibot/plugin.py @@ -58,6 +58,9 @@ class Plugin: def send_to(self, conn: IrcProtocol, who: str, message: str): message = Message(None, None, "PRIVMSG", who, message[:1024]) + self.send_message(conn, message) + + def send_message(self, conn: IrcProtocol, message: Message): conn.send(str(message)) async def on_join(self, conn: IrcProtocol, channel: str, who: Prefix):