From b68aad07ca94035442309cf8c68b039573f949ca Mon Sep 17 00:00:00 2001 From: Sergey Bolhovskoy Date: Wed, 24 Sep 2014 09:19:28 +0000 Subject: VKontakte: add PopUp support add AuthRequest (add as friend support) version bump git-svn-id: http://svn.miranda-ng.org/main/trunk@10569 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/VKontakte/src/misc.cpp | 9 +++ protocols/VKontakte/src/stdafx.h | 1 + protocols/VKontakte/src/version.h | 2 +- protocols/VKontakte/src/vk.h | 4 ++ protocols/VKontakte/src/vk_proto.cpp | 119 ++++++++++++++++++++++++++++++++-- protocols/VKontakte/src/vk_proto.h | 10 ++- protocols/VKontakte/src/vk_thread.cpp | 20 ++++-- 7 files changed, 154 insertions(+), 11 deletions(-) diff --git a/protocols/VKontakte/src/misc.cpp b/protocols/VKontakte/src/misc.cpp index d2fed8d7b8..f7856a5b4f 100644 --- a/protocols/VKontakte/src/misc.cpp +++ b/protocols/VKontakte/src/misc.cpp @@ -114,6 +114,13 @@ bool CVkProto::CheckJsonResult(AsyncHttpRequest *pReq, NETLIBHTTPREQUEST *reply, pReq->bNeedsRestart = true; Sleep(500); //Pause for fix err break; + case VKERR_HIMSELF_AS_FRIEND: + case VKERR_YOU_ON_BLACKLIST: + case VKERR_USER_ON_BLACKLIST: + CVkSendMsgParam *param = (CVkSendMsgParam*)pReq->pUserInfo; + if (param) + param->iCount = iErrorCode; + break; } return iErrorCode == 0; @@ -231,6 +238,7 @@ AsyncHttpRequest::AsyncHttpRequest() m_bApiReq = true; AddHeader("Connection", "keep-alive"); AddHeader("Accept-Encoding", "booo"); + pUserInfo = NULL; } AsyncHttpRequest::AsyncHttpRequest(CVkProto *ppro, int iRequestType, LPCSTR _url, bool bSecure, VK_REQUEST_HANDLER pFunc) @@ -256,6 +264,7 @@ AsyncHttpRequest::AsyncHttpRequest(CVkProto *ppro, int iRequestType, LPCSTR _url requestType = iRequestType; m_pFunc = pFunc; + pUserInfo = NULL; } AsyncHttpRequest::~AsyncHttpRequest() diff --git a/protocols/VKontakte/src/stdafx.h b/protocols/VKontakte/src/stdafx.h index b1de929d8c..5693d2e46f 100644 --- a/protocols/VKontakte/src/stdafx.h +++ b/protocols/VKontakte/src/stdafx.h @@ -62,6 +62,7 @@ along with this program. If not, see . #include #include +#include #include #include "win2k.h" diff --git a/protocols/VKontakte/src/version.h b/protocols/VKontakte/src/version.h index 07eecbd9c4..7603c033e3 100644 --- a/protocols/VKontakte/src/version.h +++ b/protocols/VKontakte/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 1 #define __RELEASE_NUM 0 -#define __BUILD_NUM 15 +#define __BUILD_NUM 16 #include diff --git a/protocols/VKontakte/src/vk.h b/protocols/VKontakte/src/vk.h index 9f7ae21514..2663031fa2 100644 --- a/protocols/VKontakte/src/vk.h +++ b/protocols/VKontakte/src/vk.h @@ -43,12 +43,16 @@ along with this program. If not, see . #define VKFLAG_MSGMEDIA 512 // сообщение содержит медиаконтент // Errors +#define VKERR_NOERRORS 0 // No error #define VKERR_UNKNOWN 1 // Unknown error occurred #define VKERR_TOO_MANY_REQ_PER_SEC 6 // Too many requests per second #define VKERR_AUTHORIZATION_FAILED 5 // User authorization failed #define VKERR_FLOOD_CONTROL 9 // Flood control #define VKERR_INTERNAL_SERVER_ERR 10 // Internal server error #define VKERR_CAPTCHA_NEEDED 14 // Captcha needed +#define VKERR_HIMSELF_AS_FRIEND 174 // Cannot add user himself as friend +#define VKERR_YOU_ON_BLACKLIST 175 // Cannot add this user to friends as they have put you on their blacklist +#define VKERR_USER_ON_BLACKLIST 176 // Cannot add this user to friends as you put him on blacklist #define VK_API_VER "5.24" #define VER_API CHAR_PARAM("v", VK_API_VER) diff --git a/protocols/VKontakte/src/vk_proto.cpp b/protocols/VKontakte/src/vk_proto.cpp index 43fe08645c..c240af46f7 100644 --- a/protocols/VKontakte/src/vk_proto.cpp +++ b/protocols/VKontakte/src/vk_proto.cpp @@ -79,6 +79,8 @@ CVkProto::~CVkProto() Netlib_CloseHandle(m_hNetlibUser); m_hNetlibUser = NULL; UninitQueue(); UnInitMenus(); + if (m_hPopupClass) + Popup_UnregisterClass(m_hPopupClass); vk_Instances.remove(this); } @@ -95,10 +97,14 @@ int CVkProto::OnModulesLoaded(WPARAM wParam, LPARAM lParam) HookProtoEvent(ME_GC_EVENT, &CVkProto::OnChatEvent); HookProtoEvent(ME_GC_BUILDMENU, &CVkProto::OnGcMenuHook); + InitPopups(); InitMenus(); return 0; } +///////////////////////////////////////////////////////////////////////////////////////// +// Menu support + void CVkProto::InitMenus() { HookProtoEvent(ME_CLIST_PREBUILDCONTACTMENU, &CVkProto::OnPreBuildContactMenu); @@ -107,7 +113,8 @@ void CVkProto::InitMenus() CreateProtoService(PS_GETALLSERVERHISTORY, &CVkProto::SvcGetAllServerHistory); CreateProtoService(PS_VISITPROFILE, &CVkProto::SvcVisitProfile); CreateProtoService(PS_CREATECHAT, &CVkProto::SvcCreateChat); - + CreateProtoService(PS_ADDASFRIEND, &CVkProto::SvcAddAsFriend); + CLISTMENUITEM mi = { sizeof(mi) }; char szService[100]; @@ -144,12 +151,23 @@ void CVkProto::InitMenus() mi.pszService = szService; g_hContactMenuItems[CMI_GETALLSERVERHISTORY] = Menu_AddContactMenuItem(&mi); + mir_snprintf(szService, sizeof(szService), "%s%s", m_szModuleName, PS_ADDASFRIEND); + mi.position = -200001000 + CMI_ADDASFRIEND; + mi.icolibItem = LoadSkinnedIconHandle(SKINICON_OTHER_ADDCONTACT); + mi.ptszName = LPGENT("Add as friend"); + mi.pszService = szService; + g_hContactMenuItems[CMI_ADDASFRIEND] = Menu_AddContactMenuItem(&mi); + } int CVkProto::OnPreBuildContactMenu(WPARAM hContact, LPARAM) { - for (int i = 0; i < CMI_COUNT; i++) - Menu_ShowItem(g_hContactMenuItems[i], !isChatRoom(hContact)); + bool isFriend = getBool(hContact, "friend", false); + + Menu_ShowItem(g_hContactMenuItems[CMI_GETALLSERVERHISTORY], !isChatRoom(hContact)); + Menu_ShowItem(g_hContactMenuItems[CMI_VISITPROFILE], !isChatRoom(hContact)); + Menu_ShowItem(g_hContactMenuItems[CMI_ADDASFRIEND], !isFriend); + return 0; } @@ -162,6 +180,52 @@ void CVkProto::UnInitMenus() CallService(MO_REMOVEMENUITEM, (WPARAM)g_hContactMenuItems[i], 0); } +///////////////////////////////////////////////////////////////////////////////////////// +// PopUp support + +void CVkProto::InitPopups(void) +{ + TCHAR desc[256]; + mir_sntprintf(desc, SIZEOF(desc), _T("%s %s"), m_tszUserName, TranslateT("Errors")); + + char name[256]; + mir_snprintf(name, SIZEOF(name), "%s_%s", m_szModuleName, "Error"); + + POPUPCLASS ppc = { sizeof(ppc) }; + ppc.flags = PCF_TCHAR; + ppc.ptszDescription = desc; + ppc.pszName = name; + ppc.hIcon = LoadSkinnedIcon(SKINICON_ERROR); + ppc.colorBack = RGB(191, 0, 0); //Red + ppc.colorText = RGB(255, 245, 225); //Yellow + ppc.iSeconds = 60; + m_hPopupClass = Popup_RegisterClass(&ppc); + + Skin_ReleaseIcon(ppc.hIcon); +} + +void CVkProto::MsgPopup(MCONTACT hContact, const TCHAR *szMsg, const TCHAR *szTitle) +{ + if (ServiceExists(MS_POPUP_ADDPOPUPCLASS)) { + char name[256]; + + POPUPDATACLASS ppd = { sizeof(ppd) }; + ppd.ptszTitle = szTitle; + ppd.ptszText = szMsg; + ppd.pszClassName = name; + ppd.hContact = hContact; + mir_snprintf(name, SIZEOF(name), "%s_%s", m_szModuleName, "Error"); + + CallService(MS_POPUP_ADDPOPUPCLASS, 0, (LPARAM)&ppd); + } + else { + DWORD mtype = MB_OK | MB_SETFOREGROUND | MB_ICONSTOP; + MessageBox(NULL, szMsg, szTitle, mtype); + } +} + +////////////////////////////////////////////////////////////////////////////// + int CVkProto::OnPreShutdown(WPARAM wParam, LPARAM lParam) { m_bTerminated = true; @@ -367,11 +431,58 @@ MCONTACT CVkProto::AddToList(int flags, PROTOSEARCHRESULT* psr) return hConnact; } -int CVkProto::AuthRequest(MCONTACT hContact,const PROTOCHAR *message) +int CVkProto::AuthRequest(MCONTACT hContact,const PROTOCHAR* message) { + debugLogA("CVkProto::AuthRequest"); + if (!IsOnline()) + return 1; + bool bIsFriend = getBool(hContact, "friend", false); + LONG userID = getDword(hContact, "ID", -1); + if (bIsFriend || (userID == -1) || !hContact) + return 1; + + TCHAR msg[501]; + _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::OnReceiveAuthRequest(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq) +{ + CVkSendMsgParam *param = (CVkSendMsgParam*)pReq->pUserInfo; + debugLogA("CVkProto::OnReceiveAuthRequest %d", reply->resultCode); + if (reply->resultCode == 200){ + JSONROOT pRoot; + JSONNODE *pResponse = CheckJsonResponse(pReq, reply, pRoot); + if (pResponse != NULL) { + int iRet = json_as_int(pResponse); + if (iRet == 2){ + setByte(param->hContact, "friend", 1); + MsgPopup(param->hContact, TranslateT("User add as friend"), _T("")); + } + } + else{ + switch (param->iCount){ + case VKERR_HIMSELF_AS_FRIEND: + MsgPopup(param->hContact, TranslateT("Cannot add user himself as friend"), TranslateT("Error")); + break; + case VKERR_YOU_ON_BLACKLIST: + MsgPopup(param->hContact, TranslateT("Cannot add this user to friends as they have put you on their blacklist"), TranslateT("Error")); + break; + case VKERR_USER_ON_BLACKLIST: + MsgPopup(param->hContact, TranslateT("Cannot add this user to friends as you put him on blacklist"), TranslateT("Error")); + break; + } + } + } + delete param; +} + int CVkProto::Authorize(HANDLE hDbEvent) { return 1; diff --git a/protocols/VKontakte/src/vk_proto.h b/protocols/VKontakte/src/vk_proto.h index 344639496e..a9d19234ae 100644 --- a/protocols/VKontakte/src/vk_proto.h +++ b/protocols/VKontakte/src/vk_proto.h @@ -18,6 +18,7 @@ along with this program. If not, see . #define PS_CREATECHAT "/CreateNewChat" #define PS_GETALLSERVERHISTORY "/GetAllServerHystory" #define PS_VISITPROFILE "/VisitProfile" +#define PS_ADDASFRIEND "/AddAsFriend" #define MAXHISTORYMIDSPERONE 200 struct CVkProto; @@ -78,7 +79,7 @@ AsyncHttpRequest* operator<<(AsyncHttpRequest*, const TCHAR_PARAM&); struct CVkSendMsgParam { - CVkSendMsgParam(MCONTACT _p1, int _p2, int _p3 = 0) : + CVkSendMsgParam(MCONTACT _p1, int _p2 = 0, int _p3 = 0) : hContact(_p1), iMsgID(_p2), iCount(_p3) @@ -198,6 +199,7 @@ struct CVkProto : public PROTO //==== Menus ========================================================================== INT_PTR __cdecl SvcVisitProfile(WPARAM hContact, LPARAM); + INT_PTR __cdecl SvcAddAsFriend(WPARAM hContact, LPARAM); INT_PTR __cdecl SvcGetAllServerHistory(WPARAM hContact, LPARAM); void InitMenus(); void UnInitMenus(); @@ -236,6 +238,7 @@ struct CVkProto : public PROTO void GetHistoryDlgMessages(MCONTACT hContact, int iOffset, int iMaxCount, int lastcount); void RetrievePollingInfo(); void OnReceivePollingInfo(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveAuthRequest(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void __cdecl PollingThread(void*); int PollServer(); @@ -267,6 +270,7 @@ private: enum CLMenuIndexes { CMI_GETALLSERVERHISTORY, CMI_VISITPROFILE, + CMI_ADDASFRIEND, CMI_COUNT }; enum ProtoMenuIndexes { @@ -361,4 +365,8 @@ private: INT_PTR __cdecl SvcCreateChat(WPARAM, LPARAM); CMString GetAttachmentDescr(JSONNODE*); + + HANDLE m_hPopupClass; + void InitPopups(void); + void MsgPopup(MCONTACT hContact, const TCHAR *szMsg, const TCHAR *szTitle); }; diff --git a/protocols/VKontakte/src/vk_thread.cpp b/protocols/VKontakte/src/vk_thread.cpp index d0e20419db..45fb8ca95d 100644 --- a/protocols/VKontakte/src/vk_thread.cpp +++ b/protocols/VKontakte/src/vk_thread.cpp @@ -389,10 +389,12 @@ void CVkProto::OnReceiveFriends(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq bool bCleanContacts = getBool("AutoClean", false); LIST arContacts(10, PtrKeySortT); - if (bCleanContacts) - for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)) - if (!isChatRoom(hContact)) - arContacts.insert((HANDLE)hContact); + + for (MCONTACT hContact = db_find_first(m_szModuleName); hContact; hContact = db_find_next(hContact, m_szModuleName)){ + db_unset(hContact, m_szModuleName, "friend"); + if (bCleanContacts&&!isChatRoom(hContact)) + arContacts.insert((HANDLE)hContact); + } int iCount = json_as_int(json_get(pResponse, "count")); JSONNODE *pItems = json_get(pResponse, "items"); @@ -403,12 +405,14 @@ void CVkProto::OnReceiveFriends(NETLIBHTTPREQUEST *reply, AsyncHttpRequest *pReq continue; CMString tszNick; + MCONTACT hContact = FindUser(_ttoi(szValue), true); arContacts.remove((HANDLE)hContact); + setByte(hContact, "friend", 1); + szValue = json_as_string(json_get(pInfo, "first_name")); if (szValue) { setTString(hContact, "FirstName", szValue); - tszNick.Append(szValue); tszNick.AppendChar(' '); } @@ -838,6 +842,12 @@ INT_PTR __cdecl CVkProto::SvcSetListeningTo(WPARAM wParam, LPARAM lParam) } ///////////////////////////////////////////////////////////////////////////////////////// +INT_PTR __cdecl CVkProto::SvcAddAsFriend(WPARAM hContact, LPARAM) +{ + CallContactService(hContact, PSS_AUTHREQUESTW, 0, (LPARAM)TranslateT("Please authorize me to add you to my friend list.")); + return 0; +} + INT_PTR __cdecl CVkProto::SvcVisitProfile(WPARAM hContact, LPARAM) { -- cgit v1.2.3