From 55d856b8fd0a3f4684a7297b2d3984bf5999f3b1 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sun, 19 Feb 2023 21:12:01 -0600 Subject: [PATCH] account for the discord bridge in the core bot addressed flag --- ircbot/bot.py | 13 ++++++- tests/test_ircbot_bot.py | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/test_ircbot_bot.py diff --git a/ircbot/bot.py b/ircbot/bot.py index 07d6343..67ec77f 100644 --- a/ircbot/bot.py +++ b/ircbot/bot.py @@ -171,7 +171,18 @@ class DrReactor(irc.client.Reactor): else: all_nicks = connection.get_nickname() addressed_pattern = r'^(({nicks})[:,]|@({nicks}))\s+(?P.*)'.format(nicks=all_nicks) - match = re.match(addressed_pattern, what, re.IGNORECASE) + + # ignore the first word, a nick, if the speaker is the bridge + try: + channel = IrcChannel.objects.get(name=sent_location) + if sender_nick == channel.discord_bridge: + short_what = ' '.join(what.split(' ')[1:]) + match = re.match(addressed_pattern, short_what, re.IGNORECASE) + else: + match = re.match(addressed_pattern, what, re.IGNORECASE) + except IrcChannel.DoesNotExist: + match = re.match(addressed_pattern, what, re.IGNORECASE) + if match: event.addressed = True event.addressed_msg = match.group('addressed_msg') diff --git a/tests/test_ircbot_bot.py b/tests/test_ircbot_bot.py new file mode 100644 index 0000000..2d4c27c --- /dev/null +++ b/tests/test_ircbot_bot.py @@ -0,0 +1,84 @@ +"""Test core methods of the IRC bot.""" +from unittest import mock + +from django.test import TestCase + +from ircbot.bot import DrReactor +from ircbot.models import IrcServer + + +class DrReactorTestCase(TestCase): + """Test the bot innards.""" + + fixtures = ['tests/fixtures/irc_server_fixture.json'] + + def setUp(self): + """Create common objects.""" + self.bot = DrReactor() + self.mock_connection = mock.MagicMock() + + self.mock_connection.get_nickname.return_value = 'test_bot' + self.mock_connection.server_config = IrcServer.objects.get(pk=1) + + def test_handle_event_not_addressed(self): + """Test that the core identifies not being addressed.""" + mock_event = mock.MagicMock() + mock_event.arguments = ['someone: hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertFalse(mock_event.addressed) + + def test_handle_event_addressed(self): + """Test that the core identifies being addressed IRC style.""" + mock_event = mock.MagicMock() + mock_event.arguments = ['test_bot: hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertTrue(mock_event.addressed) + + def test_handle_event_addressed_alternate(self): + """Test that the core identifies being addressed in the alternate (e.g. discord) style.""" + mock_event = mock.MagicMock() + mock_event.arguments = ['@test_bot hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertTrue(mock_event.addressed) + + def test_handle_event_bridge_addressed(self): + """Test that the core identifies being addressed IRC style through a discord bridge.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' test_bot: hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + mock_event.source = 'bridge!bridge@localhost' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertTrue(mock_event.addressed) + + def test_handle_event_bridge_addressed_alternate(self): + """Test that the core identifies being addressed in the alternate style through a discord bridge.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' @test_bot hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + mock_event.source = 'bridge!bridge@localhost' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertTrue(mock_event.addressed) + + def test_handle_event_not_addressed_not_bridge(self): + """Test that the core identifies not to chop the first word from a paste not from the bridge user.""" + mock_event = mock.MagicMock() + mock_event.arguments = [' test_bot: hello this is a test message'] + mock_event.type = 'pubmsg' + mock_event.target = '#test' + mock_event.source = 'not-bridge!not-bridge@localhost' + self.bot._handle_event(self.mock_connection, mock_event) + + self.assertFalse(mock_event.addressed)