From fa58f69fe117640e29cefb1b699bede4d045bc2f Mon Sep 17 00:00:00 2001 From: aunsane Date: Sat, 3 Mar 2018 16:35:09 +0300 Subject: Tox: - updated toxcore due to release v0.2 - removed message correction - reworked nodes update --- protocols/Tox/src/stdafx.h | 2 +- protocols/Tox/src/tox_bootstrap.cpp | 146 ++++++++++++++++++------------------ protocols/Tox/src/tox_messages.cpp | 11 --- protocols/Tox/src/tox_options.cpp | 44 +++-------- protocols/Tox/src/tox_proto.h | 1 + 5 files changed, 87 insertions(+), 117 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/stdafx.h b/protocols/Tox/src/stdafx.h index 6d0f70935f..0c53490d34 100644 --- a/protocols/Tox/src/stdafx.h +++ b/protocols/Tox/src/stdafx.h @@ -70,7 +70,7 @@ extern HINSTANCE g_hInstance; #define TOX_MAX_RECONNECT_RETRIES 10 #define TOX_INI_PATH "%miranda_path%\\Plugins\\tox.ini" -#define TOX_JSON_PATH "%miranda_userdata%\\tox.json" +#define TOX_JSON_PATH L"%miranda_userdata%\\tox.json" #define TOX_SETTINGS_ID "ToxID" #define TOX_SETTINGS_DNS "DnsID" diff --git a/protocols/Tox/src/tox_bootstrap.cpp b/protocols/Tox/src/tox_bootstrap.cpp index f144c0af16..7bdf45bbdc 100644 --- a/protocols/Tox/src/tox_bootstrap.cpp +++ b/protocols/Tox/src/tox_bootstrap.cpp @@ -33,76 +33,57 @@ void CToxProto::BootstrapNodesFromDb(Tox *tox, bool isIPv6) char module[MAX_PATH]; mir_snprintf(module, "%s_Nodes", m_szModuleName); int nodeCount = db_get_w(NULL, module, TOX_SETTINGS_NODE_COUNT, 0); - if (nodeCount > 0) { - char setting[MAX_PATH]; - for (int i = 0; i < nodeCount; i++) { - mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i); - ptrA address(db_get_sa(NULL, module, setting)); - mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i); - int port = db_get_w(NULL, module, setting, 33445); - mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i); - ptrA pubKey(db_get_sa(NULL, module, setting)); + if (nodeCount == 0) + return; + + char setting[MAX_PATH]; + for (int i = 0; i < nodeCount; i++) { + mir_snprintf(setting, TOX_SETTINGS_NODE_IPV4, i); + ptrA address(db_get_sa(NULL, module, setting)); + mir_snprintf(setting, TOX_SETTINGS_NODE_PORT, i); + int port = db_get_w(NULL, module, setting, 33445); + mir_snprintf(setting, TOX_SETTINGS_NODE_PKEY, i); + ptrA pubKey(db_get_sa(NULL, module, setting)); + BootstrapUdpNode(tox, address, port, pubKey); + BootstrapTcpRelay(tox, address, port, pubKey); + if (isIPv6) { + mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i); + address = db_get_sa(NULL, module, setting); BootstrapUdpNode(tox, address, port, pubKey); BootstrapTcpRelay(tox, address, port, pubKey); - if (isIPv6) { - mir_snprintf(setting, TOX_SETTINGS_NODE_IPV6, i); - address = db_get_sa(NULL, module, setting); - BootstrapUdpNode(tox, address, port, pubKey); - BootstrapTcpRelay(tox, address, port, pubKey); - } } } } void CToxProto::BootstrapNodesFromJson(Tox *tox, bool isIPv6) { - ptrA json; - - VARSW path(_A2W(TOX_JSON_PATH)); - - if (!IsFileExists(path)) + VARSW path(TOX_JSON_PATH); + long lastUpdate = getDword("NodesUpdate", 0); + if (!IsFileExists(path) || lastUpdate < (now() - 86400 /* 24h */)) UpdateNodes(); - if (IsFileExists(path)) { - FILE *hFile = _wfopen(path, L"r"); - if (hFile != nullptr) { - _fseeki64(hFile, 0, SEEK_END); - size_t size = _ftelli64(hFile); - json = (char*)mir_calloc(size); - rewind(hFile); - fread(json, sizeof(char), size, hFile); - fclose(hFile); - } - } + JSONNode nodes = ParseNodes(); + for (const auto &node : nodes) { + JSONNode address = node.at("ipv4"); + JSONNode pubKey = node.at("public_key"); - if (json) { - JSONNode root = JSONNode::parse(json); - if (!root.empty()) { - JSONNode nodes = root.at("nodes").as_array(); - for (size_t i = 0; i < nodes.size(); i++) { - JSONNode node = nodes[i]; - JSONNode address = node.at("ipv4"); - JSONNode pubKey = node.at("public_key"); - - if (node.at("status_udp").as_bool()) { - int port = node.at("port").as_int(); - BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - if (isIPv6) { - address = node.at("ipv6"); - BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - } - } + if (node.at("status_udp").as_bool()) { + int port = node.at("port").as_int(); + BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + if (isIPv6) { + address = node.at("ipv6"); + BootstrapUdpNode(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + } + } - if (node.at("status_tcp").as_bool()) { - JSONNode tcpPorts = node.at("tcp_ports").as_array(); - for (size_t k = 0; k < tcpPorts.size(); k++) { - int port = tcpPorts[k].as_int(); - BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - if (isIPv6) { - address = node.at("ipv6"); - BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); - } - } + if (node.at("status_tcp").as_bool()) { + JSONNode tcpPorts = node.at("tcp_ports").as_array(); + for (size_t k = 0; k < tcpPorts.size(); k++) { + int port = tcpPorts[k].as_int(); + BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); + if (isIPv6) { + address = node.at("ipv6"); + BootstrapTcpRelay(tox, address.as_string().c_str(), port, pubKey.as_string().c_str()); } } } @@ -111,16 +92,16 @@ void CToxProto::BootstrapNodesFromJson(Tox *tox, bool isIPv6) void CToxProto::BootstrapNodes(Tox *tox) { - UpdateNodes(); debugLogA(__FUNCTION__": bootstraping DHT"); - // bool isUdp = getBool("EnableUDP", 1); bool isIPv6 = getBool("EnableIPv6", 0); - BootstrapNodesFromDb(tox, isIPv6); BootstrapNodesFromJson(tox, isIPv6); + BootstrapNodesFromDb(tox, isIPv6); } void CToxProto::UpdateNodes() { + VARSW path(TOX_JSON_PATH); + debugLogA(__FUNCTION__": updating nodes"); HttpRequest request(REQUEST_GET, "https://nodes.tox.chat/json"); NLHR_PTR response(request.Send(m_hNetlibUser)); @@ -139,18 +120,8 @@ void CToxProto::UpdateNodes() if (lastUpdate <= getDword("NodesUpdate", 0)) return; - ptrW path(mir_wstrdup((wchar_t*)VARSW(_A2W(TOX_JSON_PATH)))); - if (!IsFileExists(path)) { - HANDLE hProfile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); - if (hProfile == nullptr) { - debugLogA(__FUNCTION__": failed to create tox.json"); - return; - } - CloseHandle(hProfile); - } - FILE *hFile = _wfopen(path, L"w"); - if (!hFile) { + if (hFile == nullptr) { debugLogA(__FUNCTION__": failed to open tox.json"); return; } @@ -162,3 +133,34 @@ void CToxProto::UpdateNodes() setDword("NodesUpdate", lastUpdate); } + +JSONNode CToxProto::ParseNodes() +{ + VARSW path(TOX_JSON_PATH); + + if (!IsFileExists(path)) { + debugLogA(__FUNCTION__": could not find tox.json"); + return JSONNode(JSON_ARRAY); + } + + FILE *hFile = _wfopen(path, L"r"); + if (hFile == nullptr) { + debugLogA(__FUNCTION__": failed to open tox.json"); + return JSONNode(JSON_ARRAY); + } + + _fseeki64(hFile, 0, SEEK_END); + size_t size = _ftelli64(hFile); + ptrA json((char*)mir_calloc(size)); + rewind(hFile); + fread(json, sizeof(char), size, hFile); + fclose(hFile); + + JSONNode root = JSONNode::parse(json); + if (root.empty()) { + debugLogA(__FUNCTION__": failed to parse tox.json"); + return JSONNode(JSON_ARRAY); + } + + return root.at("nodes").as_array(); +} diff --git a/protocols/Tox/src/tox_messages.cpp b/protocols/Tox/src/tox_messages.cpp index 8d39d1d6d7..3ca2d81ca6 100644 --- a/protocols/Tox/src/tox_messages.cpp +++ b/protocols/Tox/src/tox_messages.cpp @@ -9,10 +9,6 @@ void CToxProto::InitCustomDbEvents() dbEventType.eventType = DB_EVENT_ACTION; dbEventType.descr = Translate("Action"); DbEvent_RegisterType(&dbEventType); - - dbEventType.eventType = DB_EVENT_CORRECTION; - dbEventType.descr = Translate("Correction"); - DbEvent_RegisterType(&dbEventType); } INT_PTR CToxProto::EventGetIcon(WPARAM wParam, LPARAM lParam) @@ -25,10 +21,6 @@ INT_PTR CToxProto::EventGetIcon(WPARAM wParam, LPARAM lParam) icon = GetIcon(IDI_ME); break; - case DB_EVENT_CORRECTION: - icon = GetIcon(IDI_EDIT); - break; - default: icon = Skin_LoadIcon(SKINICON_EVENT_MESSAGE); break; @@ -69,9 +61,6 @@ void CToxProto::OnFriendMessage(Tox *tox, uint32_t friendNumber, TOX_MESSAGE_TYP case TOX_MESSAGE_TYPE_ACTION: recv.lParam = DB_EVENT_ACTION; break; - case TOX_MESSAGE_TYPE_CORRECTION: - recv.lParam = DB_EVENT_CORRECTION; - break; } ProtoChainRecvMsg(hContact, &recv); diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 119bce5a0d..f52202c1eb 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -439,42 +439,20 @@ void CToxOptionsNodeList::ReloadNodeList() int iItem = -1; - VARSW path(_A2W(TOX_JSON_PATH)); - if (CToxProto::IsFileExists(path)) { - ptrA json; - - FILE *hFile = _wfopen(path, L"r"); - if (hFile != nullptr) { - _fseeki64(hFile, 0, SEEK_END); - size_t size = _ftelli64(hFile); - json = (char*)mir_calloc(size); - rewind(hFile); - fread(json, sizeof(char), size, hFile); - fclose(hFile); - } - - if (json) { - JSONNode root = JSONNode::parse(json); - if (!root.empty()) { - JSONNode nodes = root.at("nodes").as_array(); - for (size_t i = 0; i < nodes.size(); i++) { - JSONNode node = nodes[i]; + JSONNode nodes = m_proto->ParseNodes(); + for (const auto &node : nodes) { + ptrW ipv4(mir_utf8decodeW(node.at("ipv4").as_string().c_str())); + iItem = m_nodes.AddItem(ipv4, -1, NULL, 0); - ptrW ipv4(mir_utf8decodeW(node.at("ipv4").as_string().c_str())); - iItem = m_nodes.AddItem(ipv4, -1, NULL, 0); + ptrW ipv6(mir_utf8decodeW(node.at("ipv6").as_string().c_str())); + if (mir_wstrcmp(ipv6, L"-")) + m_nodes.SetItem(iItem, 1, ipv6); - ptrW ipv6(mir_utf8decodeW(node.at("ipv6").as_string().c_str())); - if (mir_wstrcmp(ipv6, L"-")) - m_nodes.SetItem(iItem, 1, ipv6); + ptrW port(mir_utf8decodeW(node.at("port").as_string().c_str())); + m_nodes.SetItem(iItem, 2, port); - ptrW port(mir_utf8decodeW(node.at("port").as_string().c_str())); - m_nodes.SetItem(iItem, 2, port); - - ptrW pubKey(mir_utf8decodeW(node.at("public_key").as_string().c_str())); - m_nodes.SetItem(iItem, 3, pubKey); - } - } - } + ptrW pubKey(mir_utf8decodeW(node.at("public_key").as_string().c_str())); + m_nodes.SetItem(iItem, 3, pubKey); } char module[MAX_PATH], setting[MAX_PATH]; diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 3971d76a72..b09ba4d4e8 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -104,6 +104,7 @@ private: void BootstrapNodes(Tox *tox); void UpdateNodes(); + JSONNode ParseNodes(); // tox connection bool IsOnline(); -- cgit v1.2.3