diff --git a/matrix_utils.py b/matrix_utils.py index acf910c..e868845 100644 --- a/matrix_utils.py +++ b/matrix_utils.py @@ -46,11 +46,34 @@ matrix_client = None +def bot_command(command, event): + """ + Checks if the given command is directed at the bot, + accounting for variations in different Matrix clients. + """ + full_message = event.body.strip() + content = event.source.get("content", {}) + formatted_body = content.get("formatted_body", "") + + # Remove HTML tags and extract the text content + text_content = re.sub(r"<[^>]+>", "", formatted_body).strip() + + # Check if the message starts with bot_user_id or bot_user_name + if full_message.startswith(bot_user_id) or text_content.startswith(bot_user_id): + # Construct a regex pattern to match variations of bot mention and command + pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$" + return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) + elif full_message.startswith(bot_user_name) or text_content.startswith(bot_user_name): + # Construct a regex pattern to match variations of bot mention and command + pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$" + return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) + else: + return False + # # Construct a regex pattern to match variations of bot mention and command + # pattern = rf"^(?:{re.escape(bot_user_id)}|{re.escape(bot_user_name)}|[#@].+?)[,:;]?\s*!{command}$" -def bot_command(command, payload): - # Checks if the given command is directed at the bot - return f"{bot_user_name}: !{command}" in payload - + # # Check if the message matches the pattern + # return bool(re.match(pattern, full_message)) or bool(re.match(pattern, text_content)) async def connect_matrix(): """ @@ -470,7 +493,7 @@ async def on_room_message( is_command = False for plugin in plugins: for command in plugin.get_matrix_commands(): - if bot_command(command, text): + if bot_command(command, event): is_command = True break if is_command: diff --git a/plugins/base_plugin.py b/plugins/base_plugin.py index fdab5ca..b0c6f44 100644 --- a/plugins/base_plugin.py +++ b/plugins/base_plugin.py @@ -169,12 +169,11 @@ def get_node_data(self, meshtastic_id): def get_data(self): return get_plugin_data(self.plugin_name) - def matches(self, payload): + def matches(self, event): from matrix_utils import bot_command - if isinstance(payload, str): - return bot_command(self.plugin_name, payload) - return False + # Pass the entire event to bot_command + return bot_command(self.plugin_name, event) @abstractmethod async def handle_meshtastic_message( diff --git a/plugins/drop_plugin.py b/plugins/drop_plugin.py index 8c4694d..d040bfe 100644 --- a/plugins/drop_plugin.py +++ b/plugins/drop_plugin.py @@ -105,5 +105,6 @@ async def handle_meshtastic_message( return True async def handle_room_message(self, room, event, full_message): - if self.matches(full_message): + # Pass the event to matches() instead of full_message + if self.matches(event): return True diff --git a/plugins/health_plugin.py b/plugins/health_plugin.py index cc0b989..4d8fbfa 100644 --- a/plugins/health_plugin.py +++ b/plugins/health_plugin.py @@ -50,8 +50,7 @@ async def handle_meshtastic_message( async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - if not self.matches(full_message): + if not self.matches(event): return False await self.send_matrix_message( diff --git a/plugins/help_plugin.py b/plugins/help_plugin.py index c029b92..e391af1 100644 --- a/plugins/help_plugin.py +++ b/plugins/help_plugin.py @@ -23,8 +23,8 @@ def get_mesh_commands(self): return [] async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - if not self.matches(full_message): + # Pass the event to matches() + if not self.matches(event): return False command = None diff --git a/plugins/map_plugin.py b/plugins/map_plugin.py index c2950fd..ee76bd8 100644 --- a/plugins/map_plugin.py +++ b/plugins/map_plugin.py @@ -263,8 +263,8 @@ def get_mesh_commands(self): return [] async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - if not self.matches(full_message): + # Pass the event to matches() + if not self.matches(event): return False from matrix_utils import connect_matrix diff --git a/plugins/mesh_relay_plugin.py b/plugins/mesh_relay_plugin.py index aa0f756..e4e8b45 100644 --- a/plugins/mesh_relay_plugin.py +++ b/plugins/mesh_relay_plugin.py @@ -81,16 +81,19 @@ async def handle_meshtastic_message( return False - def matches(self, payload): - if isinstance(payload, str): - match = re.match(r"^Processed (.+) radio packet$", payload) - return match + def matches(self, event): + # Check for the presence of necessary keys in the event + content = event.source.get("content", {}) + body = content.get("body", "") + + if isinstance(body, str): + match = re.match(r"^Processed (.+) radio packet$", body) + return bool(match) return False async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - - if not self.matches(full_message): + # Use the event for matching instead of full_message + if not self.matches(event): return False channel = None diff --git a/plugins/nodes_plugin.py b/plugins/nodes_plugin.py index b7ef395..4c080a3 100644 --- a/plugins/nodes_plugin.py +++ b/plugins/nodes_plugin.py @@ -81,9 +81,8 @@ async def handle_meshtastic_message( return False async def handle_room_message(self, room, event, full_message): - - full_message = full_message.strip() - if not self.matches(full_message): + # Pass the event to matches() + if not self.matches(event): return False await self.send_matrix_message( diff --git a/plugins/ping_plugin.py b/plugins/ping_plugin.py index 07da344..998656a 100644 --- a/plugins/ping_plugin.py +++ b/plugins/ping_plugin.py @@ -104,8 +104,8 @@ def get_mesh_commands(self): return [self.plugin_name] async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - if not self.matches(full_message): + # Pass the event to matches() + if not self.matches(event): return False await self.send_matrix_message(room.room_id, "pong!") diff --git a/plugins/telemetry_plugin.py b/plugins/telemetry_plugin.py index 1a26e99..73b9595 100644 --- a/plugins/telemetry_plugin.py +++ b/plugins/telemetry_plugin.py @@ -78,18 +78,18 @@ def get_matrix_commands(self): def get_mesh_commands(self): return [] - def matches(self, payload): + def matches(self, event): from matrix_utils import bot_command - if isinstance(payload, str): - for option in ["batteryLevel", "voltage", "airUtilTx"]: - if bot_command(option, payload): - return True + # Use bot_command() to check if any of the commands match + for command in self.get_matrix_commands(): + if bot_command(command, event): + return True return False async def handle_room_message(self, room, event, full_message): - full_message = full_message.strip() - if not self.matches(full_message): + # Pass the event to matches() + if not self.matches(event): return False match = re.search(