From 74c5ba70878c59fffa4a0704985afefd9738a67f Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 4 Mar 2024 14:34:47 +0300 Subject: [PATCH] Add td_api::checkQuickReplyShortcutName. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/QuickReplyManager.cpp | 39 +++++++++++++++++++++++++++++++ td/telegram/QuickReplyManager.h | 5 ++++ td/telegram/Td.cpp | 15 ++++++++++++ td/telegram/Td.h | 3 +++ td/telegram/cli.cpp | 2 ++ 6 files changed, 67 insertions(+) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 6d52058e6..3eef4e32a 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7780,6 +7780,9 @@ editInlineMessageReplyMarkup inline_message_id:string reply_markup:ReplyMarkup = editMessageSchedulingState chat_id:int53 message_id:int53 scheduling_state:MessageSchedulingState = Ok; +//@description Checks validness of a name for a quick reply shortcut. Can be called synchronously @name The name of the shortcut; 1-32 characters +checkQuickReplyShortcutName name:string = Ok; + //@description Loads quick reply shortcuts created by the current user. The loaded topics will be sent through updateQuickReplyShortcuts loadQuickReplyShortcuts = Ok; diff --git a/td/telegram/QuickReplyManager.cpp b/td/telegram/QuickReplyManager.cpp index e0d1c3778..6fe763355 100644 --- a/td/telegram/QuickReplyManager.cpp +++ b/td/telegram/QuickReplyManager.cpp @@ -27,6 +27,8 @@ #include "td/utils/buffer.h" #include "td/utils/format.h" #include "td/utils/logging.h" +#include "td/utils/unicode.h" +#include "td/utils/utf8.h" namespace td { @@ -375,6 +377,43 @@ void QuickReplyManager::tear_down() { parent_.reset(); } +bool QuickReplyManager::is_shortcut_name_letter(uint32 code) { + auto category = get_unicode_simple_category(code); + if (code == '_' || code == 0x200c || code == 0xb7 || (0xd80 <= code && code <= 0xdff)) { + return true; + } + switch (category) { + case UnicodeSimpleCategory::DecimalNumber: + case UnicodeSimpleCategory::Letter: + return true; + default: + return false; + } +} + +Status QuickReplyManager::check_shortcut_name(CSlice name) { + if (!check_utf8(name)) { + return Status::Error("Strings must be encoded in UTF-8"); + } + int32 length = 0; + auto *ptr = name.ubegin(); + while (ptr != name.uend()) { + uint32 code; + ptr = next_utf8_unsafe(ptr, &code); + if (!is_shortcut_name_letter(code)) { + return Status::Error("A letter is not allowed"); + } + length++; + } + if (length == 0) { + return Status::Error("Name can't be empty"); + } + if (length > 32) { + return Status::Error("Name is too long"); + } + return Status::OK(); +} + unique_ptr QuickReplyManager::create_message( telegram_api::object_ptr message_ptr, const char *source) const { LOG(DEBUG) << "Receive from " << source << " " << to_string(message_ptr); diff --git a/td/telegram/QuickReplyManager.h b/td/telegram/QuickReplyManager.h index 0e49de1cb..4fa80f7a4 100644 --- a/td/telegram/QuickReplyManager.h +++ b/td/telegram/QuickReplyManager.h @@ -21,6 +21,7 @@ #include "td/utils/FlatHashMap.h" #include "td/utils/FlatHashSet.h" #include "td/utils/Promise.h" +#include "td/utils/Status.h" #include @@ -34,6 +35,8 @@ class QuickReplyManager final : public Actor { public: QuickReplyManager(Td *td, ActorShared<> parent); + static Status check_shortcut_name(CSlice name); + void get_quick_reply_shortcuts(Promise &&promise); void delete_quick_reply_shortcut(QuickReplyShortcutId shortcut_id, Promise &&promise); @@ -145,6 +148,8 @@ class QuickReplyManager final : public Actor { void tear_down() final; + static bool is_shortcut_name_letter(uint32 code); + void add_quick_reply_message_dependencies(Dependencies &dependencies, const QuickReplyMessage *m) const; unique_ptr create_message(telegram_api::object_ptr message_ptr, diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index acd77ccb8..e7f93ab7c 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -2780,6 +2780,7 @@ bool Td::is_synchronous_request(const td_api::Function *function) { case td_api::parseMarkdown::ID: case td_api::getMarkdownText::ID: case td_api::searchStringsByPrefix::ID: + case td_api::checkQuickReplyShortcutName::ID: case td_api::getCountryFlagEmoji::ID: case td_api::getFileMimeType::ID: case td_api::getFileExtension::ID: @@ -3022,6 +3023,7 @@ td_api::object_ptr Td::static_request(td_api::object_ptr Td::do_static_request(td_api::searchStringsBy return td_api::make_object(total_count, std::move(result)); } +td_api::object_ptr Td::do_static_request(const td_api::checkQuickReplyShortcutName &request) { + // don't check name UTF-8 correctness + auto status = QuickReplyManager::check_shortcut_name(request.name_); + if (status.is_ok()) { + return td_api::make_object(); + } + return make_error(200, status.error().message()); +} + td_api::object_ptr Td::do_static_request(const td_api::getCountryFlagEmoji &request) { // don't check country code UTF-8 correctness return td_api::make_object(CountryInfoManager::get_country_flag_emoji(request.country_code_)); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index b431bb4f2..b2b80777b 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -1802,6 +1802,8 @@ class Td final : public Actor { void on_request(uint64 id, const td_api::searchStringsByPrefix &request); + void on_request(uint64 id, const td_api::checkQuickReplyShortcutName &request); + void on_request(uint64 id, const td_api::getCountryFlagEmoji &request); void on_request(uint64 id, const td_api::getFileMimeType &request); @@ -1866,6 +1868,7 @@ class Td final : public Actor { static td_api::object_ptr do_static_request(td_api::parseMarkdown &request); static td_api::object_ptr do_static_request(td_api::getMarkdownText &request); static td_api::object_ptr do_static_request(td_api::searchStringsByPrefix &request); + static td_api::object_ptr do_static_request(const td_api::checkQuickReplyShortcutName &request); static td_api::object_ptr do_static_request(const td_api::getCountryFlagEmoji &request); static td_api::object_ptr do_static_request(const td_api::getFileMimeType &request); static td_api::object_ptr do_static_request(const td_api::getFileExtension &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 3325830ff..065e960f7 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -4845,6 +4845,8 @@ class CliClient final : public Actor { get_args(args, chat_id, message_id, date); send_request(td_api::make_object(chat_id, message_id, as_message_scheduling_state(date))); + } else if (op == "cqrsn") { + execute(td_api::make_object(args)); } else if (op == "lqrs") { send_request(td_api::make_object()); } else if (op == "dqrs") {