From 8efc51aefbfcc6a41d9acabe9383a337500b0049 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Mon, 6 Oct 2014 19:23:04 +0000 Subject: Tox: - password dialog for encrypted profile git-svn-id: http://svn.miranda-ng.org/main/trunk@10713 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- protocols/Tox/res/resource.rc | 20 ++++++ protocols/Tox/src/resource.h | 8 ++- protocols/Tox/src/tox_account.cpp | 44 +++++++++---- protocols/Tox/src/tox_accounts.cpp | 20 ++++-- protocols/Tox/src/tox_profile.cpp | 131 +++++++++++++++++++++++++------------ protocols/Tox/src/tox_proto.cpp | 1 - protocols/Tox/src/tox_proto.h | 9 ++- 7 files changed, 164 insertions(+), 69 deletions(-) (limited to 'protocols') diff --git a/protocols/Tox/res/resource.rc b/protocols/Tox/res/resource.rc index a01dba23c3..b489530e70 100644 --- a/protocols/Tox/res/resource.rc +++ b/protocols/Tox/res/resource.rc @@ -147,6 +147,19 @@ BEGIN EDITTEXT IDC_DNS_ID,2,13,217,14,ES_AUTOHSCROLL END +IDD_PASSWORD DIALOGEX 0, 0, 225, 86 +STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | DS_FIXEDSYS | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU +EXSTYLE WS_EX_TOOLWINDOW | WS_EX_APPWINDOW +CAPTION "Enter password" +FONT 8, "MS Shell Dlg", 0, 0, 0x1 +BEGIN + EDITTEXT IDC_PASSWORD,7,28,213,12,ES_PASSWORD | ES_AUTOHSCROLL + CONTROL "Save password",IDC_SAVEPERMANENTLY,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,7,45,213,12 + DEFPUSHBUTTON "OK",IDOK,117,67,50,14 + PUSHBUTTON "Cancel",IDCANCEL,170,67,50,14 + LTEXT "Tox profile is encrypted. To continue, enter the password below and click OK.",IDC_STATIC,7,5,213,19 +END + ///////////////////////////////////////////////////////////////////////////// // @@ -200,6 +213,13 @@ BEGIN VERTGUIDE, 2 VERTGUIDE, 219 END + + IDD_PASSWORD, DIALOG + BEGIN + RIGHTMARGIN, 220 + VERTGUIDE, 7 + BOTTOMMARGIN, 81 + END END #endif // APSTUDIO_INVOKED diff --git a/protocols/Tox/src/resource.h b/protocols/Tox/src/resource.h index 16a0f30593..123b897341 100644 --- a/protocols/Tox/src/resource.h +++ b/protocols/Tox/src/resource.h @@ -1,10 +1,11 @@ //{{NO_DEPENDENCIES}} // Microsoft Visual C++ generated include file. -// Used by e:\Projects\C++\MirandaNG\protocols\Tox\res\resource.rc +// Used by E:\Projects\C++\MirandaNG\protocols\Tox\res\resource.rc // #define IDD_INFO_JABBER 103 #define IDD_ACCOUNT_INFO 103 #define IDD_USER_INFO 103 +#define IDD_PASSWORD 111 #define IDI_TOX 1000 #define IDD_PROFILE_MANAGER 1001 #define IDD_PROFILE_IMPORT 1001 @@ -21,9 +22,10 @@ #define IDC_DISABLE_UDP 1013 #define IDC_DISABLE_IPV6 1014 #define IDD_SEARCH 1015 -#define IDC_EDIT1 1016 #define IDC_DNS_ID 1016 -#define IDC_TV_INFO 1276 +#define IDC_SAVEFORSESSION 1048 +#define IDC_SAVEPERMANENT 1324 +#define IDC_SAVEPERMANENTLY 1324 // Next default values for new objects // diff --git a/protocols/Tox/src/tox_account.cpp b/protocols/Tox/src/tox_account.cpp index 97c659909b..d6a34a0006 100644 --- a/protocols/Tox/src/tox_account.cpp +++ b/protocols/Tox/src/tox_account.cpp @@ -30,9 +30,11 @@ int CToxProto::OnAccountRenamed(WPARAM, LPARAM lParam) return 0; } -void CToxProto::InitToxCore() +bool CToxProto::InitToxCore() { std::tstring profilePath = GetToxProfilePath(); + bool isProfileExists = IsFileExists(profilePath); + hProfile = CreateFile( profilePath.c_str(), GENERIC_READ | GENERIC_WRITE, @@ -42,9 +44,15 @@ void CToxProto::InitToxCore() FILE_ATTRIBUTE_NORMAL, NULL); + if (hProfile == INVALID_HANDLE_VALUE) + { + debugLogA("CToxProto::InitToxCore: cannot open tox profile"); + return false; + } + Tox_Options options = { 0 }; - options.udp_disabled = getByte("DisableUDP", 0); - options.ipv6enabled = !getByte("DisableIPv6", 0); + options.udp_disabled = getBool("DisableUDP", 0); + options.ipv6enabled = !getBool("DisableIPv6", 0); if (hNetlib != NULL) { @@ -81,24 +89,32 @@ void CToxProto::InitToxCore() tox_callback_avatar_info(tox, OnGotFriendAvatarInfo, this); tox_callback_avatar_data(tox, OnGotFriendAvatarData, this); - LoadToxProfile(); - - int size = tox_get_self_name_size(tox); - std::vector username(size); - tox_get_self_name(tox, &username[0]); - std::string nick(username.begin(), username.end()); - setWString("Nick", ptrW(Utf8DecodeW(nick.c_str()))); - std::vector pubKey(TOX_FRIEND_ADDRESS_SIZE); tox_get_address(tox, &pubKey[0]); std::string address = DataToHexString(pubKey); setString(NULL, TOX_SETTINGS_ID, address.c_str()); - std::tstring avatarPath = GetAvatarFilePath(); - if (IsFileExists(avatarPath)) + if (isProfileExists) { - SetToxAvatar(avatarPath); + if (!LoadToxProfile()) + { + return false; + } + + int size = tox_get_self_name_size(tox); + std::vector username(size); + tox_get_self_name(tox, &username[0]); + std::string nick(username.begin(), username.end()); + setWString("Nick", ptrW(Utf8DecodeW(nick.c_str()))); + + std::tstring avatarPath = GetAvatarFilePath(); + if (IsFileExists(avatarPath)) + { + SetToxAvatar(avatarPath); + } } + + return true; } void CToxProto::UninitToxCore() diff --git a/protocols/Tox/src/tox_accounts.cpp b/protocols/Tox/src/tox_accounts.cpp index 875996b9a8..09eb5932c2 100644 --- a/protocols/Tox/src/tox_accounts.cpp +++ b/protocols/Tox/src/tox_accounts.cpp @@ -20,16 +20,24 @@ CToxProto* CToxProto::InitAccount(const char* protoName, const wchar_t* userName (LPARAM)userName); } - CToxProto *ppro = new CToxProto(protoName, userName); - accounts.insert(ppro); + CToxProto *proto = new CToxProto(protoName, userName); + if (proto->InitToxCore()) + { + accounts.insert(proto); + } + else + { + delete proto; + proto = NULL; + } - return ppro; + return proto; } -int CToxProto::UninitAccount(CToxProto* ppro) +int CToxProto::UninitAccount(CToxProto* proto) { - accounts.remove(ppro); - delete ppro; + accounts.remove(proto); + delete proto; return 0; } \ No newline at end of file diff --git a/protocols/Tox/src/tox_profile.cpp b/protocols/Tox/src/tox_profile.cpp index 6fe48686b6..417042a9b6 100644 --- a/protocols/Tox/src/tox_profile.cpp +++ b/protocols/Tox/src/tox_profile.cpp @@ -15,13 +15,13 @@ std::tstring CToxProto::GetToxProfilePath(const TCHAR *accountName) return profilePath; } -void CToxProto::LoadToxProfile() +bool CToxProto::LoadToxProfile() { DWORD size = GetFileSize(hProfile, NULL); if (size == 0) { debugLogA("CToxProto::LoadToxData: tox profile is empty"); - return; + return false; } DWORD read = 0; @@ -30,34 +30,53 @@ void CToxProto::LoadToxProfile() { debugLogA("CToxProto::LoadToxData: could not read tox profile"); mir_free(data); - return; + return false; } if (tox_is_data_encrypted(data)) { - ptrT password(getTStringA("Password")); - char *password_utf8 = mir_utf8encodeW(password); - if (tox_encrypted_load(tox, data, size, (uint8_t*)password_utf8, strlen(password_utf8)) == TOX_ERROR) + ptrA password(mir_utf8encodeW(ptrT(getTStringA("Password")))); + if (password == NULL || strlen(password) == 0) + { + INT_PTR result = DialogBoxParam( + g_hInstance, + MAKEINTRESOURCE(IDD_PASSWORD), + NULL, + ToxProfilePasswordProc, + (LPARAM)this); + + switch (result) + { + default: return false; + } + } + + if (tox_encrypted_load(tox, data, size, (uint8_t*)(char*)password, strlen(password)) == TOX_ERROR) { debugLogA("CToxProto::LoadToxData: could not decrypt tox profile"); + mir_free(data); + return false; } - mir_free(password_utf8); } else { if (tox_load(tox, data, size) == TOX_ERROR) { debugLogA("CToxProto::LoadToxData: could not load tox profile"); + mir_free(data); + return false; } } mir_free(data); + + return true; } void CToxProto::SaveToxProfile() { - ptrT password(getTStringA("Password")); - bool needToEncrypt = password && _tcslen(password); + ptrA password(mir_utf8encodeW(ptrT(getTStringA("Password")))); + bool needToEncrypt = password && strlen(password); DWORD size = needToEncrypt ? tox_encrypted_size(tox) : tox_size(tox); @@ -65,22 +84,19 @@ void CToxProto::SaveToxProfile() uint8_t *data = (uint8_t*)mir_calloc(size); if (needToEncrypt) { - char *passwordInUtf8 = mir_utf8encodeW(password); - if (tox_encrypted_save(tox, data, (uint8_t*)passwordInUtf8, strlen(passwordInUtf8)) == TOX_ERROR) + if (tox_encrypted_save(tox, data, (uint8_t*)(char*)password, strlen(password)) == TOX_ERROR) { debugLogA("CToxProto::LoadToxData: could not encrypt tox profile"); - mir_free(passwordInUtf8); mir_free(data); return; } - mir_free(passwordInUtf8); } else { tox_save(tox, data); } - SetFilePointer(hProfile, 0, 0, FILE_BEGIN); + SetFilePointer(hProfile, 0, NULL, FILE_BEGIN); SetEndOfFile(hProfile); DWORD written = 0; if (!WriteFile(hProfile, data, size, &written, NULL) || size != written) @@ -125,43 +141,74 @@ INT_PTR CToxProto::ToxProfileImportProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPA break; case IDC_BROWSE_PROFILE: + { + TCHAR filter[MAX_PATH] = { 0 }; + mir_sntprintf(filter, MAX_PATH, _T("%s\0*.*"), TranslateT("All files (*.*)")); + + OPENFILENAME ofn = { sizeof(ofn) }; + ofn.hwndOwner = hwnd; + ofn.lpstrFilter = filter; + ofn.nFilterIndex = 1; + ofn.lpstrFile = profilePath; + ofn.lpstrTitle = TranslateT("Select tox profile"); + ofn.nMaxFile = MAX_PATH; + ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_EXPLORER; + + if (GetOpenFileName(&ofn) && profilePath) { - TCHAR filter[MAX_PATH] = { 0 }; - mir_sntprintf(filter, MAX_PATH, _T("%s\0*.*"), TranslateT("All files (*.*)")); - - OPENFILENAME ofn = { sizeof(ofn) }; - ofn.hwndOwner = hwnd; - ofn.lpstrFilter = filter; - ofn.nFilterIndex = 1; - ofn.lpstrFile = profilePath; - ofn.lpstrTitle = TranslateT("Select tox profile"); - ofn.nMaxFile = MAX_PATH; - ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_EXPLORER; - - if (GetOpenFileName(&ofn) && profilePath) - { - EnableWindow(GetDlgItem(hwnd, IDOK), TRUE); - SetDlgItemText(hwnd, IDC_PROFILE_PATH, profilePath); - } + EnableWindow(GetDlgItem(hwnd, IDOK), TRUE); + SetDlgItemText(hwnd, IDC_PROFILE_PATH, profilePath); } + } break; case IDOK: + { + std::tstring defaultProfilePath = GetToxProfilePath(accountName); + if (profilePath && _tcslen(profilePath)) { - std::tstring defaultProfilePath = GetToxProfilePath(accountName); - if (profilePath && _tcslen(profilePath)) - { - if (_tcsicmp(profilePath, defaultProfilePath.c_str()) != 0) - { - CopyFile(profilePath, defaultProfilePath.c_str(), FALSE); - } - } - else + if (_tcsicmp(profilePath, defaultProfilePath.c_str()) != 0) { - fclose(_wfopen(defaultProfilePath.c_str(), _T("w"))); + CopyFile(profilePath, defaultProfilePath.c_str(), FALSE); } - EndDialog(hwnd, 1); } + else + { + fclose(_wfopen(defaultProfilePath.c_str(), _T("w"))); + } + EndDialog(hwnd, 1); + } + break; + + case IDCANCEL: + EndDialog(hwnd, 0); + break; + } + break; + } + + return FALSE; +} + +INT_PTR CToxProto::ToxProfilePasswordProc(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); + } + return TRUE; + + case WM_COMMAND: + switch (LOWORD(wParam)) + { + case IDOK: + EndDialog(hwnd, 1); break; case IDCANCEL: diff --git a/protocols/Tox/src/tox_proto.cpp b/protocols/Tox/src/tox_proto.cpp index 257f8a371c..15bb2e6bc6 100644 --- a/protocols/Tox/src/tox_proto.cpp +++ b/protocols/Tox/src/tox_proto.cpp @@ -4,7 +4,6 @@ CToxProto::CToxProto(const char* protoName, const TCHAR* userName) : PROTO(protoName, userName) { InitNetlib(); - InitToxCore(); accountName = mir_tstrdup(userName); CreateProtoService(PS_CREATEACCMGRUI, &CToxProto::OnAccountManagerInit); diff --git a/protocols/Tox/src/tox_proto.h b/protocols/Tox/src/tox_proto.h index c69ec385e6..c53a5d14c0 100644 --- a/protocols/Tox/src/tox_proto.h +++ b/protocols/Tox/src/tox_proto.h @@ -105,6 +105,9 @@ public: static CToxProto* InitAccount(const char* protoName, const wchar_t* userName); static int UninitAccount(CToxProto* ppro); + // tox + bool InitToxCore(); + private: Tox *tox; mir_cs toxLock; @@ -114,7 +117,6 @@ private: std::map transfers; // tox - void InitToxCore(); void UninitToxCore(); // ??? @@ -128,10 +130,11 @@ private: std::tstring GetToxProfilePath(); static std::tstring CToxProto::GetToxProfilePath(const TCHAR *accountName); - void LoadToxProfile(); + bool LoadToxProfile(); void SaveToxProfile(); - + static INT_PTR CALLBACK ToxProfileImportProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); + static INT_PTR CALLBACK ToxProfilePasswordProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); // accounts static LIST accounts; -- cgit v1.2.3