From 340312e3da200dedfb969eaaa2ab1a13343e35b6 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 28 Apr 2023 14:44:35 +0300 Subject: [PATCH] Add messageSourceScreenshot and notify about screenshotted codes. --- td/generate/scheme/td_api.tl | 3 +++ td/telegram/Account.cpp | 24 ++++++++++++++++++ td/telegram/Account.h | 2 ++ td/telegram/MessageSource.cpp | 4 +++ td/telegram/MessageSource.h | 1 + td/telegram/MessagesManager.cpp | 43 +++++++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 2 ++ td/telegram/cli.cpp | 4 ++- 8 files changed, 82 insertions(+), 1 deletion(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 51fd07545..0df862c53 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -1217,6 +1217,9 @@ messageSourceChatEventLog = MessageSource; //@description The message is from a notification messageSourceNotification = MessageSource; +//@description The message was screenshotted; the source must be used only if the message content was visible during the screenshot +messageSourceScreenshot = MessageSource; + //@description The message is from some other source messageSourceOther = MessageSource; diff --git a/td/telegram/Account.cpp b/td/telegram/Account.cpp index f7f9c4bd6..7287848ec 100644 --- a/td/telegram/Account.cpp +++ b/td/telegram/Account.cpp @@ -582,6 +582,26 @@ class ImportContactTokenQuery final : public Td::ResultHandler { } }; +class InvalidateSignInCodesQuery final : public Td::ResultHandler { + public: + void send(vector &&codes) { + send_query(G()->net_query_creator().create(telegram_api::account_invalidateSignInCodes(std::move(codes)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + LOG(DEBUG) << "Receive result for InvalidateSignInCodesQuery: " << result_ptr.ok(); + } + + void on_error(Status status) final { + LOG(DEBUG) << "Receive error for InvalidateSignInCodesQuery: " << status; + } +}; + void set_default_message_ttl(Td *td, int32 message_ttl, Promise &&promise) { td->create_handler(std::move(promise))->send(message_ttl); } @@ -658,4 +678,8 @@ void import_contact_token(Td *td, const string &token, Promisecreate_handler(std::move(promise))->send(token); } +void invalidate_authentication_codes(Td *td, vector &&authentication_codes) { + td->create_handler()->send(std::move(authentication_codes)); +} + } // namespace td diff --git a/td/telegram/Account.h b/td/telegram/Account.h index 4b43db7cc..8b9cf7364 100644 --- a/td/telegram/Account.h +++ b/td/telegram/Account.h @@ -48,4 +48,6 @@ void export_contact_token(Td *td, Promise> void import_contact_token(Td *td, const string &token, Promise> &&promise); +void invalidate_authentication_codes(Td *td, vector &&authentication_codes); + } // namespace td diff --git a/td/telegram/MessageSource.cpp b/td/telegram/MessageSource.cpp index 1e75ab0d4..af36a4d68 100644 --- a/td/telegram/MessageSource.cpp +++ b/td/telegram/MessageSource.cpp @@ -28,6 +28,8 @@ StringBuilder &operator<<(StringBuilder &string_builder, MessageSource source) { return string_builder << "DialogEventLog"; case MessageSource::Notification: return string_builder << "Notification"; + case MessageSource::Screenshot: + return string_builder << "Screenshot"; case MessageSource::Other: return string_builder << "Other"; default: @@ -56,6 +58,8 @@ MessageSource get_message_source(const td_api::object_ptr return MessageSource::DialogEventLog; case td_api::messageSourceNotification::ID: return MessageSource::Notification; + case td_api::messageSourceScreenshot::ID: + return MessageSource::Screenshot; case td_api::messageSourceOther::ID: return MessageSource::Other; default: diff --git a/td/telegram/MessageSource.h b/td/telegram/MessageSource.h index 778fd5871..33223e022 100644 --- a/td/telegram/MessageSource.h +++ b/td/telegram/MessageSource.h @@ -23,6 +23,7 @@ enum class MessageSource : int32 { Search, DialogEventLog, Notification, + Screenshot, Other }; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 497c7f52a..3b407e6be 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -6,6 +6,7 @@ // #include "td/telegram/MessagesManager.h" +#include "td/telegram/Account.h" #include "td/telegram/AuthManager.h" #include "td/telegram/BackgroundInfo.hpp" #include "td/telegram/ChainId.h" @@ -6504,6 +6505,37 @@ MessagesManager::Dialog *MessagesManager::get_service_notifications_dialog() { return get_dialog(service_notifications_dialog_id); } +void MessagesManager::extract_authentication_codes(DialogId dialog_id, const Message *m, + vector &authentication_codes) { + CHECK(m != nullptr); + if (dialog_id != DialogId(ContactsManager::get_service_notifications_user_id()) || !m->message_id.is_valid() || + !m->message_id.is_server() || m->content->get_type() != MessageContentType::Text || m->is_outgoing) { + return; + } + auto *formatted_text = get_message_content_text(m->content.get()); + CHECK(formatted_text != nullptr); + const string &text = formatted_text->text; + for (size_t i = 0; i < text.size(); i++) { + if (is_digit(text[i])) { + string code; + do { + if (is_digit(text[i])) { + code += text[i++]; + continue; + } + if (text[i] == '-') { + i++; + continue; + } + break; + } while (true); + if (5 <= code.size() && code.size() <= 7) { + authentication_codes.push_back(code); + } + } + } +} + void MessagesManager::save_auth_notification_ids() { auto min_date = G()->unix_time() - AUTH_NOTIFICATION_ID_CACHE_TIME; vector stored_ids; @@ -19843,6 +19875,9 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess source == MessageSource::DialogList || source == MessageSource::Other; bool need_mark_download_as_viewed = is_dialog_history || source == MessageSource::HistoryPreview || source == MessageSource::Search || source == MessageSource::Other; + bool need_invalidate_authentication_code = + dialog_id == DialogId(ContactsManager::get_service_notifications_user_id()) && + source == MessageSource::Screenshot; // keep only valid message identifiers size_t pos = 0; @@ -19968,6 +20003,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess vector read_reaction_message_ids; vector new_viewed_message_ids; vector viewed_reaction_message_ids; + vector authentication_codes; for (auto message_id : message_ids) { auto *m = get_message_force(d, message_id, "view_messages 4"); if (m != nullptr) { @@ -20027,6 +20063,10 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess view_id = ++info->current_view_id; info->recently_viewed_messages[view_id] = message_id; } + + if (need_invalidate_authentication_code) { + extract_authentication_codes(dialog_id, m, authentication_codes); + } } else if (!message_id.is_yet_unsent() && message_id > max_message_id) { if ((d->notification_info != nullptr && message_id <= d->notification_info->max_notification_message_id_) || message_id <= d->last_new_message_id || message_id <= max_thread_message_id) { @@ -20072,6 +20112,9 @@ Status MessagesManager::view_messages(DialogId dialog_id, vector mess if (td_->is_online() && dialog_viewed_messages_.count(dialog_id) != 0) { update_viewed_messages_timeout_.add_timeout_in(dialog_id.get(), UPDATE_VIEWED_MESSAGES_PERIOD); } + if (!authentication_codes.empty()) { + invalidate_authentication_codes(td_, std::move(authentication_codes)); + } if (!need_read) { return Status::OK(); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index d03decd43..356dea6ff 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -3107,6 +3107,8 @@ class MessagesManager final : public Actor { Dialog *get_service_notifications_dialog(); + static void extract_authentication_codes(DialogId dialog_id, const Message *m, vector &authentication_codes); + void save_auth_notification_ids(); MessageId get_next_message_id(Dialog *d, MessageType type) const; diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index b82301a56..90a34a8b3 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -5221,7 +5221,7 @@ class CliClient final : public Actor { } else if (op == "rrh") { const string &hashtag = args; send_request(td_api::make_object(hashtag)); - } else if (op == "view" || op == "viewh" || op == "viewt") { + } else if (op == "view" || op == "viewh" || op == "viewt" || op == "views") { ChatId chat_id; string message_ids; get_args(args, chat_id, message_ids); @@ -5230,6 +5230,8 @@ class CliClient final : public Actor { source = td_api::make_object(); } else if (op == "viewt") { source = td_api::make_object(); + } else if (op == "views") { + source = td_api::make_object(); } send_request( td_api::make_object(chat_id, as_message_ids(message_ids), std::move(source), true));