diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index eb94309d3..0c00ba433 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -5353,7 +5353,7 @@ void ContactsManager::check_dialog_username(DialogId dialog_id, const string &us if (username.empty()) { return promise.set_value(CheckDialogUsernameResult::Ok); } - if (!is_valid_username(username)) { + if (!is_allowed_username(username)) { return promise.set_value(CheckDialogUsernameResult::Invalid); } @@ -5409,29 +5409,18 @@ td_api::object_ptr ContactsManager::get_check_c } } -bool ContactsManager::is_valid_username(const string &username) { - if (username.size() < 5 || username.size() > 32) { +bool ContactsManager::is_allowed_username(const string &username) { + if (!is_valid_username(username)) { return false; } - if (!is_alpha(username[0])) { + if (username.size() < 5) { return false; } - for (auto c : username) { - if (!is_alpha(c) && !is_digit(c) && c != '_') { - return false; - } - } - if (username.back() == '_') { - return false; - } - for (size_t i = 1; i < username.size(); i++) { - if (username[i - 1] == '_' && username[i] == '_') { - return false; - } - } - if (username.find("admin") == 0 || username.find("telegram") == 0 || username.find("support") == 0 || - username.find("security") == 0 || username.find("settings") == 0 || username.find("contacts") == 0 || - username.find("service") == 0 || username.find("telegraph") == 0) { + auto username_lowered = to_lower(username); + if (username_lowered.find("admin") == 0 || username_lowered.find("telegram") == 0 || + username_lowered.find("support") == 0 || username_lowered.find("security") == 0 || + username_lowered.find("settings") == 0 || username_lowered.find("contacts") == 0 || + username_lowered.find("service") == 0 || username_lowered.find("telegraph") == 0) { return false; } return true; @@ -6652,7 +6641,7 @@ void ContactsManager::on_update_profile_success(int32 flags, const string &first } void ContactsManager::set_username(const string &username, Promise &&promise) { - if (!username.empty() && !is_valid_username(username)) { + if (!username.empty() && !is_allowed_username(username)) { return promise.set_error(Status::Error(400, "Username is invalid")); } td_->create_handler(std::move(promise))->send(username); @@ -6706,7 +6695,7 @@ void ContactsManager::set_channel_username(ChannelId channel_id, const string &u return promise.set_error(Status::Error(400, "Not enough rights to change supergroup username")); } - if (!username.empty() && !is_valid_username(username)) { + if (!username.empty() && !is_allowed_username(username)) { return promise.set_error(Status::Error(400, "Username is invalid")); } diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 83875a9be..885e445fd 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -1258,7 +1258,7 @@ class ContactsManager final : public Actor { void set_my_id(UserId my_id); - static bool is_valid_username(const string &username); + static bool is_allowed_username(const string &username); void on_set_emoji_status(EmojiStatus emoji_status, Promise &&promise); diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index ee021d33a..e7606ef5e 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -45,30 +45,6 @@ static bool is_valid_start_parameter(Slice start_parameter) { return start_parameter.size() <= 64 && is_base64url_characters(start_parameter); } -static bool is_valid_username(Slice username) { - if (username.empty() || username.size() > 32) { - return false; - } - if (!is_alpha(username[0])) { - return false; - } - for (auto c : username) { - if (!is_alpha(c) && !is_digit(c) && c != '_') { - return false; - } - } - if (username.back() == '_') { - return false; - } - for (size_t i = 1; i < username.size(); i++) { - if (username[i - 1] == '_' && username[i] == '_') { - return false; - } - } - - return true; -} - static bool is_valid_phone_number(Slice phone_number) { if (phone_number.empty() || phone_number.size() > 32) { return false; diff --git a/td/telegram/misc.cpp b/td/telegram/misc.cpp index cdd15ba79..7983ed64a 100644 --- a/td/telegram/misc.cpp +++ b/td/telegram/misc.cpp @@ -243,6 +243,30 @@ bool is_empty_string(const string &str) { return strip_empty_characters(str, str.size()).empty(); } +bool is_valid_username(Slice username) { + if (username.empty() || username.size() > 32) { + return false; + } + if (!is_alpha(username[0])) { + return false; + } + for (auto c : username) { + if (!is_alpha(c) && !is_digit(c) && c != '_') { + return false; + } + } + if (username.back() == '_') { + return false; + } + for (size_t i = 1; i < username.size(); i++) { + if (username[i - 1] == '_' && username[i] == '_') { + return false; + } + } + + return true; +} + int64 get_vector_hash(const vector &numbers) { uint64 acc = 0; for (auto number : numbers) { diff --git a/td/telegram/misc.h b/td/telegram/misc.h index b56bbf41b..934338f6d 100644 --- a/td/telegram/misc.h +++ b/td/telegram/misc.h @@ -7,6 +7,7 @@ #pragma once #include "td/utils/common.h" +#include "td/utils/Slice.h" namespace td { @@ -31,6 +32,9 @@ string strip_empty_characters(string str, size_t max_length, bool strip_rtlo = f // checks if string is empty after strip_empty_characters bool is_empty_string(const string &str) TD_WARN_UNUSED_RESULT; +// checks whether a string could be a valid username +bool is_valid_username(Slice username); + // calculates hash of list of uint64 int64 get_vector_hash(const vector &numbers) TD_WARN_UNUSED_RESULT;