diff --git a/CMakeLists.txt b/CMakeLists.txt index 584a975fe..2bd20cfbf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -423,6 +423,7 @@ set(TDLIB_SOURCE td/telegram/SecureManager.cpp td/telegram/SecureStorage.cpp td/telegram/SecureValue.cpp + td/telegram/SendCodeHelper.cpp td/telegram/SequenceDispatcher.cpp td/telegram/StateManager.cpp td/telegram/StickersManager.cpp @@ -488,10 +489,8 @@ set(TDLIB_SOURCE td/telegram/DocumentsManager.h td/telegram/DraftMessage.h td/telegram/FileReferenceManager.h - td/telegram/FileReferenceManager.hpp td/telegram/files/FileBitmask.h td/telegram/files/FileData.h - td/telegram/files/FileData.hpp td/telegram/files/FileDb.h td/telegram/files/FileDbId.h td/telegram/files/FileDownloader.h @@ -507,7 +506,6 @@ set(TDLIB_SOURCE td/telegram/files/FileLoaderUtils.h td/telegram/files/FileLoadManager.h td/telegram/files/FileLocation.h - td/telegram/files/FileLocation.hpp td/telegram/files/FileManager.h td/telegram/files/FileSourceId.h td/telegram/files/FileStats.h @@ -579,6 +577,7 @@ set(TDLIB_SOURCE td/telegram/SecureManager.h td/telegram/SecureStorage.h td/telegram/SecureValue.h + td/telegram/SendCodeHelper.h td/telegram/SequenceDispatcher.h td/telegram/SetWithPosition.h td/telegram/StateManager.h @@ -606,7 +605,10 @@ set(TDLIB_SOURCE td/telegram/AuthManager.hpp td/telegram/DocumentsManager.hpp td/telegram/DraftMessage.hpp + td/telegram/FileReferenceManager.hpp + td/telegram/files/FileData.hpp td/telegram/files/FileId.hpp + td/telegram/files/FileLocation.hpp td/telegram/files/FileManager.hpp td/telegram/Game.hpp td/telegram/InputMessageText.hpp @@ -616,6 +618,7 @@ set(TDLIB_SOURCE td/telegram/Photo.hpp td/telegram/ReplyMarkup.hpp td/telegram/SecureValue.hpp + td/telegram/SendCodeHelper.hpp td/telegram/StickersManager.hpp td/telegram/VideoNotesManager.hpp td/telegram/VideosManager.hpp diff --git a/td/telegram/AuthManager.cpp b/td/telegram/AuthManager.cpp index 344ce7a78..356e8baaf 100644 --- a/td/telegram/AuthManager.cpp +++ b/td/telegram/AuthManager.cpp @@ -33,151 +33,6 @@ namespace td { -// SendCodeHelper -void SendCodeHelper::on_sent_code(telegram_api::object_ptr sent_code) { - phone_registered_ = (sent_code->flags_ & SENT_CODE_FLAG_IS_USER_REGISTERED) != 0; - phone_code_hash_ = sent_code->phone_code_hash_; - sent_code_info_ = get_authentication_code_info(std::move(sent_code->type_)); - next_code_info_ = get_authentication_code_info(std::move(sent_code->next_type_)); - next_code_timestamp_ = Timestamp::in((sent_code->flags_ & SENT_CODE_FLAG_HAS_TIMEOUT) != 0 ? sent_code->timeout_ : 0); -} - -td_api::object_ptr SendCodeHelper::get_authorization_state_wait_code( - const TermsOfService &terms_of_service) const { - return make_tl_object( - phone_registered_, terms_of_service.get_terms_of_service_object(), get_authentication_code_info_object()); -} - -td_api::object_ptr SendCodeHelper::get_authentication_code_info_object() const { - return make_tl_object( - phone_number_, get_authentication_code_type_object(sent_code_info_), - get_authentication_code_type_object(next_code_info_), - max(static_cast(next_code_timestamp_.in() + 1 - 1e-9), 0)); -} - -Result SendCodeHelper::resend_code() { - if (next_code_info_.type == AuthenticationCodeInfo::Type::None) { - return Status::Error(8, "Authentication code can't be resend"); - } - sent_code_info_ = next_code_info_; - next_code_info_ = {}; - next_code_timestamp_ = {}; - return telegram_api::auth_resendCode(phone_number_, phone_code_hash_); -} - -Result SendCodeHelper::send_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number, int32 api_id, - const string &api_hash) { - if (!phone_number_.empty()) { - return Status::Error(8, "Can't change phone"); - } - phone_number_ = phone_number.str(); - int32 flags = 0; - if (allow_flash_call) { - flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; - } - return telegram_api::auth_sendCode(flags, false /*ignored*/, phone_number_, is_current_phone_number, api_id, - api_hash); -} - -Result SendCodeHelper::send_change_phone_code(Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number) { - phone_number_ = phone_number.str(); - int32 flags = 0; - if (allow_flash_call) { - flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; - } - return telegram_api::account_sendChangePhoneCode(flags, false /*ignored*/, phone_number_, is_current_phone_number); -} - -Result SendCodeHelper::send_verify_phone_code(const string &hash, - Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number) { - phone_number_ = phone_number.str(); - int32 flags = 0; - if (allow_flash_call) { - flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; - } - return telegram_api::account_sendVerifyPhoneCode(flags, false /*ignored*/, hash, is_current_phone_number); -} - -Result SendCodeHelper::send_confirm_phone_code( - Slice phone_number, bool allow_flash_call, bool is_current_phone_number) { - phone_number_ = phone_number.str(); - int32 flags = 0; - if (allow_flash_call) { - flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; - } - return telegram_api::account_sendConfirmPhoneCode(flags, false /*ignored*/, phone_number_, is_current_phone_number); -} - -SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_info( - tl_object_ptr &&code_type_ptr) { - if (code_type_ptr == nullptr) { - return AuthenticationCodeInfo(); - } - - switch (code_type_ptr->get_id()) { - case telegram_api::auth_codeTypeSms::ID: - return {AuthenticationCodeInfo::Type::Sms, 0, ""}; - case telegram_api::auth_codeTypeCall::ID: - return {AuthenticationCodeInfo::Type::Call, 0, ""}; - case telegram_api::auth_codeTypeFlashCall::ID: - return {AuthenticationCodeInfo::Type::FlashCall, 0, ""}; - default: - UNREACHABLE(); - return AuthenticationCodeInfo(); - } -} - -SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_info( - tl_object_ptr &&sent_code_type_ptr) { - CHECK(sent_code_type_ptr != nullptr); - switch (sent_code_type_ptr->get_id()) { - case telegram_api::auth_sentCodeTypeApp::ID: { - auto code_type = move_tl_object_as(sent_code_type_ptr); - return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Message, code_type->length_, ""}; - } - case telegram_api::auth_sentCodeTypeSms::ID: { - auto code_type = move_tl_object_as(sent_code_type_ptr); - return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Sms, code_type->length_, ""}; - } - case telegram_api::auth_sentCodeTypeCall::ID: { - auto code_type = move_tl_object_as(sent_code_type_ptr); - return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Call, code_type->length_, ""}; - } - case telegram_api::auth_sentCodeTypeFlashCall::ID: { - auto code_type = move_tl_object_as(sent_code_type_ptr); - return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::FlashCall, 0, code_type->pattern_}; - } - default: - UNREACHABLE(); - return AuthenticationCodeInfo(); - } -} - -tl_object_ptr SendCodeHelper::get_authentication_code_type_object( - const AuthenticationCodeInfo &authentication_code_info) { - switch (authentication_code_info.type) { - case AuthenticationCodeInfo::Type::None: - return nullptr; - case AuthenticationCodeInfo::Type::Message: - return make_tl_object(authentication_code_info.length); - case AuthenticationCodeInfo::Type::Sms: - return make_tl_object(authentication_code_info.length); - case AuthenticationCodeInfo::Type::Call: - return make_tl_object(authentication_code_info.length); - case AuthenticationCodeInfo::Type::FlashCall: - return make_tl_object(authentication_code_info.pattern); - default: - UNREACHABLE(); - return nullptr; - } -} - -// PhoneNumberManager void PhoneNumberManager::get_state(uint64 query_id) { tl_object_ptr obj; switch (state_) { @@ -411,7 +266,6 @@ void PhoneNumberManager::tear_down() { parent_.reset(); } -// AuthManager AuthManager::AuthManager(int32 api_id, const string &api_hash, ActorShared<> parent) : parent_(std::move(parent)), api_id_(api_id), api_hash_(api_hash) { string auth_str = G()->td_db()->get_binlog_pmc()->get("auth"); diff --git a/td/telegram/AuthManager.h b/td/telegram/AuthManager.h index 7b10e493b..71a6fbb12 100644 --- a/td/telegram/AuthManager.h +++ b/td/telegram/AuthManager.h @@ -8,6 +8,7 @@ #include "td/telegram/net/NetActor.h" #include "td/telegram/net/NetQuery.h" +#include "td/telegram/SendCodeHelper.h" #include "td/telegram/TermsOfService.h" #include "td/telegram/td_api.h" @@ -16,89 +17,11 @@ #include "td/actor/actor.h" #include "td/utils/common.h" -#include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/Time.h" namespace td { -class SendCodeHelper { - public: - void on_sent_code(telegram_api::object_ptr sent_code); - td_api::object_ptr get_authorization_state_wait_code( - const TermsOfService &terms_of_service) const; - td_api::object_ptr get_authentication_code_info_object() const; - Result resend_code(); - - Result send_code(Slice phone_number, bool allow_flash_call, bool is_current_phone_number, - int32 api_id, const string &api_hash); - - Result send_change_phone_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number); - - Result send_verify_phone_code(const string &hash, Slice phone_number, - bool allow_flash_call, - bool is_current_phone_number); - - Result send_confirm_phone_code(Slice phone_number, bool allow_flash_call, - bool is_current_phone_number); - - Slice phone_number() const { - return phone_number_; - } - Slice phone_code_hash() const { - return phone_code_hash_; - } - bool phone_registered() const { - return phone_registered_; - } - - template - void store(T &storer) const; - template - void parse(T &parser); - - private: - static constexpr int32 AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL = 1 << 0; - - static constexpr int32 SENT_CODE_FLAG_IS_USER_REGISTERED = 1 << 0; - static constexpr int32 SENT_CODE_FLAG_HAS_NEXT_TYPE = 1 << 1; - static constexpr int32 SENT_CODE_FLAG_HAS_TIMEOUT = 1 << 2; - - struct AuthenticationCodeInfo { - enum class Type : int32 { None, Message, Sms, Call, FlashCall }; - Type type = Type::None; - int32 length = 0; - string pattern; - - AuthenticationCodeInfo() = default; - AuthenticationCodeInfo(Type type, int length, string pattern) - : type(type), length(length), pattern(std::move(pattern)) { - } - - template - void store(T &storer) const; - template - void parse(T &parser); - }; - - string phone_number_; - bool phone_registered_; - string phone_code_hash_; - - SendCodeHelper::AuthenticationCodeInfo sent_code_info_; - SendCodeHelper::AuthenticationCodeInfo next_code_info_; - Timestamp next_code_timestamp_; - - static AuthenticationCodeInfo get_authentication_code_info( - tl_object_ptr &&code_type_ptr); - static AuthenticationCodeInfo get_authentication_code_info( - tl_object_ptr &&sent_code_type_ptr); - - static tl_object_ptr get_authentication_code_type_object( - const AuthenticationCodeInfo &authentication_code_info); -}; - class PhoneNumberManager : public NetActor { public: enum class Type : int32 { ChangePhone, VerifyPhone, ConfirmPhone }; @@ -312,4 +235,5 @@ class AuthManager : public NetActor { void start_up() override; void tear_down() override; }; + } // namespace td diff --git a/td/telegram/SendCodeHelper.cpp b/td/telegram/SendCodeHelper.cpp new file mode 100644 index 000000000..0831e61b8 --- /dev/null +++ b/td/telegram/SendCodeHelper.cpp @@ -0,0 +1,154 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#include "td/telegram/SendCodeHelper.h" + +namespace td { + +void SendCodeHelper::on_sent_code(telegram_api::object_ptr sent_code) { + phone_registered_ = (sent_code->flags_ & SENT_CODE_FLAG_IS_USER_REGISTERED) != 0; + phone_code_hash_ = sent_code->phone_code_hash_; + sent_code_info_ = get_authentication_code_info(std::move(sent_code->type_)); + next_code_info_ = get_authentication_code_info(std::move(sent_code->next_type_)); + next_code_timestamp_ = Timestamp::in((sent_code->flags_ & SENT_CODE_FLAG_HAS_TIMEOUT) != 0 ? sent_code->timeout_ : 0); +} + +td_api::object_ptr SendCodeHelper::get_authorization_state_wait_code( + const TermsOfService &terms_of_service) const { + return make_tl_object( + phone_registered_, terms_of_service.get_terms_of_service_object(), get_authentication_code_info_object()); +} + +td_api::object_ptr SendCodeHelper::get_authentication_code_info_object() const { + return make_tl_object( + phone_number_, get_authentication_code_type_object(sent_code_info_), + get_authentication_code_type_object(next_code_info_), + max(static_cast(next_code_timestamp_.in() + 1 - 1e-9), 0)); +} + +Result SendCodeHelper::resend_code() { + if (next_code_info_.type == AuthenticationCodeInfo::Type::None) { + return Status::Error(8, "Authentication code can't be resend"); + } + sent_code_info_ = next_code_info_; + next_code_info_ = {}; + next_code_timestamp_ = {}; + return telegram_api::auth_resendCode(phone_number_, phone_code_hash_); +} + +Result SendCodeHelper::send_code(Slice phone_number, bool allow_flash_call, + bool is_current_phone_number, int32 api_id, + const string &api_hash) { + if (!phone_number_.empty()) { + return Status::Error(8, "Can't change phone"); + } + phone_number_ = phone_number.str(); + int32 flags = 0; + if (allow_flash_call) { + flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; + } + return telegram_api::auth_sendCode(flags, false /*ignored*/, phone_number_, is_current_phone_number, api_id, + api_hash); +} + +Result SendCodeHelper::send_change_phone_code(Slice phone_number, + bool allow_flash_call, + bool is_current_phone_number) { + phone_number_ = phone_number.str(); + int32 flags = 0; + if (allow_flash_call) { + flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; + } + return telegram_api::account_sendChangePhoneCode(flags, false /*ignored*/, phone_number_, is_current_phone_number); +} + +Result SendCodeHelper::send_verify_phone_code(const string &hash, + Slice phone_number, + bool allow_flash_call, + bool is_current_phone_number) { + phone_number_ = phone_number.str(); + int32 flags = 0; + if (allow_flash_call) { + flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; + } + return telegram_api::account_sendVerifyPhoneCode(flags, false /*ignored*/, hash, is_current_phone_number); +} + +Result SendCodeHelper::send_confirm_phone_code( + Slice phone_number, bool allow_flash_call, bool is_current_phone_number) { + phone_number_ = phone_number.str(); + int32 flags = 0; + if (allow_flash_call) { + flags |= AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL; + } + return telegram_api::account_sendConfirmPhoneCode(flags, false /*ignored*/, phone_number_, is_current_phone_number); +} + +SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_info( + tl_object_ptr &&code_type_ptr) { + if (code_type_ptr == nullptr) { + return AuthenticationCodeInfo(); + } + + switch (code_type_ptr->get_id()) { + case telegram_api::auth_codeTypeSms::ID: + return {AuthenticationCodeInfo::Type::Sms, 0, ""}; + case telegram_api::auth_codeTypeCall::ID: + return {AuthenticationCodeInfo::Type::Call, 0, ""}; + case telegram_api::auth_codeTypeFlashCall::ID: + return {AuthenticationCodeInfo::Type::FlashCall, 0, ""}; + default: + UNREACHABLE(); + return AuthenticationCodeInfo(); + } +} + +SendCodeHelper::AuthenticationCodeInfo SendCodeHelper::get_authentication_code_info( + tl_object_ptr &&sent_code_type_ptr) { + CHECK(sent_code_type_ptr != nullptr); + switch (sent_code_type_ptr->get_id()) { + case telegram_api::auth_sentCodeTypeApp::ID: { + auto code_type = move_tl_object_as(sent_code_type_ptr); + return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Message, code_type->length_, ""}; + } + case telegram_api::auth_sentCodeTypeSms::ID: { + auto code_type = move_tl_object_as(sent_code_type_ptr); + return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Sms, code_type->length_, ""}; + } + case telegram_api::auth_sentCodeTypeCall::ID: { + auto code_type = move_tl_object_as(sent_code_type_ptr); + return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::Call, code_type->length_, ""}; + } + case telegram_api::auth_sentCodeTypeFlashCall::ID: { + auto code_type = move_tl_object_as(sent_code_type_ptr); + return AuthenticationCodeInfo{AuthenticationCodeInfo::Type::FlashCall, 0, code_type->pattern_}; + } + default: + UNREACHABLE(); + return AuthenticationCodeInfo(); + } +} + +tl_object_ptr SendCodeHelper::get_authentication_code_type_object( + const AuthenticationCodeInfo &authentication_code_info) { + switch (authentication_code_info.type) { + case AuthenticationCodeInfo::Type::None: + return nullptr; + case AuthenticationCodeInfo::Type::Message: + return make_tl_object(authentication_code_info.length); + case AuthenticationCodeInfo::Type::Sms: + return make_tl_object(authentication_code_info.length); + case AuthenticationCodeInfo::Type::Call: + return make_tl_object(authentication_code_info.length); + case AuthenticationCodeInfo::Type::FlashCall: + return make_tl_object(authentication_code_info.pattern); + default: + UNREACHABLE(); + return nullptr; + } +} + +} // namespace td diff --git a/td/telegram/SendCodeHelper.h b/td/telegram/SendCodeHelper.h new file mode 100644 index 000000000..464708f71 --- /dev/null +++ b/td/telegram/SendCodeHelper.h @@ -0,0 +1,97 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" +#include "td/telegram/TermsOfService.h" + +#include "td/utils/common.h" +#include "td/utils/Slice.h" +#include "td/utils/Status.h" +#include "td/utils/Time.h" + +namespace td { + +class SendCodeHelper { + public: + void on_sent_code(telegram_api::object_ptr sent_code); + td_api::object_ptr get_authorization_state_wait_code( + const TermsOfService &terms_of_service) const; + td_api::object_ptr get_authentication_code_info_object() const; + Result resend_code(); + + Result send_code(Slice phone_number, bool allow_flash_call, bool is_current_phone_number, + int32 api_id, const string &api_hash); + + Result send_change_phone_code(Slice phone_number, bool allow_flash_call, + bool is_current_phone_number); + + Result send_verify_phone_code(const string &hash, Slice phone_number, + bool allow_flash_call, + bool is_current_phone_number); + + Result send_confirm_phone_code(Slice phone_number, bool allow_flash_call, + bool is_current_phone_number); + + Slice phone_number() const { + return phone_number_; + } + Slice phone_code_hash() const { + return phone_code_hash_; + } + bool phone_registered() const { + return phone_registered_; + } + + template + void store(T &storer) const; + template + void parse(T &parser); + + private: + static constexpr int32 AUTH_SEND_CODE_FLAG_ALLOW_FLASH_CALL = 1 << 0; + + static constexpr int32 SENT_CODE_FLAG_IS_USER_REGISTERED = 1 << 0; + static constexpr int32 SENT_CODE_FLAG_HAS_NEXT_TYPE = 1 << 1; + static constexpr int32 SENT_CODE_FLAG_HAS_TIMEOUT = 1 << 2; + + struct AuthenticationCodeInfo { + enum class Type : int32 { None, Message, Sms, Call, FlashCall }; + Type type = Type::None; + int32 length = 0; + string pattern; + + AuthenticationCodeInfo() = default; + AuthenticationCodeInfo(Type type, int length, string pattern) + : type(type), length(length), pattern(std::move(pattern)) { + } + + template + void store(T &storer) const; + template + void parse(T &parser); + }; + + string phone_number_; + bool phone_registered_; + string phone_code_hash_; + + SendCodeHelper::AuthenticationCodeInfo sent_code_info_; + SendCodeHelper::AuthenticationCodeInfo next_code_info_; + Timestamp next_code_timestamp_; + + static AuthenticationCodeInfo get_authentication_code_info( + tl_object_ptr &&code_type_ptr); + static AuthenticationCodeInfo get_authentication_code_info( + tl_object_ptr &&sent_code_type_ptr); + + static tl_object_ptr get_authentication_code_type_object( + const AuthenticationCodeInfo &authentication_code_info); +}; + +} // namespace td diff --git a/td/telegram/SendCodeHelper.hpp b/td/telegram/SendCodeHelper.hpp new file mode 100644 index 000000000..af8893656 --- /dev/null +++ b/td/telegram/SendCodeHelper.hpp @@ -0,0 +1,53 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// Distributed under the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +// +#pragma once + +#include "td/telegram/SendCodeHelper.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void SendCodeHelper::AuthenticationCodeInfo::store(T &storer) const { + using td::store; + store(type, storer); + store(length, storer); + store(pattern, storer); +} + +template +void SendCodeHelper::AuthenticationCodeInfo::parse(T &parser) { + using td::parse; + parse(type, parser); + parse(length, parser); + parse(pattern, parser); +} + +template +void SendCodeHelper::store(T &storer) const { + using td::store; + store(phone_number_, storer); + store(phone_registered_, storer); + store(phone_code_hash_, storer); + store(sent_code_info_, storer); + store(next_code_info_, storer); + store(next_code_timestamp_, storer); +} + +template +void SendCodeHelper::parse(T &parser) { + using td::parse; + parse(phone_number_, parser); + parse(phone_registered_, parser); + parse(phone_code_hash_, parser); + parse(sent_code_info_, parser); + parse(next_code_info_, parser); + parse(next_code_timestamp_, parser); +} + +} // namespace td