From 86a89c7d448acb975fa98519b5ebb0e9bd3f5bae Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Sat, 15 Feb 2020 01:14:37 +0100 Subject: [PATCH 1/2] devlug dispatch module dispatch messages from matterbridge formatted like [irc] This is a message that is. only "This is a message" is passed to the bot command handler --- modules/devlugdispatch.py | 75 +++++++++++++++++++++++++++++++++++ test/test_devlugdispatcher.py | 29 ++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 modules/devlugdispatch.py create mode 100644 test/test_devlugdispatcher.py diff --git a/modules/devlugdispatch.py b/modules/devlugdispatch.py new file mode 100644 index 0000000..e4a391d --- /dev/null +++ b/modules/devlugdispatch.py @@ -0,0 +1,75 @@ +import re + +from nio import RoomMessageText + +from modules.common.module import BotModule + + +class MatterMessage: + def __init__(self, protocol, user, message): + self.protocol = protocol + self.username = user + self.message = message + + +class MatterBridgeParser: + + def __init__(self): + self.reg = re.compile(r"\[(.*)\] <(.*)> (.*)") + self.match = None + + def validate(self, message): + self.match = self.reg.match(message) + return self.match is not None + + def parse(self, message): + + if self.validate(message): + groups = self.match.group(1, 2, 3) + return MatterMessage(groups[0], groups[1], groups[2]) + else: + return None + + +class MatrixModule(BotModule): + """ + Everytime a matterbridge message is seen, the original message is delegated to the bot for command processing + """ + + def __init__(self, name): + super().__init__(name) + self.bot = None + self.parser = MatterBridgeParser() + + async def matrix_message(self, bot, room, event): + + # todo: add subcommand to add administrators + # todo: needs a mechanism to hook into admin check of the bot + pass + + def help(self): + return "parses matterbridge messages and delegate the parsed message to the bot" + + def matrix_start(self, bot): + self.bot = bot + super().matrix_start(bot) + bot.client.add_event_callback(self.dispatch_cb, RoomMessageText) + + def matrix_stop(self, bot): + super().matrix_stop(bot) + bot.remove_callback(self.dispatch_cb) + + async def dispatch_cb(self, room, event): + # no content at all? + if len(event.body) < 1: + return + + matter_message = self.parser.parse(event.body) + + if matter_message is None: + return + + self.logger.info(f"room: {room.name} - dispatch matterbridge message to bot") + + # dispatch. changing the body of the event triggers message_cb of the bot + event.body = matter_message.message diff --git a/test/test_devlugdispatcher.py b/test/test_devlugdispatcher.py new file mode 100644 index 0000000..2ff1282 --- /dev/null +++ b/test/test_devlugdispatcher.py @@ -0,0 +1,29 @@ +import unittest +from modules.devlugdispatch import MatterBridgeParser + + +class DevlugDispatcherTest(unittest.TestCase): + + def test_recognizes_matterbridge_message(self): + """Test that a message was send by a matterbridge bot""" + message = "[irc] say something" + parser = MatterBridgeParser() + + self.assertEqual(parser.validate(message), True) + + def test_parse_returns_None_for_none_matter_bridge_messages(self): + """Test that a normal message gets parsed to None""" + message = "a normal message" + parser = MatterBridgeParser() + + self.assertEqual(parser.parse(message), None) + + def test_parse_returns_protocol_username_and_message(self): + + message = "[irc] say something" + parser = MatterBridgeParser() + + matter_msg = parser.parse(message) + self.assertEqual(matter_msg.protocol, "irc") + self.assertEqual(matter_msg.username, "ancho") + self.assertEqual(matter_msg.message, "say something") From 419c89050212fbdf47601bfdb72c5d14f8a3e999 Mon Sep 17 00:00:00 2001 From: Frank Becker Date: Sat, 15 Feb 2020 10:47:36 +0100 Subject: [PATCH 2/2] added a few todo's --- modules/devlugdispatch.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/modules/devlugdispatch.py b/modules/devlugdispatch.py index e4a391d..1780752 100644 --- a/modules/devlugdispatch.py +++ b/modules/devlugdispatch.py @@ -44,6 +44,7 @@ class MatrixModule(BotModule): async def matrix_message(self, bot, room, event): # todo: add subcommand to add administrators + # todo: add subcommand to add known matterbridge bot # todo: needs a mechanism to hook into admin check of the bot pass @@ -60,6 +61,10 @@ class MatrixModule(BotModule): bot.remove_callback(self.dispatch_cb) async def dispatch_cb(self, room, event): + + # todo: only accept messages from matterbridge bot + # like event.sender in self.known_matterbridge_bots + # no content at all? if len(event.body) < 1: return @@ -69,7 +74,7 @@ class MatrixModule(BotModule): if matter_message is None: return - self.logger.info(f"room: {room.name} - dispatch matterbridge message to bot") + self.logger.info(f"room: {room.name} protocol: {matter_message.protocol} user: {matter_message.username} - dispatch matterbridge message to bot") # dispatch. changing the body of the event triggers message_cb of the bot event.body = matter_message.message