#include "td-client.h" #include "config.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"); m_owner->m_lastAuthState = update_authorization_state.authorization_state_->get_id(); td::td_api::downcast_call(*update_authorization_state.authorization_state_, *m_owner->m_authUpdateHandler); } void operator()(td::td_api::updateConnectionState &connectionUpdate) { purple_debug_misc(config::pluginId, "Incoming update: connection state\n"); if (connectionUpdate.state_ && (connectionUpdate.state_->get_id() == td::td_api::connectionStateReady::ID)) { g_idle_add(PurpleTdClient::connectionReady, m_owner); } } 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"); m_owner->sendQuery(td::td_api::make_object(""), &PurpleTdClient::authResponse); } void operator()(td::td_api::authorizationStateWaitTdlibParameters &) const { purple_debug_misc(config::pluginId, "Authorization state update: TDLib parameters requested\n"); m_owner->sendTdlibParameters(); } void operator()(td::td_api::authorizationStateWaitPhoneNumber &) const { purple_debug_misc(config::pluginId, "Authorization state update: phone number requested\n"); m_owner->sendPhoneNumber(); } void operator()(td::td_api::authorizationStateWaitCode &codeState) const { purple_debug_misc(config::pluginId, "Authorization state update: authentication code requested\n"); m_owner->m_authCodeInfo = std::move(codeState.code_info_); g_idle_add(PurpleTdClient::requestAuthCode, m_owner); } void operator()(td::td_api::authorizationStateReady &) const { purple_debug_misc(config::pluginId, "Authorization state update: ready\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(PurpleAccount *acct) { m_account = acct; m_updateHandler = std::make_unique(this); m_authUpdateHandler = std::make_unique(this); } PurpleTdClient::~PurpleTdClient() { m_stopThread = true; m_client->send({UINT64_MAX, td::td_api::make_object()}); m_pollThread.join(); } void PurpleTdClient::setLogLevel(int level) { // Why not just call setLogVerbosityLevel? No idea! td::Client::execute({0, td::td_api::make_object(level)}); } void PurpleTdClient::startLogin() { #if !GLIB_CHECK_VERSION(2, 32, 0) // GLib threading system is automaticaly initialized since 2.32. // For earlier versions, it have to be initialized before calling any // Glib or GTK+ functions. if (!g_thread_supported()) g_thread_init(NULL); #endif m_client = std::make_unique(); if (!m_pollThread.joinable()) { m_lastQueryId = 0; m_stopThread = false; m_pollThread = std::thread([this]() { pollThreadLoop(); }); } } void PurpleTdClient::pollThreadLoop() { while (!m_stopThread) 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 { ResponseCb callback = nullptr; { std::unique_lock dataLock(m_dataMutex); auto it = m_responseHandlers.find(response.id); if (it != m_responseHandlers.end()) { callback = it->second; m_responseHandlers.erase(it); } } if (callback) (this->*callback)(response.id, std::move(response.object)); } } else purple_debug_misc(config::pluginId, "Response id %lu timed out or something\n", response.id); } void PurpleTdClient::sendTdlibParameters() { auto parameters = td::td_api::make_object(); const char *username = purple_account_get_username(m_account); parameters->database_directory_ = std::string(purple_user_dir()) + G_DIR_SEPARATOR_S + config::configSubdir + G_DIR_SEPARATOR_S + username; purple_debug_misc(config::pluginId, "Account %s using database directory %s\n", username, parameters->database_directory_.c_str()); parameters->use_message_database_ = true; parameters->use_secret_chats_ = true; parameters->api_id_ = 94575; parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; parameters->system_language_code_ = "en"; parameters->device_model_ = "Desktop"; parameters->system_version_ = "Unknown"; parameters->application_version_ = "1.0"; parameters->enable_storage_optimizer_ = true; sendQuery(td::td_api::make_object(std::move(parameters)), &PurpleTdClient::authResponse); } void PurpleTdClient::sendPhoneNumber() { const char *number = purple_account_get_username(m_account); sendQuery(td::td_api::make_object(number, nullptr), &PurpleTdClient::authResponse); } static std::string getAuthCodeDesc(const td::td_api::AuthenticationCodeType &codeType) { switch (codeType.get_id()) { case td::td_api::authenticationCodeTypeTelegramMessage::ID: return "Telegram message (length: " + std::to_string(static_cast(codeType).length_) + ")"; case td::td_api::authenticationCodeTypeSms::ID: return "SMS (length: " + std::to_string(static_cast(codeType).length_) + ")"; case td::td_api::authenticationCodeTypeCall::ID: return "Phone call (length: " + std::to_string(static_cast(codeType).length_) + ")"; case td::td_api::authenticationCodeTypeFlashCall::ID: return "Poor man's phone call (pattern: " + static_cast(codeType).pattern_ + ")"; default: return "Pigeon post"; } } int PurpleTdClient::requestAuthCode(gpointer user_data) { PurpleTdClient *self = static_cast(user_data); std::string message = "Enter authentication code\n"; if (self->m_authCodeInfo) { if (self->m_authCodeInfo->type_) message += "Code sent via: " + getAuthCodeDesc(*self->m_authCodeInfo->type_) + "\n"; if (self->m_authCodeInfo->next_type_) message += "Next code will be: " + getAuthCodeDesc(*self->m_authCodeInfo->next_type_) + "\n"; } if (!purple_request_input (purple_account_get_connection(self->m_account), (char *)"Login code", message.c_str(), NULL, // secondary message NULL, // default value FALSE, // multiline input FALSE, // masked input (char *)"the code", (char *)"OK", G_CALLBACK(requestCodeEntered), (char *)"Cancel", G_CALLBACK(requestCodeCancelled), self->m_account, NULL, // buddy NULL, // conversation self)) { purple_connection_set_state (purple_account_get_connection(self->m_account), PURPLE_CONNECTED); PurpleConversation *conv = purple_conversation_new (PURPLE_CONV_TYPE_IM, self->m_account, "Telegram"); purple_conversation_write (conv, "Telegram", "Authentication code needs to be entered but this libpurple won't cooperate", (PurpleMessageFlags)(PURPLE_MESSAGE_RECV | PURPLE_MESSAGE_SYSTEM), 0); } return FALSE; // This idle handler will not be called again } void PurpleTdClient::requestCodeEntered(PurpleTdClient *self, const gchar *code) { purple_debug_misc(config::pluginId, "Authentication code entered: '%s'\n", code); self->sendQuery(td::td_api::make_object(code), &PurpleTdClient::authResponse); } void PurpleTdClient::requestCodeCancelled(PurpleTdClient *self) { purple_connection_error(purple_account_get_connection(self->m_account), "Authentication code required"); } void PurpleTdClient::sendQuery(td::td_api::object_ptr f, ResponseCb handler) { uint64_t queryId = ++m_lastQueryId; purple_debug_misc(config::pluginId, "Sending query id %lu\n", (unsigned long)queryId); if (handler) { std::unique_lock dataLock(m_dataMutex); m_responseHandlers.emplace(queryId, std::move(handler)); } m_client->send({queryId, std::move(f)}); } void PurpleTdClient::authResponse(uint64_t requestId, td::td_api::object_ptr object) { if (object->get_id() == td::td_api::error::ID) { td::td_api::object_ptr error = td::move_tl_object_as(object); purple_debug_misc(config::pluginId, "Authentication error on query %lu (auth step %d): code %d (%s)\n", (unsigned long)requestId, (int)m_lastAuthState, (int)error->code_, error->message_.c_str()); m_authError = std::move(error); g_idle_add(notifyAuthError, this); } else purple_debug_misc(config::pluginId, "Authentication success on query %lu\n", (unsigned long)requestId); } int PurpleTdClient::notifyAuthError(gpointer user_data) { PurpleTdClient *self = static_cast(user_data); std::string message; switch (self->m_lastAuthState) { case td::td_api::authorizationStateWaitEncryptionKey::ID: message = "Error applying database encryption key"; break; case td::td_api::authorizationStateWaitPhoneNumber::ID: message = "Authentication error after sending phone number"; break; default: message = "Authentication error"; } if (self->m_authError) { message += ": code " + std::to_string(self->m_authError->code_) + " (" + self->m_authError->message_ + ")"; self->m_authError.reset(); } purple_connection_error(purple_account_get_connection(self->m_account), message.c_str()); return FALSE; // This idle handler will not be called again } int PurpleTdClient::connectionReady(gpointer user_data) { PurpleTdClient *self = static_cast(user_data); purple_connection_set_state (purple_account_get_connection(self->m_account), PURPLE_CONNECTED); purple_blist_add_account(self->m_account); return FALSE; // This idle handler will not be called again }