From 4cf5a9ef45a6f6172e93c1f84bcbab22be00e0a4 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Wed, 7 Jan 2015 21:40:06 +0000 Subject: Tox: enabled profile encryption git-svn-id: http://svn.miranda-ng.org/main/trunk@11790 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/src/tox_account.cpp | 22 +++- protocols/Tox/src/tox_contacts.cpp | 7 +- protocols/Tox/src/tox_events.cpp | 17 --- protocols/Tox/src/tox_options.cpp | 233 +++++++++++++++++++------------------ protocols/Tox/src/tox_profile.cpp | 79 +++++++------ protocols/Tox/src/tox_proto.cpp | 10 +- protocols/Tox/src/tox_proto.h | 8 +- protocols/Tox/src/version.h | 2 +- 8 files changed, 195 insertions(+), 183 deletions(-) (limited to 'protocols/Tox/src') diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index bc9a984288..5c4154af29 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -10,7 +10,6 @@ int CToxProto::OnAccountLoaded(WPARAM, LPARAM) { HookProtoEvent(ME_OPT_INITIALISE, &CToxProto::OnOptionsInit); HookProtoEvent(ME_USERINFO_INITIALISE, &CToxProto::OnUserInfoInit); - //HookProtoEvent(ME_DB_CONTACT_SETTINGCHANGED, &CToxProto::OnSettingsChanged); HookProtoEvent(ME_MSG_PRECREATEEVENT, &CToxProto::OnPreCreateMessage); return 0; @@ -64,6 +63,7 @@ bool CToxProto::InitToxCore() } tox = tox_new(&options); + password = mir_utf8encodeW(ptrT(getTStringA("Password"))); bool isProfileLoaded = LoadToxProfile(); if (isProfileLoaded) { @@ -101,6 +101,15 @@ bool CToxProto::InitToxCore() SetToxAvatar(avatarPath); } } + else + { + if (password != NULL) + { + mir_free(password); + password = NULL; + } + tox_kill(tox); + } return isProfileLoaded; } @@ -111,6 +120,11 @@ void CToxProto::UninitToxCore() tox_set_name(tox, (uint8_t*)(char*)nickname, (uint16_t)strlen(nickname)); SaveToxProfile(); + if (password != NULL) + { + mir_free(password); + password = NULL; + } tox_kill(tox); } @@ -129,7 +143,7 @@ void CToxProto::DoBootstrap() void CToxProto::DoTox() { { - mir_cslock lck(toxLock); + mir_cslock lock(toxLock); tox_do(tox); } PulseEvent(hToxEvent); @@ -150,7 +164,9 @@ void CToxProto::PollingThread(void*) { isTerminated = true; m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE; - ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_NOSERVER); + ProtoBroadcastAck(NULL, ACKTYPE_LOGIN, ACKRESULT_FAILED, (HANDLE)NULL, LOGINERR_WRONGPASSWORD); + debugLogA("CToxProto::PollingThread: leaving"); + return; } while (!isTerminated) diff --git a/protocols/Tox/src/tox_contacts.cpp b/protocols/Tox/src/tox_contacts.cpp index 48c0de0274..110480ed8c 100644 --- a/protocols/Tox/src/tox_contacts.cpp +++ b/protocols/Tox/src/tox_contacts.cpp @@ -166,6 +166,11 @@ void CToxProto::LoadFriendList() int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam) { + if (!IsOnline()) + { + return 1; + } + std::string id = getStringA(hContact, TOX_SETTINGS_ID); std::vector clientId = HexStringToData(id); @@ -175,8 +180,6 @@ int CToxProto::OnContactDeleted(MCONTACT hContact, LPARAM lParam) return 1; } - SaveToxProfile(); - return 0; } diff --git a/protocols/Tox/src/tox_events.cpp b/protocols/Tox/src/tox_events.cpp index 789ac6039e..0f577dcdb7 100644 --- a/protocols/Tox/src/tox_events.cpp +++ b/protocols/Tox/src/tox_events.cpp @@ -28,22 +28,5 @@ int CToxProto::OnOptionsInit(WPARAM wParam, LPARAM) mir_free(title); - return 0; -} - -int CToxProto::OnSettingsChanged(WPARAM hContact, LPARAM lParam) -{ - DBCONTACTWRITESETTING* dbcws = (DBCONTACTWRITESETTING*)lParam; - if (hContact == NULL && !strcmp(dbcws->szModule, m_szModuleName)) - { - if (!strcmp(dbcws->szSetting, "Nick") && dbcws->value.pszVal) - { - if (tox && tox_set_name(tox, (uint8_t*)dbcws->value.pszVal, (uint16_t)strlen(dbcws->value.pszVal))) - { - SaveToxProfile(); - } - } - } - return 0; } \ No newline at end of file diff --git a/protocols/Tox/src/tox_options.cpp b/protocols/Tox/src/tox_options.cpp index 02748633ea..33c19a0b50 100644 --- a/protocols/Tox/src/tox_options.cpp +++ b/protocols/Tox/src/tox_options.cpp @@ -1,115 +1,120 @@ -#include "common.h" - -INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) -{ - CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA); - - switch (uMsg) - { - case WM_INITDIALOG: - TranslateDialogDefault(hwnd); - { - proto = (CToxProto*)lParam; - SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); - - ptrT nick(proto->getTStringA("Nick")); - SetDlgItemText(hwnd, IDC_NAME, nick); - - ptrT pass(proto->getTStringA("Password")); - SetDlgItemText(hwnd, IDC_PASSWORD, pass); - - ptrA address(proto->getStringA(TOX_SETTINGS_ID)); - if (address != NULL) - { - SetDlgItemTextA(hwnd, IDC_TOXID, address); - } - - ptrT group(proto->getTStringA(TOX_SETTINGS_GROUP)); - SetDlgItemText(hwnd, IDC_GROUP, group); - SendDlgItemMessage(hwnd, IDC_GROUP, EM_LIMITTEXT, 64, 0); - - CheckDlgButton(hwnd, IDC_DISABLE_UDP, proto->getBool("DisableUDP", 0)); - CheckDlgButton(hwnd, IDC_DISABLE_IPV6, proto->getBool("DisableIPv6", 0)); - - EnableWindow(GetDlgItem(hwnd, IDC_TOXID), proto->IsOnline()); - EnableWindow(GetDlgItem(hwnd, IDC_CLIPBOARD), proto->IsOnline()); - EnableWindow(GetDlgItem(hwnd, IDC_NAME), proto->IsOnline()); - EnableWindow(GetDlgItem(hwnd, IDC_PASSWORD), proto->IsOnline()); - } - return TRUE; - - case WM_COMMAND: - { - switch (LOWORD(wParam)) - { - case IDC_NAME: - case IDC_GROUP: - case IDC_PASSWORD: - if ((HWND)lParam == GetFocus()) - { - if (HIWORD(wParam) != EN_CHANGE) return 0; - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - } - break; - - case IDC_DISABLE_UDP: - case IDC_DISABLE_IPV6: - SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); - break; - } - - case IDC_CLIPBOARD: - { - char toxId[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; - GetDlgItemTextA(hwnd, IDC_TOXID, toxId, SIZEOF(toxId)); - if (OpenClipboard(GetDlgItem(hwnd, IDC_TOXID))) - { - EmptyClipboard(); - HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, sizeof(toxId)); - memcpy(GlobalLock(hMem), toxId, sizeof(toxId)); - GlobalUnlock(hMem); - SetClipboardData(CF_TEXT, hMem); - CloseClipboard(); - } - } - break; - } - break; - - case WM_NOTIFY: - if (reinterpret_cast(lParam)->code == PSN_APPLY) - { - TCHAR group[64]; - GetDlgItemText(hwnd, IDC_GROUP, group, SIZEOF(group)); - if (_tcslen(group) > 0) - { - proto->setTString(NULL, TOX_SETTINGS_GROUP, group); - Clist_CreateGroup(0, group); - } - else - { - proto->delSetting(NULL, TOX_SETTINGS_GROUP); - } - - proto->setByte("DisableUDP", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_UDP)); - proto->setByte("DisableIPv6", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_IPV6)); - - if (proto->IsOnline()) - { - TCHAR nick[TOX_MAX_NAME_LENGTH], pass[MAX_PATH]; - GetDlgItemText(hwnd, IDC_NAME, nick, TOX_MAX_NAME_LENGTH); - proto->setTString("Nick", nick); - - GetDlgItemText(hwnd, IDC_PASSWORD, pass, SIZEOF(pass)); - proto->setTString("Password", pass); - - proto->SaveToxProfile(); - } - - return TRUE; - } - break; - } - - return FALSE; +#include "common.h" + +INT_PTR CToxProto::MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) +{ + CToxProto *proto = (CToxProto*)GetWindowLongPtr(hwnd, GWLP_USERDATA); + + switch (uMsg) + { + case WM_INITDIALOG: + TranslateDialogDefault(hwnd); + { + proto = (CToxProto*)lParam; + SetWindowLongPtr(hwnd, GWLP_USERDATA, lParam); + + ptrT nick(proto->getTStringA("Nick")); + SetDlgItemText(hwnd, IDC_NAME, nick); + + ptrT pass(proto->getTStringA("Password")); + SetDlgItemText(hwnd, IDC_PASSWORD, pass); + + ptrA address(proto->getStringA(TOX_SETTINGS_ID)); + if (address != NULL) + { + SetDlgItemTextA(hwnd, IDC_TOXID, address); + } + + ptrT group(proto->getTStringA(TOX_SETTINGS_GROUP)); + SetDlgItemText(hwnd, IDC_GROUP, group); + SendDlgItemMessage(hwnd, IDC_GROUP, EM_LIMITTEXT, 64, 0); + + CheckDlgButton(hwnd, IDC_DISABLE_UDP, proto->getBool("DisableUDP", 0)); + CheckDlgButton(hwnd, IDC_DISABLE_IPV6, proto->getBool("DisableIPv6", 0)); + + EnableWindow(GetDlgItem(hwnd, IDC_TOXID), proto->IsOnline()); + EnableWindow(GetDlgItem(hwnd, IDC_CLIPBOARD), proto->IsOnline()); + } + return TRUE; + + case WM_COMMAND: + { + switch (LOWORD(wParam)) + { + case IDC_NAME: + case IDC_GROUP: + case IDC_PASSWORD: + if ((HWND)lParam == GetFocus()) + { + if (HIWORD(wParam) != EN_CHANGE) return 0; + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + } + break; + + case IDC_DISABLE_UDP: + case IDC_DISABLE_IPV6: + SendMessage(GetParent(hwnd), PSM_CHANGED, 0, 0); + break; + } + + case IDC_CLIPBOARD: + { + char toxId[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; + GetDlgItemTextA(hwnd, IDC_TOXID, toxId, SIZEOF(toxId)); + if (OpenClipboard(GetDlgItem(hwnd, IDC_TOXID))) + { + EmptyClipboard(); + HGLOBAL hMem = GlobalAlloc(GMEM_FIXED, sizeof(toxId)); + memcpy(GlobalLock(hMem), toxId, sizeof(toxId)); + GlobalUnlock(hMem); + SetClipboardData(CF_TEXT, hMem); + CloseClipboard(); + } + } + break; + } + break; + + case WM_NOTIFY: + if (reinterpret_cast(lParam)->code == PSN_APPLY) + { + TCHAR nick[TOX_MAX_NAME_LENGTH]; + GetDlgItemText(hwnd, IDC_NAME, nick, TOX_MAX_NAME_LENGTH); + proto->setTString("Nick", nick); + + TCHAR password[MAX_PATH]; + GetDlgItemText(hwnd, IDC_PASSWORD, password, SIZEOF(password)); + proto->setTString("Password", password); + if (proto->password != NULL) + { + mir_free(proto->password); + proto->password = NULL; + } + proto->password = mir_utf8encodeW(password); + + TCHAR group[64]; + GetDlgItemText(hwnd, IDC_GROUP, group, SIZEOF(group)); + if (_tcslen(group) > 0) + { + proto->setTString(NULL, TOX_SETTINGS_GROUP, group); + Clist_CreateGroup(0, group); + } + else + { + proto->delSetting(NULL, TOX_SETTINGS_GROUP); + } + + proto->setByte("DisableUDP", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_UDP)); + proto->setByte("DisableIPv6", (BYTE)IsDlgButtonChecked(hwnd, IDC_DISABLE_IPV6)); + + if (proto->IsOnline()) + { + //proto->SaveToxProfile(); + } + + return TRUE; + } + break; + } + + return FALSE; } \ No newline at end of file diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp index 136bc66ca1..4ee3abc526 100644 --- a/protocols/Tox/src/tox_profile.cpp +++ b/protocols/Tox/src/tox_profile.cpp @@ -25,9 +25,9 @@ bool CToxProto::LoadToxProfile() return true; } - fseek(profile, 0L, SEEK_END); - DWORD size = ftell(profile); - rewind(profile); + fseek(profile, 0, SEEK_END); + size_t size = ftell(profile); + fseek(profile, 0, SEEK_SET); if (size == 0) { fclose(profile); @@ -36,7 +36,7 @@ bool CToxProto::LoadToxProfile() } uint8_t *data = (uint8_t*)mir_calloc(size); - DWORD read = fread((char*)data, sizeof(char), size, profile); + size_t read = fread((char*)data, sizeof(char), size, profile); if (size != read) { fclose(profile); @@ -46,32 +46,29 @@ bool CToxProto::LoadToxProfile() } fclose(profile); - /*if (tox_is_data_encrypted(data)) + if (tox_is_data_encrypted(data)) { - ptrA password(mir_utf8encodeW(ptrT(getTStringA("Password")))); if (password == NULL || strlen(password) == 0) { - INT_PTR result = DialogBoxParam( + if (!DialogBoxParam( g_hInstance, MAKEINTRESOURCE(IDD_PASSWORD), NULL, ToxProfilePasswordProc, - (LPARAM)this); - - switch (result) + (LPARAM)this)) { - default: return false; + return false; } } - if (tox_encrypted_load(tox, data, size, (uint8_t*)(char*)password, strlen(password)) == TOX_ERROR) + if (tox_encrypted_load(tox, data, size, (uint8_t*)password, strlen(password)) == TOX_ERROR) { debugLogA("CToxProto::LoadToxData: could not decrypt tox profile"); mir_free(data); return false; } } - else*/ + else { if (tox_load(tox, data, size) == TOX_ERROR) { @@ -87,30 +84,30 @@ bool CToxProto::LoadToxProfile() void CToxProto::SaveToxProfile() { - /*ptrA password(mir_utf8encodeW(ptrT(getTStringA("Password")))); - bool needToEncrypt = password && strlen(password); - DWORD size = needToEncrypt - ? tox_encrypted_size(tox) - : tox_size(tox); + size_t size = 0; + uint8_t *data = NULL; - uint8_t *data = (uint8_t*)mir_calloc(size); - if (needToEncrypt) { - if (tox_encrypted_save(tox, data, (uint8_t*)(char*)password, strlen(password)) == TOX_ERROR) + mir_cslock lock(toxLock); + + if (password && strlen(password)) { - debugLogA("CToxProto::LoadToxData: could not encrypt tox profile"); - mir_free(data); - return; + size = tox_encrypted_size(tox); + data = (uint8_t*)mir_calloc(size); + if (tox_encrypted_save(tox, data, (uint8_t*)password, strlen(password)) == TOX_ERROR) + { + debugLogA("CToxProto::LoadToxData: could not encrypt tox profile"); + mir_free(data); + return; + } + } + else + { + size = tox_size(tox); + data = (uint8_t*)mir_calloc(size); + tox_save(tox, data); } } - else - { - tox_save(tox, data); - }*/ - - DWORD size = tox_size(tox); - uint8_t *data = (uint8_t*)mir_calloc(size); - tox_save(tox, data); std::tstring profilePath = GetToxProfilePath(); FILE *profile = _tfopen(profilePath.c_str(), _T("wb")); @@ -120,7 +117,7 @@ void CToxProto::SaveToxProfile() return; } - DWORD written = fwrite(data, sizeof(char), size, profile); + size_t written = fwrite(data, sizeof(char), size, profile); if (size != written) { fclose(profile); @@ -228,7 +225,21 @@ INT_PTR CToxProto::ToxProfilePasswordProc(HWND hwnd, UINT uMsg, WPARAM wParam, L switch (LOWORD(wParam)) { case IDOK: - EndDialog(hwnd, 1); + { + TCHAR password[MAX_PATH]; + GetDlgItemText(hwnd, IDC_PASSWORD, password, SIZEOF(password)); + if (IsDlgButtonChecked(hwnd, IDC_SAVEPERMANENTLY)) + { + proto->setTString("Password", password); + } + if (proto->password != NULL) + { + mir_free(proto->password); + } + proto->password = mir_utf8encodeW(password); + + EndDialog(hwnd, 1); + } break; case IDCANCEL: diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 89be71ab18..204777d9ec 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -1,9 +1,10 @@ #include "common.h" CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : - PROTO(protoName, userName) + PROTO(protoName, userName), password(NULL) { InitNetlib(); + accountName = mir_tstrdup(userName); CreateProtoService(PS_CREATEACCMGRUI, &CToxProto::OnAccountManagerInit); @@ -112,7 +113,6 @@ int __cdecl CToxProto::Authorize(HANDLE hDbEvent) db_unset(hContact, "CList", "NotOnList"); delSetting(hContact, "Grant"); - SaveToxProfile(); return 0; } @@ -133,8 +133,6 @@ int __cdecl CToxProto::AuthRequest(MCONTACT hContact, const PROTOCHAR *szMessage int32_t number = tox_add_friend(tox, pubKey.data(), (uint8_t*)(char*)reason, (uint16_t)strlen(reason)); if (number > TOX_ERROR) { - SaveToxProfile(); - // change tox address in contact id by tox id std::string id = ToxAddressToId(address); setString(hContact, TOX_SETTINGS_ID, id.c_str()); @@ -237,9 +235,9 @@ int __cdecl CToxProto::SetStatus(int iNewStatus) { // set tox status m_iStatus = iNewStatus; - if (tox_set_user_status(tox, MirandaToToxStatus(iNewStatus)) == 0) + if (tox_set_user_status(tox, MirandaToToxStatus(iNewStatus)) == TOX_ERROR) { - SaveToxProfile(); + debugLogA("CToxProto::SetStatus: failed to change status from %i", m_iStatus, iNewStatus); } } } diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index 2565ceb70d..faf7baa84b 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -110,11 +110,9 @@ public: static CToxProto* InitAccount(const char* protoName, const wchar_t* userName); static int UninitAccount(CToxProto* ppro); - // tox - bool InitToxCore(); - private: Tox *tox; + char *password; mir_cs toxLock; TCHAR *accountName; HANDLE hNetlib, hPollingThread, hToxEvent; @@ -122,6 +120,7 @@ private: std::map transfers; // tox + bool InitToxCore(); void UninitToxCore(); // ??? @@ -152,9 +151,6 @@ private: bool IsOnline(); int __cdecl OnAccountLoaded(WPARAM, LPARAM); int __cdecl OnAccountRenamed(WPARAM, LPARAM); - - // events - int __cdecl OnSettingsChanged(WPARAM wParam, LPARAM lParam); // options static INT_PTR CALLBACK MainOptionsProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); diff --git a/protocols/Tox/src/version.h b/protocols/Tox/src/version.h index ec3602b81e..775b7089a0 100644 --- a/protocols/Tox/src/version.h +++ b/protocols/Tox/src/version.h @@ -5,7 +5,7 @@ #include -#define __PLUGIN_NAME "Tox Protocol" +#define __PLUGIN_NAME "Tox protocol" #define __FILENAME "Tox.dll" #define __DESCRIPTION "Tox protocol support for Miranda NG." #define __AUTHOR "ForNeVeR, Mataes, unsane" -- cgit v1.2.3