#pragma once #define DBKEY_ID "id" class CMTProto; typedef void (CMTProto:: *TG_QUERY_HANDLER)(td::ClientManager::Response &response); typedef void (CMTProto:: *TG_QUERY_HANDLER_FULL)(td::ClientManager::Response &response, void *pUserInfo); struct TG_REQUEST_BASE { TG_REQUEST_BASE(td::ClientManager::RequestId _1) : requestId(_1) {} virtual ~TG_REQUEST_BASE() {} td::ClientManager::RequestId requestId; virtual void Execute(CMTProto *ppro, td::ClientManager::Response &response) = 0; }; struct TG_REQUEST : public TG_REQUEST_BASE { TG_REQUEST(td::ClientManager::RequestId _1, TG_QUERY_HANDLER _2) : TG_REQUEST_BASE(_1), pHandler(_2) {} TG_QUERY_HANDLER pHandler; void Execute(CMTProto *ppro, td::ClientManager::Response &response) override { (ppro->*pHandler)(response); } }; struct TG_REQUEST_FULL : public TG_REQUEST_BASE { TG_REQUEST_FULL(td::ClientManager::RequestId _1, TG_QUERY_HANDLER_FULL _2, void *_3) : TG_REQUEST_BASE(_1), pHandler(_2), pUserInfo(_3) {} TG_QUERY_HANDLER_FULL pHandler; void *pUserInfo; void Execute(CMTProto *ppro, td::ClientManager::Response &response) override { (ppro->*pHandler)(response, pUserInfo); } }; ///////////////////////////////////////////////////////////////////////////////////////// struct TG_USER { TG_USER(uint64_t _1, MCONTACT _2, bool _3 = false) : id(_1), hContact(_2), isGroupChat(_3) {} uint64_t id; MCONTACT hContact; bool isGroupChat; time_t m_timer1 = 0, m_timer2 = 0; }; class CMTProto : public PROTO { class CProtoImpl { friend class CMTProto; CMTProto &m_proto; CTimer m_keepAlive; void OnKeepAlive(CTimer *) { m_proto.SendKeepAlive(); } CProtoImpl(CMTProto &pro) : m_proto(pro), m_keepAlive(Miranda_GetSystemWindow(), UINT_PTR(this)) { m_keepAlive.OnEvent = Callback(this, &CProtoImpl::OnKeepAlive); } } m_impl; std::unique_ptr m_pClientMmanager; TD::object_ptr pAuthState; bool m_bAuthorized, m_bRunning = false, m_bTerminated; int32_t m_iClientId, m_iMsgId; uint64_t m_iQueryId; OBJLIST m_arRequests; static INT_PTR CALLBACK EnterPhoneCode(void *param); static INT_PTR CALLBACK EnterPassword(void *param); CMStringW GetProtoFolder() const { return CMStringW(VARSW(L"%miranda_userdata%")) + L"\\" + _A2T(m_szModuleName); } void OnUpdateAuth(td::ClientManager::Response &response); void LogOut(void); void OnLoggedIn(void); void ProcessResponse(td::ClientManager::Response); void SendKeepAlive(void); void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr); void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER_FULL pHandler, void *pUserInfo); void ProcessAuth(TD::updateAuthorizationState *pObj); void ProcessChat(TD::updateNewChat *pObj); void ProcessGroups(TD::updateChatFilters *pObj); void ProcessMessage(TD::updateNewMessage *pObj); void ProcessStatus(TD::updateUserStatus *pObj); void ProcessUser(TD::updateUser *pObj); void OnSendMessage(td::ClientManager::Response &response, void *pUserInfo); int SendTextMessage(uint64_t chatId, const char *pszMessage); void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str); // Users int64_t m_iOwnId; OBJLIST m_arUsers; TG_USER* FindUser(uint64_t id); TG_USER* AddUser(uint64_t id, bool bIsChat); // Popups HANDLE m_hPopupClass; void InitPopups(void); void Popup(MCONTACT hContact, const wchar_t *szMsg, const wchar_t *szTitle); public: ////////////////////////////////////////////////////////////////////////////////////// // Ctors CMTProto(const char *protoName, const wchar_t *userName); ~CMTProto(); ////////////////////////////////////////////////////////////////////////////////////// // Virtual functions INT_PTR GetCaps(int type, MCONTACT hContact = NULL) override; int SendMsg(MCONTACT hContact, int flags, const char *pszMessage) override; int SetStatus(int iNewStatus) override; void OnModulesLoaded() override; void OnShutdown() override; void OnErase() override; // Services ////////////////////////////////////////////////////////////////////////// INT_PTR __cdecl SvcCreateAccMgrUI(WPARAM, LPARAM); // Events //////////////////////////////////////////////////////////////////////////// int __cdecl OnOptionsInit(WPARAM, LPARAM); // Options /////////////////////////////////////////////////////////////////////////// CMOption m_szOwnPhone; // our own phone number CMOption m_wszDefaultGroup; // clist group to store contacts CMOption m_bHideGroupchats; // do not open chat windows on creation CMOption m_bUsePopups; // Processing Threads //////////////////////////////////////////////////////////////// void __cdecl ServerThread(void *); };