From 40ca3f169e61d1273d47ddcee277bbc5bcb6ffdf Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 10 Aug 2014 16:10:24 +0000 Subject: Tox: first approach to saving and restoring data git-svn-id: http://svn.miranda-ng.org/main/trunk@10145 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_account.cpp | 13 ++++++ protocols/Tox/src/tox_proto.cpp | 16 +++++++ protocols/Tox/src/tox_proto.h | 7 +++- protocols/Tox/src/tox_utils.cpp | 88 +++++++++++++++++++++++++++++++++++---- 4 files changed, 114 insertions(+), 10 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index 9c59965bc7..c63328f6ac 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -45,9 +45,22 @@ void CToxProto::ConnectionThread(void*) { DoBootstrap(); + uint8_t name[TOX_MAX_NAME_LENGTH + 1]; + uint16_t namelen = tox_get_self_name(tox, name); + name[namelen] = 0; + if (tox_isconnected(tox)) { isConnected = true; + + char dataPath[MAX_PATH]; + mir_snprintf(dataPath, MAX_PATH, "%s\\%s.tox", VARS("%miranda_profile%\\%miranda_profilename%"), m_tszUserName); + + SaveToxData(dataPath); + + char idstring3[200] = { 0 }; + get_id(tox, idstring3); + break; } diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 3123640ec2..41abbb673a 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -5,6 +5,17 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : { tox = tox_new(TOX_ENABLE_IPV6_DEFAULT); + char idstring[200] = { 0 }; + get_id(tox, idstring); + + char dataPath[MAX_PATH]; + mir_snprintf(dataPath, MAX_PATH, "%s\\%s.tox", VARS("%miranda_profile%\\%miranda_profilename%"), m_tszUserName); + + LoadToxData(dataPath); + + char idstring2[200] = { 0 }; + get_id(tox, idstring2); + tox_callback_friend_request(tox, OnFriendRequest, this); tox_callback_friend_message(tox, OnFriendMessage, this); tox_callback_friend_action(tox, OnAction, this); @@ -18,6 +29,11 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : CToxProto::~CToxProto() { + char dataPath[MAX_PATH]; + mir_snprintf(dataPath, MAX_PATH, "%s\\%s.tox", VARS("%miranda_profile%\\%miranda_profilename%"), m_tszUserName); + + SaveToxData(dataPath); + tox_kill(tox); } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 49f8a6c131..3ebd7fe88b 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -112,7 +112,12 @@ private: TOX_USERSTATUS MirandaToToxStatus(int status); uint8_t *HexStringToData(const char *hex_string); char *DataToHexString(const uint8_t *bin_string); - void do_bootstrap(Tox *tox); + + int LoadToxData(const char *path); + int SaveToxData(const char *path); + + static void fraddr_to_str(uint8_t *id_bin, char *id_str); + static void get_id(Tox *m, char *data); // dialogs static INT_PTR CALLBACK AccountManagerProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); diff --git a/protocols/Tox/src/tox_utils.cpp b/protocols/Tox/src/tox_utils.cpp index f7f6c027ad..d7c1763145 100644 --- a/protocols/Tox/src/tox_utils.cpp +++ b/protocols/Tox/src/tox_utils.cpp @@ -38,12 +38,11 @@ uint8_t *HexStringToData(const char *hex_string) char *CToxProto::DataToHexString(const uint8_t *bin_string) { - uint32_t i, delta = 0, pos_extra, sum_extra = 0; - char *ret = (char*)mir_alloc(TOX_FRIEND_ADDRESS_SIZE); + uint32_t delta = 0, pos_extra, sum_extra = 0; + char *ret = (char*)mir_alloc(TOX_FRIEND_ADDRESS_SIZE + 1); - for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) - { - sprintf(&ret[2 * i + delta], "%02hhX", bin_string[i]); + for (uint32_t i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { + sprintf(&ret[2 * i + delta], "%02X", bin_string[i]); if ((i + 1) == TOX_CLIENT_ID_SIZE) pos_extra = 2 * (i + 1) + delta; @@ -54,10 +53,10 @@ char *CToxProto::DataToHexString(const uint8_t *bin_string) /*if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { id_str[2 * (i + 1) + delta] = ' '; delta++; - }*/ + }*/ } - ret[2 * i + delta] = 0; + //ret[2 * i + delta] = 0; if (!sum_extra) ret[pos_extra] = 0; @@ -65,9 +64,80 @@ char *CToxProto::DataToHexString(const uint8_t *bin_string) return ret; } +int CToxProto::LoadToxData(const char *path) +{ + FILE *hFile = fopen(path, "r"); + + if (hFile) + { + fseek(hFile, 0, SEEK_END); + size_t size = ftell(hFile); + rewind(hFile); + + uint8_t *data = (uint8_t*)mir_alloc(size); + + if (fread(data, sizeof(uint8_t), size, hFile) != size) + { + mir_free(data); + //fputs("[!] could not read data file!\n", stderr); + fclose(hFile); + return 0; + } + + tox_load(tox, data, size); + + mir_free(data); + + if (fclose(hFile) < 0) + { + //perror("[!] fclose failed"); + /* we got it open and the expected data read... let it be ok */ + /* return 0; */ + } + + return 1; + } + + return 0; +} + +int CToxProto::SaveToxData(const char *path) +{ + FILE *hFile = fopen(path, "w"); + + if (!hFile) + { + //perror("[!] load_key"); + return 0; + } + + int res = 1; + size_t size = tox_size(tox); + uint8_t *data = (uint8_t*)mir_alloc(size); + + tox_save(tox, data); + + if (fwrite(data, sizeof(uint8_t), size, hFile) != size) + { + mir_free(data); + //fputs("[!] could not write data file (1)!", stderr); + res = 0; + } + + mir_free(data); + + if (fclose(hFile) < 0) + { + //perror("[!] could not write data file (2)"); + res = 0; + } + + return res; +} + #define FRADDR_TOSTR_CHUNK_LEN 8 -static void fraddr_to_str(uint8_t *id_bin, char *id_str) +void CToxProto::fraddr_to_str(uint8_t *id_bin, char *id_str) { uint32_t i, delta = 0, pos_extra, sum_extra = 0; @@ -92,7 +162,7 @@ static void fraddr_to_str(uint8_t *id_bin, char *id_str) id_str[pos_extra] = 0; } -void get_id(Tox *m, char *data) +void CToxProto::get_id(Tox *m, char *data) { int offset = strlen(data); uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; -- cgit v1.2.3