2
0
mirror of https://github.com/ars3niy/tdlib-purple synced 2025-08-22 18:07:26 +00:00
tdlib-purple/td-client.cpp

70 lines
2.1 KiB
C++
Raw Normal View History

#include "td-client.h"
#include "config.h"
#include <purple.h>
class UpdateHandler {
public:
UpdateHandler(PurpleTdClient *owner) : m_owner(owner) {}
void operator()(td::td_api::updateAuthorizationState &update_authorization_state) const {
purple_debug_misc(config::pluginId, "Incoming update: authorization state\n");
td::td_api::downcast_call(*update_authorization_state.authorization_state_, *m_owner->m_authUpdateHandler);
}
void operator()(auto &update) const {
purple_debug_misc(config::pluginId, "Incoming update: ignorig ID=%d\n", update.get_id());
}
private:
PurpleTdClient *m_owner;
};
class AuthUpdateHandler {
public:
AuthUpdateHandler(PurpleTdClient *owner) : m_owner(owner) {}
void operator()(td::td_api::authorizationStateWaitEncryptionKey &) const {
purple_debug_misc(config::pluginId, "Authorization state update: encriytion key requested\n");
}
void operator()(auto &update) const {
purple_debug_misc(config::pluginId, "Authorization state update: ignorig ID=%d\n", update.get_id());
}
private:
PurpleTdClient *m_owner;
};
PurpleTdClient::PurpleTdClient()
{
m_updateHandler = std::make_unique<UpdateHandler>(this);
m_authUpdateHandler = std::make_unique<AuthUpdateHandler>(this);
}
PurpleTdClient::~PurpleTdClient()
{
}
void PurpleTdClient::setLogLevel(int level)
{
// Why not just call setLogVerbosityLevel? No idea!
td::Client::execute({0, td::td_api::make_object<td::td_api::setLogVerbosityLevel>(level)});
}
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) {
if (response.id == 0) {
purple_debug_misc(config::pluginId, "Incoming update\n");
td::td_api::downcast_call(*response.object, *m_updateHandler);
} else {
// Response to a request
}
} else
purple_debug_misc(config::pluginId, "Response id %lu timed out or something\n", response.id);
}