summaryrefslogtreecommitdiff
path: root/protocols/Tox/src/tox_profile.cpp
blob: a5ced0c8f947dc009c3a03d723bd024d2287a2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "stdafx.h"

HANDLE CToxProto::hProfileFolderPath;

wchar_t* CToxProto::GetToxProfilePath()
{
	return GetToxProfilePath(m_tszUserName);
}

wchar_t* CToxProto::GetToxProfilePath(const wchar_t *accountName)
{
	wchar_t *profilePath = (wchar_t*)mir_calloc(MAX_PATH * sizeof(wchar_t) + 1);
	wchar_t profileRootPath[MAX_PATH];
	FoldersGetCustomPathT(hProfileFolderPath, profileRootPath, _countof(profileRootPath), VARSW(L"%miranda_userdata%"));
	mir_snwprintf(profilePath, MAX_PATH, L"%s\\%s.tox", profileRootPath, accountName);
	return profilePath;
}

static INT_PTR CALLBACK EnterPassword(void *param)
{
	CToxProto *proto = (CToxProto*)param;

	pass_ptrW password(proto->getWStringA(TOX_SETTINGS_PASSWORD));
	if (mir_wstrlen(password) == 0) {
		CToxEnterPasswordDlg passwordDlg(proto);
		if (!passwordDlg.DoModal())
			return 0;
		password = proto->getWStringA(TOX_SETTINGS_PASSWORD);
	}
	return (INT_PTR)password.detach();
}

bool CToxProto::LoadToxProfile(Tox_Options *options)
{
	debugLogA(__FUNCTION__": loading tox profile");

	mir_cslock lock(m_profileLock);

	ptrW profilePath(GetToxProfilePath());
	if (!IsFileExists(profilePath))
		return false;

	FILE *profile = _wfopen(profilePath, L"rb");
	if (profile == nullptr) {
		ShowNotification(TranslateT("Unable to open Tox profile"), MB_ICONERROR);
		debugLogA(__FUNCTION__": failed to open tox profile");
		return false;
	}

	fseek(profile, 0, SEEK_END);
	long size = ftell(profile);
	rewind(profile);
	if (size < 0) {
		fclose(profile);
		return false;
	}

	if (size == 0) {
		fclose(profile);
		return true;
	}

	uint8_t *data = (uint8_t*)mir_calloc(size);
	if (fread((char*)data, sizeof(char), size, profile) != (size_t)size) {
		fclose(profile);
		ShowNotification(TranslateT("Unable to read Tox profile"), MB_ICONERROR);
		debugLogA(__FUNCTION__": failed to read tox profile");
		mir_free(data);
		return false;
	}
	fclose(profile);

	if (tox_is_data_encrypted(data)) {
		pass_ptrA password(mir_utf8encodeW(pass_ptrW((wchar_t*)CallFunctionSync(EnterPassword, this))));
		if (mir_strlen(password) == 0) {
			mir_free(data);
			return false;
		}

		size_t decryptedSize = size - TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
		uint8_t *decryptedData = (uint8_t*)mir_calloc(decryptedSize);
		TOX_ERR_DECRYPTION coreDecryptError;
		if (!tox_pass_decrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), decryptedData, &coreDecryptError)) {
			ShowNotification(TranslateT("Unable to decrypt Tox profile"), MB_ICONERROR);
			debugLogA(__FUNCTION__": failed to decrypt tox profile (%d)", coreDecryptError);
			delSetting(TOX_SETTINGS_PASSWORD);
			mir_free(data);
			return false;
		}
		mir_free(data);
		data = decryptedData;
		size = decryptedSize;
	}

	if (data) {
		options->savedata_data = data;
		options->savedata_length = size;
		options->savedata_type = TOX_SAVEDATA_TYPE_TOX_SAVE;
		return true;
	}

	return false;
}

void CToxProto::SaveToxProfile(Tox *tox)
{
	mir_cslock lock(m_profileLock);

	size_t size = tox_get_savedata_size(tox);
	uint8_t *data = (uint8_t*)mir_calloc(size);
	tox_get_savedata(tox, data);

	pass_ptrA password(mir_utf8encodeW(pass_ptrW(getWStringA(TOX_SETTINGS_PASSWORD))));
	if (password && mir_strlen(password))
	{
		TOX_ERR_ENCRYPTION coreEncryptError;
		size_t encryptedSize = size + TOX_PASS_ENCRYPTION_EXTRA_LENGTH;
		uint8_t *encryptedData = (uint8_t*)mir_calloc(encryptedSize);
		if (!tox_pass_encrypt(data, size, (uint8_t*)(char*)password, mir_strlen(password), encryptedData, &coreEncryptError))
		{
			debugLogA(__FUNCTION__": failed to encrypt tox profile");
			mir_free(data);
			mir_free(encryptedData);
			return;
		}
		mir_free(data);
		data = encryptedData;
		size = encryptedSize;
	}

	ptrW profilePath(GetToxProfilePath());
	FILE *profile = _wfopen(profilePath, L"wb");
	if (profile == nullptr) {
		debugLogA(__FUNCTION__": failed to open tox profile");
		mir_free(data);
		return;
	}

	size_t written = fwrite(data, sizeof(char), size, profile);
	if (size != written)
		debugLogA(__FUNCTION__": failed to write tox profile");

	fclose(profile);
	mir_free(data);
}

int CToxProto::OnDeleteToxProfile()
{
	ptrW profilePath(GetToxProfilePath());
	_wunlink(profilePath);
	return 0;
}

INT_PTR CToxProto::OnCopyToxID(WPARAM, LPARAM)
{
	ptrA address(getStringA(TOX_SETTINGS_ID));
	size_t length = mir_strlen(address) + 1;
	if (OpenClipboard(nullptr)) {
		EmptyClipboard();
		HGLOBAL hMemory = GlobalAlloc(GMEM_FIXED, length);
		memcpy(GlobalLock(hMemory), address, length);
		GlobalUnlock(hMemory);
		SetClipboardData(CF_TEXT, hMemory);
		CloseClipboard();
	}
	return 0;
}

INT_PTR CToxProto::OnCreatePassword(WPARAM, LPARAM)
{
	pass_ptrW password(getWStringA(TOX_SETTINGS_PASSWORD));
	CToxCreatePasswordDlg passwordDlg(this);
	passwordDlg.DoModal();
	return 0;
}

INT_PTR CToxProto::OnChangePassword(WPARAM, LPARAM)
{
	CToxChangePasswordDlg passwordDlg(this);
	passwordDlg.DoModal();
	return 0;
}

INT_PTR CToxProto::OnRemovePassword(WPARAM, LPARAM)
{
	const wchar_t *message = TranslateT("Removing the password will lead to decryption of the profile.\r\nAre you sure to remove password?");
	int result = MessageBox(nullptr, message, TranslateT("Remove password"), MB_YESNO | MB_ICONQUESTION);
	if (result == IDYES) {
		delSetting(TOX_SETTINGS_PASSWORD);
		SaveToxProfile(m_toxThread->Tox());
	}
	return 0;
}

/* ENTER PASSWORD */

CToxEnterPasswordDlg::CToxEnterPasswordDlg(CToxProto *proto)
	: CToxDlgBase(proto, IDD_PASSWORD_ENTER, false),
	m_password(this, IDC_PASSWORD),
	m_ok(this, IDOK)
{
	m_password.OnChange = Callback(this, &CToxEnterPasswordDlg::Password_OnChange);
	m_ok.OnClick = Callback(this, &CToxEnterPasswordDlg::OnOk);
}

void CToxEnterPasswordDlg::OnInitDialog()
{
	m_ok.Disable();
}

void CToxEnterPasswordDlg::Password_OnChange(CCtrlBase*)
{
	m_ok.Enable(GetWindowTextLength(m_password.GetHwnd()) != 0);
}

void CToxEnterPasswordDlg::OnOk(CCtrlButton*)
{
	m_proto->setWString(TOX_SETTINGS_PASSWORD, pass_ptrW(m_password.GetText()));
	EndDialog(m_hwnd, 1);
}

/* CREATE PASSWORD */

CToxCreatePasswordDlg::CToxCreatePasswordDlg(CToxProto *proto)
	: CToxDlgBase(proto, IDD_PASSWORD_CREATE, false),
	m_newPassword(this, IDC_PASSWORD_NEW),
	m_confirmPassword(this, IDC_PASSWORD_CONFIRM),
	m_passwordValidation(this, IDC_PASSWORD_VALIDATION),
	m_ok(this, IDOK)
{
	m_newPassword.OnChange = Callback(this, &CToxCreatePasswordDlg::Password_OnChange);
	m_confirmPassword.OnChange = Callback(this, &CToxCreatePasswordDlg::Password_OnChange);
	m_ok.OnClick = Callback(this, &CToxCreatePasswordDlg::OnOk);
}

void CToxCreatePasswordDlg::OnInitDialog()
{
	LOGFONT lf;
	HFONT hFont = (HFONT)m_passwordValidation.SendMsg(WM_GETFONT, 0, 0);
	GetObject(hFont, sizeof(lf), &lf);
	lf.lfWeight = FW_BOLD;
	m_passwordValidation.SendMsg(WM_SETFONT, (WPARAM)CreateFontIndirect(&lf), 0);

	m_ok.Disable();
}

void CToxCreatePasswordDlg::Password_OnChange(CCtrlBase*)
{
	pass_ptrW newPassword(m_newPassword.GetText());
	if (mir_wstrlen(newPassword) == 0) {
		m_ok.Disable();
		m_passwordValidation.SetText(TranslateT("New password is empty"));
		return;
	}

	pass_ptrW confirmPassword(m_confirmPassword.GetText());
	if (mir_wstrcmp(newPassword, confirmPassword) != 0) {
		m_ok.Disable();
		m_passwordValidation.SetText(TranslateT("New password is not equal to confirmation"));
		return;
	}

	m_passwordValidation.SetText(L"");
	m_ok.Enable();
}

void CToxCreatePasswordDlg::OnOk(CCtrlButton*)
{
	m_proto->setWString(TOX_SETTINGS_PASSWORD, pass_ptrW(m_newPassword.GetText()));
	m_proto->SaveToxProfile(m_proto->m_toxThread->Tox());
	EndDialog(m_hwnd, 1);
}

/* CHANGE PASSWORD */

CToxChangePasswordDlg::CToxChangePasswordDlg(CToxProto *proto)
	: CToxDlgBase(proto, IDD_PASSWORD_CHANGE, false),
	m_oldPassword(this, IDC_PASSWORD),
	m_newPassword(this, IDC_PASSWORD_NEW),
	m_confirmPassword(this, IDC_PASSWORD_CONFIRM),
	m_passwordValidation(this, IDC_PASSWORD_VALIDATION),
	m_ok(this, IDOK)
{
	m_oldPassword.OnChange = Callback(this, &CToxChangePasswordDlg::Password_OnChange);
	m_newPassword.OnChange = Callback(this, &CToxChangePasswordDlg::Password_OnChange);
	m_confirmPassword.OnChange = Callback(this, &CToxChangePasswordDlg::Password_OnChange);
	m_ok.OnClick = Callback(this, &CToxChangePasswordDlg::OnOk);
}

void CToxChangePasswordDlg::OnInitDialog()
{
	LOGFONT lf;
	HFONT hFont = (HFONT)m_passwordValidation.SendMsg(WM_GETFONT, 0, 0);
	GetObject(hFont, sizeof(lf), &lf);
	lf.lfWeight = FW_BOLD;
	m_passwordValidation.SendMsg(WM_SETFONT, (WPARAM)CreateFontIndirect(&lf), 0);

	m_ok.Disable();
}

void CToxChangePasswordDlg::Password_OnChange(CCtrlBase*)
{
	pass_ptrW dbPassword(m_proto->getWStringA(TOX_SETTINGS_PASSWORD));
	pass_ptrW oldPassword(m_oldPassword.GetText());
	if (mir_wstrlen(dbPassword) > 0 && mir_wstrcmp(dbPassword, oldPassword) != 0) {
		m_ok.Disable();
		m_passwordValidation.SetText(TranslateT("Old password is not valid"));
		return;
	}

	pass_ptrW newPassword(m_newPassword.GetText());
	if (mir_wstrlen(newPassword) == 0) {
		m_ok.Disable();
		m_passwordValidation.SetText(TranslateT("New password is empty"));
		return;
	}

	pass_ptrW confirmPassword(m_confirmPassword.GetText());
	if (mir_wstrcmp(newPassword, confirmPassword) != 0) {
		m_ok.Disable();
		m_passwordValidation.SetText(TranslateT("New password is not equal to confirmation"));
		return;
	}

	m_passwordValidation.SetText(L"");
	m_ok.Enable();
}

void CToxChangePasswordDlg::OnOk(CCtrlButton*)
{
	m_proto->setWString(TOX_SETTINGS_PASSWORD, pass_ptrW(m_newPassword.GetText()));
	m_proto->SaveToxProfile(m_proto->m_toxThread->Tox());
	EndDialog(m_hwnd, 1);
}