From 7a7cdb9f05f05f0082504ffa2d626b123e4d3ca9 Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Sat, 17 Aug 2019 22:23:51 +0200 Subject: [PATCH] Add a 'channel' attribute to IrcMsg objects. It's nicer to use 'irc.channel' instead of 'irc.args[0]', and .channel provides the actual channel name (stripped of the statusmsg prefix), so it can be used by plugins when they want to use the actual channel name. --- src/irclib.py | 11 ++++++++++- src/ircmsgs.py | 2 +- test/test_irclib.py | 27 +++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/irclib.py b/src/irclib.py index 744e0a5b5..bef88fb7a 100644 --- a/src/irclib.py +++ b/src/irclib.py @@ -881,10 +881,19 @@ class Irc(IrcCommandDispatcher, log.Firewalled): msg.tag('receivedBy', self) msg.tag('receivedOn', self.network) msg.tag('receivedAt', time.time()) - if msg.args and self.isChannel(msg.args[0]): + + # Check if the message is addressed to a channel + if msg.args: channel = msg.args[0] + if not conf.supybot.protocols.irc.strictRfc(): + statusmsg_chars = self.state.supported.get('statusmsg', '') + channel = channel.lstrip(statusmsg_chars) + if not self.isChannel(channel): + channel = None else: channel = None + msg.channel = channel + preInFilter = str(msg).rstrip('\r\n') log.debug('Incoming message (%s): %s', self.network, preInFilter) diff --git a/src/ircmsgs.py b/src/ircmsgs.py index 760e7d7e5..d8af5025f 100644 --- a/src/ircmsgs.py +++ b/src/ircmsgs.py @@ -116,7 +116,7 @@ class IrcMsg(object): # On second thought, let's use methods for tagging. __slots__ = ('args', 'command', 'host', 'nick', 'prefix', 'user', '_hash', '_str', '_repr', '_len', 'tags', 'reply_env', - 'server_tags', 'time') + 'server_tags', 'time', 'channel') def __init__(self, s='', command='', args=(), prefix='', msg=None, reply_env=None): assert not (msg and s), 'IrcMsg.__init__ cannot accept both s and msg' diff --git a/test/test_irclib.py b/test/test_irclib.py index f320b6cd8..d00f754be 100644 --- a/test/test_irclib.py +++ b/test/test_irclib.py @@ -470,6 +470,33 @@ class IrcTestCase(SupyTestCase): self.irc.feedMsg(msg2) self.assertEqual(list(self.irc.state.history), [msg1, msg2]) + def testMsgChannel(self): + self.irc.reset() + + self.irc.state.supported['statusmsg'] = '@' + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG #linux :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux') + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG @#linux2 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux2') + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG +#linux3 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, None) + + self.irc.state.supported['statusmsg'] = '+@' + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG #linux :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux') + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG @#linux2 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux2') + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG +#linux3 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux3') + + del self.irc.state.supported['statusmsg'] + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG #linux :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, '#linux') + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG @#linux2 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, None) + self.irc.feedMsg(ircmsgs.IrcMsg('PRIVMSG +#linux3 :foo bar baz!')) + self.assertEqual(self.irc.state.history[-1].channel, None) + def testQuit(self): self.irc.reset() self.irc.feedMsg(ircmsgs.IrcMsg(':someuser JOIN #foo'))