84 lines
2.6 KiB
C++
84 lines
2.6 KiB
C++
#include <cstdint>
|
|
#include <deque>
|
|
#include <functional>
|
|
#include <iostream>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <mutex>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
#include <uv.h>
|
|
#include <td/telegram/Client.h>
|
|
#include <td/telegram/td_api.h>
|
|
#include <td/telegram/td_api.hpp>
|
|
|
|
namespace tg {
|
|
namespace td_api = td::td_api;
|
|
using TgObject = td_api::object_ptr<td_api::Object>;
|
|
|
|
namespace detail {
|
|
template <class... Fs>
|
|
struct overload;
|
|
|
|
template <class F>
|
|
struct overload<F> : public F {
|
|
explicit overload(F f) : F(f) {
|
|
}
|
|
};
|
|
template <class F, class... Fs>
|
|
struct overload<F, Fs...>
|
|
: public overload<F>
|
|
, public overload<Fs...> {
|
|
overload(F f, Fs... fs) : overload<F>(f), overload<Fs...>(fs...) {
|
|
}
|
|
using overload<F>::operator();
|
|
using overload<Fs...>::operator();
|
|
};
|
|
} // namespace detail
|
|
|
|
typedef std::function<void(std::function<void(std::string)>)> AuthCodeProvider;
|
|
typedef std::function<void(std::function<void(std::string)>)> PasswordProvider;
|
|
|
|
class TelegramClient {
|
|
public:
|
|
TelegramClient(uv_loop_t *eventLoop, long apiId, std::string apiHash, std::string phoneNumber);
|
|
~TelegramClient();
|
|
void add_update_handler(std::function<void(void*, td_api::Object&)> handler, void *context = nullptr);
|
|
void send_query(td_api::object_ptr<td_api::Function> f, std::function<void(TgObject)> callback);
|
|
bool start();
|
|
|
|
AuthCodeProvider authCodeProvider;
|
|
PasswordProvider passwordProvider;
|
|
private:
|
|
static void uv_callback(uv_async_t *h);
|
|
static void uv_close_handle_callback(uv_handle_t *h);
|
|
void run_thread();
|
|
void run_telegram_main_loop();
|
|
bool process_response(td::ClientManager::Response response);
|
|
void dispatch_response(std::uint64_t queryId, TgObject response);
|
|
void dispatch_update(TgObject &update);
|
|
void set_tdlib_parameters();
|
|
void set_phone_number();
|
|
void set_code(std::string code);
|
|
void set_password(std::string password);
|
|
|
|
bool m_running = false;
|
|
bool m_authorized = false;
|
|
std::uint64_t m_nextQueryId = 0;
|
|
std::map<std::uint64_t, std::function<void(TgObject)>> m_handlers;
|
|
std::vector<std::pair<std::function<void(void*, td_api::Object&)>, void*>> m_updateHandlers;
|
|
std::thread m_thread;
|
|
std::unique_ptr<td::ClientManager> m_clientManager;
|
|
std::int32_t m_clientId;
|
|
uv_loop_t *m_eventLoop;
|
|
uv_async_t *m_uvHandle = nullptr;
|
|
long m_apiId;
|
|
std::string m_apiHash;
|
|
std::string m_phoneNumber;
|
|
std::string m_password;
|
|
std::deque<td::ClientManager::Response> m_responseQueue;
|
|
std::mutex m_responseQueueMutex;
|
|
};
|
|
} |