From e59573073508be15ea40ac07588348f1aa7b0229 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 2 Sep 2022 16:34:05 +0300 Subject: [PATCH] Support t.me subdomains. --- td/telegram/LinkManager.cpp | 24 ++++++++++++++++++------ test/link.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index 6886df6ec..20f644252 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -885,6 +885,24 @@ LinkManager::LinkInfo LinkManager::get_link_info(Slice link) { return result; } + auto host = url_decode(http_url.host_, false); + to_lower_inplace(host); + if (ends_with(host, ".t.me") && host.size() >= 9 && host.find('.') == host.size() - 5) { + Slice subdomain(&host[0], host.size() - 5); + if (is_valid_username(subdomain) && subdomain != "addemoji" && subdomain != "addstickers" && + subdomain != "addtheme" && subdomain != "auth" && subdomain != "confirmphone" && subdomain != "invoice" && + subdomain != "joinchat" && subdomain != "login" && subdomain != "proxy" && subdomain != "setlanguage" && + subdomain != "share" && subdomain != "socks") { + result.is_internal_ = true; + result.is_tg_ = false; + result.query_ = PSTRING() << '/' << subdomain << http_url.query_; + return result; + } + } + if (begins_with(host, "www.")) { + host = host.substr(4); + } + vector t_me_urls{Slice("t.me"), Slice("telegram.me"), Slice("telegram.dog")}; if (Scheduler::context() != nullptr) { // for tests only string cur_t_me_url = G()->get_option_string("t_me_url"); @@ -897,12 +915,6 @@ LinkManager::LinkInfo LinkManager::get_link_info(Slice link) { } } - auto host = url_decode(http_url.host_, false); - to_lower_inplace(host); - if (begins_with(host, "www.")) { - host = host.substr(4); - } - for (auto t_me_url : t_me_urls) { if (host == t_me_url) { result.is_internal_ = true; diff --git a/test/link.cpp b/test/link.cpp index eab877481..b81774f31 100644 --- a/test/link.cpp +++ b/test/link.cpp @@ -921,4 +921,29 @@ TEST(Link, parse_internal_link) { parse_internal_link("tg://settings/filters", settings()); parse_internal_link("tg://settings/language", language_settings()); parse_internal_link("tg://settings/privacy", privacy_and_security_settings()); + + parse_internal_link("username.t.me////0/a//s/as?start=", bot_start("username", "")); + parse_internal_link("username.t.me?start=as", bot_start("username", "as")); + parse_internal_link("username.t.me", public_chat("username")); + parse_internal_link("aAAb.t.me/12345?single", message("tg:resolve?domain=aaab&post=12345&single")); + parse_internal_link("telegram.t.me/195", message("tg:resolve?domain=telegram&post=195")); + parse_internal_link("shares.t.me", public_chat("shares")); + + parse_internal_link("c.t.me/12345?single", nullptr); + parse_internal_link("aaa.t.me/12345?single", nullptr); + parse_internal_link("aaa_.t.me/12345?single", nullptr); + parse_internal_link("0aaa.t.me/12345?single", nullptr); + parse_internal_link("_aaa.t.me/12345?single", nullptr); + parse_internal_link("addemoji.t.me", nullptr); + parse_internal_link("addstickers.t.me", nullptr); + parse_internal_link("addtheme.t.me", nullptr); + parse_internal_link("auth.t.me", nullptr); + parse_internal_link("confirmphone.t.me", nullptr); + parse_internal_link("invoice.t.me", nullptr); + parse_internal_link("joinchat.t.me", nullptr); + parse_internal_link("login.t.me", nullptr); + parse_internal_link("proxy.t.me", nullptr); + parse_internal_link("setlanguage.t.me", nullptr); + parse_internal_link("share.t.me", nullptr); + parse_internal_link("socks.t.me", nullptr); }