irclib: add method getClientTagDenied

To allow plugins to check if they should send a tag or not.
This commit is contained in:
Valentin Lorentz 2021-03-18 19:53:35 +01:00
parent 544f137c64
commit d60cc5c92a
2 changed files with 44 additions and 0 deletions

View File

@ -821,6 +821,27 @@ class IrcState(IrcCommandDispatcher, log.Firewalled):
return batches return batches
def getClientTagDenied(self, tag):
"""Returns whether the given tag is denied by the server, according
to its CLIENTTAGDENY policy.
This is only informative, and servers may still allow or deny tags
at their discretion.
For details, see the RPL_ISUPPORT section in
<https://ircv3.net/specs/extensions/message-tags>
"""
tag = tag.lstrip("+")
denied_tags = self.supported.get('CLIENTTAGDENY')
if not denied_tags:
return False
denied_tags = denied_tags.split(',')
if '*' in denied_tags:
# All tags are denied by default, check the whitelist
return ('-' + tag) not in denied_tags
else:
return tag in denied_tags
def do004(self, irc, msg): def do004(self, irc, msg):
"""Handles parsing the 004 reply """Handles parsing the 004 reply

View File

@ -420,6 +420,29 @@ class IrcStateTestCase(SupyTestCase):
self.assertEqual(state.supported['chanmodes'], self.assertEqual(state.supported['chanmodes'],
frozenset('biklmnopstveI')) frozenset('biklmnopstveI'))
def testClientTagDenied(self):
state = irclib.IrcState()
# By default, every tag is assumed allowed
self.assertEqual(state.getClientTagDenied('foo'), False)
self.assertEqual(state.getClientTagDenied('bar'), False)
# Blacklist
state.addMsg(self.irc, ircmsgs.IrcMsg(
':example.org 005 mybot CLIENTTAGDENY=foo :are supported by this server'))
self.assertEqual(state.getClientTagDenied('foo'), True)
self.assertEqual(state.getClientTagDenied('bar'), False)
self.assertEqual(state.getClientTagDenied('+foo'), True)
self.assertEqual(state.getClientTagDenied('+bar'), False)
# Whitelist
state.addMsg(self.irc, ircmsgs.IrcMsg(
':example.org 005 mybot CLIENTTAGDENY=*,-foo :are supported by this server'))
self.assertEqual(state.getClientTagDenied('foo'), False)
self.assertEqual(state.getClientTagDenied('bar'), True)
self.assertEqual(state.getClientTagDenied('+foo'), False)
self.assertEqual(state.getClientTagDenied('+bar'), True)
def testShort004(self): def testShort004(self):
state = irclib.IrcState() state = irclib.IrcState()
state.addMsg(self.irc, ircmsgs.IrcMsg(':coulomb.oftc.net 004 testnick coulomb.oftc.net hybrid-7.2.2+oftc1.6.8')) state.addMsg(self.irc, ircmsgs.IrcMsg(':coulomb.oftc.net 004 testnick coulomb.oftc.net hybrid-7.2.2+oftc1.6.8'))