From a76bba9de47615946e4a14c67a20c109ee06d235 Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Sat, 18 May 2024 08:33:40 +0200 Subject: [PATCH 1/2] Add an info message on mention for ModBot --- xpartamupp/modbot.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/xpartamupp/modbot.py b/xpartamupp/modbot.py index d4d98c7..4dc208f 100755 --- a/xpartamupp/modbot.py +++ b/xpartamupp/modbot.py @@ -27,7 +27,7 @@ ArgumentError, ArgumentParser, HelpFormatter, Namespace, _MutuallyExclusiveGroup) from asyncio import Future, Task -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone from typing import Iterable, Optional, Tuple import dateparser @@ -44,6 +44,10 @@ UnmuteEvent) from xpartamupp.utils import ArgumentParserWithConfigFile +# Number of seconds to not respond to mentions after having responded +# to a mention. +INFO_MSG_COOLDOWN_SECONDS = 120 + logger = logging.getLogger(__name__) @@ -232,9 +236,12 @@ def __init__(self, jid: JID, password: str, nick: str, rooms: list[JID], command self.unmute_tasks: dict[JID, asyncio.Task] = {} self.cmd_parser = get_cmd_parser() + self.last_info_msg = None + self.add_event_handler("session_start", self._session_start) for room in self.rooms: + self.add_event_handler(f"muc::{room}::message", self._muc_message) self.add_event_handler(f"muc::{room}::presence", self._muc_presence_change) self.add_event_handler(f"muc::{self.command_room}::message", self._muc_command_message) @@ -341,6 +348,34 @@ async def _muc_presence_change(self, presence: MUCPresence) -> None: except IqError: logger.exception("Unmuting %s (%s) on join failed.", nick, jid) + async def _muc_message(self, msg): + """Process messages in the MUC room. + + Respond to messages highlighting the bots name with an + informative message. After responding once, cool down before + responding again to avoid spamming info messages when mentioned + repeatedly. + + Arguments: + msg (slixmpp.stanza.message.Message): Received MUC message + """ + if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower(): + return + + if ( + self.last_info_msg and + self.last_info_msg + timedelta(seconds=INFO_MSG_COOLDOWN_SECONDS) > datetime.now( + tz=timezone.utc) + ): + return + + self.last_info_msg = datetime.now(tz=timezone.utc) + self.send_message(mto=msg['from'].bare, + mbody="I am just a bot and I'm here to monitor that you respect the " + "terms of use and interact with each other in a respectful " + "manner.", + mtype='groupchat') + async def _muc_command_message(self, msg: Message) -> None: """Process messages in the command MUC room. From f131d4b90a5aaee50f5b9ec8159cf3c6aa872ddc Mon Sep 17 00:00:00 2001 From: Daniel Roschka Date: Sat, 18 May 2024 08:33:44 +0200 Subject: [PATCH 2/2] Ignore historic chat messages When joining a MUC room, we receive the last messages written before joining the room. This adds code to ignore them, as we don't want the bots to respond to messages written before they joined a room. --- xpartamupp/echelon.py | 3 +++ xpartamupp/modbot.py | 3 +++ xpartamupp/xpartamupp.py | 3 +++ 3 files changed, 9 insertions(+) diff --git a/xpartamupp/echelon.py b/xpartamupp/echelon.py index 3362fe5..4f26879 100755 --- a/xpartamupp/echelon.py +++ b/xpartamupp/echelon.py @@ -630,6 +630,9 @@ def _muc_message(self, msg): msg (slixmpp.stanza.message.Message): Received MUC message """ + if msg["delay"]["stamp"]: + return + if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower(): return diff --git a/xpartamupp/modbot.py b/xpartamupp/modbot.py index 4dc208f..ac2dd5b 100755 --- a/xpartamupp/modbot.py +++ b/xpartamupp/modbot.py @@ -359,6 +359,9 @@ async def _muc_message(self, msg): Arguments: msg (slixmpp.stanza.message.Message): Received MUC message """ + if msg["delay"]["stamp"]: + return + if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower(): return diff --git a/xpartamupp/xpartamupp.py b/xpartamupp/xpartamupp.py index f6a5547..0e190ae 100755 --- a/xpartamupp/xpartamupp.py +++ b/xpartamupp/xpartamupp.py @@ -283,6 +283,9 @@ def _muc_message(self, msg): msg (slixmpp.stanza.message.Message): Received MUC message """ + if msg["delay"]["stamp"]: + return + if msg['mucnick'] == self.nick or self.nick.lower() not in msg['body'].lower(): return