void CVkProto::LogMenuHook(CVkChatInfo *cc, GCHOOK *gch) { HANDLE hContact; switch (gch->dwData) { case IDM_TOPIC: if (LPTSTR ptszNew = ChangeChatTopic(cc)) { Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.editChat.json", true, &CVkProto::OnReceiveSmth) << TCHAR_PARAM("title", ptszNew) << INT_PARAM("chat_id", cc->m_chatid)); mir_free(ptszNew); } break; case IDM_INVITE: hContact = (HANDLE)DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_INVITE), NULL, InviteDlgProc, (LPARAM)this); if (hContact != NULL) { int uid = getDword(hContact, "ID", -1); if (uid != -1) Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.addChatUser.json", true, &CVkProto::OnReceiveSmth) << INT_PARAM("uid", uid) << INT_PARAM("chat_id", cc->m_chatid)); } break; case IDM_DESTROY: 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)) { Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.deleteDialog.json", true, &CVkProto::OnChatDestroy) << INT_PARAM("chat_id", cc->m_chatid))->pUserInfo = cc; } break; } }
void CVkProto::RetrieveUsersInfo(bool flag) { debugLogA("CVkProto::RetrieveUsersInfo"); if (!IsOnline()) return; CMString userIDs, code; for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) { LONG userID = getDword(hContact, "ID", -1); if (userID == -1 || userID == VK_FEED_USER) continue; if (!userIDs.IsEmpty()) userIDs.AppendChar(','); userIDs.AppendFormat(_T("%i"), userID); } CMString codeformat("var userIDs=\"%s\";"); if (m_bNeedSendOnline) codeformat += _T("API.account.setOnline();"); if (flag) codeformat += CMString("var US=API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"});" "var res=[];var index=US.length;while(index >0){index=index-1;if(US[index].online==1){res.unshift(US[index]);};};" "return{\"freeoffline\":1,\"users\":res,\"requests\":API.friends.getRequests({\"extended\":0,\"need_mutual\":0,\"out\":0})};"); else codeformat += CMString("var res=API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"});" "return{\"freeoffline\":0,\"users\":res};"); code.AppendFormat(codeformat, userIDs, CMString(flag ? "online,status" : fieldsName)); Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) << TCHAR_PARAM("code", code) << VER_API); }
int CVkProto::OnChatEvent(WPARAM, LPARAM lParam) { GCHOOK *gch = (GCHOOK*)lParam; if (gch == NULL) return 0; if (lstrcmpiA(gch->pDest->pszModule, m_szModuleName)) return 0; CVkChatInfo *cc = GetChatById(gch->pDest->ptszID); if (cc == NULL) return 0; switch (gch->pDest->iType) { case GC_USER_MESSAGE: if (m_bOnline && lstrlen(gch->ptszText) > 0) { TCHAR *buf = NEWTSTR_ALLOCA(gch->ptszText); rtrimt(buf); UnEscapeChatTags(buf); Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.send.json", true, &CVkProto::OnSendChatMsg) << INT_PARAM("type", 1) << INT_PARAM("chat_id", cc->m_chatid) << TCHAR_PARAM("message", buf)); } case GC_USER_LOGMENU: LogMenuHook(cc, gch); break; case GC_USER_NICKLISTMENU: NickMenuHook(cc, gch); break; } return 0; }
void CVkProto::RetrieveStatusMsg(const CMString &StatusMsg) { debugLogA("CVkProto::RetrieveStatusMsg"); if (!IsOnline()) return; Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/status.set.json", true, &CVkProto::OnReceiveSmth) << TCHAR_PARAM("text", StatusMsg)); }
void CVkProto::RetrieveUserInfo(LONG userID) { debugLogA("CVkProto::RetrieveUserInfo (%d)", userID); if (userID == VK_FEED_USER || !IsOnline()) return; CMString code(FORMAT, _T("var userIDs=\"%i\";var res=API.users.get({\"user_ids\":userIDs,\"fields\":\"%s\",\"name_case\":\"nom\"});return{\"freeoffline\":0,\"norepeat\":1,\"usercount\":res.length,\"users\":res};"), userID, CMString(fieldsName)); Push(new AsyncHttpRequest(this, REQUEST_POST, "/method/execute.json", true, &CVkProto::OnReceiveUserInfo) << TCHAR_PARAM("code", code) << VER_API); }
void CVkProto::RetrieveStatusMusic(const CMString &StatusMsg) { debugLogA("CVkProto::RetrieveStatusMusic"); if (!IsOnline() || m_iStatus == ID_STATUS_INVISIBLE || m_vkOptions.iMusicSendMetod == MusicSendMetod::sendNone) return; CMString code; CMString tszOldStatusMsg(db_get_tsa(0, m_szModuleName, "OldStatusMsg")); if (StatusMsg.IsEmpty()) { if (m_vkOptions.iMusicSendMetod == MusicSendMetod::sendBroadcastOnly) code = "API.audio.setBroadcast();return null;"; else { CMString codeformat("API.status.set({text:\"%s\"});return null;"); code.AppendFormat(codeformat, tszOldStatusMsg); } m_bSetBroadcast = false; } else { if (m_vkOptions.iMusicSendMetod == MusicSendMetod::sendBroadcastOnly) { CMString codeformat("var StatusMsg=\"%s\";var CntLmt=100;var OldMsg=API.status.get();" "var Tracks=API.audio.search({\"q\":StatusMsg,\"count\":CntLmt,\"search_own\":1});" "var Cnt=Tracks.count;if(Cnt>CntLmt){Cnt=CntLmt;}" "if(Cnt==0){API.audio.setBroadcast();}" "else{var i=0;var j=0;var Track=\" \";" "while(i<Cnt){Track=Tracks.items[i].artist+\" - \"+Tracks.items[i].title;if(Track==StatusMsg){j=i;}i=i+1;}" "Track=Tracks.items[j].owner_id+\"_\"+Tracks.items[j].id;API.audio.setBroadcast({\"audio\":Track});" "};return OldMsg;"); code.AppendFormat(codeformat, StatusMsg); } else if (m_vkOptions.iMusicSendMetod == MusicSendMetod::sendStatusOnly) { CMString codeformat("var StatusMsg=\"♫ %s\";var OldMsg=API.status.get();" "API.status.set({\"text\":StatusMsg});" "return OldMsg;"); code.AppendFormat(codeformat, StatusMsg); } else if (m_vkOptions.iMusicSendMetod == MusicSendMetod::sendBroadcastAndStatus) { CMString codeformat("var StatusMsg=\"%s\";var CntLmt=100;var Track=\" \";var OldMsg=API.status.get();" "var Tracks=API.audio.search({\"q\":StatusMsg,\"count\":CntLmt,\"search_own\":1});" "var Cnt=Tracks.count;if(Cnt>CntLmt){Cnt=CntLmt;}" "if(Cnt==0){Track=\"♫ \"+StatusMsg;API.status.set({\"text\":Track});}" "else{var i=0;var j=-1;" "while(i<Cnt){Track=Tracks.items[i].artist+\" - \"+Tracks.items[i].title;if(Track==StatusMsg){j=i;}i=i+1;}" "if(j==-1){Track=\"♫ \"+StatusMsg;API.status.set({\"text\":Track});}else{" "Track=Tracks.items[j].owner_id+\"_\"+Tracks.items[j].id;};API.audio.setBroadcast({\"audio\":Track});" "};return OldMsg;"); code.AppendFormat(codeformat, StatusMsg); } m_bSetBroadcast = true; } Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.json", true, &CVkProto::OnReceiveStatus) << TCHAR_PARAM("code", code)); }
void CVkProto::WallPost(MCONTACT hContact, TCHAR *ptszMsg, TCHAR *ptszUrl, bool bFriendsOnly) { debugLogA("CVkProto::WallPost"); if (!IsOnline() || (IsEmpty(ptszMsg) && IsEmpty(ptszUrl))) return; LONG userID = hContact ? m_myUserId : getDword(hContact, "ID", -1); if (userID == -1 || userID == VK_FEED_USER) return; AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/wall.post.json", true, &CVkProto::OnReceiveSmth) << INT_PARAM("owner_id", userID) << INT_PARAM("friends_only", bFriendsOnly ? 1 : 0); if (!IsEmpty(ptszMsg)) pReq << TCHAR_PARAM("message", ptszMsg); if (!IsEmpty(ptszUrl)) pReq << TCHAR_PARAM("attachments", ptszUrl); Push(pReq); }
AsyncHttpRequest* CVkProto::Push(AsyncHttpRequest *pReq, int iTimeout) { debugLogA("CVkProto::Push"); pReq->timeout = iTimeout; if (pReq->m_bApiReq) { pReq << VER_API; if (!IsEmpty(m_vkOptions.ptszVKLang)) pReq << TCHAR_PARAM("lang", m_vkOptions.ptszVKLang); } { mir_cslock lck(m_csRequestsQueue); m_arRequestsQueue.insert(pReq); } SetEvent(m_evRequestsQueue); return pReq; }
void CVkProto::RetrieveUnreadNotifications(time_t tLastNotificationsTime) { debugLogA("CVkProto::RetrieveUnreadNotifications"); if (!IsOnline()) return; time_t tLastNotificationsReqTime = getDword("LastNotificationsReqTime", time(NULL) - 24 * 60 * 60); if (time(NULL) - tLastNotificationsReqTime < 3 * 60) return; CMString code(FORMAT, _T("return{\"notifications\":API.notifications.get({\"count\": 100, \"start_time\":%d})%s"), (LONG)(tLastNotificationsTime + 1), m_bNotificationFilterInvites ? _T(",\"groupinvates\":API.groups.getInvites({\"extended\":1})};") : _T("};")); Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.json", true, &CVkProto::OnReceiveUnreadNotifications) << TCHAR_PARAM("code", code) << VER_API); setDword("LastNotificationsReqTime", (DWORD)time(NULL)); }
int CVkProto::AuthRequest(MCONTACT hContact,const TCHAR* message) { debugLogA("CVkProto::AuthRequest"); if (!IsOnline()) return 1; LONG userID = getDword(hContact, "ID", -1); if (userID == -1 || !hContact || userID == VK_FEED_USER) return 1; TCHAR msg[501] = {0}; if (message) _tcsncpy_s(msg, 500, message, _TRUNCATE); Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/friends.add.json", true, &CVkProto::OnReceiveAuthRequest) << INT_PARAM("user_id", userID) << TCHAR_PARAM("text", msg) << VER_API)->pUserInfo = new CVkSendMsgParam(hContact); return 0; }
void CVkProto::CreateNewChat(LPCSTR uids, LPCTSTR ptszTitle) { Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.createChat.json", true, &CVkProto::OnCreateNewChat) << TCHAR_PARAM("title", ptszTitle) << CHAR_PARAM("uids", uids)); }
void CVkProto::OnReciveUploadFile(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { SendFileFiled(fup, VKERR_OFFLINE); return; } debugLogA("CVkProto::OnReciveUploadFile %d", reply->resultCode); if (reply->resultCode != 200) { SendFileFiled(fup, VKERR_FILE_NOT_UPLOADED); return; } JSONNode jnRoot; const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot); if (!jnResponse || pReq->m_iErrorCode) { SendFileFiled(fup, pReq->m_iErrorCode); return; } int id = fup->GetType() == CVkFileUploadParam::typeAudio ? jnResponse["id"].as_int() : (*jnResponse.begin())["id"].as_int(); int owner_id = fup->GetType() == CVkFileUploadParam::typeAudio ? jnResponse["owner_id"].as_int() : (*jnResponse.begin())["owner_id"].as_int(); if ((id == 0) || (owner_id == 0)) { SendFileFiled(fup, VKERR_INVALID_PARAMETERS); return; } CMString Attachment; switch (fup->GetType()) { case CVkFileUploadParam::typeImg: Attachment.AppendFormat(_T("photo%d_%d"), owner_id, id); break; case CVkFileUploadParam::typeAudio: Attachment.AppendFormat(_T("audio%d_%d"), owner_id, id); break; case CVkFileUploadParam::typeDoc: Attachment.AppendFormat(_T("doc%d_%d"), owner_id, id); break; default: SendFileFiled(fup, VKERR_FTYPE_NOT_SUPPORTED); return; } AsyncHttpRequest *pMsgReq; if (isChatRoom(fup->hContact)) { ptrT tszChatID(getTStringA(fup->hContact, "ChatRoomID")); if (!tszChatID) { SendFileFiled(fup, VKERR_INVALID_USER); return; } CVkChatInfo *cc = GetChatById(tszChatID); if (cc == NULL) { SendFileFiled(fup, VKERR_INVALID_USER); return; } pMsgReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/messages.send.json", true, &CVkProto::OnSendChatMsg, AsyncHttpRequest::rpHigh) << INT_PARAM("chat_id", cc->m_chatid); pMsgReq->pUserInfo = pReq->pUserInfo; } else { LONG userID = getDword(fup->hContact, "ID", -1); if (userID == -1 || userID == VK_FEED_USER) { SendFileFiled(fup, VKERR_INVALID_USER); return; } pMsgReq = new AsyncHttpRequest(this, REQUEST_POST, "/method/messages.send.json", true, &CVkProto::OnSendMessage, AsyncHttpRequest::rpHigh) << INT_PARAM("user_id", userID); pMsgReq->pUserInfo = new CVkSendMsgParam(fup->hContact, fup); } pMsgReq << TCHAR_PARAM("message", fup->Desc) << TCHAR_PARAM("attachment", Attachment) << VER_API; pMsgReq->AddHeader("Content-Type", "application/x-www-form-urlencoded"); Push(pMsgReq); }
void CVkProto::OnReciveUpload(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) { CVkFileUploadParam *fup = (CVkFileUploadParam *)pReq->pUserInfo; if (!IsOnline()) { SendFileFiled(fup, VKERR_OFFLINE); return; } debugLogA("CVkProto::OnReciveUploadServer %d", reply->resultCode); if (reply->resultCode != 200) { SendFileFiled(fup, VKERR_FILE_NOT_UPLOADED); return; } JSONNode jnRoot; CheckJsonResponse(pReq, reply, jnRoot); if (pReq->m_iErrorCode) { SendFileFiled(fup, pReq->m_iErrorCode); return; } if ((!jnRoot["server"] || !jnRoot["hash"]) && !jnRoot["file"]) { SendFileFiled(fup, VKERR_INVALID_PARAMETERS); return; } CMString server(jnRoot["server"].as_mstring()); CMString hash(jnRoot["hash"].as_mstring()); CMString upload; AsyncHttpRequest *pUploadReq; ProtoBroadcastAck(fup->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, (HANDLE)fup); switch (fup->GetType()) { case CVkFileUploadParam::typeImg: upload = jnRoot["photo"].as_mstring(); if (upload == _T("[]")) { SendFileFiled(fup, VKERR_INVALID_PARAMETERS); return; } pUploadReq = new AsyncHttpRequest(this, REQUEST_GET, "/method/photos.saveMessagesPhoto.json", true, &CVkProto::OnReciveUploadFile) << TCHAR_PARAM("server", server) << TCHAR_PARAM("photo", upload) << TCHAR_PARAM("hash", hash) << VER_API; break; case CVkFileUploadParam::typeAudio: upload = jnRoot["audio"].as_mstring(); if (upload == _T("[]")) { SendFileFiled(fup, VKERR_INVALID_PARAMETERS); return; } pUploadReq = new AsyncHttpRequest(this, REQUEST_GET, "/method/audio.save.json", true, &CVkProto::OnReciveUploadFile) << TCHAR_PARAM("server", server) << TCHAR_PARAM("audio", upload) << TCHAR_PARAM("hash", hash) << VER_API; break; case CVkFileUploadParam::typeDoc: upload = jnRoot["file"].as_mstring(); if (upload.IsEmpty()) { SendFileFiled(fup, VKERR_INVALID_PARAMETERS); return; } pUploadReq = new AsyncHttpRequest(this, REQUEST_GET, "/method/docs.save.json", true, &CVkProto::OnReciveUploadFile) << CHAR_PARAM("title", fup->fileName()) << TCHAR_PARAM("file", upload) << VER_API; break; default: SendFileFiled(fup, VKERR_FTYPE_NOT_SUPPORTED); return; } pUploadReq->pUserInfo = pReq->pUserInfo; Push(pUploadReq); }