From c546793b2a39c59a688e972e06138f0c6b432f63 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 21 Aug 2014 04:28:31 +0000 Subject: Tox: ability to search friends via toxme.se git-svn-id: http://svn.miranda-ng.org/main/trunk@10249 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/common.h | 1 + protocols/Tox/src/tox_contacts.cpp | 62 +++++++++++++++++++++++++++++++++++--- protocols/Tox/src/tox_proto.cpp | 15 +++++++-- protocols/Tox/src/tox_proto.h | 1 + 4 files changed, 72 insertions(+), 7 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/common.h b/protocols/Tox/src/common.h index 745216b542..c039f5fc75 100644 --- a/protocols/Tox/src/common.h +++ b/protocols/Tox/src/common.h @@ -9,6 +9,7 @@ #include #include #include +#include #include diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 4bb4c583a2..91d62b8907 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -134,6 +134,7 @@ void CToxProto::SearchByIdAsync(void* arg) std::string clientId = mir_utf8encodeT((TCHAR*)arg); clientId.erase(clientId.begin() + TOX_CLIENT_ID_SIZE * 2, clientId.end()); + std::string toxId = clientId; MCONTACT hContact = FindContact(clientId.c_str()); if (hContact) { @@ -142,10 +143,61 @@ void CToxProto::SearchByIdAsync(void* arg) return; } - PROTOSEARCHRESULT psr = { sizeof(PROTOSEARCHRESULT) }; - psr.flags = PSR_TCHAR; - psr.id = (TCHAR*)arg; + ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)1, 0); +} + +void CToxProto::SearchByNameAsync(void* arg) +{ + NETLIBHTTPREQUEST request = { sizeof(NETLIBHTTPREQUEST) }; + request.requestType = REQUEST_POST; + request.szUrl = "https://toxme.se/api"; + request.flags = NLHRF_HTTP11 | NLHRF_SSL | NLHRF_NODUMP; + + request.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)*2); + request.headers[0].szName = mir_strdup("Content-Type"); + request.headers[0].szValue = mir_strdup("text/plain; charset=utf-8"); + request.headersCount = 1; + + char data[128]; + ptrA search(mir_utf8encodeT((TCHAR*)arg)); + ptrA searchEncoded(mir_urlEncode(search)); + mir_snprintf(data, SIZEOF(data), "{\"action\":3,\"name\":\"%s\"}", searchEncoded); + + request.dataLength = strlen(data); + request.pData = (char*)mir_alloc(request.dataLength + 1); + memcpy(request.pData, data, request.dataLength); + request.pData[request.dataLength] = 0; + + NETLIBHTTPREQUEST* response = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlibUser, (LPARAM)&request); + + if (response) + { + std::smatch match; + std::regex regex("\"public_key\": \"(.+?)\""); + + const std::string content = response->pData; + + if (std::regex_search(content, match, regex)) + { + std::string toxId = match[1]; + + PROTOSEARCHRESULT psr = { sizeof(PROTOSEARCHRESULT) }; + psr.flags = PSR_TCHAR; + psr.id = mir_a2t(toxId.c_str()); + + ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)&psr); + ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0); + + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)response); + return; + } + } + + CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)response); + mir_free(request.pData); + mir_free(request.headers[0].szName); + mir_free(request.headers[0].szValue); + mir_free(request.headers); - ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)&psr); - ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0); + ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)1, 0); } \ No newline at end of file diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 7682f80216..18af6e9a73 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -40,7 +40,7 @@ DWORD_PTR __cdecl CToxProto::GetCaps(int type, MCONTACT hContact) switch (type) { case PFLAGNUM_1: - return PF1_IM | PF1_AUTHREQ | PF1_BASICSEARCH; + return PF1_IM | PF1_AUTHREQ | PF1_BASICSEARCH | PF1_SEARCHBYEMAIL; case PFLAGNUM_2: return PF2_ONLINE | PF2_SHORTAWAY | PF2_LIGHTDND; case PFLAGNUM_4: @@ -141,7 +141,18 @@ HANDLE __cdecl CToxProto::SearchBasic(const PROTOCHAR* id) return (HANDLE)1; } -HANDLE __cdecl CToxProto::SearchByEmail(const PROTOCHAR* email) { return 0; } +HANDLE __cdecl CToxProto::SearchByEmail(const PROTOCHAR* email) +{ + if (!this->IsOnline()) + { + return 0; + } + + ForkThread(&CToxProto::SearchByNameAsync, mir_tstrdup(email)); + + return (HANDLE)1; +} + HANDLE __cdecl CToxProto::SearchByName(const PROTOCHAR* nick, const PROTOCHAR* firstName, const PROTOCHAR* lastName) { return 0; } HWND __cdecl CToxProto::SearchAdvanced(HWND owner) { return 0; } HWND __cdecl CToxProto::CreateExtendedSearchUI(HWND owner) { return 0; } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index c2f982c4a3..83f4ef026c 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -130,6 +130,7 @@ private: void LoadContactList(); void __cdecl SearchByIdAsync(void* arg); + void __cdecl SearchByNameAsync(void* arg); // utils TOX_USERSTATUS MirandaToToxStatus(int status); -- cgit v1.2.3