summaryrefslogtreecommitdiff
path: root/plugins/MirOTR/src/dialogs.cpp
blob: 16d79ed09e5a6298ffc7570cfe060d561c200ba9 (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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
#include "stdafx.h"

struct SmpData
{
	HWND dialog;
	TrustLevel oldlevel;
	ConnContext *context;
	bool responder;
	wchar_t *question;
};
typedef std::map<HANDLE, SmpData> SmpForContactMap;
SmpForContactMap smp_for_contact;


static INT_PTR CALLBACK DlgSMPUpdateProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_DESTROY: {
			ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			if (context)
				otr_abort_smp(context);
			break; }
	case WM_INITDIALOG:
		{
			if (!lParam) {
				DestroyWindow(hwndDlg);
				return FALSE;
			}
			TranslateDialogDefault(hwndDlg);

			SmpData *data = (SmpData*)lParam;
			ConnContext *context = data->context;
			MCONTACT hContact = (UINT_PTR)context->app_data;
			data->dialog = hwndDlg;
			//smp_for_contact.insert(SmpForContactMap::value_type(context->app_data, *data));
			if (smp_for_contact[context->app_data].dialog) SendMessage(smp_for_contact[context->app_data].dialog, WMU_REFRESHSMP, 0, 0);
			smp_for_contact[context->app_data].context = data->context;
			smp_for_contact[context->app_data].dialog = hwndDlg;
			smp_for_contact[context->app_data].oldlevel = data->oldlevel;
			smp_for_contact[context->app_data].responder = data->responder;
			mir_free(data);

			wchar_t title[512], *proto = mir_a2u(GetContactProto(hContact));
			const wchar_t *name = contact_get_nameT(hContact);
			mir_snwprintf(title, TranslateW(LANG_SMP_PROGRESS_TITLE), name, proto);
			SetWindowText(hwndDlg, title);
			mir_snwprintf(title, TranslateW(LANG_SMP_PROGRESS_DESC), name, proto);
			mir_free(proto);
			SetDlgItemText(hwndDlg, IDC_STC_SMP_HEADPRO, title);
			SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)context);

			// Move window to screen center
			// Get the owner window and dialog box rectangles. 
			HWND hwndOwner; RECT rcOwner, rcDlg, rc;
			if ((hwndOwner = GetParent(hwndDlg)) == nullptr) {
				hwndOwner = GetDesktopWindow();
			}

			GetWindowRect(hwndOwner, &rcOwner);
			GetWindowRect(hwndDlg, &rcDlg);
			CopyRect(&rc, &rcOwner);

			// Offset the owner and dialog box rectangles so that right and bottom 
			// values represent the width and height, and then offset the owner again 
			// to discard space taken up by the dialog box. 

			OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
			OffsetRect(&rc, -rc.left, -rc.top);
			OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

			// The new position is the sum of half the remaining space and the owner's 
			// original position. 

			SetWindowPos(hwndDlg,
				HWND_TOP,
				rcOwner.left + (rc.right / 2),
				rcOwner.top + (rc.bottom / 2),
				0, 0,          // Ignores size arguments. 
				SWP_NOSIZE);

			// end center dialog

			SendDlgItemMessage(hwndDlg, IDC_PGB_SMP, PBM_SETRANGE, 0, MAKELONG(0, 100));
			SendDlgItemMessage(hwndDlg, IDC_PGB_SMP, PBM_SETPOS, 10, 0);

			EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), true);
			EnableWindow(GetDlgItem(hwndDlg, IDOK), false);

			return TRUE;
		}

	case WMU_REFRESHSMP:
		{
			ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			SendDlgItemMessage(hwndDlg, IDC_PGB_SMP, PBM_SETPOS, wParam, 0);
			switch (wParam) {
			case 0:
				EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), false);
				EnableWindow(GetDlgItem(hwndDlg, IDOK), true);
				SetDlgItemText(hwndDlg, IDC_STC_SMP_PROGRESS, TranslateW(LANG_SMP_ERROR));
				smp_for_contact.erase(context->app_data);
				break;
			case 100:
				{
					EnableWindow(GetDlgItem(hwndDlg, IDCANCEL), false);
					EnableWindow(GetDlgItem(hwndDlg, IDOK), true);
					smp_for_contact.erase(context->app_data);
					if (context->smstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED) {
						if (context->active_fingerprint->trust && context->active_fingerprint->trust[0]) {
							SetDlgItemText(hwndDlg, IDC_STC_SMP_PROGRESS, TranslateW(LANG_SMP_SUCCESS));
						}
						else {
							SetDlgItemText(hwndDlg, IDC_STC_SMP_PROGRESS, TranslateW(LANG_SMP_SUCCESS_VERIFY));
						}
					}
					else {
						SetDlgItemText(hwndDlg, IDC_STC_SMP_PROGRESS, TranslateW(LANG_SMP_FAILED));
					}
				}
				break;
			default:
				SetDlgItemText(hwndDlg, IDC_STC_SMP_PROGRESS, L"");
			}
		}
		break;

	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case BN_CLICKED:
			{
				ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
				switch (LOWORD(wParam)) {
				case IDOK:
					smp_for_contact.erase(context->app_data);
					SetWindowLongPtr(hwndDlg, GWLP_USERDATA, NULL);
					// fall through
				case IDCANCEL:
					DestroyWindow(hwndDlg);
					break;
				}
			}break;
		}

	}

	return FALSE;
}

static void SMPInitUpdateDialog(ConnContext *context, bool responder)
{
	if (!context) return;
	SmpData *data = (SmpData*)mir_calloc(sizeof(SmpData));
	data->context = context;
	data->oldlevel = otr_context_get_trust(context);
	data->responder = responder;
	CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_SMP_PROGRESS), nullptr, DlgSMPUpdateProc, (LPARAM)data);
}

static INT_PTR CALLBACK DlgSMPResponseProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_DESTROY: {
			ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			if (context)
				otr_abort_smp(context);
			break; }
	case WM_INITDIALOG:
		{
			if (!lParam) {
				DestroyWindow(hwndDlg);
				return FALSE;
			}
			TranslateDialogDefault(hwndDlg);

			SmpData *data = (SmpData*)lParam;
			ConnContext *context = data->context;
			MCONTACT hContact = (UINT_PTR)context->app_data;
			data->dialog = hwndDlg;
			//smp_for_contact.insert(SmpForContactMap::value_type(context->app_data, *data));
			if (smp_for_contact[context->app_data].dialog) SendMessage(smp_for_contact[context->app_data].dialog, WMU_REFRESHSMP, 0, 0);
			smp_for_contact[context->app_data].context = data->context;
			smp_for_contact[context->app_data].dialog = hwndDlg;
			smp_for_contact[context->app_data].oldlevel = data->oldlevel;
			smp_for_contact[context->app_data].responder = data->responder;

			wchar_t buff[512], *proto = mir_a2u(GetContactProto(hContact));
			mir_snwprintf(buff, TranslateW(LANG_SMP_VERIFY_TITLE), contact_get_nameT(hContact), proto);
			mir_free(proto);
			SetWindowText(hwndDlg, buff);
			SetDlgItemText(hwndDlg, IDC_STC_SMP_HEAD, buff);
			SetWindowLongPtr(hwndDlg, GWLP_USERDATA, (LONG_PTR)context);

			if (data->question) {
				mir_snwprintf(buff, TranslateW(LANG_OTR_SMPQUESTION_RESPOND_DESC), contact_get_nameT(hContact));

				SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

				SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, data->question);
				SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, TRUE, 0);
				SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, TranslateW(LANG_SMP_QUESTION));

				SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, L"");
				SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, FALSE, 0);
				SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_SMP_ANSWER));


				ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_SHOWNA);
				ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_HIDE);
				ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_HIDE);

				mir_free(data->question);
			}
			else {
				mir_snwprintf(buff, TranslateW(LANG_OTR_SMPPASSWORD_RESPOND_DESC), contact_get_nameT(hContact));

				SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

				SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, L"");
				SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, TRUE, 0);
				SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, L"");

				SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, L"");
				SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, FALSE, 0);
				SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_SMP_PASSWORD));


				ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_SHOWNA);
				ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_HIDE);
				ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_HIDE);
			}
			mir_free(data);

			// Move window to screen center
			// Get the owner window and dialog box rectangles. 
			HWND hwndOwner = GetParent(hwndDlg); 
			if (hwndOwner == nullptr)
				hwndOwner = GetDesktopWindow();

			RECT rcOwner, rcDlg, rc;
			GetWindowRect(hwndOwner, &rcOwner);
			GetWindowRect(hwndDlg, &rcDlg);
			CopyRect(&rc, &rcOwner);

			// Offset the owner and dialog box rectangles so that right and bottom 
			// values represent the width and height, and then offset the owner again 
			// to discard space taken up by the dialog box. 

			OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
			OffsetRect(&rc, -rc.left, -rc.top);
			OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

			// The new position is the sum of half the remaining space and the owner's 
			// original position. 

			SetWindowPos(hwndDlg,
				HWND_TOP,
				rcOwner.left + (rc.right / 2),
				rcOwner.top + (rc.bottom / 2),
				0, 0,          // Ignores size arguments. 
				SWP_NOSIZE);

			// end center dialog
			return TRUE;
		}

	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case BN_CLICKED:
			ConnContext *context = (ConnContext *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
			switch (LOWORD(wParam)) {
			case IDOK:
				SMPInitUpdateDialog(context, true);
				{
					int len = SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, WM_GETTEXTLENGTH, 0, 0);
					wchar_t *answer = new wchar_t[len + 1];
					GetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, answer, len + 1);

					T2Utf ans(answer);
					otr_continue_smp(context, (const unsigned char *)ans, mir_strlen(ans));
					delete[] answer;

					SetWindowLongPtr(hwndDlg, GWLP_USERDATA, NULL);
					DestroyWindow(hwndDlg);
				}
				break;

			case IDCANCEL:
				smp_for_contact.erase(context->app_data);
				DestroyWindow(hwndDlg);
				break;
			}
		}
		break;
	}

	return FALSE;
}

/*
void SMPInitResponseDialog(ConnContext *context, const wchar_t *question) {
	if (!context) return;
	SmpData *data = (SmpData*)mir_calloc(sizeof(SmpData));
	data->context = context;
	data->oldlevel = TRUST_NOT_PRIVATE;
	data->responder = true;
	data->question = (question) ? mir_wstrdup(question) : NULL;
	CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_SMP_INPUT), 0, DlgSMPResponseProc, (LPARAM) data);
}
*/

static INT_PTR CALLBACK DlgProcSMPInitProc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
		{
			if (!lParam) {
				DestroyWindow(hwndDlg);
				return FALSE;
			}
			TranslateDialogDefault(hwndDlg);

			ConnContext *context = (ConnContext*)lParam;
			if (smp_for_contact.find(context->app_data) != smp_for_contact.end()) {
				DestroyWindow(hwndDlg);
				return FALSE;
			}

			MCONTACT hContact = (UINT_PTR)context->app_data;
			wchar_t title[512], *proto = mir_a2u(GetContactProto(hContact));
			mir_snwprintf(title, TranslateW(LANG_SMP_VERIFY_TITLE), contact_get_nameT(hContact), proto);
			mir_free(proto);
			SetWindowText(hwndDlg, title);
			SetDlgItemText(hwndDlg, IDC_STC_SMP_HEAD, title);
			SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);

			// Move window to screen center
			// Get the owner window and dialog box rectangles. 
			HWND hwndOwner = GetParent(hwndDlg); 
			if (hwndOwner == nullptr)
				hwndOwner = GetDesktopWindow();

			RECT rcOwner, rcDlg, rc;
			GetWindowRect(hwndOwner, &rcOwner);
			GetWindowRect(hwndDlg, &rcDlg);
			CopyRect(&rc, &rcOwner);

			// Offset the owner and dialog box rectangles so that right and bottom 
			// values represent the width and height, and then offset the owner again 
			// to discard space taken up by the dialog box. 

			OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
			OffsetRect(&rc, -rc.left, -rc.top);
			OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

			// The new position is the sum of half the remaining space and the owner's 
			// original position. 

			SetWindowPos(hwndDlg,
				HWND_TOP,
				rcOwner.left + (rc.right / 2),
				rcOwner.top + (rc.bottom / 2),
				0, 0,          // Ignores size arguments. 
				SWP_NOSIZE);

			// end center dialog


			HWND cmb = GetDlgItem(hwndDlg, IDC_CBO_SMP_CHOOSE);
			SendMessage(cmb, CB_ADDSTRING, 0, (WPARAM)TranslateW(LANG_SMPTYPE_QUESTION));
			SendMessage(cmb, CB_ADDSTRING, 0, (WPARAM)TranslateW(LANG_SMPTYPE_PASSWORD));
			SendMessage(cmb, CB_ADDSTRING, 0, (WPARAM)TranslateW(LANG_SMPTYPE_FINGERPRINT));
			SendMessage(cmb, CB_SELECTSTRING, -1, (WPARAM)TranslateW(LANG_SMPTYPE_QUESTION));
			EnableWindow(GetDlgItem(hwndDlg, IDC_CBO_SMP_CHOOSE), TRUE);


			Fingerprint *fp = context->active_fingerprint;
			if (!fp) {
				DestroyWindow(hwndDlg);
				return FALSE;
			}
			wchar_t buff[1024];
			if (!fp->trust || fp->trust[0] == '\0')
				mir_snwprintf(buff, TranslateW(LANG_OTR_SMPQUESTION_VERIFY_DESC), contact_get_nameT(hContact));
			else
				mir_snwprintf(buff, TranslateW(LANG_OTR_SMPQUESTION_VERIFIED_DESC), contact_get_nameT(hContact));

			SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

			SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, L"");
			SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, FALSE, 0);
			SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, TranslateW(LANG_SMP_QUESTION));

			SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, L"");
			SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, FALSE, 0);
			SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_SMP_ANSWER));


			ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_SHOWNA);
			ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_HIDE);
			ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_HIDE);
			SetFocus(GetDlgItem(hwndDlg, IDC_CBO_SMP_CHOOSE));

			return FALSE;
		}

	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case BN_CLICKED:
			{
				ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
				MCONTACT hContact = (UINT_PTR)context->app_data;
				wchar_t szMsg[1024];
				switch (LOWORD(wParam)) {
				case IDCANCEL:
					DestroyWindow(hwndDlg);
					break;
				case IDOK:
					GetDlgItemText(hwndDlg, IDC_CBO_SMP_CHOOSE, szMsg, _countof(szMsg));
					if (wcsncmp(szMsg, TranslateW(LANG_SMPTYPE_QUESTION), _countof(szMsg)) == 0) {
						if (smp_for_contact.find(context->app_data) != smp_for_contact.end()) {
							mir_snwprintf(szMsg, TranslateW(LANG_SMP_IN_PROGRESS), contact_get_nameT(hContact));
							ShowError(szMsg);
						}
						else {
							int len = SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, WM_GETTEXTLENGTH, 0, 0);
							wchar_t *question = new wchar_t[len + 1];
							GetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, question, len + 1);
							T2Utf quest(question);
							delete[] question;

							len = SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, WM_GETTEXTLENGTH, 0, 0);
							wchar_t *answer = new wchar_t[len + 1];
							GetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, answer, len + 1);
							T2Utf ans(answer);
							delete[] answer;

							SMPInitUpdateDialog(context, false);
							otr_start_smp(context, quest, (const unsigned char*)ans, mir_strlen(ans));
						}

					}
					else if (wcsncmp(szMsg, TranslateW(LANG_SMPTYPE_PASSWORD), _countof(szMsg)) == 0) {
						if (smp_for_contact.find(context->app_data) != smp_for_contact.end()) {
							mir_snwprintf(szMsg, TranslateW(LANG_SMP_IN_PROGRESS), contact_get_nameT(hContact));
							ShowError(szMsg);
						}
						else {
							int len = SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, WM_GETTEXTLENGTH, 0, 0);
							wchar_t *answer = new wchar_t[len + 1];
							GetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, answer, len + 1);
							T2Utf ans(answer);
							delete[] answer;

							SMPInitUpdateDialog(context, false);
							otr_start_smp(context, nullptr, (const unsigned char*)ans, mir_strlen(ans));
						}

					}
					else break;
					DestroyWindow(hwndDlg);
					break;
				case IDYES:
					VerifyFingerprint(context, true);
					DestroyWindow(hwndDlg);
					break;
				case IDNO:
					VerifyFingerprint(context, false);
					DestroyWindow(hwndDlg);
					break;
				}
			}
			break;
		
		case CBN_SELCHANGE:
			switch (LOWORD(wParam)) {
			case IDC_CBO_SMP_CHOOSE:
				{
					ConnContext *context = (ConnContext*)GetWindowLongPtr(hwndDlg, GWLP_USERDATA);
					MCONTACT hContact = (UINT_PTR)context->app_data;
					Fingerprint *fp = context->active_fingerprint;
					if (!fp) {
						DestroyWindow(hwndDlg);
						return TRUE;
					}
					BOOL trusted = false;
					if (fp->trust && fp->trust[0] != '\0') trusted = true;

					wchar_t buff[512];
					GetDlgItemText(hwndDlg, IDC_CBO_SMP_CHOOSE, buff, 255);
					if (wcsncmp(buff, TranslateW(LANG_SMPTYPE_QUESTION), 255) == 0) {
						if (trusted)
							mir_snwprintf(buff, TranslateW(LANG_OTR_SMPQUESTION_VERIFIED_DESC), contact_get_nameT(hContact));
						else
							mir_snwprintf(buff, TranslateW(LANG_OTR_SMPQUESTION_VERIFY_DESC), contact_get_nameT(hContact));

						SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, L"");
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, FALSE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, TranslateW(LANG_SMP_QUESTION));

						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, L"");
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, FALSE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_SMP_ANSWER));


						ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_SHOWNA);
						ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_HIDE);
						ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_HIDE);
					}
					else if (wcsncmp(buff, TranslateW(LANG_SMPTYPE_PASSWORD), 255) == 0) {
						if (trusted)
							mir_snwprintf(buff, TranslateW(LANG_OTR_SMPPASSWORD_VERIFIED_DESC), contact_get_nameT(hContact));
						else
							mir_snwprintf(buff, TranslateW(LANG_OTR_SMPPASSWORD_VERIFY_DESC), contact_get_nameT(hContact));

						SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, L"");
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, TRUE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, L"");

						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, L"");
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, FALSE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_SMP_PASSWORD));


						ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_SHOWNA);
						ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_HIDE);
						ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_HIDE);
					}
					else if (wcsncmp(buff, TranslateW(LANG_SMPTYPE_FINGERPRINT), 255) == 0) {
						if (trusted)
							mir_snwprintf(buff, TranslateW(LANG_OTR_FPVERIFIED_DESC), contact_get_nameT(hContact));
						else
							mir_snwprintf(buff, TranslateW(LANG_OTR_FPVERIFY_DESC), contact_get_nameT(hContact));

						SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

						unsigned char hash[20];
						lib_cs_lock();
						if (!otrl_privkey_fingerprint_raw(otr_user_state, hash, context->accountname, context->protocol)) {
							DestroyWindow(hwndDlg);
							return FALSE;
						}
						otrl_privkey_hash_to_humanT(buff, hash);
						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, buff);
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, TRUE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, TranslateW(LANG_YOUR_PRIVKEY));

						otrl_privkey_hash_to_humanT(buff, fp->fingerprint);
						SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, buff);
						SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, TRUE, 0);
						SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_CONTACT_FINGERPRINT));

						ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_HIDE);
						ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_SHOWNA);
						ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_SHOWNA);

					}
				}break;
			}
		}
		break;

	}

	return FALSE;
}
void SMPInitDialog(ConnContext *context)
{
	if (context)
		CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_SMP_INPUT), nullptr, DlgProcSMPInitProc, (LPARAM)context);
}

void SMPDialogUpdate(ConnContext *context, int percent)
{
	if (!context) return;
	SmpForContactMap::iterator it = smp_for_contact.find(context->app_data);
	if (it == smp_for_contact.end())	return;
	if (it->second.dialog) PostMessage(it->second.dialog, WMU_REFRESHSMP, percent, 0);
	TrustLevel level = otr_context_get_trust(context);
	if (!it->second.responder && it->second.oldlevel != level) {
		if (level == TRUST_PRIVATE)
			VerifyFingerprintMessage(context, true);
		else if (level == TRUST_UNVERIFIED)
			VerifyFingerprintMessage(context, false);
	}
	//if (percent == 100)
	/*
	switch (percent){
		case 0:
			VerifyFingerprint(context, false);
			ShowWarning(L"SMP failed");
			break;
		case 100:
			VerifyFingerprint(context, true);
			ShowWarning(L"SMP successful");
			break;
		default:
			ShowWarning(L"Received an SMP update");
	}
	*/
}
void SMPDialogReply(ConnContext *context, const char* question)
{
	SmpData *data = (SmpData*)mir_calloc(sizeof(SmpData));
	data->context = context;
	data->oldlevel = TRUST_NOT_PRIVATE;
	data->responder = true;
	data->question = (question) ? mir_utf8decodeW(question) : nullptr;
	CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_SMP_INPUT), nullptr, DlgSMPResponseProc, (LPARAM)data);
	/*
	ShowError(L"SMP requires user password (NOT IMPL YET)");
	otr_abort_smp(context);
	*/
	//otr_continue_smp(context, pass, mir_strlen(pass));
}

static INT_PTR CALLBACK DlgBoxProcVerifyContext(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) {
	case WM_INITDIALOG:
	{
		if (!lParam) {
			EndDialog(hwndDlg, IDCANCEL);
			return FALSE;
		}
		SetWindowText(hwndDlg, LANG_OTR_FPVERIFY_TITLE);
		SetDlgItemText(hwndDlg, IDC_STC_SMP_HEAD, LANG_OTR_FPVERIFY_TITLE);
		TranslateDialogDefault(hwndDlg);
		SetWindowLongPtr(hwndDlg, GWLP_USERDATA, lParam);

		// Move window to screen center
		// Get the owner window and dialog box rectangles. 
		HWND hwndOwner = GetParent(hwndDlg);
		if (hwndOwner == nullptr)
			hwndOwner = GetDesktopWindow();

		RECT rcOwner, rcDlg, rc;
		GetWindowRect(hwndOwner, &rcOwner);
		GetWindowRect(hwndDlg, &rcDlg);
		CopyRect(&rc, &rcOwner);

		// Offset the owner and dialog box rectangles so that right and bottom 
		// values represent the width and height, and then offset the owner again 
		// to discard space taken up by the dialog box. 

		OffsetRect(&rcDlg, -rcDlg.left, -rcDlg.top);
		OffsetRect(&rc, -rc.left, -rc.top);
		OffsetRect(&rc, -rcDlg.right, -rcDlg.bottom);

		// The new position is the sum of half the remaining space and the owner's 
		// original position. 

		SetWindowPos(hwndDlg,
			HWND_TOP,
			rcOwner.left + (rc.right / 2),
			rcOwner.top + (rc.bottom / 2),
			0, 0,          // Ignores size arguments. 
			SWP_NOSIZE);

		// end center dialog

		ConnContext *context = (ConnContext*)lParam;
		MCONTACT hContact = (UINT_PTR)context->app_data;
		Fingerprint *fp = context->active_fingerprint;
		if (!fp) {
			EndDialog(hwndDlg, IDCANCEL);
			return FALSE;
		}
		wchar_t buff[512];
		if (!fp->trust || fp->trust[0] == '\0')
			mir_snwprintf(buff, TranslateW(LANG_OTR_FPVERIFY_DESC), contact_get_nameT(hContact));
		else
			mir_snwprintf(buff, TranslateW(LANG_OTR_FPVERIFIED_DESC), contact_get_nameT(hContact));

		SetDlgItemText(hwndDlg, IDC_STC_SMP_INFO, buff);

		unsigned char hash[20];
		lib_cs_lock();
		if (!otrl_privkey_fingerprint_raw(otr_user_state, hash, context->accountname, context->protocol)) {
			EndDialog(hwndDlg, IDCANCEL);
			return FALSE;
		}
		otrl_privkey_hash_to_humanT(buff, hash);
		SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD1, buff);
		SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD1, EM_SETREADONLY, TRUE, 0);
		SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD1, TranslateW(LANG_YOUR_PRIVKEY));

		otrl_privkey_hash_to_humanT(buff, fp->fingerprint);
		SetDlgItemText(hwndDlg, IDC_EDT_SMP_FIELD2, buff);
		SendDlgItemMessage(hwndDlg, IDC_EDT_SMP_FIELD2, EM_SETREADONLY, TRUE, 0);
		SetDlgItemText(hwndDlg, IDC_STC_SMP_FIELD2, TranslateW(LANG_CONTACT_FINGERPRINT));

		EnableWindow(GetDlgItem(hwndDlg, IDC_CBO_SMP_CHOOSE), FALSE);

		ShowWindow(GetDlgItem(hwndDlg, IDOK), SW_HIDE);
		ShowWindow(GetDlgItem(hwndDlg, IDYES), SW_SHOWNA);
		ShowWindow(GetDlgItem(hwndDlg, IDNO), SW_SHOWNA);
		SetFocus(GetDlgItem(hwndDlg, IDCANCEL));
	}
	return FALSE;

	case WM_COMMAND:
		switch (HIWORD(wParam)) {
		case BN_CLICKED:
			switch (LOWORD(wParam)) {
			case IDYES:
			case IDNO:
			case IDCANCEL:
			case IDOK:
				EndDialog(hwndDlg, LOWORD(wParam));
				break;
			}
		}
		break;

	}

	return FALSE;
}

static unsigned int CALLBACK verify_context_thread(void *param)
{
	Thread_Push(nullptr);

	if (param) {
		ConnContext *context = (ConnContext *)param;
		MCONTACT hContact = (UINT_PTR)context->app_data;
		wchar_t msg[1024];
		switch (DialogBoxParam(hInst, MAKEINTRESOURCE(IDD_SMP_INPUT), nullptr, DlgBoxProcVerifyContext, (LPARAM)param)) {
		case IDOK:
		case IDYES:
			lib_cs_lock();
			otrl_context_set_trust(context->active_fingerprint, "verified");
			otrl_privkey_write_fingerprints(otr_user_state, _T2A(g_fingerprint_store_filename));
			mir_snwprintf(msg, TranslateW(LANG_FINGERPRINT_VERIFIED), contact_get_nameT(hContact));
			ShowMessage(hContact, msg);
			SetEncryptionStatus(hContact, otr_context_get_trust(context));
			break;

		case IDNO:
			lib_cs_lock();
			otrl_context_set_trust(context->active_fingerprint, nullptr);
			otrl_privkey_write_fingerprints(otr_user_state, _T2A(g_fingerprint_store_filename));
			mir_snwprintf(msg, TranslateW(LANG_FINGERPRINT_NOT_VERIFIED), contact_get_nameT(hContact));
			ShowMessage(hContact, msg);
			SetEncryptionStatus(hContact, otr_context_get_trust(context));
			break;
		}
	}

	Thread_Pop();
	return 0;
}

void VerifyContextDialog(ConnContext* context)
{
	if (!context) return;
	CloseHandle((HANDLE)_beginthreadex(nullptr, 0, verify_context_thread, context, 0, nullptr));
}