Skip to content

Commit

Permalink
Add option to resolve redirects in invite links. Fixes #559
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 19, 2020
1 parent b22e3d2 commit 3d403c2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mautrix_telegram/commands/telegram/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import base64
import re

from aiohttp import ClientSession, InvalidURL

from telethon.errors import (InviteHashInvalidError, InviteHashExpiredError, OptionsTooMuchError,
UserAlreadyParticipantError, ChatIdInvalidError,
TakeoutInitDelayError, EmoticonInvalidError)
Expand Down Expand Up @@ -145,9 +147,17 @@ async def join(evt: CommandEvent) -> Optional[EventID]:
if len(evt.args) == 0:
return await evt.reply("**Usage:** `$cmdprefix+sp join <invite link>`")

url = evt.args[0]
if evt.config["bridge.invite_link_resolve"]:
try:
async with ClientSession() as sess, sess.get(url) as resp:

This comment has been minimized.

Copy link
@JuniorJPDJ

JuniorJPDJ Dec 19, 2020

shouldn't it be HEAD request instead of GET?

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"(?:/(?P<type>joinchat|s))?/(?P<id>[^/]+)/?", 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.")

Expand Down
1 change: 1 addition & 0 deletions mautrix_telegram/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
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")
Expand Down
3 changes: 3 additions & 0 deletions mautrix_telegram/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3d403c2

Please sign in to comment.