From 40d90d097b40f76b156b5fc1da6422ee03772951 Mon Sep 17 00:00:00 2001 From: Sergey Bolhovskoy Date: Wed, 15 Oct 2014 09:54:32 +0000 Subject: VKontakte: fix C4018 warning rework leave chat support add leave chat by kick add rejoin to chat git-svn-id: http://svn.miranda-ng.org/main/trunk@10787 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/vk_chats.cpp | 116 ++++++++++++++++++++++++++-------- protocols/VKontakte/src/vk_proto.cpp | 1 + protocols/VKontakte/src/vk_proto.h | 4 ++ protocols/VKontakte/src/vk_thread.cpp | 10 ++- 4 files changed, 104 insertions(+), 27 deletions(-) (limited to 'protocols/VKontakte') diff --git a/protocols/VKontakte/src/vk_chats.cpp b/protocols/VKontakte/src/vk_chats.cpp index 97323ca1a8..c5ce7f733d 100644 --- a/protocols/VKontakte/src/vk_chats.cpp +++ b/protocols/VKontakte/src/vk_chats.cpp @@ -28,6 +28,7 @@ static LPCTSTR sttStatuses[] = { LPGENT("Participants"), LPGENT("Owners") }; CVkChatInfo* CVkProto::AppendChat(int id, JSONNODE *pDlg) { + debugLog(L"CVkProto::AppendChat"); if (id == 0) return NULL; @@ -69,6 +70,12 @@ CVkChatInfo* CVkProto::AppendChat(int id, JSONNODE *pDlg) CallServiceSync(MS_GC_EVENT, NULL, (LPARAM)&gce); } + setDword(gci.hContact, "vk_chat_id", id); + + if (json_as_int(json_get(pDlg, "left")) == 1){ + m_chats.remove(c); + return NULL; + } gcd.iType = GC_EVENT_CONTROL; gce.ptszStatus = 0; CallServiceSync(MS_GC_EVENT, (m_bHideChats) ? WINDOW_HIDDEN : SESSION_INITDONE, (LPARAM)&gce); @@ -129,8 +136,11 @@ void CVkProto::OnReceiveChatInfo(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe gce.ptszText = tszTitle; CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce); } - - cc->m_admin_id = json_as_int(json_get(info, "admin_id")); + if (json_as_int(json_get(info, "left")) == 1){ + LeaveChat(cc->m_chatid); + return; + } + cc->m_admin_id = json_as_int(json_get(info, "admin_id")); } JSONNODE *users = json_get(pResponse, "users"); @@ -466,6 +476,25 @@ void CVkProto::LogMenuHook(CVkChatInfo *cc, GCHOOK *gch) } } +INT_PTR __cdecl CVkProto::OnJoinChat(WPARAM hContact, LPARAM) +{ + debugLogA("CVkProto::OnJoinChat"); + + int chat_id = getDword(hContact, "vk_chat_id", -1); + + if (chat_id == -1) + return 1; + + AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/messages.send.json", true, &CVkProto::OnSendChatMsg) + << INT_PARAM("chat_id", chat_id) + << TCHAR_PARAM("message", TranslateT("I'm back")) + << VER_API; + pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); + Push(pReq); + + return 0; +} + INT_PTR __cdecl CVkProto::OnLeaveChat(WPARAM hContact, LPARAM) { debugLogA("CVkProto::OnLeaveChat"); @@ -477,37 +506,72 @@ INT_PTR __cdecl CVkProto::OnLeaveChat(WPARAM hContact, LPARAM) if (cc == NULL) return 1; - if (IDYES == MessageBox(NULL, - TranslateT("This chat is going to be destroyed forever with all its contents. This action cannot be undone. Are you sure?"), - TranslateT("Warning"), MB_YESNOCANCEL | MB_ICONQUESTION)) - { - CMStringA code; - code.Format("var Hist = API.messages.getHistory({\"chat_id\":%d, \"count\":200});" - "var countMsg = Hist.count;var itemsMsg = Hist.items@.id; " - "while (countMsg > 0) { API.messages.delete({\"message_ids\":itemsMsg});" - "Hist=API.messages.getHistory({\"chat_id\":%d, \"count\":200});" - "countMsg = Hist.count;itemsMsg = Hist.items@.id;}; return 1;", cc->m_chatid, cc->m_chatid); - Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.json", true, &CVkProto::OnChatDestroy) - << CHAR_PARAM("code", code) - << VER_API)->pUserInfo = cc; - return 0; + Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.removeChatUser.json", true, &CVkProto::OnChatLeave) + << INT_PARAM("chat_id", cc->m_chatid) + << INT_PARAM("user_id", m_myUserId) + << VER_API)->pUserInfo = cc; + + return 0; +} + +void CVkProto::LeaveChat(int chat_id, bool close_window, bool delete_chat) +{ + debugLogA("CVkProto::LeaveChat"); + CVkChatInfo *cc = (CVkChatInfo*)m_chats.find((CVkChatInfo*)&chat_id); + if (cc == NULL) + return; + + GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_QUIT }; + GCEVENT gce = { sizeof(GCEVENT), &gcd }; + CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce); + gcd.iType = GC_EVENT_CONTROL; + CallServiceSync(MS_GC_EVENT, close_window? SESSION_TERMINATE:SESSION_OFFLINE, (LPARAM)&gce); + if (delete_chat) + CallService(MS_DB_CONTACT_DELETE, (WPARAM)cc->m_hContact, 0); + m_chats.remove(cc); +} + +void CVkProto::KickFromChat(int chat_id, int user_id, JSONNODE* pMsg) +{ + debugLogA("CVkProto::KickFromChat (%d)", user_id); + if (user_id == m_myUserId) + return; + + MCONTACT hContact = FindUser(user_id, false); + CMString msg = json_as_string(json_get(pMsg, "body")); + if (msg.IsEmpty()) { + msg = TranslateT("You've been kicked by "); + if (hContact != NULL) + msg += db_get_tsa(hContact, m_szModuleName, "Nick"); + else + msg += TranslateT("(Unknown contact)"); } - return 1; + else + AppendChatMessage(chat_id, pMsg, false); + + MsgPopup(hContact, msg, TranslateT("Kick")); + LeaveChat(chat_id, false); +} + +void CVkProto::OnChatLeave(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +{ + debugLogA("CVkProto::OnChatLeave %d", reply->resultCode); + if (reply->resultCode != 200) + return; + + CVkChatInfo *cc = (CVkChatInfo*)pReq->pUserInfo; + LeaveChat(cc->m_chatid); + } void CVkProto::OnChatDestroy(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) { debugLogA("CVkProto::OnChatDestroy %d", reply->resultCode); - if (reply->resultCode == 200) { - CVkChatInfo *cc = (CVkChatInfo*)pReq->pUserInfo; + if (reply->resultCode != 200) + return; - GCDEST gcd = { m_szModuleName, cc->m_tszId, GC_EVENT_QUIT }; - GCEVENT gce = { sizeof(GCEVENT), &gcd }; - CallServiceSync(MS_GC_EVENT, 0, (LPARAM)&gce); - gcd.iType = GC_EVENT_CONTROL; - CallServiceSync(MS_GC_EVENT, SESSION_TERMINATE, (LPARAM)&gce); - CallService(MS_DB_CONTACT_DELETE, (WPARAM)cc->m_hContact, 0); - } + CVkChatInfo *cc = (CVkChatInfo*)pReq->pUserInfo; + LeaveChat(cc->m_chatid, true, true); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index 5270c99f2e..d8ad1d70d8 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -104,6 +104,7 @@ int CVkProto::OnModulesLoaded(WPARAM wParam, LPARAM lParam) gcr.pColors = sttColors; CallServiceSync(MS_GC_REGISTER, NULL, (LPARAM)&gcr); CreateProtoService(PS_LEAVECHAT, &CVkProto::OnLeaveChat); + CreateProtoService(PS_JOINCHAT, &CVkProto::OnJoinChat); HookProtoEvent(ME_GC_EVENT, &CVkProto::OnChatEvent); HookProtoEvent(ME_GC_BUILDMENU, &CVkProto::OnGcMenuHook); // Other hooks diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index 8659df0a07..85208c1f14 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -445,10 +445,14 @@ private: void RetrieveChatInfo(CVkChatInfo*); void OnReceiveChatInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnSendChatMsg(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnChatLeave(NETLIBHTTPREQUEST*, AsyncHttpRequest *); void OnChatDestroy(NETLIBHTTPREQUEST*, AsyncHttpRequest*); int __cdecl OnChatEvent(WPARAM, LPARAM); int __cdecl OnGcMenuHook(WPARAM, LPARAM); + void KickFromChat(int chat_id, int user_id, JSONNODE* pMsg); + void LeaveChat(int chat_id, bool close_window = true, bool delete_chat = false); INT_PTR __cdecl OnLeaveChat(WPARAM, LPARAM); + INT_PTR __cdecl OnJoinChat(WPARAM, LPARAM); void LogMenuHook(CVkChatInfo*, GCHOOK*); void NickMenuHook(CVkChatInfo*, GCHOOK*); LPTSTR ChangeChatTopic(CVkChatInfo*); diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index 7fea5b8ab6..d0dacb36df 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -573,7 +573,12 @@ void CVkProto::OnReceiveMessages(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pRe } if (chat_id != 0) { - AppendChatMessage(chat_id, pMsg, false); + CMString action_chat = json_as_string(json_get(pMsg, "action")); + CMString action_mid = json_as_string(json_get(pMsg, "action_mid")); + if (action_chat == "chat_kick_user") + KickFromChat(chat_id, _ttoi(action_mid.GetBuffer()), pMsg); + else + AppendChatMessage(chat_id, pMsg, false); continue; } @@ -621,7 +626,9 @@ void CVkProto::OnReceiveDlgs(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) return; if (m_bAutoSyncHistory&&m_bPopUpSyncHistory) MsgPopup(NULL, TranslateT("Start sync history"), TranslateT("Sync history")); + debugLogA("CVkProto::OnReceiveDlgs numDlgs = %d", numDlgs); for (int i = 0; i < numDlgs; i++) { + debugLogA("CVkProto::OnReceiveDlgs i = %d", i); JSONNODE *pDlg = json_at(pDlgs, i); if (pDlg == NULL) break; @@ -634,6 +641,7 @@ void CVkProto::OnReceiveDlgs(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) int chatid = json_as_int(json_get(pDlg, "chat_id")); if (chatid != 0) { + debugLogA("CVkProto::OnReceiveDlgs chatid = %d", chatid); if (m_chats.find((CVkChatInfo*)&chatid) == NULL) AppendChat(chatid, pDlg); } -- cgit v1.2.3