2020-02-16 17:46:52 +01:00
|
|
|
#ifndef _TD_CLIENT_H
|
|
|
|
#define _TD_CLIENT_H
|
|
|
|
|
2020-02-23 14:02:47 +01:00
|
|
|
#include "account-data.h"
|
2020-02-22 11:42:04 +01:00
|
|
|
#include <purple.h>
|
2020-02-16 17:46:52 +01:00
|
|
|
#include <td/telegram/Client.h>
|
|
|
|
#include <td/telegram/td_api.hpp>
|
|
|
|
|
|
|
|
#include <memory>
|
2020-02-22 11:10:30 +01:00
|
|
|
#include <thread>
|
|
|
|
#include <atomic>
|
2020-02-22 11:42:04 +01:00
|
|
|
#include <map>
|
2020-02-22 14:58:32 +01:00
|
|
|
#include <mutex>
|
2020-02-22 23:15:43 +01:00
|
|
|
#include <list>
|
2020-02-16 17:46:52 +01:00
|
|
|
|
|
|
|
class UpdateHandler;
|
|
|
|
class AuthUpdateHandler;
|
|
|
|
|
|
|
|
class PurpleTdClient {
|
|
|
|
public:
|
2020-02-22 11:42:04 +01:00
|
|
|
PurpleTdClient(PurpleAccount *acct);
|
2020-02-16 17:46:52 +01:00
|
|
|
~PurpleTdClient();
|
|
|
|
|
2020-02-17 23:30:24 +01:00
|
|
|
static void setLogLevel(int level);
|
2020-02-16 17:46:52 +01:00
|
|
|
void startLogin();
|
2020-02-27 21:49:02 +01:00
|
|
|
int sendMessage(const char *buddyName, const char *message);
|
2020-02-16 17:46:52 +01:00
|
|
|
private:
|
|
|
|
friend class UpdateHandler;
|
|
|
|
friend class AuthUpdateHandler;
|
2020-02-22 14:58:32 +01:00
|
|
|
using TdObjectPtr = td::td_api::object_ptr<td::td_api::Object>;
|
|
|
|
using ResponseCb = void (PurpleTdClient::*)(uint64_t requestId, TdObjectPtr object);
|
|
|
|
using TdErrorPtr = td::td_api::object_ptr<td::td_api::error>;
|
|
|
|
using TdAuthCodePtr = td::td_api::object_ptr<td::td_api::authenticationCodeInfo>;
|
2020-02-16 17:46:52 +01:00
|
|
|
|
2020-02-22 23:15:43 +01:00
|
|
|
// All non-static response handling functions are called from the poll thread
|
|
|
|
// Static response handling functions are called from main thread via g_idle_add
|
|
|
|
// Functions sending requests can be called from either thread
|
2020-02-22 11:10:30 +01:00
|
|
|
void pollThreadLoop();
|
2020-02-16 17:46:52 +01:00
|
|
|
void processResponse(td::Client::Response response);
|
2020-02-22 11:10:30 +01:00
|
|
|
void sendTdlibParameters();
|
2020-02-22 12:08:02 +01:00
|
|
|
void sendPhoneNumber();
|
2020-02-22 14:58:32 +01:00
|
|
|
static int requestAuthCode(gpointer user_data);
|
|
|
|
static void requestCodeEntered(PurpleTdClient *self, const gchar *code);
|
|
|
|
static void requestCodeCancelled(PurpleTdClient *self);
|
2020-02-22 23:15:43 +01:00
|
|
|
uint64_t sendQuery(td::td_api::object_ptr<td::td_api::Function> f, ResponseCb handler);
|
2020-02-22 11:42:04 +01:00
|
|
|
|
2020-02-22 15:53:07 +01:00
|
|
|
void authResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);
|
2020-02-22 13:23:01 +01:00
|
|
|
static int notifyAuthError(gpointer user_data);
|
2020-02-22 15:53:07 +01:00
|
|
|
void connectionReady();
|
2020-02-29 15:30:24 +01:00
|
|
|
static int setPurpleConnectionInProgress(gpointer user_data);
|
|
|
|
static int setPurpleConnectionUpdating(gpointer user_data);
|
2020-02-22 19:03:14 +01:00
|
|
|
void getChatsResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);
|
2020-02-29 15:30:24 +01:00
|
|
|
// List of chats is requested after connection is ready, and when response is received,
|
|
|
|
// then we report to libpurple that we are connected
|
|
|
|
static int updatePurpleChatListAndReportConnected(gpointer user_data);
|
2020-02-22 23:15:43 +01:00
|
|
|
static int showUnreadMessages(gpointer user_data);
|
|
|
|
void showMessage(const td::td_api::message &message);
|
2020-02-27 21:16:12 +01:00
|
|
|
void onIncomingMessage(td::td_api::object_ptr<td::td_api::message> message);
|
2020-02-27 22:27:56 +01:00
|
|
|
void updateUserStatus(uint32_t userId, td::td_api::object_ptr<td::td_api::UserStatus> status);
|
|
|
|
static int showUpdatedStatus(gpointer user_data);
|
2020-02-22 13:23:01 +01:00
|
|
|
|
2020-02-22 11:42:04 +01:00
|
|
|
PurpleAccount *m_account;
|
|
|
|
std::unique_ptr<UpdateHandler> m_updateHandler;
|
|
|
|
std::unique_ptr<AuthUpdateHandler> m_authUpdateHandler;
|
|
|
|
std::unique_ptr<td::Client> m_client;
|
|
|
|
std::thread m_pollThread;
|
|
|
|
std::atomic_bool m_stopThread;
|
2020-02-22 14:58:32 +01:00
|
|
|
std::atomic<uint64_t> m_lastQueryId;
|
2020-02-22 11:42:04 +01:00
|
|
|
std::map<std::uint64_t, ResponseCb> m_responseHandlers;
|
2020-02-22 23:15:43 +01:00
|
|
|
std::mutex m_queryMutex;
|
2020-02-22 19:03:14 +01:00
|
|
|
|
2020-02-23 14:02:47 +01:00
|
|
|
TdAccountData m_data;
|
2020-02-22 13:23:01 +01:00
|
|
|
|
2020-02-22 14:58:32 +01:00
|
|
|
int32_t m_lastAuthState = 0;
|
|
|
|
TdErrorPtr m_authError;
|
|
|
|
TdAuthCodePtr m_authCodeInfo;
|
2020-02-16 17:46:52 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|