2020-02-16 17:46:52 +01:00
|
|
|
#include "td-client.h"
|
2020-02-16 17:59:19 +01:00
|
|
|
#include "config.h"
|
2020-02-16 17:46:52 +01:00
|
|
|
#include <purple.h>
|
|
|
|
|
|
|
|
class UpdateHandler {
|
|
|
|
public:
|
|
|
|
UpdateHandler(PurpleTdClient *owner) : m_owner(owner) {}
|
|
|
|
|
|
|
|
void operator()(td::td_api::updateAuthorizationState &update_authorization_state) const {
|
2020-02-16 17:59:19 +01:00
|
|
|
purple_debug_misc(config::pluginId, "Incoming update: authorization state\n");
|
2020-02-16 17:46:52 +01:00
|
|
|
td::td_api::downcast_call(*update_authorization_state.authorization_state_, *m_owner->m_authUpdateHandler);
|
|
|
|
}
|
|
|
|
|
2020-02-16 17:59:19 +01:00
|
|
|
void operator()(auto &update) const {
|
|
|
|
purple_debug_misc(config::pluginId, "Incoming update: ignorig ID=%d\n", update.get_id());
|
|
|
|
}
|
2020-02-16 17:46:52 +01:00
|
|
|
private:
|
|
|
|
PurpleTdClient *m_owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AuthUpdateHandler {
|
|
|
|
public:
|
|
|
|
AuthUpdateHandler(PurpleTdClient *owner) : m_owner(owner) {}
|
|
|
|
|
|
|
|
void operator()(td::td_api::authorizationStateWaitEncryptionKey &) const {
|
2020-02-16 17:59:19 +01:00
|
|
|
purple_debug_misc(config::pluginId, "Authorization state update: encriytion key requested\n");
|
2020-02-16 17:46:52 +01:00
|
|
|
}
|
|
|
|
|
2020-02-16 17:59:19 +01:00
|
|
|
void operator()(auto &update) const {
|
|
|
|
purple_debug_misc(config::pluginId, "Authorization state update: ignorig ID=%d\n", update.get_id());
|
|
|
|
}
|
2020-02-16 17:46:52 +01:00
|
|
|
private:
|
|
|
|
PurpleTdClient *m_owner;
|
|
|
|
};
|
|
|
|
|
|
|
|
PurpleTdClient::PurpleTdClient()
|
|
|
|
{
|
|
|
|
m_updateHandler = std::make_unique<UpdateHandler>(this);
|
|
|
|
m_authUpdateHandler = std::make_unique<AuthUpdateHandler>(this);
|
2020-02-16 18:19:12 +01:00
|
|
|
|
|
|
|
if (purple_debug_is_verbose())
|
|
|
|
// Log everything
|
|
|
|
// Why not just call setLogVerbosityLevel? No idea!
|
|
|
|
td::Client::execute({0, td::td_api::make_object<td::td_api::setLogVerbosityLevel>(1024)});
|
|
|
|
else if (purple_debug_is_enabled())
|
|
|
|
// Log up to info
|
|
|
|
td::Client::execute({0, td::td_api::make_object<td::td_api::setLogVerbosityLevel>(3)});
|
|
|
|
else
|
|
|
|
// Log up to fatal errors and errors
|
|
|
|
td::Client::execute({0, td::td_api::make_object<td::td_api::setLogVerbosityLevel>(1)});
|
2020-02-16 17:46:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PurpleTdClient::~PurpleTdClient()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void PurpleTdClient::startLogin()
|
|
|
|
{
|
|
|
|
m_client = std::make_unique<td::Client>();
|
|
|
|
processResponse(m_client->receive(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PurpleTdClient::processResponse(td::Client::Response response)
|
|
|
|
{
|
|
|
|
if (response.object) {
|
2020-02-16 17:59:19 +01:00
|
|
|
if (response.id == 0) {
|
|
|
|
purple_debug_misc(config::pluginId, "Incoming update\n");
|
2020-02-16 17:46:52 +01:00
|
|
|
td::td_api::downcast_call(*response.object, *m_updateHandler);
|
2020-02-16 17:59:19 +01:00
|
|
|
} else {
|
2020-02-16 17:46:52 +01:00
|
|
|
// Response to a request
|
|
|
|
}
|
2020-02-16 17:59:19 +01:00
|
|
|
} else
|
|
|
|
purple_debug_misc(config::pluginId, "Response id %lu timed out or something\n", response.id);
|
2020-02-16 17:46:52 +01:00
|
|
|
}
|