From 14b105e74fbc8b92e521c3babba97a9d7e86ad0a Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 5 Aug 2018 20:11:07 +0300 Subject: [PATCH] Never bridge messages from the relay bot. Fixes #202 --- example-config.yaml | 2 ++ mautrix_telegram/abstract_user.py | 9 ++++++++- mautrix_telegram/config.py | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/example-config.yaml b/example-config.yaml index 4f0aa3b1..9c32844a 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -196,6 +196,8 @@ bridge: authless_portals: true # Whether or not to allow Telegram group admins to use the bot commands. whitelist_group_admins: true + # Whether or not to ignore incoming events sent by the relay bot. + ignore_own_incoming_events: true # List of usernames/user IDs who are also allowed to use the bot commands. whitelist: - myusername diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index 49632378..249a8d77 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -40,6 +40,7 @@ from .tgclient import MautrixTelegramClient if TYPE_CHECKING: from .context import Context from .config import Config + from .bot import Bot config = None # type: Config # Value updated from config in init() @@ -56,6 +57,8 @@ class AbstractUser(ABC): log = None # type: logging.Logger db = None # type: orm.Session az = None # type: AppService + bot = None # type: Bot + ignore_incoming_bot_events = True # type: bool def __init__(self): self.puppet_whitelisted = False # type: bool @@ -338,6 +341,9 @@ class AbstractUser(ABC): async def update_message(self, original_update: UpdateMessage): update, sender, portal = self.get_message_details(original_update) + if self.ignore_incoming_bot_events and self.bot and sender.id == self.bot.tgid: + self.log.debug(f"Ignoring relaybot-sent message %s to %s", update, portal.tgid_log) + return if isinstance(update, MessageService): if isinstance(update.action, MessageActionChannelMigrateFrom): @@ -364,6 +370,7 @@ class AbstractUser(ABC): def init(context: "Context"): global config, MAX_DELETIONS - AbstractUser.az, AbstractUser.db, config, AbstractUser.loop, _ = context + AbstractUser.az, AbstractUser.db, config, AbstractUser.loop, AbstractUser.relaybot = context + AbstractUser.ignore_incoming_bot_events = config["bridge.relaybot.ignore_own_incoming_events"] AbstractUser.session_container = context.session_container MAX_DELETIONS = config.get("bridge.max_telegram_delete", 10) diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 72e61f27..8a68b2f8 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -225,6 +225,7 @@ class Config(DictWithRecursion): copy("bridge.relaybot.authless_portals") copy("bridge.relaybot.whitelist_group_admins") copy("bridge.relaybot.whitelist") + copy("bridge.relaybot.ignore_own_incoming_events") copy("telegram.api_id") copy("telegram.api_hash")