forwarding messages from outsiders to a relay group

This commit is contained in:
missytake 2023-10-07 17:20:58 +02:00
parent 1f0f520c9e
commit bf3c88d47a
3 changed files with 82 additions and 17 deletions

View file

@ -46,30 +46,76 @@ class RelayPlugin:
if message.chat.id == get_crew_id(self.account): if message.chat.id == get_crew_id(self.account):
if message.text.startswith("/"): 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""" """:TODO handle command"""
else: else:
logging.debug("Ignoring message, just admins chatting") logging.debug("Ignoring message, just admins chatting")
elif self.is_relay_group(message.chat): elif self.is_relay_group(message.chat):
if message.quote.get_sender_contact() == self.account.get_self_contact(): if hasattr(message, "quote"):
""":TODO forward to original sender""" 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: else:
logging.debug("Ignoring message, just admins chatting") logging.debug("Ignoring message, just admins chatting")
else: else:
logging.debug("Forwarding message to relay group") 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: def is_relay_group(self, chat: deltachat.Chat) -> bool:
"""Check whether a chat is a relay group.""" """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 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 return False # all relay groups were started by the teamsbot
if chat.is_protected(): if chat.is_protected():
return False # relay groups don't need to be protected, so they are not 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(): if crew_member not in chat.get_contacts():
return False # all crew members have to be in any relay group return False # all crew members have to be in any relay group
return True return True

View file

@ -90,6 +90,15 @@ def crew(teams_bot, teams_user):
crew.user = teams_user crew.user = teams_user
crew.bot = teams_bot crew.bot = teams_bot
crew.bot.setupplugin = setupplugin 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 yield crew

View file

@ -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.""" """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) 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 # outsider fires up the command line and creates a new crew
new_crew = crew.bot.create_group_chat( new_crew = crew.bot.create_group_chat(
f"Team: {crew.bot.get_config('addr')}", verified=True 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): def test_is_relay_group(crew, outsider):
crew.bot.relayplugin = RelayPlugin(crew.bot) crew.bot.relayplugin = RelayPlugin(crew.bot)
crew.bot.add_account_plugin(crew.bot.relayplugin)
assert not crew.bot.relayplugin.is_relay_group(crew) assert not crew.bot.relayplugin.is_relay_group(crew)
botcontact_outsider = outsider.create_contact(crew.bot.get_config("addr")) 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() message_from_outsider = crew.bot.wait_next_incoming_message()
assert not crew.bot.relayplugin.is_relay_group(message_from_outsider.chat) 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( outsider_to_bot = outsider.create_group_chat(
"test with outsider", contacts=[botcontact_outsider] "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") user_group.send_text("testing message to user group")
message_from_user = crew.bot.wait_next_incoming_message() message_from_user = crew.bot.wait_next_incoming_message()
assert not crew.bot.relayplugin.is_relay_group(message_from_user.chat) assert not crew.bot.relayplugin.is_relay_group(message_from_user.chat)
# get example from actual relay group as soon as it works