From 3f2c9369bda08c0d70c6a9df574294be73d1a3cb Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Thu, 27 Jun 2024 09:52:00 -0500 Subject: [PATCH] test (and implement) the whoreply method to log existing users in Signed-off-by: Brian S. Stephan --- idlerpg/ircplugin.py | 19 +++++++++++++++++++ tests/test_idlerpg_ircplugin.py | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/idlerpg/ircplugin.py b/idlerpg/ircplugin.py index 69af385..ea4cf3a 100644 --- a/idlerpg/ircplugin.py +++ b/idlerpg/ircplugin.py @@ -96,6 +96,25 @@ class IdleRPG(Plugin): super(IdleRPG, self).stop() + def handle_whoreply_response(self, connection, event): + """Track who is in the channel and flag who is online and log them in. + + We do a /WHO when coming online and joining the game channel, presuming that everyone in it + *would have* logged themselves in if they were able to, but the bot wasn't online. This may + log in people who are not, but that doesn't seem like a huge risk to the spirit of the game. + """ + user = event.arguments[1] + host = event.arguments[2] + nick = event.arguments[4] + hostmask = f'{nick}!{user}@{host}' + self.seen_hostmasks.add(hostmask) + logger.info("Added %s to the set of seen hostmasks.", hostmask) + for character in Character.objects.filter(enabled=True, status=Character.CHARACTER_STATUS_OFFLINE, + hostmask=hostmask): + character.status = Character.CHARACTER_STATUS_LOGGED_IN + logger.info("Marked %s as logged in.", character) + character.save() + def handle_join(self, connection, event): """Track a character as online if their player joins the game channel.""" self.seen_hostmasks.add(event.source) diff --git a/tests/test_idlerpg_ircplugin.py b/tests/test_idlerpg_ircplugin.py index 71ec6e6..94aa2f2 100644 --- a/tests/test_idlerpg_ircplugin.py +++ b/tests/test_idlerpg_ircplugin.py @@ -209,6 +209,22 @@ class IrcPluginTest(TestCase): test_char.delete() + def test_whoreply(self): + """When joining, we do a /WHO to see who is online.""" + mock_event = mock.MagicMock() + mock_event.arguments = ['', 'bss', 'bss', '', 'bss2'] + + assert 'bss2!bss@bss' not in self.plugin.seen_hostmasks + char = Character.objects.get(name='bss2', game=self.game) + assert char.status == Character.CHARACTER_STATUS_OFFLINE + self.plugin.handle_whoreply_response(self.mock_connection, mock_event) + + assert 'bss2!bss@bss' in self.plugin.seen_hostmasks + char = Character.objects.get(name='bss2', game=self.game) + assert char.status == Character.CHARACTER_STATUS_LOGGED_IN + # clean up + char.log_out() + def test_login_character_not_found(self): """Test the LOGIN command sent to the bot.""" mock_event = mock.MagicMock()