2019-02-16 17:01:47 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2019-02-16 17:01:47 +01:00
|
|
|
//
|
|
|
|
// 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/PhoneNumberManager.h"
|
|
|
|
|
2021-06-11 01:13:46 +02:00
|
|
|
#include "td/telegram/ConfigManager.h"
|
2021-10-20 09:46:01 +02:00
|
|
|
#include "td/telegram/ContactsManager.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/net/NetQueryDispatcher.h"
|
2021-06-11 01:13:46 +02:00
|
|
|
#include "td/telegram/SuggestedAction.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
#include "td/telegram/Td.h"
|
2019-02-17 14:52:34 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
2019-02-16 17:01:47 +01:00
|
|
|
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
|
|
|
void PhoneNumberManager::get_state(uint64 query_id) {
|
|
|
|
tl_object_ptr<td_api::Object> obj;
|
|
|
|
switch (state_) {
|
|
|
|
case State::Ok:
|
|
|
|
obj = make_tl_object<td_api::ok>();
|
|
|
|
break;
|
|
|
|
case State::WaitCode:
|
|
|
|
obj = send_code_helper_.get_authentication_code_info_object();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CHECK(obj);
|
|
|
|
send_closure(G()->td(), &Td::send_result, query_id, std::move(obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
PhoneNumberManager::PhoneNumberManager(PhoneNumberManager::Type type, ActorShared<> parent)
|
|
|
|
: type_(type), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:46:01 +02:00
|
|
|
void PhoneNumberManager::send_new_send_code_query(uint64 query_id, const telegram_api::Function &send_code) {
|
2019-02-16 17:01:47 +01:00
|
|
|
on_new_query(query_id);
|
2020-03-15 22:17:11 +01:00
|
|
|
start_net_query(NetQueryType::SendCode, G()->net_query_creator().create(send_code));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2019-05-03 04:44:59 +02:00
|
|
|
void PhoneNumberManager::set_phone_number(uint64 query_id, string phone_number, Settings settings) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (phone_number.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
2021-06-11 01:13:46 +02:00
|
|
|
send_closure(G()->config_manager(), &ConfigManager::hide_suggested_action,
|
|
|
|
SuggestedAction{SuggestedAction::Type::CheckPhoneNumber});
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id, send_code_helper_.send_change_phone_code(phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::VerifyPhone:
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id, send_code_helper_.send_verify_phone_code(phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ConfirmPhone:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::set_phone_number_and_hash(uint64 query_id, string hash, string phone_number,
|
2019-05-03 04:44:59 +02:00
|
|
|
Settings settings) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (phone_number.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Phone number must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
if (hash.empty()) {
|
2022-09-10 21:48:34 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Hash must be non-empty"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ConfirmPhone:
|
2021-10-20 09:46:01 +02:00
|
|
|
return send_new_send_code_query(query_id,
|
2019-09-02 17:11:47 +02:00
|
|
|
send_code_helper_.send_confirm_phone_code(hash, phone_number, settings));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ChangePhone:
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::resend_authentication_code(uint64 query_id) {
|
|
|
|
if (state_ != State::WaitCode) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Can't resend code"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r_resend_code = send_code_helper_.resend_code();
|
|
|
|
if (r_resend_code.is_error()) {
|
|
|
|
return on_query_error(query_id, r_resend_code.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
2020-03-15 23:01:14 +01:00
|
|
|
start_net_query(NetQueryType::SendCode, G()->net_query_creator().create_unauth(r_resend_code.move_as_ok()));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 14:38:39 +02:00
|
|
|
void PhoneNumberManager::send_new_check_code_query(const telegram_api::Function &check_code) {
|
|
|
|
start_net_query(NetQueryType::CheckCode, G()->net_query_creator().create(check_code));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::check_code(uint64 query_id, string code) {
|
|
|
|
if (state_ != State::WaitCode) {
|
2023-09-02 21:11:01 +02:00
|
|
|
return on_query_error(query_id, Status::Error(400, "Can't check code"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_new_query(query_id);
|
|
|
|
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
|
|
|
return send_new_check_code_query(telegram_api::account_changePhone(
|
|
|
|
send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
case Type::ConfirmPhone:
|
|
|
|
return send_new_check_code_query(
|
|
|
|
telegram_api::account_confirmPhone(send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
case Type::VerifyPhone:
|
|
|
|
return send_new_check_code_query(telegram_api::account_verifyPhone(
|
|
|
|
send_code_helper_.phone_number().str(), send_code_helper_.phone_code_hash().str(), code));
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_new_query(uint64 query_id) {
|
|
|
|
if (query_id_ != 0) {
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_error(Status::Error(400, "Another query has started"));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = query_id;
|
|
|
|
// TODO: cancel older net_query
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:39:26 +02:00
|
|
|
void PhoneNumberManager::on_current_query_error(Status status) {
|
|
|
|
if (query_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
auto id = query_id_;
|
|
|
|
query_id_ = 0;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
on_query_error(id, std::move(status));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::on_query_error(uint64 id, Status status) {
|
|
|
|
send_closure(G()->td(), &Td::send_error, id, std::move(status));
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:39:26 +02:00
|
|
|
void PhoneNumberManager::on_current_query_ok() {
|
|
|
|
if (query_id_ == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-16 17:01:47 +01:00
|
|
|
auto id = query_id_;
|
|
|
|
net_query_id_ = 0;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
query_id_ = 0;
|
|
|
|
get_state(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::start_net_query(NetQueryType net_query_type, NetQueryPtr net_query) {
|
|
|
|
// TODO: cancel old net_query?
|
|
|
|
net_query_type_ = net_query_type;
|
|
|
|
net_query_id_ = net_query->id();
|
|
|
|
G()->net_query_dispatcher().dispatch_with_callback(std::move(net_query), actor_shared(this));
|
|
|
|
}
|
|
|
|
|
2021-10-20 09:46:01 +02:00
|
|
|
void PhoneNumberManager::process_check_code_result(Result<tl_object_ptr<telegram_api::User>> &&result) {
|
|
|
|
if (result.is_error()) {
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_error(result.move_as_error());
|
2021-10-20 09:46:01 +02:00
|
|
|
}
|
2023-06-23 17:22:58 +02:00
|
|
|
send_closure(G()->contacts_manager(), &ContactsManager::on_get_user, result.move_as_ok(),
|
|
|
|
"process_check_code_result");
|
2021-10-20 09:46:01 +02:00
|
|
|
state_ = State::Ok;
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2021-10-20 09:46:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::process_check_code_result(Result<bool> &&result) {
|
2019-02-16 17:01:47 +01:00
|
|
|
if (result.is_error()) {
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_error(result.move_as_error());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
state_ = State::Ok;
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void PhoneNumberManager::on_check_code_result(NetQueryPtr &&net_query) {
|
2019-02-16 17:01:47 +01:00
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_changePhone>(std::move(net_query)));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::VerifyPhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_verifyPhone>(std::move(net_query)));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ConfirmPhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return process_check_code_result(fetch_result<telegram_api::account_confirmPhone>(std::move(net_query)));
|
2019-02-16 17:01:47 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void PhoneNumberManager::on_send_code_result(NetQueryPtr &&net_query) {
|
2019-02-16 17:01:47 +01:00
|
|
|
auto r_sent_code = [&] {
|
|
|
|
switch (type_) {
|
|
|
|
case Type::ChangePhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return fetch_result<telegram_api::account_sendChangePhoneCode>(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::VerifyPhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return fetch_result<telegram_api::account_sendVerifyPhoneCode>(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
case Type::ConfirmPhone:
|
2023-09-02 21:39:26 +02:00
|
|
|
return fetch_result<telegram_api::account_sendConfirmPhoneCode>(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2023-09-02 21:39:26 +02:00
|
|
|
return fetch_result<telegram_api::account_sendChangePhoneCode>(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
}();
|
|
|
|
if (r_sent_code.is_error()) {
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_error(r_sent_code.move_as_error());
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
2023-01-06 10:42:16 +01:00
|
|
|
auto sent_code_ptr = r_sent_code.move_as_ok();
|
|
|
|
auto sent_code_id = sent_code_ptr->get_id();
|
|
|
|
if (sent_code_id != telegram_api::auth_sentCode::ID) {
|
|
|
|
CHECK(sent_code_id == telegram_api::auth_sentCodeSuccess::ID);
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_error(Status::Error(500, "Receive invalid response"));
|
2023-01-06 10:42:16 +01:00
|
|
|
}
|
|
|
|
auto sent_code = telegram_api::move_object_as<telegram_api::auth_sentCode>(sent_code_ptr);
|
2019-02-16 17:01:47 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Receive " << to_string(sent_code);
|
|
|
|
|
2022-09-07 20:22:21 +02:00
|
|
|
switch (sent_code->type_->get_id()) {
|
|
|
|
case telegram_api::auth_sentCodeTypeSetUpEmailRequired::ID:
|
|
|
|
case telegram_api::auth_sentCodeTypeEmailCode::ID:
|
2023-09-02 21:39:26 +02:00
|
|
|
return on_current_query_error(Status::Error(500, "Receive incorrect response"));
|
2022-09-07 20:22:21 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-02-16 17:01:47 +01:00
|
|
|
send_code_helper_.on_sent_code(std::move(sent_code));
|
|
|
|
|
|
|
|
state_ = State::WaitCode;
|
2023-09-02 21:39:26 +02:00
|
|
|
on_current_query_ok();
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
|
2023-09-02 21:20:05 +02:00
|
|
|
void PhoneNumberManager::on_result(NetQueryPtr net_query) {
|
2019-02-16 17:01:47 +01:00
|
|
|
NetQueryType type = NetQueryType::None;
|
2023-09-02 21:20:05 +02:00
|
|
|
if (net_query->id() == net_query_id_) {
|
2019-02-16 17:01:47 +01:00
|
|
|
net_query_id_ = 0;
|
|
|
|
type = net_query_type_;
|
|
|
|
net_query_type_ = NetQueryType::None;
|
|
|
|
}
|
|
|
|
switch (type) {
|
|
|
|
case NetQueryType::None:
|
2023-09-02 21:39:26 +02:00
|
|
|
net_query->clear();
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::SendCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_send_code_result(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
|
|
|
case NetQueryType::CheckCode:
|
2023-09-02 21:20:05 +02:00
|
|
|
on_check_code_result(std::move(net_query));
|
2019-02-16 17:01:47 +01:00
|
|
|
break;
|
2023-09-02 21:20:05 +02:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
2019-02-16 17:01:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhoneNumberManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|