Skip to content

Commit

Permalink
Merge pull request #2088 from sopel-irc/botmode
Browse files Browse the repository at this point in the history
coretasks: handle `BOT` ISUPPORT token & mode
  • Loading branch information
dgw authored Jul 1, 2021
2 parents 6950249 + ea64af1 commit 6481dca
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
10 changes: 9 additions & 1 deletion sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ def startup(bot, trigger):
@plugin.priority('medium')
def handle_isupport(bot, trigger):
"""Handle ``RPL_ISUPPORT`` events."""
# remember if NAMESX is known to be supported, before parsing RPL_ISUPPORT
# remember if certain actionable tokens are known to be supported,
# before parsing RPL_ISUPPORT
botmode_support = 'BOT' in bot.isupport
namesx_support = 'NAMESX' in bot.isupport

# parse ISUPPORT message from server
Expand All @@ -325,6 +327,12 @@ def handle_isupport(bot, trigger):

bot._isupport = bot._isupport.apply(**parameters)

# was BOT mode support status updated?
if not botmode_support and 'BOT' in bot.isupport:
# yes it was! set our mode unless the config overrides it
botmode = bot.isupport['BOT']
if botmode not in bot.config.core.modes:
bot.write(('MODE', bot.nick, '+' + botmode))
# was NAMESX support status updated?
if not namesx_support and 'NAMESX' in bot.isupport:
# yes it was!
Expand Down
45 changes: 45 additions & 0 deletions test/test_coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,51 @@ def test_handle_isupport(mockbot):
assert 'CNOTICE' in mockbot.isupport


@pytest.mark.parametrize('modes', ['', 'Rw'])
def test_handle_isupport_bot_mode(mockbot, modes):
mockbot.config.core.modes = modes

mockbot.on_message(
':irc.example.com 005 Sopel '
'SAFELIST ELIST=CTU CPRIVMSG CNOTICE '
':are supported by this server')

assert 'BOT' not in mockbot.isupport
assert mockbot.backend.message_sent == []

mockbot.on_message(
':irc.example.com 005 Sopel '
'BOT=B '
':are supported by this server')

assert 'BOT' in mockbot.isupport
assert mockbot.isupport['BOT'] == 'B'
assert mockbot.backend.message_sent == rawlist('MODE TestBot +B')

mockbot.on_message(
':irc.example.com 005 Sopel '
'BOT=B '
':are supported by this server')

assert len(mockbot.backend.message_sent) == 1, 'No need to resend!'


@pytest.mark.parametrize('modes', ['B', 'RBw'])
def test_handle_isupport_bot_mode_override(mockbot, modes):
mockbot.config.core.modes = modes

mockbot.on_message(
':irc.example.com 005 Sopel '
'BOT=B '
':are supported by this server')

assert 'BOT' in mockbot.isupport
assert mockbot.isupport['BOT'] == 'B'
assert mockbot.backend.message_sent == [], (
'Bot should not set mode overridden by config setting'
)


def test_handle_isupport_namesx(mockbot):
mockbot.on_message(
':irc.example.com 005 Sopel '
Expand Down

0 comments on commit 6481dca

Please sign in to comment.