summaryrefslogtreecommitdiff
path: root/plugins/Boltun/src/boltun.cpp
blob: 3c77218d317accfa22616bc648e1b590f9ad4e39 (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
//***********************************************************
//	Copyright © 2003-2008 Alexander S. Kiselev, Valentin Pavlyuchenko
//
//	This file is part of Boltun.
//
//    Boltun 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.
//
//    Boltun 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 Boltun. If not, see <http://www.gnu.org/licenses/>.
//
//***********************************************************

#include "stdafx.h"

int hLangpack;

TalkBot* bot = NULL;


#define MAX_WARN_TEXT    1024
#define MAX_MIND_FILE    1024

HINSTANCE hInst;
BOOL blInit = FALSE;
UINT pTimer = 0;
TCHAR *path;

PLUGININFOEX pluginInfo = {
	sizeof(PLUGININFOEX),
	__PLUGIN_NAME,
	PLUGIN_MAKE_VERSION(__MAJOR_VERSION, __MINOR_VERSION, __RELEASE_NUM, __BUILD_NUM),
	__DESCRIPTION,
	__AUTHOR,
	__AUTHOREMAIL,
	__COPYRIGHT,
	__AUTHORWEB,
	UNICODE_AWARE,
	// {488C5C84-56DA-434F-96F1-B18900DEF760}
	{ 0x488c5c84, 0x56da, 0x434f, { 0x96, 0xf1, 0xb1, 0x89, 0x0, 0xde, 0xf7, 0x60 } }
};

static HGENMENU hMenuItemAutoChat, hMenuItemNotToChat, hMenuItemStartChatting;

#define MIND_DIALOG_FILTER _T("%s (*.mindw)\1*.mindw\1%s (*.*)\1*.*\1")

#ifdef DEBUG_LOAD_TIME
#include <intrin.h>
#endif

void UpdateEngine()
{
	if (bot)
	{
		bot->SetSilent(Config.EngineStaySilent);
		bot->SetLowercase(Config.EngineMakeLowerCase);
		bot->SetUnderstandAlways(Config.EngineUnderstandAlways);
	}
}

TCHAR* GetFullName(const TCHAR* filename)
{
	size_t flen = mir_tstrlen(filename);
	TCHAR* fullname = const_cast<TCHAR*>(filename);
	if (!_tcschr(filename, _T(':')))
	{
		size_t plen = mir_tstrlen(path);
		fullname = new TCHAR[plen + flen + 1];
		fullname[0] = NULL;
		mir_tstrcat(fullname, path);
		mir_tstrcat(fullname, filename);
	}
	return fullname;
}

static bool LoadMind(const TCHAR* filename, int &line)
{
	TCHAR* fullname = GetFullName(filename);
	HCURSOR newCur = LoadCursor(NULL, MAKEINTRESOURCE(IDC_WAIT));
	HCURSOR oldCur = SetCursor(newCur);
#ifdef DEBUG_LOAD_TIME
	unsigned __int64 t = __rdtsc();
#endif
	Mind* mind = new Mind();
	line = -1;
	try
	{
		mind->Load(fullname);
	}
	catch (Mind::CorruptedMind c)
	{
		line = c.line;
		delete mind;
		if (fullname != filename)
			delete[] fullname;
		SetCursor(oldCur);
		return false;
	}
	catch (...)
	{
		delete mind;
		if (fullname != filename)
			delete[] fullname;
		SetCursor(oldCur);
		return false;
	}
	if (fullname != filename)
		delete[] fullname;

#ifdef DEBUG_LOAD_TIME
	t = __rdtsc() - t;
	char dest[101];
	mir_snprintf(dest, _countof(dest), "%I64d ticks\n", t / 3200000);
	MessageBoxA(NULL, dest, NULL, 0);
	//exit(0);
#endif
	SetCursor(oldCur);
	HRSRC hRes = FindResource(hInst, MAKEINTRESOURCE(IDR_SMILES), _T("SMILES"));
	if (!hRes)
	{
		delete mind;
		return false;
	}
	DWORD size = SizeofResource(hInst, hRes);
	if (!size)
	{
		delete mind;
		return false;
	}
	HGLOBAL hGlob = LoadResource(hInst, hRes);
	if (!hGlob)
	{
		delete mind;
		return false;
	}
	void *data = LockResource(hGlob);
	if (!data)
	{
		FreeResource(hGlob);
		delete mind;
		return false;
	}
	bool res = true;
	try
	{
		mind->LoadSmiles(data, size);
	}
	catch (...)
	{
		res = false;
	}
	UnlockResource(data);
	FreeResource(hGlob);
	if (!res)
	{
		delete mind;
		return false;
	}
	delete bot;
	bot = new TalkBot(*mind);
	delete mind;
	UpdateEngine();
	return true;
}

/*static bool SaveMind(const TCHAR* filename)
{
if (!bot)
return false;
bot->GetMind().Save(filename);
return true;
}*/

static bool BoltunAutoChat(MCONTACT hContact)
{
	if (db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_NOT_TO_CHAT
		, FALSE) == TRUE)
		return false;

	if (Config.TalkWithEverybody)
		return true;

	if (Config.TalkEveryoneWhileAway)
	{
		int status = CallService(MS_CLIST_GETSTATUSMODE, 0, 0);
		if (status == ID_STATUS_AWAY ||
			status == ID_STATUS_DND ||
			status == ID_STATUS_NA ||
			status == ID_STATUS_OCCUPIED ||
			status == ID_STATUS_ONTHEPHONE ||
			status == ID_STATUS_OUTTOLUNCH)
			return true;
	}

	if ((db_get_b(hContact, "CList", "NotOnList", 0) == 1) &&
		Config.TalkWithNotInList)
		return true;

	if (db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_AUTO_CHAT,
		FALSE) == TRUE)
		return true;

	return false;
}

static int MessageEventAdded(WPARAM hContact, LPARAM hDbEvent)
{
	if (!BoltunAutoChat(hContact))
		return 0;

	DBEVENTINFO dbei;
	memset(&dbei, 0, sizeof(dbei));
	dbei.cbSize = sizeof(dbei);
	dbei.cbBlob = 0;
	dbei.cbBlob = db_event_getBlobSize(hDbEvent);
	if (dbei.cbBlob == -1)
		return 0;

	dbei.pBlob = (PBYTE)malloc(dbei.cbBlob);
	if (dbei.pBlob == NULL)
		return 0;

	db_event_get(hDbEvent, &dbei);
	if (dbei.flags & DBEF_SENT || dbei.flags & DBEF_READ || dbei.eventType != EVENTTYPE_MESSAGE)
		return 0;
	DBEVENTGETTEXT egt;
	egt.codepage = CP_ACP;
	egt.datatype = DBVT_TCHAR;
	egt.dbei = &dbei;
	TCHAR* s = (TCHAR*)(void*)CallService(MS_DB_EVENT_GETTEXT, 0, (LPARAM)&egt);
	free(dbei.pBlob);
	if (Config.MarkAsRead)
		db_event_markRead(hContact, hDbEvent);

	AnswerToContact(hContact, s);
	mir_free(s);
	return 0;
}

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD, LPVOID)
{
	hInst = hinstDLL;
	return TRUE;
}

void UpdateEverybodyCheckboxes(HWND hwndDlg)
{
	bool Enable = BST_UNCHECKED == IsDlgButtonChecked(hwndDlg, IDC_EVERYBODY) == BST_CHECKED;
	HWND wnd;
	wnd = GetDlgItem(hwndDlg, IDC_NOTINLIST);
	EnableWindow(wnd, Enable);
	wnd = GetDlgItem(hwndDlg, IDC_AUTOAWAY);
	EnableWindow(wnd, Enable);
}

static INT_PTR CALLBACK MainDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	BOOL bTranslated = FALSE;
	static bool loading = true;
	switch (uMsg)
	{
	case WM_INITDIALOG:
		loading = true;
		TranslateDialogDefault(hwndDlg);
		CheckDlgButton(hwndDlg, IDC_EVERYBODY, Config.TalkWithEverybody ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_NOTINLIST, Config.TalkWithNotInList ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_AUTOAWAY, Config.TalkEveryoneWhileAway ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_WARN, Config.TalkWarnContacts ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_MARKREAD, Config.MarkAsRead ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_PAUSEDEPENDS, Config.PauseDepends ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_PAUSERANDOM, Config.PauseRandom ? BST_CHECKED : BST_UNCHECKED);
		SendDlgItemMessage(hwndDlg, IDC_WAITTIME, EM_SETLIMITTEXT, 3, 0);
		SetDlgItemInt(hwndDlg, IDC_WAITTIME, Config.AnswerPauseTime, FALSE);
		SendDlgItemMessage(hwndDlg, IDC_THINKTIME, EM_SETLIMITTEXT, 3, 0);
		SetDlgItemInt(hwndDlg, IDC_THINKTIME, Config.AnswerThinkTime, FALSE);
		SendDlgItemMessage(hwndDlg, IDC_WARNTXT, EM_SETLIMITTEXT, MAX_WARN_TEXT, 0);
		SetDlgItemText(hwndDlg, IDC_WARNTXT, Config.WarnText);
		UpdateEverybodyCheckboxes(hwndDlg);
		loading = false;
		return TRUE;
	case WM_COMMAND:
		if (LOWORD(wParam) == IDC_EVERYBODY && HIWORD(wParam) == BN_CLICKED)
			UpdateEverybodyCheckboxes(hwndDlg);
		if (!loading)
		{
			bool notify = true;
			switch (LOWORD(wParam))
			{
			case IDC_WARNTXT:
			case IDC_WAITTIME:
			case IDC_THINKTIME:
				if (HIWORD(wParam) != EN_CHANGE)
					notify = false;
				break;
			}
			if (notify)
				SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
		}
		break;
	case WM_NOTIFY:
	{
		NMHDR* nmhdr = (NMHDR*)lParam;
		switch (nmhdr->code)
		{
		case PSN_APPLY:
		case PSN_KILLACTIVE:
		{
			Config.TalkWithEverybody = IsDlgButtonChecked(hwndDlg, IDC_EVERYBODY) == BST_CHECKED ? TRUE : FALSE;
			Config.TalkWithNotInList = IsDlgButtonChecked(hwndDlg, IDC_NOTINLIST) == BST_CHECKED ? TRUE : FALSE;
			Config.TalkEveryoneWhileAway = IsDlgButtonChecked(hwndDlg, IDC_AUTOAWAY) == BST_CHECKED ? TRUE : FALSE;
			Config.TalkWarnContacts = IsDlgButtonChecked(hwndDlg, IDC_WARN) == BST_CHECKED ? TRUE : FALSE;
			Config.MarkAsRead = IsDlgButtonChecked(hwndDlg, IDC_MARKREAD) == BST_CHECKED ? TRUE : FALSE;
			Config.PauseDepends = IsDlgButtonChecked(hwndDlg, IDC_PAUSEDEPENDS) == BST_CHECKED ? TRUE : FALSE;
			Config.PauseRandom = IsDlgButtonChecked(hwndDlg, IDC_PAUSERANDOM) == BST_CHECKED ? TRUE : FALSE;
			Config.AnswerPauseTime = GetDlgItemInt(hwndDlg, IDC_WAITTIME, &bTranslated, FALSE);
			if (!bTranslated)
				Config.AnswerPauseTime = 2;
			Config.AnswerThinkTime = GetDlgItemInt(hwndDlg, IDC_THINKTIME, &bTranslated, FALSE);
			if (!bTranslated)
				Config.AnswerThinkTime = 4;
			TCHAR c[MAX_WARN_TEXT];
			bTranslated = GetDlgItemText(hwndDlg, IDC_WARNTXT, c, _countof(c));
			if (bTranslated)
				Config.WarnText = c;
			else
				Config.WarnText = TranslateTS(DEFAULT_WARN_TEXT);
		}
		return TRUE;
		}
		break;
	}
	break;
	}
	return 0;
}

void UpdateUnderstandAlwaysCheckbox(HWND hwndDlg)
{
	bool Enable = BST_UNCHECKED == IsDlgButtonChecked(hwndDlg, IDC_ENGINE_SILENT) == BST_CHECKED;
	HWND wnd;
	wnd = GetDlgItem(hwndDlg, IDC_ENGINE_UNDERSTAND_ALWAYS);
	EnableWindow(wnd, Enable);
}

static INT_PTR CALLBACK EngineDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	WORD param;
	BOOL bTranslated = FALSE;
	static bool loading = true;
	static int changeCount = 0;
	switch (uMsg)
	{
	case WM_INITDIALOG:
		loading = true;
		TranslateDialogDefault(hwndDlg);
		CheckDlgButton(hwndDlg, IDC_ENGINE_SILENT, Config.EngineStaySilent ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_ENGINE_LOWERCASE, Config.EngineMakeLowerCase ? BST_CHECKED : BST_UNCHECKED);
		CheckDlgButton(hwndDlg, IDC_ENGINE_UNDERSTAND_ALWAYS, Config.EngineUnderstandAlways ? BST_CHECKED : BST_UNCHECKED);
		SetDlgItemText(hwndDlg, IDC_MINDFILE, Config.MindFileName);
		EnableWindow(GetDlgItem(hwndDlg, IDC_BTNSAVE), blInit);
		UpdateUnderstandAlwaysCheckbox(hwndDlg);
		loading = false;
		return TRUE;
	case WM_COMMAND:
		param = LOWORD(wParam);
		if (param == IDC_ENGINE_SILENT && HIWORD(wParam) == BN_CLICKED)
			UpdateUnderstandAlwaysCheckbox(hwndDlg);
		OPENFILENAME ofn;
		switch (param)
		{
		case IDC_BTNPATH:
		{
			const size_t fileNameSize = 5000;
			TCHAR *filename = new TCHAR[fileNameSize];
			TCHAR *fullname = GetFullName(Config.MindFileName);
			mir_tstrcpy(filename, fullname);
			if (fullname != Config.MindFileName)
				delete[] fullname;

			memset(&ofn, 0, sizeof(ofn));
			ofn.lStructSize = sizeof(OPENFILENAME);
			ofn.hwndOwner = GetParent(hwndDlg);

			TCHAR* mind = TranslateTS(MIND_FILE_DESC);
			TCHAR* anyfile = TranslateTS(ALL_FILES_DESC);
			size_t l = mir_tstrlen(MIND_DIALOG_FILTER)
				+ mir_tstrlen(mind) + mir_tstrlen(anyfile);
			TCHAR *filt = new TCHAR[l];
			mir_sntprintf(filt, l, MIND_DIALOG_FILTER, mind, anyfile);
			for (size_t i = 0; i < l; i++)
				if (filt[i] == '\1')
					filt[i] = '\0';
			ofn.lpstrFilter = filt;

			ofn.lpstrFile = filename;
			ofn.nMaxFile = fileNameSize;
			ofn.Flags = OFN_FILEMUSTEXIST;
			ofn.lpstrInitialDir = path;
			if (!GetOpenFileName(&ofn))
			{
				delete[] filename;
				delete[] filt;
				break;
			}
			delete[] filt;
			TCHAR* origf = filename;
			TCHAR* f = filename;
			TCHAR* p = path;
			while (*p && *f)
			{
				TCHAR p1 = (TCHAR)CharLower((TCHAR*)(long)*p++);
				TCHAR f1 = (TCHAR)CharLower((TCHAR*)(long)*f++);
				if (p1 != f1)
					break;
			}
			if (!*p)
				filename = f;
			Config.MindFileName = filename;
			SetDlgItemText(hwndDlg, IDC_MINDFILE, filename);
			delete[] origf;
		}
		case IDC_BTNRELOAD:
		{
			const TCHAR *c = Config.MindFileName;
			int line;
			bTranslated = blInit = LoadMind(c, line);
			if (!bTranslated)
			{
				TCHAR message[5000];
				mir_sntprintf(message, _countof(message), TranslateTS(FAILED_TO_LOAD_BASE), line, c);
				MessageBox(NULL, message, TranslateTS(BOLTUN_ERROR), MB_ICONERROR | MB_TASKMODAL | MB_OK);
			}
			break;
		}
		default:
			if (!loading)
			{
				if (param == IDC_MINDFILE/* && HIWORD(wParam) != EN_CHANGE*/)
					break;
				SendMessage(GetParent(hwndDlg), PSM_CHANGED, 0, 0);
			}
		}
		break;
	case WM_NOTIFY:
	{
		NMHDR* nmhdr = (NMHDR*)lParam;
		switch (nmhdr->code)
		{
		case PSN_APPLY:
		case PSN_KILLACTIVE:
		{
			Config.EngineStaySilent = IsDlgButtonChecked(hwndDlg, IDC_ENGINE_SILENT) == BST_CHECKED ? TRUE : FALSE;
			Config.EngineMakeLowerCase = IsDlgButtonChecked(hwndDlg, IDC_ENGINE_LOWERCASE) == BST_CHECKED ? TRUE : FALSE;
			Config.EngineUnderstandAlways = IsDlgButtonChecked(hwndDlg, IDC_ENGINE_UNDERSTAND_ALWAYS) == BST_CHECKED ? TRUE : FALSE;
			UpdateEngine();
			TCHAR c[MAX_MIND_FILE];
			bTranslated = GetDlgItemText(hwndDlg, IDC_MINDFILE, c, _countof(c));
			if (bTranslated)
				Config.MindFileName = c;
			else
				Config.MindFileName = DEFAULT_MIND_FILE;
		}
		return TRUE;
		}
		break;
	}
	break;
	}
	return 0;
}

static int MessageOptInit(WPARAM wParam, LPARAM)
{
	OPTIONSDIALOGPAGE odp = { 0 };
	odp.position = 910000000;
	odp.hInstance = hInst;
	odp.pszGroup = BOLTUN_GROUP;
	odp.pszTitle = BOLTUN_NAME;
	odp.pfnDlgProc = MainDlgProc;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_MAIN);
	odp.pszTab = TAB_GENERAL;
	Options_AddPage(wParam, &odp);

	odp.pfnDlgProc = EngineDlgProc;
	odp.pszTemplate = MAKEINTRESOURCEA(IDD_ENGINE);
	odp.pszTab = TAB_ENGINE;
	Options_AddPage(wParam, &odp);
	return 0;
}

static int ContactClick(WPARAM hContact, LPARAM, BOOL clickNotToChat)
{
	BOOL boltunautochat = db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_AUTO_CHAT, FALSE);
	BOOL boltunnottochat = db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_NOT_TO_CHAT, FALSE);

	if (clickNotToChat)
	{
		boltunnottochat = !boltunnottochat;
		if (boltunnottochat)
		{
			boltunautochat = FALSE;
		}
	}
	else
	{
		boltunautochat = !boltunautochat;
		if (boltunautochat)
		{
			boltunnottochat = FALSE;
		}
		else
		{
			db_set_b(hContact, BOLTUN_KEY, DB_CONTACT_WARNED, FALSE);
		}
	}

	db_set_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_AUTO_CHAT, (BYTE)boltunautochat);
	db_set_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_NOT_TO_CHAT, (BYTE)boltunnottochat);

	return 0;
}

static INT_PTR ContactClickAutoChat(WPARAM hContact, LPARAM lParam)
{
	return ContactClick(hContact, lParam, 0);
}

static INT_PTR ContactClickNotToChat(WPARAM hContact, LPARAM lParam)
{
	return ContactClick(hContact, lParam, 1);
}

static INT_PTR ContactClickStartChatting(WPARAM hContact, LPARAM)
{
	StartChatting(hContact);
	return 0;
}

static int MessagePrebuild(WPARAM hContact, LPARAM)
{
	if (!blInit || (db_get_b(hContact, "CList", "NotOnList", 0) == 1)) {
		Menu_EnableItem(hMenuItemAutoChat, false);
		Menu_EnableItem(hMenuItemNotToChat, false);
	}
	else {
		if (db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_AUTO_CHAT, FALSE))
			Menu_ModifyItem(hMenuItemAutoChat, NULL, Skin_LoadIcon(SKINICON_OTHER_TICK), CMIF_CHECKED);
		else
			Menu_ModifyItem(hMenuItemAutoChat, NULL, Skin_LoadIcon(SKINICON_OTHER_NOTICK), 0);

		if (db_get_b(hContact, BOLTUN_KEY, DB_CONTACT_BOLTUN_NOT_TO_CHAT, FALSE))
			Menu_ModifyItem(hMenuItemNotToChat, NULL, Skin_LoadIcon(SKINICON_OTHER_TICK), CMIF_CHECKED);
		else
			Menu_ModifyItem(hMenuItemNotToChat, NULL, Skin_LoadIcon(SKINICON_OTHER_NOTICK), 0);
	}
	return 0;
}

extern "C" __declspec(dllexport) PLUGININFOEX* MirandaPluginInfoEx(DWORD)
{
	return &pluginInfo;
}

extern "C" int __declspec(dllexport) Load(void)
{
	mir_getLP(&pluginInfo);

	path = new TCHAR[MAX_PATH];
	int len = GetModuleFileName(hInst, path, MAX_PATH);
	if (len > MAX_PATH)
	{
		delete[] path;
		TCHAR *path = new TCHAR[len];
		int len2 = GetModuleFileName(hInst, path, len);
		if (len2 != len)
		{
			delete[] path;
			return false;
		}
	}
	*(_tcsrchr(path, _T('\\')) + 1) = _T('\0');

	/*initialize miranda hooks and services on options dialog*/
	HookEvent(ME_OPT_INITIALISE, MessageOptInit);
	/*initialize miranda hooks and services*/
	HookEvent(ME_DB_EVENT_ADDED, MessageEventAdded);
	HookEvent(ME_CLIST_PREBUILDCONTACTMENU, MessagePrebuild);

	CreateServiceFunction(SERV_CONTACT_AUTO_CHAT, ContactClickAutoChat);
	CreateServiceFunction(SERV_CONTACT_NOT_TO_CHAT, ContactClickNotToChat);
	CreateServiceFunction(SERV_CONTACT_START_CHATTING, ContactClickStartChatting);
	{
		CLISTMENUITEM mi = { 0 };
		mi.position = -50010002; //TODO: check the warning
		mi.pszName = BOLTUN_AUTO_CHAT;
		mi.pszService = SERV_CONTACT_AUTO_CHAT;
		hMenuItemAutoChat = Menu_AddContactMenuItem(&mi);

		mi.position = -50010001; //TODO: check the warning
		mi.pszName = BOLTUN_NOT_TO_CHAT;
		mi.pszService = SERV_CONTACT_NOT_TO_CHAT;
		hMenuItemNotToChat = Menu_AddContactMenuItem(&mi);

		mi.flags = CMIF_NOTOFFLINE;
		mi.position = -50010000; //TODO: check the warning
		mi.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_RECVMSG));
		mi.pszName = BOLTUN_START_CHATTING;
		mi.pszService = SERV_CONTACT_START_CHATTING;
		hMenuItemStartChatting = Menu_AddContactMenuItem(&mi);
	}

	int line;
	blInit = LoadMind(Config.MindFileName, line);
	if (!blInit)
	{
		TCHAR path[2000];
		mir_sntprintf(path, _countof(path), TranslateTS(FAILED_TO_LOAD_BASE), line, (const TCHAR*)Config.MindFileName);
		MessageBox(NULL, path, TranslateTS(BOLTUN_ERROR), MB_ICONERROR | MB_TASKMODAL | MB_OK);
	}
	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	if (pTimer)
		KillTimer(NULL, pTimer);
	if (blInit)
	{
#if 0 //No need to save, we don't have studying algorithm
		if(Config.MindFileName && !SaveMind(Config.MindFileName))
		{
			//This causes errors with development core when calling MessageBox.
			//It seems that it's now a Boltun problem.
			//So in case of saving error we will remain silent
#if 0
			TCHAR path[MAX_PATH];
			mir_sntprintf(path, _countof(path), TranslateTS(FAILED_TO_SAVE_BASE), (const TCHAR*)Config.MindFileName);
			TCHAR* err = TranslateTS(BOLTUN_ERROR);
			MessageBox(NULL, path, err, MB_ICONERROR|MB_TASKMODAL|MB_OK);*/
#endif
		}
#endif
		delete bot;
	}
	delete[] path;
	return 0;
}