diff options
-rw-r--r-- | protocols/Telegram/src/auth.cpp | 30 | ||||
-rw-r--r-- | protocols/Telegram/src/mt_proto.cpp | 2 | ||||
-rw-r--r-- | protocols/Telegram/src/mt_proto.h | 15 | ||||
-rw-r--r-- | protocols/Telegram/src/server.cpp | 60 | ||||
-rw-r--r-- | protocols/Telegram/src/stdafx.h | 1 | ||||
-rw-r--r-- | protocols/Telegram/src/utils.cpp | 53 |
6 files changed, 90 insertions, 71 deletions
diff --git a/protocols/Telegram/src/auth.cpp b/protocols/Telegram/src/auth.cpp index a2ff59713c..c29053af18 100644 --- a/protocols/Telegram/src/auth.cpp +++ b/protocols/Telegram/src/auth.cpp @@ -27,7 +27,7 @@ INT_PTR CALLBACK CMTProto::EnterPhoneCode(void *param) es.szModuleName = ppro->m_szModuleName; es.caption = TranslateT("Enter secret code sent to your phone"); if (EnterString(&es)) { - ppro->SendQuery(new td::td_api::checkAuthenticationCode(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth); + ppro->SendQuery(new TD::checkAuthenticationCode(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth); mir_free(es.ptszResult); } else ppro->LogOut(); @@ -39,7 +39,7 @@ INT_PTR CALLBACK CMTProto::EnterPassword(void *param) auto *ppro = (CMTProto *)param; CMStringW wszTitle(TranslateT("Enter password")); - auto *pAuth = (td::td_api::authorizationStateWaitPassword *)ppro->pAuthState.get(); + auto *pAuth = (TD::authorizationStateWaitPassword *)ppro->pAuthState.get(); if (!pAuth->password_hint_.empty()) wszTitle.AppendFormat(TranslateT(" (hint: %s)"), Utf2T(pAuth->password_hint_.c_str()).get()); @@ -48,25 +48,25 @@ INT_PTR CALLBACK CMTProto::EnterPassword(void *param) es.caption = wszTitle; es.type = ESF_PASSWORD; if (EnterString(&es)) { - ppro->SendQuery(new td::td_api::checkAuthenticationPassword(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth); + ppro->SendQuery(new TD::checkAuthenticationPassword(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth); mir_free(es.ptszResult); } else ppro->LogOut(); return 0; } -void CMTProto::ProcessAuth(td::td_api::updateAuthorizationState *pObj) +void CMTProto::ProcessAuth(TD::updateAuthorizationState *pObj) { pAuthState = std::move(pObj->authorization_state_); switch (pAuthState->get_id()) { - case td::td_api::authorizationStateWaitTdlibParameters::ID: + case TD::authorizationStateWaitTdlibParameters::ID: { char text[100]; Miranda_GetVersionText(text, sizeof(text)); CMStringW wszPath(GetProtoFolder()); - auto *request = new td::td_api::setTdlibParameters(); + auto *request = new TD::setTdlibParameters(); request->database_directory_ = T2Utf(wszPath).get(); request->use_message_database_ = false; request->use_secret_chats_ = true; @@ -80,28 +80,28 @@ void CMTProto::ProcessAuth(td::td_api::updateAuthorizationState *pObj) } break; - case td::td_api::authorizationStateWaitPhoneNumber::ID: - SendQuery(new td::td_api::setAuthenticationPhoneNumber(_T2A(m_szOwnPhone).get(), nullptr), &CMTProto::OnUpdateAuth); + case TD::authorizationStateWaitPhoneNumber::ID: + SendQuery(new TD::setAuthenticationPhoneNumber(_T2A(m_szOwnPhone).get(), nullptr), &CMTProto::OnUpdateAuth); break; - case td::td_api::authorizationStateWaitCode::ID: + case TD::authorizationStateWaitCode::ID: CallFunctionSync(EnterPhoneCode, this); break; - case td::td_api::authorizationStateWaitPassword::ID: + case TD::authorizationStateWaitPassword::ID: CallFunctionSync(EnterPassword, this); break; - case td::td_api::authorizationStateReady::ID: + case TD::authorizationStateReady::ID: OnLoggedIn(); break; - case td::td_api::authorizationStateLoggingOut::ID: + case TD::authorizationStateLoggingOut::ID: debugLogA("Server required us to log out, exiting"); LogOut(); break; - case td::td_api::authorizationStateClosing::ID: + case TD::authorizationStateClosing::ID: debugLogA("Connection terminated, exiting"); LogOut(); break; @@ -110,8 +110,8 @@ void CMTProto::ProcessAuth(td::td_api::updateAuthorizationState *pObj) void CMTProto::OnUpdateAuth(td::ClientManager::Response &response) { - if (response.object->get_id() == td::td_api::error::ID) { - auto *pError = (td::td_api::error *)response.object.get(); + if (response.object->get_id() == TD::error::ID) { + auto *pError = (TD::error *)response.object.get(); debugLogA("error happened: %s", to_string(*pError).c_str()); if (pError->message_ == "PHONE_CODE_EXPIRED") diff --git a/protocols/Telegram/src/mt_proto.cpp b/protocols/Telegram/src/mt_proto.cpp index 3550329e45..4b7c6fb211 100644 --- a/protocols/Telegram/src/mt_proto.cpp +++ b/protocols/Telegram/src/mt_proto.cpp @@ -111,7 +111,7 @@ int CMTProto::SetStatus(int iNewStatus) if (m_iDesiredStatus == ID_STATUS_OFFLINE) { if (m_bRunning) - SendQuery(new td::td_api::close()); + SendQuery(new TD::close()); m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; ProtoBroadcastAck(NULL, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)oldStatus, m_iStatus); diff --git a/protocols/Telegram/src/mt_proto.h b/protocols/Telegram/src/mt_proto.h index 3c3410cd29..6376a00610 100644 --- a/protocols/Telegram/src/mt_proto.h +++ b/protocols/Telegram/src/mt_proto.h @@ -32,7 +32,7 @@ struct TG_USER class CMTProto : public PROTO<CMTProto> { std::unique_ptr<td::ClientManager> m_pClientMmanager; - td::td_api::object_ptr<td::td_api::AuthorizationState> pAuthState; + TD::object_ptr<TD::AuthorizationState> pAuthState; bool m_bAuthorized, m_bRunning = false, m_bTerminated; int32_t m_iClientId; @@ -52,19 +52,20 @@ class CMTProto : public PROTO<CMTProto> void LogOut(void); void OnLoggedIn(void); void ProcessResponse(td::ClientManager::Response); - void SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr); + void SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler = nullptr); - void ProcessAuth(td::td_api::updateAuthorizationState *pObj); - void ProcessGroups(td::td_api::updateChatFilters *pObj); - void ProcessUser(td::td_api::updateUser *pObj); + void ProcessAuth(TD::updateAuthorizationState *pObj); + void ProcessChat(TD::updateNewChat *pObj); + void ProcessGroups(TD::updateChatFilters *pObj); + void ProcessUser(TD::updateUser *pObj); void UpdateString(MCONTACT hContact, const char *pszSetting, const std::string &str); // Users OBJLIST<TG_USER> m_arUsers; - MCONTACT FindUser(uint64_t id); - MCONTACT AddUser(uint64_t id, bool bIsChat); + TG_USER* FindUser(uint64_t id); + TG_USER* AddUser(uint64_t id, bool bIsChat); // Popups HANDLE m_hPopupClass; diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index 4d45416ab7..b5670b27fc 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -22,7 +22,7 @@ void __cdecl CMTProto::ServerThread(void *) m_bRunning = true; m_bTerminated = m_bAuthorized = false; - SendQuery(new td::td_api::getOption("version")); + SendQuery(new TD::getOption("version")); while (!m_bTerminated) { ProcessResponse(m_pClientMmanager->receive(1)); @@ -55,7 +55,7 @@ void CMTProto::OnLoggedIn() ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, m_iDesiredStatus); m_iStatus = m_iDesiredStatus; - SendQuery(new td::td_api::getChats(td::tl::unique_ptr<td::td_api::chatListMain>(), 1000)); + SendQuery(new TD::getChats(td::tl::unique_ptr<TD::chatListMain>(), 1000)); } /////////////////////////////////////////////////////////////////////////////// @@ -77,25 +77,28 @@ void CMTProto::ProcessResponse(td::ClientManager::Response response) } switch (response.object->get_id()) { - case td::td_api::updateAuthorizationState::ID: - ProcessAuth((td::td_api::updateAuthorizationState *)response.object.get()); + case TD::updateAuthorizationState::ID: + ProcessAuth((TD::updateAuthorizationState *)response.object.get()); break; - case td::td_api::updateChatFilters::ID: - ProcessGroups((td::td_api::updateChatFilters *)response.object.get()); + case TD::updateChatFilters::ID: + ProcessGroups((TD::updateChatFilters *)response.object.get()); break; - case td::td_api::updateUser::ID: - ProcessUser((td::td_api::updateUser *)response.object.get()); + case TD::updateNewChat::ID: + ProcessChat((TD::updateNewChat *)response.object.get()); break; + case TD::updateUser::ID: + ProcessUser((TD::updateUser *)response.object.get()); + break; } } -void CMTProto::SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler) +void CMTProto::SendQuery(TD::Function *pFunc, TG_QUERY_HANDLER pHandler) { int queryId = ++m_iQueryId; - m_pClientMmanager->send(m_iClientId, queryId, td::td_api::object_ptr<td::td_api::Function>(pFunc)); + m_pClientMmanager->send(m_iClientId, queryId, TD::object_ptr<TD::Function>(pFunc)); if (pHandler) m_arRequests.insert(new TG_REQUEST(queryId, pHandler)); @@ -103,7 +106,20 @@ void CMTProto::SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler) /////////////////////////////////////////////////////////////////////////////// -void CMTProto::ProcessGroups(td::td_api::updateChatFilters *pObj) +void CMTProto::ProcessChat(TD::updateNewChat *pObj) +{ + auto &pChat = pObj->chat_; + if (pChat->type_->get_id() != TD::chatTypePrivate::ID) { + debugLogA("Only private chats are currently supported"); + return; + } + + auto *pUser = AddUser(pChat->id_, false); + if (!pChat->title_.empty()) + setUString(pUser->hContact, "Nick", pChat->title_.c_str()); +} + +void CMTProto::ProcessGroups(TD::updateChatFilters *pObj) { for (auto &grp : pObj->chat_filters_) { if (grp->icon_name_ != "Custom") @@ -127,26 +143,26 @@ void CMTProto::ProcessGroups(td::td_api::updateChatFilters *pObj) } } -void CMTProto::ProcessUser(td::td_api::updateUser *pObj) +void CMTProto::ProcessUser(TD::updateUser *pObj) { auto *pUser = pObj->user_.get(); - MCONTACT hContact = AddUser(pUser->id_, false); - UpdateString(hContact, "FirstName", pUser->first_name_); - UpdateString(hContact, "LastName", pUser->last_name_); - UpdateString(hContact, "Phone", pUser->phone_number_); + auto *pu = AddUser(pUser->id_, false); + UpdateString(pu->hContact, "FirstName", pUser->first_name_); + UpdateString(pu->hContact, "LastName", pUser->last_name_); + UpdateString(pu->hContact, "Phone", pUser->phone_number_); if (pUser->usernames_) - UpdateString(hContact, "Nick", pUser->usernames_->editable_username_); + UpdateString(pu->hContact, "Nick", pUser->usernames_->editable_username_); if (pUser->is_premium_) - ExtraIcon_SetIconByName(g_plugin.m_hIcon, hContact, "tg_premium"); + ExtraIcon_SetIconByName(g_plugin.m_hIcon, pu->hContact, "tg_premium"); else - ExtraIcon_SetIconByName(g_plugin.m_hIcon, hContact, nullptr); + ExtraIcon_SetIconByName(g_plugin.m_hIcon, pu->hContact, nullptr); if (pUser->status_) { - if (pUser->status_->get_id() == td::td_api::userStatusOffline::ID) { - auto *pOffline = (td::td_api::userStatusOffline *)pUser->status_.get(); - setDword(hContact, "LastSeen", pOffline->was_online_); + if (pUser->status_->get_id() == TD::userStatusOffline::ID) { + auto *pOffline = (TD::userStatusOffline *)pUser->status_.get(); + setDword(pu->hContact, "LastSeen", pOffline->was_online_); } } } diff --git a/protocols/Telegram/src/stdafx.h b/protocols/Telegram/src/stdafx.h index 957cb73f55..83217608d0 100644 --- a/protocols/Telegram/src/stdafx.h +++ b/protocols/Telegram/src/stdafx.h @@ -25,6 +25,7 @@ #include "td/telegram/Client.h" #include "td/telegram/td_api.h" #include "td/telegram/td_api.hpp" +namespace TD = td::td_api; #define MODULE "Telegram" diff --git a/protocols/Telegram/src/utils.cpp b/protocols/Telegram/src/utils.cpp index 7d16c34246..baf10e031d 100644 --- a/protocols/Telegram/src/utils.cpp +++ b/protocols/Telegram/src/utils.cpp @@ -28,39 +28,40 @@ void CMTProto::UpdateString(MCONTACT hContact, const char *pszSetting, const std ///////////////////////////////////////////////////////////////////////////////////////// // Users -MCONTACT CMTProto::FindUser(uint64_t id) +TG_USER* CMTProto::FindUser(uint64_t id) { if (auto *pCache = m_arUsers.find((TG_USER *)&id)) - return pCache->hContact; + return pCache; - return INVALID_CONTACT_ID; + return nullptr; } -MCONTACT CMTProto::AddUser(uint64_t id, bool bIsChat) +TG_USER* CMTProto::AddUser(uint64_t id, bool bIsChat) { - MCONTACT hContact = FindUser(id); - if (hContact == INVALID_CONTACT_ID) { - hContact = db_add_contact(); - Proto_AddToContact(hContact, m_szModuleName); - - char szId[100]; - _i64toa(id, szId, 10); - - if (bIsChat) { - Clist_SetGroup(hContact, TranslateT("Chat rooms")); - setByte(hContact, "ChatRoom", 1); - setString(hContact, "ChatRoomID", szId); - } - else { - setString(hContact, DBKEY_ID, szId); - if (mir_wstrlen(m_wszDefaultGroup)) - Clist_SetGroup(hContact, m_wszDefaultGroup); - } - - m_arUsers.insert(new TG_USER(id, hContact, bIsChat)); + auto *pUser = FindUser(id); + if (pUser != nullptr) + return pUser; + + MCONTACT hContact = db_add_contact(); + Proto_AddToContact(hContact, m_szModuleName); + + char szId[100]; + _i64toa(id, szId, 10); + + if (bIsChat) { + Clist_SetGroup(hContact, TranslateT("Chat rooms")); + setByte(hContact, "ChatRoom", 1); + setString(hContact, "ChatRoomID", szId); } - - return hContact; + else { + setString(hContact, DBKEY_ID, szId); + if (mir_wstrlen(m_wszDefaultGroup)) + Clist_SetGroup(hContact, m_wszDefaultGroup); + } + + pUser = new TG_USER(id, hContact, bIsChat); + m_arUsers.insert(pUser); + return pUser; } ///////////////////////////////////////////////////////////////////////////////////////// |