diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2014-08-21 04:28:31 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2014-08-21 04:28:31 +0000 |
commit | c546793b2a39c59a688e972e06138f0c6b432f63 (patch) | |
tree | d553f59e1999b62e49afbcb815305e41649dc290 /protocols/Tox/src/tox_contacts.cpp | |
parent | 78bcdef0ca6f1ee54b5e3d1d728e5d6808fd6ed0 (diff) |
Tox: ability to search friends via toxme.se
git-svn-id: http://svn.miranda-ng.org/main/trunk@10249 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Tox/src/tox_contacts.cpp')
-rw-r--r-- | protocols/Tox/src/tox_contacts.cpp | 62 |
1 files changed, 57 insertions, 5 deletions
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 |