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.
This commit is contained in:
Valentin Lorentz 2019-08-17 22:23:51 +02:00
parent d4cac026d4
commit 7a7cdb9f05
3 changed files with 38 additions and 2 deletions

View File

@ -881,10 +881,19 @@ class Irc(IrcCommandDispatcher, log.Firewalled):
msg.tag('receivedBy', self) msg.tag('receivedBy', self)
msg.tag('receivedOn', self.network) msg.tag('receivedOn', self.network)
msg.tag('receivedAt', time.time()) 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] 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: else:
channel = None channel = None
msg.channel = channel
preInFilter = str(msg).rstrip('\r\n') preInFilter = str(msg).rstrip('\r\n')
log.debug('Incoming message (%s): %s', self.network, preInFilter) log.debug('Incoming message (%s): %s', self.network, preInFilter)

View File

@ -116,7 +116,7 @@ class IrcMsg(object):
# On second thought, let's use methods for tagging. # On second thought, let's use methods for tagging.
__slots__ = ('args', 'command', 'host', 'nick', 'prefix', 'user', __slots__ = ('args', 'command', 'host', 'nick', 'prefix', 'user',
'_hash', '_str', '_repr', '_len', 'tags', 'reply_env', '_hash', '_str', '_repr', '_len', 'tags', 'reply_env',
'server_tags', 'time') 'server_tags', 'time', 'channel')
def __init__(self, s='', command='', args=(), prefix='', msg=None, def __init__(self, s='', command='', args=(), prefix='', msg=None,
reply_env=None): reply_env=None):
assert not (msg and s), 'IrcMsg.__init__ cannot accept both s and msg' assert not (msg and s), 'IrcMsg.__init__ cannot accept both s and msg'

View File

@ -470,6 +470,33 @@ class IrcTestCase(SupyTestCase):
self.irc.feedMsg(msg2) self.irc.feedMsg(msg2)
self.assertEqual(list(self.irc.state.history), [msg1, 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): def testQuit(self):
self.irc.reset() self.irc.reset()
self.irc.feedMsg(ircmsgs.IrcMsg(':someuser JOIN #foo')) self.irc.feedMsg(ircmsgs.IrcMsg(':someuser JOIN #foo'))