Move is_valid_username to misc.h.
This commit is contained in:
parent
ee28a37906
commit
c1a3fa633f
@ -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<td_api::CheckChatUsernameResult> 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<Unit> &&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<UpdateUsernameQuery>(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"));
|
||||
}
|
||||
|
||||
|
@ -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<Unit> &&promise);
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<uint64> &numbers) {
|
||||
uint64 acc = 0;
|
||||
for (auto number : numbers) {
|
||||
|
@ -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<uint64> &numbers) TD_WARN_UNUSED_RESULT;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user