diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2014-09-10 20:29:30 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2014-09-10 20:29:30 +0000 |
commit | 9b11e2e558cdee65b42ffd56601488ec3cbce534 (patch) | |
tree | cfac16c5acaf8c34350246e08c04960b8d9ae356 /protocols/Tox/src/tox_search.cpp | |
parent | b8a7168685627ff4ea79dcaf3d917f6b40a23f3b (diff) |
Tox:
- reworked searching
- updated tox core
- updated icon
git-svn-id: http://svn.miranda-ng.org/main/trunk@10423 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'protocols/Tox/src/tox_search.cpp')
-rw-r--r-- | protocols/Tox/src/tox_search.cpp | 164 |
1 files changed, 123 insertions, 41 deletions
diff --git a/protocols/Tox/src/tox_search.cpp b/protocols/Tox/src/tox_search.cpp index 82dcbd5be7..88726cadb8 100644 --- a/protocols/Tox/src/tox_search.cpp +++ b/protocols/Tox/src/tox_search.cpp @@ -1,58 +1,71 @@ #include "common.h"
+#include "tox_dns.h"
-void CToxProto::SearchByIdAsync(void*)
+void CToxProto::SearchFailedAsync(void*)
{
ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HWND)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;
+ char *query = (char*)arg;
+ char *name = strtok(query, "@");
+ char *domain = strtok(NULL, "");
- request.headers = (NETLIBHTTPHEADER*)mir_alloc(sizeof(NETLIBHTTPHEADER)* 2);
- request.headers[0].szName = "Content-Type";
- request.headers[0].szValue = "text/plain; charset=utf-8";
- request.headersCount = 1;
-
- std::string query = "{\"action\":3,\"name\":\"";
- query += (char*)arg;
- query += "\"}";
-
- request.dataLength = query.length();
- request.pData = (char*)query.c_str();
-
- NETLIBHTTPREQUEST* response = (NETLIBHTTPREQUEST*)CallService(MS_NETLIB_HTTPTRANSACTION, (WPARAM)hNetlib, (LPARAM)&request);
-
- if (response)
+ int i = 0;
+ static int j = 0;
+ while (i < 2)
{
- std::smatch match;
- std::regex regex("\"public_key\": \"(.+?)\"");
-
- const std::string content = response->pData;
-
- if (std::regex_search(content, match, regex))
+ struct dns_server *server = &dns_servers[j % SIZEOF(dns_servers)];
+ if (domain == NULL || strcmp(domain, server->domain) == 0)
{
- 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;
+ void *dns = tox_dns3_new(server->key);
+
+ uint8_t dnsString[256];
+ uint32_t requestId = 0;
+ int length = tox_generate_dns3_string(dns, dnsString, sizeof(dnsString), &requestId, (uint8_t*)name, strlen(name));
+ if (length != TOX_ERROR)
+ {
+ dnsString[length] = 0;
+
+ char dnsQuery[512];
+ mir_snprintf(dnsQuery, 512, "_%s._tox.%s", dnsString, server->domain);
+
+ bool success = false;
+ DNS_RECORDA *record = NULL;
+ DnsQuery_A(dnsQuery, DNS_TYPE_TEXT, 0, NULL, (PDNS_RECORD*)&record, NULL);
+ while (record)
+ {
+ DNS_TXT_DATAA *txt = &record->Data.Txt;
+ if (record->wType == DNS_TYPE_TEXT && txt->dwStringCount)
+ {
+ char *recordId = &txt->pStringArray[0][10];
+ std::vector<uint8_t> address(TOX_FRIEND_ADDRESS_SIZE);
+ if (tox_decrypt_dns3_TXT(dns, &address[0], (uint8_t*)recordId, strlen(recordId), requestId) != TOX_ERROR)
+ {
+ std::string id = DataToHexString(address);
+
+ PROTOSEARCHRESULT psr = { sizeof(PROTOSEARCHRESULT) };
+ psr.flags = PSR_TCHAR;
+ psr.id = mir_a2t(id.c_str());
+ psr.nick = mir_a2t(name);
+
+ TCHAR email[MAX_PATH];
+ mir_sntprintf(email, SIZEOF(email), _T("%s@%s"), _A2T(name), _A2T(server->domain));
+ psr.email = mir_tstrdup(email);
+
+ ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_DATA, (HANDLE)1, (LPARAM)&psr);
+ }
+ }
+ record = record->pNext;
+ }
+ }
+ tox_dns3_kill(dns);
}
+ i++; j++;
}
- CallService(MS_NETLIB_FREEHTTPREQUESTSTRUCT, 0, (LPARAM)response);
- mir_free(request.headers);
-
- ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_FAILED, (HANDLE)1, 0);
+ ProtoBroadcastAck(NULL, ACKTYPE_SEARCH, ACKRESULT_SUCCESS, (HANDLE)1, 0);
mir_free(arg);
}
@@ -76,4 +89,73 @@ INT_PTR CToxProto::SearchDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lPa }
return FALSE;
+}
+
+HANDLE __cdecl CToxProto::SearchBasic(const PROTOCHAR* id) { return 0; }
+
+HANDLE __cdecl CToxProto::SearchByEmail(const PROTOCHAR* email) { return 0; }
+
+HANDLE __cdecl CToxProto::SearchByName(const PROTOCHAR* nick, const PROTOCHAR* firstName, const PROTOCHAR* lastName) { return 0; }
+
+HWND __cdecl CToxProto::SearchAdvanced(HWND owner)
+{
+ if (!IsOnline())
+ {
+ // we cannot add someone to friend list while tox is offline
+ return NULL;
+ }
+
+ std::smatch match;
+ std::regex regex("^\\s*([A-Fa-f0-9]{76})\\s*$");
+
+ char text[TOX_FRIEND_ADDRESS_SIZE * 2 + 1];
+ GetDlgItemTextA(owner, IDC_SEARCH, text, SIZEOF(text));
+
+ const std::string query = text;
+ if (std::regex_search(query, match, regex))
+ {
+ std::string address = match[1];
+ std::vector<uint8_t> id = HexStringToData(address);
+ MCONTACT hContact = FindContact(id);
+ if (!hContact)
+ {
+ PROTOSEARCHRESULT psr = { sizeof(psr) };
+ psr.flags = PSR_TCHAR;
+ psr.id = mir_a2t(query.c_str());
+
+ ADDCONTACTSTRUCT acs = { HANDLE_SEARCHRESULT };
+ acs.szProto = m_szModuleName;
+ acs.psr = &psr;
+
+ CallService(MS_ADDCONTACT_SHOW, (WPARAM)owner, (LPARAM)&acs);
+ }
+ else
+ {
+ ShowNotification(TranslateT("Contact already in your contact list"), 0, hContact);
+ }
+ ForkThread(&CToxProto::SearchFailedAsync, NULL);
+ }
+ else
+ {
+ regex = "^\\s*(([^ @/:;()\"']+)(@[A-Za-z]+.[A-Za-z]{2,6})?)\\s*$";
+ if (std::regex_search(query, match, regex))
+ {
+ ForkThread(&CToxProto::SearchByNameAsync, mir_strdup(match[1].str().c_str()));
+ }
+ else
+ {
+ ForkThread(&CToxProto::SearchFailedAsync, NULL);
+ }
+ }
+ return (HWND)1;
+}
+
+HWND __cdecl CToxProto::CreateExtendedSearchUI(HWND owner)
+{
+ return CreateDialogParam(
+ g_hInstance,
+ MAKEINTRESOURCE(IDD_SEARCH),
+ owner,
+ SearchDlgProc,
+ (LPARAM)this);
}
\ No newline at end of file |