2019-04-30 11:12:30 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "main/main_account.h"
|
|
|
|
|
2020-12-29 07:17:43 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2019-04-30 11:12:30 +00:00
|
|
|
#include "core/application.h"
|
2019-07-24 08:46:23 +00:00
|
|
|
#include "core/shortcuts.h"
|
2020-06-09 16:57:05 +00:00
|
|
|
#include "storage/storage_account.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "storage/storage_domain.h" // Storage::StartResult.
|
2019-07-24 08:46:23 +00:00
|
|
|
#include "storage/serialize_common.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "storage/serialize_peer.h"
|
2019-07-24 08:46:23 +00:00
|
|
|
#include "storage/localstorage.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_user.h"
|
2020-06-12 12:12:34 +00:00
|
|
|
#include "data/data_changes.h"
|
2019-07-24 08:46:23 +00:00
|
|
|
#include "window/window_controller.h"
|
|
|
|
#include "media/audio/media_audio.h"
|
2020-06-17 09:36:25 +00:00
|
|
|
#include "mtproto/mtproto_config.h"
|
|
|
|
#include "mtproto/mtproto_dc_options.h"
|
|
|
|
#include "mtproto/mtp_instance.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
#include "ui/image/image.h"
|
2019-07-24 08:46:23 +00:00
|
|
|
#include "mainwidget.h"
|
2020-06-11 09:41:03 +00:00
|
|
|
#include "api/api_updates.h"
|
2019-11-27 09:45:23 +00:00
|
|
|
#include "main/main_app_config.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "main/main_domain.h"
|
2020-06-18 12:47:09 +00:00
|
|
|
#include "main/main_session_settings.h"
|
2019-04-30 11:12:30 +00:00
|
|
|
|
|
|
|
namespace Main {
|
2020-06-15 16:25:02 +00:00
|
|
|
namespace {
|
2019-04-30 11:12:30 +00:00
|
|
|
|
2021-04-01 21:04:10 +00:00
|
|
|
constexpr auto kWideIdsTag = ~uint64(0);
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
[[nodiscard]] QString ComposeDataString(const QString &dataName, int index) {
|
|
|
|
auto result = dataName;
|
|
|
|
result.replace('#', QString());
|
|
|
|
if (index > 0) {
|
|
|
|
result += '#' + QString::number(index + 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Account::Account(not_null<Domain*> domain, const QString &dataName, int index)
|
|
|
|
: _domain(domain)
|
|
|
|
, _local(std::make_unique<Storage::Account>(
|
2020-06-15 16:25:02 +00:00
|
|
|
this,
|
|
|
|
ComposeDataString(dataName, index))) {
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
Account::~Account() {
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = maybeSession()) {
|
|
|
|
session->saveSettingsNowIfNeeded();
|
2020-06-17 09:36:25 +00:00
|
|
|
}
|
2020-06-26 10:36:22 +00:00
|
|
|
destroySession(DestroyReason::Quitting);
|
2020-06-17 09:36:25 +00:00
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2020-06-18 18:04:16 +00:00
|
|
|
Storage::Domain &Account::domainLocal() const {
|
|
|
|
return _domain->local();
|
|
|
|
}
|
|
|
|
|
2020-06-15 16:25:02 +00:00
|
|
|
[[nodiscard]] Storage::StartResult Account::legacyStart(
|
|
|
|
const QByteArray &passcode) {
|
|
|
|
Expects(!_appConfig);
|
|
|
|
|
2020-06-29 16:07:56 +00:00
|
|
|
return _local->legacyStart(passcode);
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
std::unique_ptr<MTP::Config> Account::prepareToStart(
|
|
|
|
std::shared_ptr<MTP::AuthKey> localKey) {
|
|
|
|
return _local->start(std::move(localKey));
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
void Account::start(std::unique_ptr<MTP::Config> config) {
|
2020-06-17 09:36:25 +00:00
|
|
|
startMtp(config
|
|
|
|
? std::move(config)
|
|
|
|
: std::make_unique<MTP::Config>(
|
|
|
|
Core::App().fallbackProductionConfig()));
|
2020-06-15 16:25:02 +00:00
|
|
|
_appConfig = std::make_unique<AppConfig>(this);
|
|
|
|
watchProxyChanges();
|
|
|
|
watchSessionChanges();
|
|
|
|
}
|
|
|
|
|
2020-06-18 11:17:58 +00:00
|
|
|
void Account::prepareToStartAdded(
|
|
|
|
std::shared_ptr<MTP::AuthKey> localKey) {
|
|
|
|
_local->startAdded(std::move(localKey));
|
|
|
|
}
|
|
|
|
|
2019-07-24 08:46:23 +00:00
|
|
|
void Account::watchProxyChanges() {
|
|
|
|
using ProxyChange = Core::Application::ProxyChange;
|
|
|
|
|
|
|
|
Core::App().proxyChanges(
|
|
|
|
) | rpl::start_with_next([=](const ProxyChange &change) {
|
2019-11-13 14:12:04 +00:00
|
|
|
const auto key = [&](const MTP::ProxyData &proxy) {
|
|
|
|
return (proxy.type == MTP::ProxyData::Type::Mtproto)
|
2019-07-24 08:46:23 +00:00
|
|
|
? std::make_pair(proxy.host, proxy.port)
|
|
|
|
: std::make_pair(QString(), uint32(0));
|
|
|
|
};
|
|
|
|
if (_mtp) {
|
|
|
|
_mtp->restart();
|
|
|
|
if (key(change.was) != key(change.now)) {
|
|
|
|
_mtp->reInitConnection(_mtp->mainDcId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_mtpForKeysDestroy) {
|
|
|
|
_mtpForKeysDestroy->restart();
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2019-04-30 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 08:46:23 +00:00
|
|
|
void Account::watchSessionChanges() {
|
|
|
|
sessionChanges(
|
2020-06-15 16:25:02 +00:00
|
|
|
) | rpl::start_with_next([=](Session *session) {
|
|
|
|
if (!session && _mtp) {
|
|
|
|
_mtp->setUserPhone(QString());
|
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
}, _lifetime);
|
2019-06-05 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 09:32:07 +00:00
|
|
|
uint64 Account::willHaveSessionUniqueId(MTP::Config *config) const {
|
|
|
|
// See also Session::uniqueId.
|
|
|
|
if (!_sessionUserId) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-01 21:04:10 +00:00
|
|
|
return _sessionUserId.bare
|
2020-06-24 09:32:07 +00:00
|
|
|
| (config && config->isTestMode() ? 0x0100'0000'0000'0000ULL : 0ULL);
|
2020-06-15 16:25:02 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 07:02:44 +00:00
|
|
|
void Account::createSession(
|
|
|
|
const MTPUser &user,
|
|
|
|
std::unique_ptr<SessionSettings> settings) {
|
|
|
|
createSession(
|
|
|
|
user,
|
|
|
|
QByteArray(),
|
|
|
|
0,
|
|
|
|
settings ? std::move(settings) : std::make_unique<SessionSettings>());
|
2019-10-02 10:46:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Account::createSession(
|
|
|
|
UserId id,
|
|
|
|
QByteArray serialized,
|
|
|
|
int streamVersion,
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> settings) {
|
2019-10-02 10:46:02 +00:00
|
|
|
DEBUG_LOG(("sessionUserSerialized.size: %1").arg(serialized.size()));
|
|
|
|
QDataStream peekStream(serialized);
|
|
|
|
const auto phone = Serialize::peekUserPhone(streamVersion, peekStream);
|
|
|
|
const auto flags = MTPDuser::Flag::f_self | (phone.isEmpty()
|
|
|
|
? MTPDuser::Flag()
|
|
|
|
: MTPDuser::Flag::f_phone);
|
2021-08-25 12:18:47 +00:00
|
|
|
|
2019-10-02 10:46:02 +00:00
|
|
|
createSession(
|
|
|
|
MTP_user(
|
|
|
|
MTP_flags(flags),
|
2021-08-25 08:15:05 +00:00
|
|
|
MTP_long(base::take(_sessionUserId).bare),
|
2019-10-02 10:46:02 +00:00
|
|
|
MTPlong(), // access_hash
|
|
|
|
MTPstring(), // first_name
|
|
|
|
MTPstring(), // last_name
|
|
|
|
MTPstring(), // username
|
|
|
|
MTP_string(phone),
|
|
|
|
MTPUserProfilePhoto(),
|
|
|
|
MTPUserStatus(),
|
|
|
|
MTPint(), // bot_info_version
|
|
|
|
MTPVector<MTPRestrictionReason>(),
|
|
|
|
MTPstring(), // bot_inline_placeholder
|
|
|
|
MTPstring()), // lang_code
|
|
|
|
serialized,
|
|
|
|
streamVersion,
|
|
|
|
std::move(settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::createSession(
|
|
|
|
const MTPUser &user,
|
|
|
|
QByteArray serialized,
|
|
|
|
int streamVersion,
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> settings) {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(_mtp != nullptr);
|
2019-06-06 09:37:12 +00:00
|
|
|
Expects(_session == nullptr);
|
|
|
|
Expects(_sessionValue.current() == nullptr);
|
|
|
|
|
2019-10-02 10:46:02 +00:00
|
|
|
_session = std::make_unique<Session>(this, user, std::move(settings));
|
|
|
|
if (!serialized.isEmpty()) {
|
2020-06-10 13:24:41 +00:00
|
|
|
local().readSelf(_session.get(), serialized, streamVersion);
|
2019-10-02 10:46:02 +00:00
|
|
|
}
|
2020-06-10 10:49:10 +00:00
|
|
|
_sessionValue = _session.get();
|
2020-06-16 06:42:47 +00:00
|
|
|
|
|
|
|
Ensures(_session != nullptr);
|
2019-06-06 09:37:12 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 10:36:22 +00:00
|
|
|
void Account::destroySession(DestroyReason reason) {
|
2020-06-18 12:47:09 +00:00
|
|
|
_storedSessionSettings.reset();
|
2019-07-24 11:45:24 +00:00
|
|
|
_sessionUserId = 0;
|
|
|
|
_sessionUserSerialized = {};
|
2019-07-24 08:46:23 +00:00
|
|
|
if (!sessionExists()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-06 09:37:12 +00:00
|
|
|
_sessionValue = nullptr;
|
2020-06-26 10:36:22 +00:00
|
|
|
|
|
|
|
if (reason == DestroyReason::LoggedOut) {
|
|
|
|
_session->finishLogout();
|
|
|
|
}
|
2019-06-06 09:37:12 +00:00
|
|
|
_session = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-04-30 11:12:30 +00:00
|
|
|
bool Account::sessionExists() const {
|
2019-06-06 09:37:12 +00:00
|
|
|
return (_sessionValue.current() != nullptr);
|
2019-04-30 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 13:24:41 +00:00
|
|
|
Session &Account::session() const {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(sessionExists());
|
|
|
|
|
|
|
|
return *_sessionValue.current();
|
|
|
|
}
|
|
|
|
|
2020-06-23 13:29:02 +00:00
|
|
|
Session *Account::maybeSession() const {
|
|
|
|
return _sessionValue.current();
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
rpl::producer<Session*> Account::sessionValue() const {
|
2019-06-06 09:37:12 +00:00
|
|
|
return _sessionValue.value();
|
2019-04-30 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
rpl::producer<Session*> Account::sessionChanges() const {
|
2019-06-06 09:37:12 +00:00
|
|
|
return _sessionValue.changes();
|
2019-06-05 20:41:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
rpl::producer<not_null<MTP::Instance*>> Account::mtpValue() const {
|
|
|
|
return _mtpValue.value() | rpl::map([](MTP::Instance *instance) {
|
|
|
|
return not_null{ instance };
|
|
|
|
});
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 17:32:30 +00:00
|
|
|
rpl::producer<not_null<MTP::Instance*>> Account::mtpMainSessionValue() const {
|
|
|
|
return mtpValue() | rpl::map([=](not_null<MTP::Instance*> instance) {
|
|
|
|
return instance->mainDcIdValue() | rpl::map_to(instance);
|
|
|
|
}) | rpl::flatten_latest();
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:40:52 +00:00
|
|
|
rpl::producer<MTPUpdates> Account::mtpUpdates() const {
|
|
|
|
return _mtpUpdates.events();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpl::producer<> Account::mtpNewSessionCreated() const {
|
|
|
|
return _mtpNewSessionCreated.events();
|
|
|
|
}
|
|
|
|
|
2020-06-29 18:14:16 +00:00
|
|
|
void Account::setMtpMainDcId(MTP::DcId mainDcId) {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(!_mtp);
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
_mtpFields.mainDcId = mainDcId;
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 14:52:44 +00:00
|
|
|
void Account::setLegacyMtpKey(std::shared_ptr<MTP::AuthKey> key) {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(!_mtp);
|
2020-06-10 14:52:44 +00:00
|
|
|
Expects(key != nullptr);
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
_mtpFields.keys.push_back(std::move(key));
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Account::serializeMtpAuthorization() const {
|
|
|
|
const auto serialize = [&](
|
|
|
|
MTP::DcId mainDcId,
|
|
|
|
const MTP::AuthKeysList &keys,
|
|
|
|
const MTP::AuthKeysList &keysToDestroy) {
|
|
|
|
const auto keysSize = [](auto &list) {
|
|
|
|
const auto keyDataSize = MTP::AuthKey::Data().size();
|
|
|
|
return sizeof(qint32)
|
|
|
|
+ list.size() * (sizeof(qint32) + keyDataSize);
|
|
|
|
};
|
|
|
|
const auto writeKeys = [](
|
|
|
|
QDataStream &stream,
|
|
|
|
const MTP::AuthKeysList &keys) {
|
|
|
|
stream << qint32(keys.size());
|
|
|
|
for (const auto &key : keys) {
|
|
|
|
stream << qint32(key->dcId());
|
|
|
|
key->write(stream);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
auto result = QByteArray();
|
2021-04-01 21:04:10 +00:00
|
|
|
// wide tag + userId + mainDcId
|
|
|
|
auto size = 2 * sizeof(quint64) + sizeof(qint32);
|
2019-07-24 08:46:23 +00:00
|
|
|
size += keysSize(keys) + keysSize(keysToDestroy);
|
|
|
|
result.reserve(size);
|
|
|
|
{
|
|
|
|
QDataStream stream(&result, QIODevice::WriteOnly);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
|
|
|
|
|
|
|
const auto currentUserId = sessionExists()
|
|
|
|
? session().userId()
|
2021-04-01 21:04:10 +00:00
|
|
|
: UserId();
|
|
|
|
stream
|
|
|
|
<< quint64(kWideIdsTag)
|
|
|
|
<< quint64(currentUserId.bare)
|
|
|
|
<< qint32(mainDcId);
|
2019-07-24 08:46:23 +00:00
|
|
|
writeKeys(stream, keys);
|
|
|
|
writeKeys(stream, keysToDestroy);
|
|
|
|
|
2021-04-01 21:04:10 +00:00
|
|
|
DEBUG_LOG(("MTP Info: Keys written, userId: %1, dcId: %2"
|
|
|
|
).arg(currentUserId.bare
|
|
|
|
).arg(mainDcId));
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
if (_mtp) {
|
|
|
|
const auto keys = _mtp->getKeysForWrite();
|
|
|
|
const auto keysToDestroy = _mtpForKeysDestroy
|
|
|
|
? _mtpForKeysDestroy->getKeysForWrite()
|
|
|
|
: MTP::AuthKeysList();
|
|
|
|
return serialize(_mtp->mainDcId(), keys, keysToDestroy);
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto &keys = _mtpFields.keys;
|
2019-07-24 08:46:23 +00:00
|
|
|
const auto &keysToDestroy = _mtpKeysToDestroy;
|
2020-06-17 09:36:25 +00:00
|
|
|
return serialize(_mtpFields.mainDcId, keys, keysToDestroy);
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
void Account::setSessionUserId(UserId userId) {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(!sessionExists());
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
_sessionUserId = userId;
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
void Account::setSessionFromStorage(
|
2020-06-18 12:47:09 +00:00
|
|
|
std::unique_ptr<SessionSettings> data,
|
2019-07-24 08:46:23 +00:00
|
|
|
QByteArray &&selfSerialized,
|
|
|
|
int32 selfStreamVersion) {
|
|
|
|
Expects(!sessionExists());
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
DEBUG_LOG(("sessionUserSerialized set: %1"
|
2019-07-24 08:46:23 +00:00
|
|
|
).arg(selfSerialized.size()));
|
|
|
|
|
2020-06-18 12:47:09 +00:00
|
|
|
_storedSessionSettings = std::move(data);
|
2019-07-24 11:45:24 +00:00
|
|
|
_sessionUserSerialized = std::move(selfSerialized);
|
|
|
|
_sessionUserStreamVersion = selfStreamVersion;
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 12:47:09 +00:00
|
|
|
SessionSettings *Account::getSessionSettings() {
|
2019-07-24 11:45:24 +00:00
|
|
|
if (_sessionUserId) {
|
2020-06-18 12:47:09 +00:00
|
|
|
return _storedSessionSettings
|
|
|
|
? _storedSessionSettings.get()
|
|
|
|
: nullptr;
|
2020-06-23 13:29:02 +00:00
|
|
|
} else if (const auto session = maybeSession()) {
|
|
|
|
return &session->settings();
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::setMtpAuthorization(const QByteArray &serialized) {
|
|
|
|
Expects(!_mtp);
|
|
|
|
|
|
|
|
QDataStream stream(serialized);
|
|
|
|
stream.setVersion(QDataStream::Qt_5_1);
|
|
|
|
|
2021-04-01 21:04:10 +00:00
|
|
|
auto legacyUserId = Serialize::read<qint32>(stream);
|
|
|
|
auto legacyMainDcId = Serialize::read<qint32>(stream);
|
|
|
|
auto userId = quint64();
|
|
|
|
auto mainDcId = qint32();
|
|
|
|
if (((uint64(legacyUserId) << 32) | uint64(legacyMainDcId))
|
|
|
|
== kWideIdsTag) {
|
|
|
|
userId = Serialize::read<quint64>(stream);
|
|
|
|
mainDcId = Serialize::read<qint32>(stream);
|
|
|
|
} else {
|
|
|
|
userId = legacyUserId;
|
|
|
|
mainDcId = legacyMainDcId;
|
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
LOG(("MTP Error: "
|
|
|
|
"Could not read main fields from mtp authorization."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
setSessionUserId(userId);
|
2020-06-17 09:36:25 +00:00
|
|
|
_mtpFields.mainDcId = mainDcId;
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
const auto readKeys = [&](auto &keys) {
|
2019-07-24 08:46:23 +00:00
|
|
|
const auto count = Serialize::read<qint32>(stream);
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
LOG(("MTP Error: "
|
|
|
|
"Could not read keys count from mtp authorization."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
keys.reserve(count);
|
|
|
|
for (auto i = 0; i != count; ++i) {
|
|
|
|
const auto dcId = Serialize::read<qint32>(stream);
|
|
|
|
const auto keyData = Serialize::read<MTP::AuthKey::Data>(stream);
|
|
|
|
if (stream.status() != QDataStream::Ok) {
|
|
|
|
LOG(("MTP Error: "
|
|
|
|
"Could not read key from mtp authorization."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
keys.push_back(std::make_shared<MTP::AuthKey>(MTP::AuthKey::Type::ReadFromFile, dcId, keyData));
|
|
|
|
}
|
|
|
|
};
|
2020-06-17 09:36:25 +00:00
|
|
|
readKeys(_mtpFields.keys);
|
2019-07-24 08:46:23 +00:00
|
|
|
readKeys(_mtpKeysToDestroy);
|
|
|
|
LOG(("MTP Info: "
|
|
|
|
"read keys, current: %1, to destroy: %2"
|
2020-06-17 09:36:25 +00:00
|
|
|
).arg(_mtpFields.keys.size()
|
2019-07-24 08:46:23 +00:00
|
|
|
).arg(_mtpKeysToDestroy.size()));
|
|
|
|
}
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
void Account::startMtp(std::unique_ptr<MTP::Config> config) {
|
2019-07-24 08:46:23 +00:00
|
|
|
Expects(!_mtp);
|
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
auto fields = base::take(_mtpFields);
|
|
|
|
fields.config = std::move(config);
|
2020-12-29 07:17:43 +00:00
|
|
|
fields.deviceModel = Platform::DeviceModelPretty();
|
|
|
|
fields.systemVersion = Platform::SystemVersionPretty();
|
2019-07-24 08:46:23 +00:00
|
|
|
_mtp = std::make_unique<MTP::Instance>(
|
|
|
|
MTP::Instance::Mode::Normal,
|
2020-06-17 09:36:25 +00:00
|
|
|
std::move(fields));
|
|
|
|
|
|
|
|
const auto writingKeys = _mtp->lifetime().make_state<bool>(false);
|
2020-06-09 16:57:05 +00:00
|
|
|
_mtp->writeKeysRequests(
|
2020-06-17 09:36:25 +00:00
|
|
|
) | rpl::filter([=] {
|
|
|
|
return !*writingKeys;
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
*writingKeys = true;
|
|
|
|
Ui::PostponeCall(_mtp.get(), [=] {
|
|
|
|
local().writeMtpData();
|
|
|
|
*writingKeys = false;
|
|
|
|
});
|
2020-06-09 16:57:05 +00:00
|
|
|
}, _mtp->lifetime());
|
2020-06-17 09:36:25 +00:00
|
|
|
|
|
|
|
const auto writingConfig = _lifetime.make_state<bool>(false);
|
|
|
|
rpl::merge(
|
|
|
|
_mtp->config().updates(),
|
2020-06-21 16:25:29 +00:00
|
|
|
_mtp->dcOptions().changed() | rpl::to_empty
|
2020-06-17 09:36:25 +00:00
|
|
|
) | rpl::filter([=] {
|
|
|
|
return !*writingConfig;
|
|
|
|
}) | rpl::start_with_next([=] {
|
|
|
|
*writingConfig = true;
|
|
|
|
Ui::PostponeCall(_mtp.get(), [=] {
|
|
|
|
local().writeMtpConfig();
|
|
|
|
*writingConfig = false;
|
|
|
|
});
|
|
|
|
}, _lifetime);
|
|
|
|
|
|
|
|
_mtpFields.mainDcId = _mtp->mainDcId();
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
_mtp->setUpdatesHandler([=](const MTP::Response &message) {
|
|
|
|
checkForUpdates(message) || checkForNewSession(message);
|
|
|
|
});
|
2021-03-12 12:48:00 +00:00
|
|
|
_mtp->setGlobalFailHandler([=](const MTP::Error &, const MTP::Response &) {
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = maybeSession()) {
|
|
|
|
crl::on_main(session, [=] { logOut(); });
|
2019-11-22 09:40:52 +00:00
|
|
|
}
|
2021-03-12 10:45:13 +00:00
|
|
|
});
|
2020-06-11 16:09:46 +00:00
|
|
|
_mtp->setStateChangedHandler([=](MTP::ShiftedDcId dc, int32 state) {
|
|
|
|
if (dc == _mtp->mainDcId()) {
|
2021-06-10 22:49:08 +00:00
|
|
|
Core::App().settings().proxy().connectionTypeChangesNotify();
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
});
|
2020-06-11 09:41:03 +00:00
|
|
|
_mtp->setSessionResetHandler([=](MTP::ShiftedDcId shiftedDcId) {
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = maybeSession()) {
|
2020-06-11 16:09:46 +00:00
|
|
|
if (shiftedDcId == _mtp->mainDcId()) {
|
2020-06-23 13:29:02 +00:00
|
|
|
session->updates().getDifference();
|
2020-06-11 09:41:03 +00:00
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!_mtpKeysToDestroy.empty()) {
|
|
|
|
destroyMtpKeys(base::take(_mtpKeysToDestroy));
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
if (_sessionUserId) {
|
2019-10-02 10:46:02 +00:00
|
|
|
createSession(
|
|
|
|
_sessionUserId,
|
2019-07-24 11:45:24 +00:00
|
|
|
base::take(_sessionUserSerialized),
|
2019-10-02 10:46:02 +00:00
|
|
|
base::take(_sessionUserStreamVersion),
|
2020-06-18 12:47:09 +00:00
|
|
|
(_storedSessionSettings
|
|
|
|
? std::move(_storedSessionSettings)
|
|
|
|
: std::make_unique<SessionSettings>()));
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
2020-06-18 12:47:09 +00:00
|
|
|
_storedSessionSettings = nullptr;
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2020-06-23 13:29:02 +00:00
|
|
|
if (const auto session = maybeSession()) {
|
2020-06-09 16:57:05 +00:00
|
|
|
// Skip all pending self updates so that we won't local().writeSelf.
|
2020-06-23 13:29:02 +00:00
|
|
|
session->changes().sendNotifications();
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_mtpValue = _mtp.get();
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Account::checkForUpdates(const MTP::Response &message) {
|
2019-12-02 09:16:15 +00:00
|
|
|
auto updates = MTPUpdates();
|
2021-03-12 10:45:13 +00:00
|
|
|
auto from = message.reply.constData();
|
|
|
|
if (!updates.read(from, from + message.reply.size())) {
|
2019-12-02 09:16:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_mtpUpdates.fire(std::move(updates));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-12 10:45:13 +00:00
|
|
|
bool Account::checkForNewSession(const MTP::Response &message) {
|
2019-12-02 09:16:15 +00:00
|
|
|
auto newSession = MTPNewSession();
|
2021-03-12 10:45:13 +00:00
|
|
|
auto from = message.reply.constData();
|
|
|
|
if (!newSession.read(from, from + message.reply.size())) {
|
2019-12-02 09:16:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_mtpNewSessionCreated.fire({});
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-24 08:46:23 +00:00
|
|
|
|
|
|
|
void Account::logOut() {
|
2019-11-20 13:33:45 +00:00
|
|
|
if (_loggingOut) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_loggingOut = true;
|
2019-07-24 08:46:23 +00:00
|
|
|
if (_mtp) {
|
2019-11-20 13:33:45 +00:00
|
|
|
_mtp->logout([=] { loggedOut(); });
|
2019-07-24 08:46:23 +00:00
|
|
|
} else {
|
|
|
|
// We log out because we've forgotten passcode.
|
|
|
|
loggedOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:43 +00:00
|
|
|
bool Account::loggingOut() const {
|
|
|
|
return _loggingOut;
|
|
|
|
}
|
|
|
|
|
2019-07-24 08:46:23 +00:00
|
|
|
void Account::forcedLogOut() {
|
|
|
|
if (sessionExists()) {
|
|
|
|
resetAuthorizationKeys();
|
|
|
|
loggedOut();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::loggedOut() {
|
2019-11-20 13:33:45 +00:00
|
|
|
_loggingOut = false;
|
2019-07-24 08:46:23 +00:00
|
|
|
Media::Player::mixer()->stopAndClear();
|
2020-06-26 10:36:22 +00:00
|
|
|
destroySession(DestroyReason::LoggedOut);
|
2020-06-09 16:57:05 +00:00
|
|
|
local().reset();
|
2019-07-24 08:46:23 +00:00
|
|
|
cSetOtherOnline(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::destroyMtpKeys(MTP::AuthKeysList &&keys) {
|
2020-06-17 09:36:25 +00:00
|
|
|
Expects(_mtp != nullptr);
|
|
|
|
|
2019-07-24 08:46:23 +00:00
|
|
|
if (keys.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_mtpForKeysDestroy) {
|
|
|
|
_mtpForKeysDestroy->addKeysForDestroy(std::move(keys));
|
2020-06-09 16:57:05 +00:00
|
|
|
local().writeMtpData();
|
2019-07-24 08:46:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-06-17 09:36:25 +00:00
|
|
|
auto destroyFields = MTP::Instance::Fields();
|
|
|
|
|
|
|
|
destroyFields.mainDcId = MTP::Instance::Fields::kNoneMainDc;
|
|
|
|
destroyFields.config = std::make_unique<MTP::Config>(_mtp->config());
|
|
|
|
destroyFields.keys = std::move(keys);
|
2020-12-29 07:17:43 +00:00
|
|
|
destroyFields.deviceModel = Platform::DeviceModelPretty();
|
|
|
|
destroyFields.systemVersion = Platform::SystemVersionPretty();
|
2019-07-24 08:46:23 +00:00
|
|
|
_mtpForKeysDestroy = std::make_unique<MTP::Instance>(
|
|
|
|
MTP::Instance::Mode::KeysDestroyer,
|
2020-06-17 09:36:25 +00:00
|
|
|
std::move(destroyFields));
|
2020-06-09 16:57:05 +00:00
|
|
|
_mtpForKeysDestroy->writeKeysRequests(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
local().writeMtpData();
|
|
|
|
}, _mtpForKeysDestroy->lifetime());
|
2019-11-27 14:22:22 +00:00
|
|
|
_mtpForKeysDestroy->allKeysDestroyed(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
LOG(("MTP Info: all keys scheduled for destroy are destroyed."));
|
|
|
|
crl::on_main(this, [=] {
|
|
|
|
_mtpForKeysDestroy = nullptr;
|
2020-06-09 16:57:05 +00:00
|
|
|
local().writeMtpData();
|
2019-11-27 14:22:22 +00:00
|
|
|
});
|
2020-06-09 16:57:05 +00:00
|
|
|
}, _mtpForKeysDestroy->lifetime());
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Account::suggestMainDcId(MTP::DcId mainDcId) {
|
|
|
|
Expects(_mtp != nullptr);
|
|
|
|
|
|
|
|
_mtp->suggestMainDcId(mainDcId);
|
2020-06-17 09:36:25 +00:00
|
|
|
if (_mtpFields.mainDcId != MTP::Instance::Fields::kNotSetMainDc) {
|
|
|
|
_mtpFields.mainDcId = mainDcId;
|
2019-07-24 08:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::destroyStaleAuthorizationKeys() {
|
|
|
|
Expects(_mtp != nullptr);
|
|
|
|
|
|
|
|
for (const auto &key : _mtp->getKeysForWrite()) {
|
|
|
|
// Disable this for now.
|
|
|
|
if (key->type() == MTP::AuthKey::Type::ReadFromFile) {
|
|
|
|
_mtpKeysToDestroy = _mtp->getKeysForWrite();
|
|
|
|
LOG(("MTP Info: destroying stale keys, count: %1"
|
|
|
|
).arg(_mtpKeysToDestroy.size()));
|
|
|
|
resetAuthorizationKeys();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Account::resetAuthorizationKeys() {
|
2020-06-17 09:36:25 +00:00
|
|
|
Expects(_mtp != nullptr);
|
2019-07-24 08:46:23 +00:00
|
|
|
|
2020-06-17 09:36:25 +00:00
|
|
|
{
|
|
|
|
const auto old = base::take(_mtp);
|
|
|
|
auto config = std::make_unique<MTP::Config>(old->config());
|
|
|
|
startMtp(std::move(config));
|
|
|
|
}
|
|
|
|
local().writeMtpData();
|
2019-04-30 11:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Main
|