diff --git a/sopel/coretasks.py b/sopel/coretasks.py index 5fe5d026ff..74c17ec4c6 100644 --- a/sopel/coretasks.py +++ b/sopel/coretasks.py @@ -311,7 +311,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 @@ -326,6 +328,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! diff --git a/test/test_coretasks.py b/test/test_coretasks.py index 9dc2ee0e2a..49d39408a6 100644 --- a/test/test_coretasks.py +++ b/test/test_coretasks.py @@ -278,6 +278,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 '