2020-02-16 17:46:52 +01:00
|
|
|
#ifndef _TD_CLIENT_H
|
|
|
|
#define _TD_CLIENT_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.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;
|
|
|
|
|
2020-02-22 23:15:43 +01:00
|
|
|
struct RequestHistoryState {
|
|
|
|
uint64_t queryId;
|
|
|
|
int64_t chatId;
|
|
|
|
int64_t lastReadInId;
|
|
|
|
int64_t lastReadOutId;
|
|
|
|
int64_t oldestSeenInId;
|
|
|
|
int64_t oldestSeenOutId;
|
|
|
|
bool inboxFinished;
|
|
|
|
bool outboxFinished;
|
|
|
|
|
|
|
|
std::vector<td::td_api::object_ptr<td::td_api::message>> messages;
|
|
|
|
};
|
|
|
|
|
2020-02-16 17:46:52 +01:00
|
|
|
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();
|
|
|
|
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-22 19:03:14 +01:00
|
|
|
using TdUserPtr = td::td_api::object_ptr<td::td_api::user>;
|
|
|
|
using TdChatPtr = td::td_api::object_ptr<td::td_api::chat>;
|
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
|
|
|
void retreiveUnreadHistory(int64_t chatId, int64_t lastReadInId, int64_t lastReadOutId);
|
|
|
|
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();
|
|
|
|
static int setPurpleConnectionReady(gpointer user_data);
|
2020-02-22 19:03:14 +01:00
|
|
|
void updateUser(TdUserPtr user);
|
|
|
|
void addNewChat(TdChatPtr chat);
|
|
|
|
void getChatsResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);
|
|
|
|
static int updatePurpleChatList(gpointer user_data);
|
2020-02-22 23:15:43 +01:00
|
|
|
void chatHistoryResponse(uint64_t requestId, td::td_api::object_ptr<td::td_api::Object> object);
|
|
|
|
static int showUnreadMessages(gpointer user_data);
|
|
|
|
void showMessage(const td::td_api::message &message);
|
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
|
|
|
|
|
|
|
std::map<int32_t, TdUserPtr> m_userInfo;
|
|
|
|
std::map<int64_t, TdChatPtr> m_chatInfo;
|
|
|
|
// m_chatInfo can contain chats that are not in m_activeChats if some other chat contains
|
|
|
|
// messages forwarded from another channel
|
|
|
|
std::vector<int64_t> m_activeChats;
|
2020-02-22 23:15:43 +01:00
|
|
|
std::vector<RequestHistoryState> m_chatHistoryRequests;
|
2020-02-22 14:58:32 +01:00
|
|
|
std::mutex m_dataMutex;
|
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
|