From 23723638987aab032817995c0b1d2da07da5fcfd Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Mon, 15 Jul 2024 18:44:58 -0500 Subject: [PATCH] the kick event source is the kicker, so find the kickee by nick Signed-off-by: Brian S. Stephan --- idlerpg/ircplugin.py | 15 ++++++++++++--- tests/test_idlerpg_ircplugin.py | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/idlerpg/ircplugin.py b/idlerpg/ircplugin.py index defa580..01cf160 100644 --- a/idlerpg/ircplugin.py +++ b/idlerpg/ircplugin.py @@ -122,9 +122,18 @@ class IdleRPG(Plugin): def handle_kick_penalty(self, connection, event): """Penalize characters for their user getting kicked from the channel.""" - self.seen_hostmasks.discard(event.source) - logger.info("Removed %s from the set of seen hostmasks.", event.source) - return self._handle_generic_penalty_and_logout(event.source, event.target, 250, + logger.debug("kick: %s", event) + + # somewhat hacky attempt to find the hostmask for the kicked nick + kicked_nick_hostmask_prefix = f'{event.arguments[0]}!' + try: + source = [x for x in self.seen_hostmasks if x.startswith(kicked_nick_hostmask_prefix)][0] + except IndexError: + return + + self.seen_hostmasks.discard(source) + logger.info("Removed %s from the set of seen hostmasks.", source) + return self._handle_generic_penalty_and_logout(source, event.target, 250, "getting kicked from the game channel", 'time_penalized_kicked') diff --git a/tests/test_idlerpg_ircplugin.py b/tests/test_idlerpg_ircplugin.py index f92c365..0579e29 100644 --- a/tests/test_idlerpg_ircplugin.py +++ b/tests/test_idlerpg_ircplugin.py @@ -45,21 +45,30 @@ class IrcPluginTest(TestCase): def test_kick_penalty(self): """Test that if a character is kicked from the game channel, they get penalized.""" mock_event = mock.MagicMock() - mock_event.source = 'bss!bss@test_kick_penalty' + mock_event.source = 'admin!admin@the_kicker' mock_event.target = '#test' + mock_event.arguments = ['bss', ''] mock_event.recursing = False # make a character so as to not disturb other tests test_char = Character.objects.register('testkickpen', self.game, 'test', 'bss!bss@test_kick_penalty', 'tester') - with mock.patch('idlerpg.models.Character.penalize', return_value=5) as mock_penalize: + self.plugin.seen_hostmasks.add('bss!bss@test_kick_penalty') + with mock.patch('idlerpg.models.Character.penalize', return_value=250) as mock_penalize: with mock.patch('idlerpg.models.Character.log_out') as mock_log_out: self.plugin.handle_kick_penalty(self.mock_connection, mock_event) mock_penalize.assert_called_with(250, "getting kicked from the game channel") mock_log_out.assert_called() test_char = Character.objects.get(name='testkickpen') - assert test_char.time_penalized_kicked == 5 + assert test_char.time_penalized_kicked == 250 + assert 'bss!bss@test_kick_penalty' not in self.plugin.seen_hostmasks + self.mock_bot.reply.assert_called_once_with( + None, + "testkickpen, the level 0 tester has been penalized 250 seconds for getting kicked from the game channel, " + "and has been logged out.", + explicit_target='bss', + ) test_char.delete()