From bf3c88d47a41b0349161d4dbf09c97e64eb4bd26 Mon Sep 17 00:00:00 2001 From: missytake Date: Sat, 7 Oct 2023 17:20:58 +0200 Subject: [PATCH] forwarding messages from outsiders to a relay group --- src/teams_bot/bot.py | 60 ++++++++++++++++++++++++++++++++++++++------ tests/conftest.py | 9 +++++++ tests/test_bot.py | 30 ++++++++++++++-------- 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/src/teams_bot/bot.py b/src/teams_bot/bot.py index 995bb6c..1256c42 100644 --- a/src/teams_bot/bot.py +++ b/src/teams_bot/bot.py @@ -46,30 +46,76 @@ class RelayPlugin: if message.chat.id == get_crew_id(self.account): if message.text.startswith("/"): - logging.debug("handling command by %s: %s", message.get_sender_contact().addr, message.text) + logging.debug( + "handling command by %s: %s", + message.get_sender_contact().addr, + message.text, + ) """:TODO handle command""" else: logging.debug("Ignoring message, just admins chatting") elif self.is_relay_group(message.chat): - if message.quote.get_sender_contact() == self.account.get_self_contact(): - """:TODO forward to original sender""" + if hasattr(message, "quote"): + if ( + message.quote.get_sender_contact() + == self.account.get_self_contact() + ): + logging.debug("Forwarding message to outsider") + """:TODO forward to outsider""" + else: + logging.debug("Ignoring message, just admins chatting") else: logging.debug("Ignoring message, just admins chatting") else: logging.debug("Forwarding message to relay group") - """:TODO forward message to relay group""" + self.forward_to_relay_group(message) + + def forward_to_relay_group(self, message: deltachat.Message): + """forward a request to a relay group; create one if it doesn't exist yet.""" + outsider = message.get_sender_contact().addr + crew_members = self.account.get_chat_by_id( + get_crew_id(self.account) + ).get_contacts() + crew_members.remove(self.account.get_self_contact()) + group_name = "[%s] %s" % ( + self.account.get_config("addr").split("@")[0], + message.chat.get_name(), + ) + for chat in self.account.get_chats(): + if chat.get_name() == group_name: + relay_group = chat + break + else: + logging.info("creating new relay group: '%s'", group_name) + relay_group = self.account.create_group_chat( + group_name, crew_members, verified=False + ) + # relay_group.set_profile_image("assets/avatar.jpg") + relay_group.send_text( + "This is the relay group for %s; I'll only forward 'direct replies' to the outside." + % (message.chat.get_name()) + ) + message.set_override_sender_name(outsider) + relay_group.send_msg(message) def is_relay_group(self, chat: deltachat.Chat) -> bool: """Check whether a chat is a relay group.""" - if not chat.get_name().startswith("[%s] " % (self.account.get_config("addr").split("@")[0],)): + if not chat.get_name().startswith( + "[%s] " % (self.account.get_config("addr").split("@")[0],) + ): return False # all relay groups' names begin with a [tag] with the localpart of the teamsbot's address - if chat.get_messages()[0].get_sender_contact() != self.account.get_self_contact(): + if ( + chat.get_messages()[0].get_sender_contact() + != self.account.get_self_contact() + ): return False # all relay groups were started by the teamsbot if chat.is_protected(): return False # relay groups don't need to be protected, so they are not - for crew_member in self.account.get_chat_by_id(get_crew_id(self.account)).get_contacts(): + for crew_member in self.account.get_chat_by_id( + get_crew_id(self.account) + ).get_contacts(): if crew_member not in chat.get_contacts(): return False # all crew members have to be in any relay group return True diff --git a/tests/conftest.py b/tests/conftest.py index e13bab9..1764d2b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -90,6 +90,15 @@ def crew(teams_bot, teams_user): crew.user = teams_user crew.bot = teams_bot crew.bot.setupplugin = setupplugin + + # wait until old user is properly added to crew + last_message = teams_user.wait_next_incoming_message().text + while ( + f"Member Me ({teams_user.get_config('addr')}) added by bot" not in last_message + ): + print("User received message:", last_message) + last_message = teams_user.wait_next_incoming_message().text + yield crew diff --git a/tests/test_bot.py b/tests/test_bot.py index 7c0843a..0e4f2a4 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -10,14 +10,6 @@ def test_disable_old_crew(crew, outsider): """Test if crew is properly disabled if someone else creates a new crew on the command line.""" old_crew_id = get_crew_id(crew.bot) - # wait until old user is properly added to crew - last_message = crew.user.wait_next_incoming_message().text - while ( - f"Member Me ({crew.user.get_config('addr')}) added by bot" not in last_message - ): - print("User received message:", last_message) - last_message = crew.user.wait_next_incoming_message().text - # outsider fires up the command line and creates a new crew new_crew = crew.bot.create_group_chat( f"Team: {crew.bot.get_config('addr')}", verified=True @@ -46,6 +38,7 @@ def test_disable_old_crew(crew, outsider): def test_is_relay_group(crew, outsider): crew.bot.relayplugin = RelayPlugin(crew.bot) + crew.bot.add_account_plugin(crew.bot.relayplugin) assert not crew.bot.relayplugin.is_relay_group(crew) botcontact_outsider = outsider.create_contact(crew.bot.get_config("addr")) @@ -54,6 +47,25 @@ def test_is_relay_group(crew, outsider): message_from_outsider = crew.bot.wait_next_incoming_message() assert not crew.bot.relayplugin.is_relay_group(message_from_outsider.chat) + crew.user.wait_next_incoming_message() # group added message + forwarded_message_from_outsider = crew.user.wait_next_incoming_message() + user_relay_group = forwarded_message_from_outsider.create_chat() + user_relay_group.send_text("Harmless reply in relay group") + message_in_relay_group = crew.bot.wait_next_incoming_message() + assert message_in_relay_group.chat.get_name().startswith( + "[%s] " % (crew.bot.get_config("addr").split("@")[0],) + ) + assert ( + message_in_relay_group.chat.get_messages()[0].get_sender_contact() + == crew.bot.get_self_contact() + ) + assert not message_in_relay_group.chat.is_protected() + assert ( + crew.bot.get_chat_by_id(get_crew_id(crew.bot)).get_contacts() + == message_in_relay_group.chat.get_contacts() + ) + assert crew.bot.relayplugin.is_relay_group(message_in_relay_group.chat) + outsider_to_bot = outsider.create_group_chat( "test with outsider", contacts=[botcontact_outsider] ) @@ -73,5 +85,3 @@ def test_is_relay_group(crew, outsider): user_group.send_text("testing message to user group") message_from_user = crew.bot.wait_next_incoming_message() assert not crew.bot.relayplugin.is_relay_group(message_from_user.chat) - - # get example from actual relay group as soon as it works