From 3d403c2471f38d93fd89fad78e714727242a454b Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 19 Dec 2020 13:15:27 +0200 Subject: [PATCH] Add option to resolve redirects in invite links. Fixes #559 --- mautrix_telegram/commands/telegram/misc.py | 12 +++++++++++- mautrix_telegram/config.py | 1 + mautrix_telegram/example-config.yaml | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/mautrix_telegram/commands/telegram/misc.py b/mautrix_telegram/commands/telegram/misc.py index 976fdf17..0060619c 100644 --- a/mautrix_telegram/commands/telegram/misc.py +++ b/mautrix_telegram/commands/telegram/misc.py @@ -18,6 +18,8 @@ import codecs import base64 import re +from aiohttp import ClientSession, InvalidURL + from telethon.errors import (InviteHashInvalidError, InviteHashExpiredError, OptionsTooMuchError, UserAlreadyParticipantError, ChatIdInvalidError, TakeoutInitDelayError, EmoticonInvalidError) @@ -145,9 +147,17 @@ async def join(evt: CommandEvent) -> Optional[EventID]: if len(evt.args) == 0: return await evt.reply("**Usage:** `$cmdprefix+sp join `") + url = evt.args[0] + if evt.config["bridge.invite_link_resolve"]: + try: + async with ClientSession() as sess, sess.get(url) as resp: + url = resp.url + except InvalidURL: + return await evt.reply("That doesn't look like a Telegram invite link.") + regex = re.compile(r"(?:https?://)?t(?:elegram)?\.(?:dog|me)" r"(?:/(?Pjoinchat|s))?/(?P[^/]+)/?", flags=re.IGNORECASE) - arg = regex.match(evt.args[0]) + arg = regex.match(url) if not arg: return await evt.reply("That doesn't look like a Telegram invite link.") diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 3613a374..f2a5f97c 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -114,6 +114,7 @@ class Config(BaseBridgeConfig): else: copy("bridge.login_shared_secret_map") copy("bridge.telegram_link_preview") + copy("bridge.invite_link_resolve") copy("bridge.inline_images") copy("bridge.image_as_file_size") copy("bridge.max_document_size") diff --git a/mautrix_telegram/example-config.yaml b/mautrix_telegram/example-config.yaml index 4401afd8..f73b4733 100644 --- a/mautrix_telegram/example-config.yaml +++ b/mautrix_telegram/example-config.yaml @@ -194,6 +194,9 @@ bridge: example.com: foobar # Set to false to disable link previews in messages sent to Telegram. telegram_link_preview: true + # Whether or not the !tg join command should do a HTTP request + # to resolve redirects in invite links. + invite_link_resolve: false # Use inline images instead of a separate message for the caption. # N.B. Inline images are not supported on all clients (e.g. Riot iOS). inline_images: false