summaryrefslogtreecommitdiff
path: root/messages.cpp
blob: 27c31fbb1cca0041cc8e417906d9559444d5f80a (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
// Copyright © 2010 sss
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include "commonheaders.h"


wstring new_key;
HANDLE new_key_hcnt = NULL;
HANDLE new_key_hcnt_mutex = NULL;
bool _terminate = false;

BOOL isProtoMetaContacts(HANDLE hContact);

int RecvMsgSvc(WPARAM w, LPARAM l)
{
	CCSDATA *ccs = (CCSDATA*)l;
	if (!ccs)
		return CallService(MS_PROTO_CHAINRECV, w, l);
	PROTORECVEVENT *pre = (PROTORECVEVENT*)(ccs->lParam);
	if (!pre)
		return CallService(MS_PROTO_CHAINRECV, w, l);
	char *msg = pre->szMessage;
	if (!msg)
		return CallService(MS_PROTO_CHAINRECV, w, l);
	HANDLE hContact = ccs->hContact;
	if(isProtoMetaContacts(hContact))
		hContact = metaGetMostOnline(hContact);


	{ //check for gpg related data
		wchar_t *tmp = mir_utf8decodeW(msg);
		wstring str = tmp;
		mir_free(tmp);
		wstring::size_type s1 = wstring::npos, s2 = wstring::npos;

		if((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos))
		{
			s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----"));
			s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----"));
		}
		else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos))
		{
			s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----"));
			s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----"));
		}
		if((s2 != wstring::npos) && (s1 != wstring::npos))
		{  //this is public key
			debuglog<<"info: "<<"received key from: "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<"\n";
			void ShowNewKeyDialog();
			s1 = 0;
			while((s1 = str.find(_T("\r"), s1)) != wstring::npos)
			{
				str.erase(s1, 1);
			}
			if((str.find(_T("-----END PGP PUBLIC KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----")) != wstring::npos))
			{
				s2 = str.find(_T("-----END PGP PUBLIC KEY BLOCK-----"));
				s1 = str.find(_T("-----BEGIN PGP PUBLIC KEY BLOCK-----"));
				s2 += _tcslen(_T("-----END PGP PUBLIC KEY BLOCK-----"));
			}
			else if((str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----")) != wstring::npos) && (str.find(_T("-----END PGP PRIVATE KEY BLOCK-----")) != wstring::npos))
			{
				s2 = str.find(_T("-----END PGP PRIVATE KEY BLOCK-----"));
				s1 = str.find(_T("-----BEGIN PGP PRIVATE KEY BLOCK-----"));
				s2 += _tcslen(_T("-----END PGP PRIVATE KEY BLOCK-----"));
			}
			new_key.append(str.substr(s1,s2-s1));
			WaitForSingleObject(new_key_hcnt_mutex, INFINITE);
			new_key_hcnt_mutex = CreateMutex(NULL, FALSE, NULL);
			new_key_hcnt = hContact;
			ShowNewKeyDialog();
			return CallService(MS_PROTO_CHAINRECV, w, l);
		}
		s1 = str.find(_T("-----BEGIN PGP MESSAGE-----"));
		s2 = str.find(_T("-----END PGP MESSAGE-----"));
		if((s2 != wstring::npos) && (s1 != wstring::npos))
		{ //this is generic encrypted data block
			void setSrmmIcon(HANDLE);
			void setClistIcon(HANDLE);
			bool isContactHaveKey(HANDLE hContact);
			if(!DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0))
			{
				debuglog<<"info: "<<"received message from: "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" whith tyrned off encryption\n";
				if(MessageBox(0, _T("We received encrypted message from contact with encryption turned off.\nDo you want turn on encryption for this contact ?"), _T("Warning"), MB_YESNO) == IDYES)
				{
					if(!isContactHaveKey(hContact))
					{
						void ShowLoadPublicKeyDialog();
						extern map<int, HANDLE> user_data;
						extern int item_num;
						item_num = 0;		 //black magic here
						user_data[1] = hContact;
						ShowLoadPublicKeyDialog();
					}
					else
					{
						DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1);
						setSrmmIcon(hContact);
						setClistIcon(hContact);
					}
					if(isContactHaveKey(hContact))
					{
						DBWriteContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 1);
						setSrmmIcon(hContact);
						setClistIcon(hContact);
					}
				}
				else if(MessageBox(0, _T("Do you want try to decrypt encrypted message ?"), _T("Warning"), MB_YESNO) == IDNO)
					return CallService(MS_PROTO_CHAINRECV, w, l);
			}
			{
				wstring::size_type p = 0;
				while((p = str.find(_T("\r"), p)) != wstring::npos)
					str.erase(p, 1);
			}
			s2 += _tcslen(_T("-----END PGP MESSAGE-----"));
			char *tmp = mir_t2a(str.substr(s1,s2-s1).c_str());
			TCHAR *tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
			wstring path = tmp2;
			path.append(_T("\\encrypted_data.asc"));
			DeleteFile(path.c_str());
			wfstream f(path.c_str(), std::ios::out);
			f<<tmp;
			mir_free(tmp);
			f.close();
			{
				extern TCHAR *password;
				string out;
				DWORD code;
				wstring cmd;
				cmd += _T("--batch ");
				{
					char *inkeyid = UniGetContactSettingUtf(hContact, szGPGModuleName, "InKeyID", "");
					TCHAR *pass = NULL;
					if(strlen(inkeyid) > 0)
					{
						string dbsetting = "szKey_";
						dbsetting += inkeyid;
						dbsetting += "_Password";
						pass = UniGetContactSettingUtf(NULL, szGPGModuleName, dbsetting.c_str(), _T(""));
						if(_tcslen(pass) > 0)
							debuglog<<"info: found password in database for key id: "<<inkeyid<<", trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" with password\n";
					}
					else
					{
						pass = UniGetContactSettingUtf(NULL, szGPGModuleName, "szKeyPassword", _T(""));
						if(_tcslen(pass) > 0)
							debuglog<<"info: found password for all keys in database, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" with password\n";
					}
					if(_tcslen(pass) > 0)
					{
						cmd += _T("--passphrase \"");
						cmd += pass;
						cmd += _T("\" ");
					}
					else if(password)
					{
						debuglog<<"info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" with password\n";
						cmd += _T("--passphrase \"");
						cmd += password;
						cmd += _T("\" ");
					}
					else
						debuglog<<"info: passwords not found in database or memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" with out password\n";
					mir_free(pass);
					mir_free(inkeyid);
				}
				{
					wstring path = tmp2;
					path += _T("\\decrypted_data");
					DeleteFile(path.c_str());
				}
				cmd += _T("--output \"");
				cmd += tmp2;
				cmd += _T("\\decrypted_data\"");
				cmd += _T(" -d -a \"");
				cmd += path;
				cmd += _T("\"");
				gpg_execution_params params;
				pxResult result;
				params.cmd = &cmd;
				params.useless = "";
				params.out = &out;
				params.code = &code;
				params.result = &result;
				HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)&params);
				if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT)
				{
					TerminateThread(gpg_thread, 0);
					MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK);
				}
				if(result == pxNotFound)
				{
					MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);
					DeleteFile(path.c_str());
					return CallService(MS_PROTO_CHAINRECV, w, l);
				}
				_terminate = false;
				while(out.find("public key decryption failed: bad passphrase") != string::npos)
				{
					debuglog<<"info: failed to decrypt messaage from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<" password needed, trying to get one\n";
					if(_terminate)
						break;
					{ //save inkey id
						string::size_type s = out.find(" encrypted with ");
						s = out.find(" ID ", s);
						s += strlen(" ID ");
						string::size_type s2 = out.find(",",s);
						DBWriteContactSettingString(hContact, szGPGModuleName, "InKeyID", out.substr(s, s2-s).c_str());
					}
					void ShowLoadKeyPasswordWindow();
					WaitForSingleObject(new_key_hcnt_mutex, INFINITE);
					new_key_hcnt_mutex = CreateMutex(NULL, FALSE, NULL);
					new_key_hcnt = hContact;
					ShowLoadKeyPasswordWindow();
					wstring cmd2 = cmd;
					if(password)
					{
						debuglog<<"info: found password in memory, trying to decrypt message from "<<(TCHAR*)CallService(MS_CLIST_GETCONTACTDISPLAYNAME, (WPARAM)hContact, GCDNF_TCHAR)<<"\n";
						wstring tmp = _T("--passphrase \"");
						tmp += password;
						tmp += _T("\" ");
						cmd2.insert(0, tmp);
					}
					out.clear();
					gpg_execution_params params;
					pxResult result;
					params.cmd = &cmd2;
					params.useless = "";
					params.out = &out;
					params.code = &code;
					params.result = &result;
					HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)&params);
					if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT)
					{
						TerminateThread(gpg_thread, 0);
						MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK);
					}
					if(result == pxNotFound)
					{
						MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);				  
						DeleteFile(path.c_str());
						return CallService(MS_PROTO_CHAINRECV, w, l);
					}
				}
				out.clear();
				gpg_thread = mir_forkthread(pxEexcute_thread, (void*)&params);
				if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT)
				{
					TerminateThread(gpg_thread, 0);
					MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK);
				}
				if(result == pxNotFound)
				{
					MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);				  
					DeleteFile(path.c_str());
					return CallService(MS_PROTO_CHAINRECV, w, l);
				}
				{
					wstring tmp = tmp2;
					tmp += _T("\\encrypted_data.asc");
					DeleteFile(tmp.c_str());
				}
				{
					wstring tmp = tmp2;
					tmp += _T("\\decrypted_data");
					if(_waccess(tmp.c_str(), 0) == -1)
					{
						if(errno == ENOENT)
						{
							string str = pre->szMessage;
							mir_free((void**)pre->szMessage);
							str.insert(0, "Failed to decrypt GPG encrypted message:\n");
							char *tmp = new char [str.length()+1];
							strcpy(tmp, str.c_str());
							pre->szMessage = tmp;
							return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs);
						}
					}
				}

				
				str.clear();
				{
					wstring path = tmp2;
					mir_free(tmp2);
					path += _T("\\decrypted_data");
					fstream f(path.c_str(), std::ios::in | std::ios::binary);
					while(f.is_open())
					{
						char tmp[256];
						f.getline(tmp, 255);
						if(strlen(tmp) == 0)
							break;
						TCHAR *tmp2;
						tmp2 = mir_utf8decodeW(tmp);
						str.append(tmp2).append(_T("\n"));
						mir_free(tmp2);
					}
					f.close();
					DeleteFile(path.c_str());
					str.erase(str.find_last_of(_T("\n")), 1);
					if(str.empty())
					{
						string str = pre->szMessage;
						mir_free((void**)pre->szMessage);
						str.insert(0, "Failed to decrypt GPG encrypted message:\n");
						pre->szMessage = mir_strdup(str.c_str());
						return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs);
					}
					else
					{
						mir_free((void**)pre->szMessage);
						if(bAppendTags)
						{
							str.insert(0, inopentag);
							str.append(inclosetag);
						}
						pre->szMessage = mir_utf8encodeW(str.c_str());
						return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs);
					}
				}
			}
		}
	}
	if(DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0))
	{
		wchar_t *tmp = mir_utf8decodeW(msg);
		wstring str = tmp;
		mir_free(tmp);
		mir_free((void**)pre->szMessage);
		str.insert(0, _T("Received unencrypted message:\n"));
		pre->szMessage = mir_utf8encodeW(str.c_str());
		return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs);
	}
	return CallService(MS_PROTO_CHAINRECV, w, l);
}

int SendMsgSvc(WPARAM w, LPARAM l)
{
	CCSDATA *ccs = (CCSDATA*)l;
	if (!ccs)
		return CallService(MS_PROTO_CHAINSEND, w, l);

	char *msg = (char*)(ccs->lParam);
	if (!msg)
		return CallService(MS_PROTO_CHAINSEND, w, l);
	HANDLE hContact = ccs->hContact;
	if(isProtoMetaContacts(hContact))
		hContact = metaGetMostOnline(hContact);
	if(!DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0))
		return CallService(MS_PROTO_CHAINSEND, w, l);


	{ //encrypt data here
		wchar_t *tmp = mir_utf8decodeW(msg);
		wstring str = tmp;
		mir_free(tmp);
		{ //not xmpp, just replace whole message
			string out;
			DWORD code;
			wstring cmd;
			wstring path;
			extern bool bJabberAPI, bIsMiranda09;
			char *tmp = UniGetContactSettingUtf(hContact, szGPGModuleName, "KeyID", "");
			if(strlen(tmp) < 2)
			{
				mir_free(tmp);
				HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT);
				return CallService(MS_PROTO_CHAINSEND, w, l);
			}
			if(!bJabberAPI || !bIsMiranda09)
				cmd += _T("--comment \"\" --no-version ");
			if(DBGetContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 0))
				cmd += _T("--trust-model always ");
			cmd += _T("--batch --yes -e -a -r ");
			TCHAR *tmp2 = mir_a2t(tmp);
			mir_free(tmp);
			cmd += tmp2;
			mir_free(tmp2);
			cmd += _T(" \"");
			tmp2 = UniGetContactSettingUtf(NULL, szGPGModuleName, "szHomePath", _T(""));
			path.append(tmp2);
			cmd += tmp2;
			mir_free(tmp2);
			cmd += _T("\\exported_data");
			path.append(_T("\\exported_data"));
			cmd += _T("\"");
			{
				char *tmp;
//				if(unicode)
					tmp = mir_utf8encodeW(str.c_str());
//				else
//					tmp = mir_t2a(str.c_str());
				wfstream f(path.c_str(), std::ios::out);
				f<<tmp;
				mir_free(tmp);
				f.close();
			}
			gpg_execution_params params;
			pxResult result;
			params.cmd = &cmd;
			params.useless = "";
			params.out = &out;
			params.code = &code;
			params.result = &result;
			HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)&params);
			if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT)
			{
				TerminateThread(gpg_thread, 0);
				MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK);
			}
			if(result == pxNotFound)
			{
				MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);
				return CallService(MS_PROTO_CHAINSEND, w, l);
			}
			if(out.find("There is no assurance this key belongs to the named user") != string::npos)
			{
				out.clear();
				if(MessageBox(0, _T("We trying to encrypt with untrusted key, do you want to trust this key permanently ?"), _T("Warning"), MB_YESNO) == IDYES)
				{
					DBWriteContactSettingByte(hContact, szGPGModuleName, "bAlwaysTrust", 1);
					cmd.insert(0, _T("--trust-model always "));
					gpg_execution_params params;
					pxResult result;
					params.cmd = &cmd;
					params.useless = "";
					params.out = &out;
					params.code = &code;
					params.result = &result;
					HANDLE gpg_thread = mir_forkthread(pxEexcute_thread, (void*)&params);
					if(WaitForSingleObject(gpg_thread, 10000) == WAIT_TIMEOUT)
					{
						TerminateThread(gpg_thread, 0);
						MessageBox(0, _T("GPG execution timed out, aborted"), _T(""), MB_OK);
					}
					if(result == pxNotFound)
					{
						MessageBox(0, _T("Set path to gpg.exe first!"), _T("Warning"), MB_OK);
						return CallService(MS_PROTO_CHAINSEND, w, l);
					}
				}
				else
					return 0;
			}
			DeleteFile(path.c_str());
			path.append(_T(".asc"));
			wfstream f(path.c_str(), std::ios::in);
			str.clear();
			while(!f.eof() && f.is_open())
			{
				TCHAR tmp[128];
				f.getline(tmp, 128);
				str.append(tmp);
				if(bJabberAPI && bIsMiranda09)
					str.append(_T("\n"));
				else
					str.append(_T("\r\n"));
			}
			f.close();
			DeleteFile(path.c_str());
			if(!str.length())
			{
				HistoryLog(hContact, "Failed to encrypt message with GPG", EVENTTYPE_MESSAGE, DBEF_SENT);
				return CallService(MS_PROTO_CHAINRECV, w, (LPARAM)ccs);
			}
//			mir_free((void**)ccs->lParam);
			ccs->lParam = (LPARAM)mir_utf8encodeW(str.c_str());
			if(bAppendTags)
				DBWriteContactSettingByte(hContact, szGPGModuleName, "MsgsForTagging", DBGetContactSettingByte(ccs->hContact, szGPGModuleName, "MsgsForTagging", 0) + 1);
		}		
	}
//	ccs->wParam&=~PREF_UNICODE; //memory leak ?
	return CallService(MS_PROTO_CHAINSEND, w, l);
}

int HookSendMsg(WPARAM w, LPARAM l)
{
	HANDLE hContact = (HANDLE)w;
	if(isProtoMetaContacts(hContact))
		hContact = metaGetMostOnline(hContact);
	if(!DBGetContactSettingByte(hContact, szGPGModuleName, "GPGEncryption", 0))
		return 0;

	if(bAppendTags)
	{
		if(!l)
			return 0;
		BYTE Msgs = DBGetContactSettingByte(hContact, szGPGModuleName, "MsgsForTagging", 0);
		if(!Msgs)
			return 0;
		DBEVENTINFO * dbei = (DBEVENTINFO*)l;
		if((dbei->eventType == EVENTTYPE_MESSAGE) && (dbei->flags & DBEF_SENT))
		{
			char *msg = (char*)dbei->pBlob;
			TCHAR *tmp = mir_utf8decodeW(msg);
			wstring str = tmp;
			mir_free(tmp);
			str.insert(0, outopentag);
			str.append(outclosetag);
			char *msg2 = mir_utf8encodeW(str.c_str());
			mir_free(dbei->pBlob);
			dbei->pBlob = (PBYTE)msg2;
			dbei->cbBlob = strlen(msg2)+1;
			DBWriteContactSettingByte(hContact, szGPGModuleName, "MsgsForTagging", Msgs - 1);
		}
	}
	return 0;
}

int TestHook(WPARAM w, LPARAM l)
{
	return 0;
}


static BOOL CALLBACK DlgProcKeyPassword(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	char *inkeyid = UniGetContactSettingUtf(new_key_hcnt, szGPGModuleName, "InKeyID", "");
	ReleaseMutex(new_key_hcnt_mutex);
	TCHAR *tmp = NULL;
	
  switch (msg)
  {
  case WM_INITDIALOG:
    {
		TranslateDialogDefault(hwndDlg);
		string questionstr = "Please enter password for key with ID: ";
		questionstr += inkeyid;
		SetDlgItemTextA(hwndDlg, IDC_KEYID, questionstr.c_str());
		EnableWindow(GetDlgItem(hwndDlg, IDC_DEFAULT_PASSWORD), 0);
      return TRUE;
    }
    
 
  case WM_COMMAND:
    {
      switch (LOWORD(wParam))
      {
      case IDOK:
        {
			TCHAR tmp[64];
			GetDlgItemText(hwndDlg, IDC_KEY_PASSWORD, tmp, 64);
			if(_tcslen(tmp) > 0)
			{
				extern TCHAR *password;
				if(IsDlgButtonChecked(hwndDlg, IDC_SAVE_PASSWORD))
				{
					if((strlen(inkeyid) > 0) && !IsDlgButtonChecked(hwndDlg, IDC_DEFAULT_PASSWORD))
					{
						string dbsetting = "szKey_";
						dbsetting += inkeyid;
						dbsetting += "_Password";
						DBWriteContactSettingTString(NULL, szGPGModuleName, dbsetting.c_str(), tmp);
					}
					else
						DBWriteContactSettingTString(NULL, szGPGModuleName, "szKeyPassword", tmp);
				}
				if(password)
					delete [] password;
				password = new TCHAR [_tcslen(tmp)+1];
				_tcscpy(password, tmp);
			}
			mir_free(tmp);
			mir_free(inkeyid);
			DestroyWindow(hwndDlg);
          break;
        }
	  case IDCANCEL:
		  mir_free(inkeyid);
		  _terminate = true;
		  DestroyWindow(hwndDlg);
		  break;
	  default:
		break;
      }
      
      break;
    }
    
  case WM_NOTIFY:
    {
      switch (((LPNMHDR)lParam)->code)
      {
	  default:
		  EnableWindow(GetDlgItem(hwndDlg, IDC_DEFAULT_PASSWORD), IsDlgButtonChecked(hwndDlg, IDC_SAVE_PASSWORD)?1:0);
		  break;
      }
	}
    break;
  case WM_CLOSE:
	  mir_free(inkeyid);
	  DestroyWindow(hwndDlg);
	  break;
  case WM_DESTROY:
	  break;

  }
  return FALSE;
}

void ShowLoadKeyPasswordWindow()
{
	extern HINSTANCE hInst;
	DialogBox(hInst, MAKEINTRESOURCE(IDD_KEY_PASSWD), NULL, DlgProcKeyPassword);
}