diff options
Diffstat (limited to 'protocols/Telegram/src/server.cpp')
-rw-r--r-- | protocols/Telegram/src/server.cpp | 163 |
1 files changed, 41 insertions, 122 deletions
diff --git a/protocols/Telegram/src/server.cpp b/protocols/Telegram/src/server.cpp index a852c1ac93..6700034f60 100644 --- a/protocols/Telegram/src/server.cpp +++ b/protocols/Telegram/src/server.cpp @@ -54,113 +54,51 @@ 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)); } /////////////////////////////////////////////////////////////////////////////// -INT_PTR CALLBACK CMTProto::EnterPhoneCode(void *param) +void CMTProto::ProcessResponse(td::ClientManager::Response response) { - auto *ppro = (CMTProto *)param; - - ENTER_STRING es = {}; - 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); - mir_free(es.ptszResult); - } - else ppro->LogOut(); - return 0; -} + if (!response.object) + return; -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(); - if (!pAuth->password_hint_.empty()) - wszTitle.AppendFormat(TranslateT(" (hint: %s)"), Utf2T(pAuth->password_hint_.c_str()).get()); - - ENTER_STRING es = {}; - es.szModuleName = ppro->m_szModuleName; - es.caption = wszTitle; - es.type = ESF_PASSWORD; - if (EnterString(&es)) { - ppro->SendQuery(new td::td_api::checkAuthenticationPassword(_T2A(es.ptszResult).get()), &CMTProto::OnUpdateAuth); - mir_free(es.ptszResult); - } - else ppro->LogOut(); - return 0; -} + debugLogA("ProcessResponse: id=%d (%s)", int(response.request_id), to_string(response.object).c_str()); -void CMTProto::ProcessAuth(td::td_api::updateAuthorizationState *pObj) -{ - pAuthState = std::move(pObj->authorization_state_); - switch (pAuthState->get_id()) { - case td::td_api::authorizationStateWaitTdlibParameters::ID: - { - char text[100]; - Miranda_GetVersionText(text, sizeof(text)); - - CMStringW wszPath(GetProtoFolder()); - - auto *request = new td::td_api::setTdlibParameters(); - request->database_directory_ = T2Utf(wszPath).get(); - request->use_message_database_ = false; - request->use_secret_chats_ = true; - request->api_id_ = 94575; - request->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2"; - request->system_language_code_ = "en"; - request->device_model_ = "Miranda NG"; - request->application_version_ = text; - request->enable_storage_optimizer_ = true; - SendQuery(request, &CMTProto::OnUpdateAuth); + if (response.request_id) { + auto *p = m_arRequests.find((TG_REQUEST *)&response.request_id); + if (p) { + (this->*p->pHandler)(response); + m_arRequests.remove(p); } - break; - - case td::td_api::authorizationStateWaitPhoneNumber::ID: - SendQuery(new td::td_api::setAuthenticationPhoneNumber(_T2A(m_szOwnPhone).get(), nullptr), &CMTProto::OnUpdateAuth); - break; - - case td::td_api::authorizationStateWaitCode::ID: - CallFunctionSync(EnterPhoneCode, this); - break; + return; + } - case td::td_api::authorizationStateWaitPassword::ID: - CallFunctionSync(EnterPassword, this); + switch (response.object->get_id()) { + case td::td_api::updateAuthorizationState::ID: + ProcessAuth((td::td_api::updateAuthorizationState *)response.object.get()); break; - case td::td_api::authorizationStateReady::ID: - OnLoggedIn(); + case td::td_api::updateChatFilters::ID: + ProcessGroups((td::td_api::updateChatFilters *)response.object.get()); break; - case td::td_api::authorizationStateLoggingOut::ID: - debugLogA("Server required us to log out, exiting"); - LogOut(); + case td::td_api::updateUser::ID: + ProcessUser((td::td_api::updateUser *)response.object.get()); break; - case td::td_api::authorizationStateClosing::ID: - debugLogA("Connection terminated, exiting"); - LogOut(); - break; } } -void CMTProto::OnUpdateAuth(td::ClientManager::Response &response) +void CMTProto::SendQuery(td::td_api::Function *pFunc, TG_QUERY_HANDLER pHandler) { - if (response.object->get_id() == td::td_api::error::ID) { - auto *pError = (td::td_api::error*)response.object.get(); - debugLogA("error happened: %s", to_string(*pError).c_str()); - - if (pError->message_ == "PHONE_CODE_EXPIRED") - Popup(0, TranslateT("Phone code expired"), TranslateT("Error")); - else if(pError->message_ == "INVALID_PHONE_CODE") - Popup(0, TranslateT("Invalid phone code"), TranslateT("Error")); - - pAuthState = std::move(nullptr); - LogOut(); - } + int queryId = ++m_iQueryId; + m_pClientMmanager->send(m_iClientId, queryId, td::td_api::object_ptr<td::td_api::Function>(pFunc)); + + if (pHandler) + m_arRequests.insert(new TG_REQUEST(queryId, pHandler)); } /////////////////////////////////////////////////////////////////////////////// @@ -171,7 +109,7 @@ void CMTProto::ProcessGroups(td::td_api::updateChatFilters *pObj) if (grp->icon_name_ != "Custom") continue; - CMStringA szSetting("ChatFilter%d", grp->id_); + CMStringA szSetting(FORMAT, "ChatFilter%d", grp->id_); CMStringW wszOldValue(getMStringW(szSetting)); Utf2T wszNewValue(grp->title_.c_str()); if (wszOldValue.IsEmpty()) { @@ -189,40 +127,21 @@ void CMTProto::ProcessGroups(td::td_api::updateChatFilters *pObj) } } -/////////////////////////////////////////////////////////////////////////////// - -void CMTProto::ProcessResponse(td::ClientManager::Response response) +void CMTProto::ProcessUser(td::td_api::updateUser *pObj) { - if (!response.object) - return; - - debugLogA("ProcessResponse: id=%d (%s)", int(response.request_id), to_string(response.object).c_str()); - - if (response.request_id) { - auto *p = m_arRequests.find((TG_REQUEST *)&response.request_id); - if (p) { - (this->*p->pHandler)(response); - m_arRequests.remove(p); + 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_); + if (pUser->usernames_) + UpdateString(hContact, "Nick", pUser->usernames_->editable_username_); + + 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_); } - return; } - - switch (response.object->get_id()) { - case td::td_api::updateAuthorizationState::ID: - ProcessAuth((td::td_api::updateAuthorizationState *)response.object.get()); - break; - - case td::td_api::updateChatFilters::ID: - ProcessGroups((td::td_api::updateChatFilters *)response.object.get()); - break; - } -} - -void CMTProto::SendQuery(td::td_api::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)); - - if (pHandler) - m_arRequests.insert(new TG_REQUEST(queryId, pHandler)); } |