summaryrefslogtreecommitdiff
path: root/BanControl/plugin/BanControl.asm
blob: b8e7e8df652293b411b2a6a4e335361ed930a610 (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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
;Ban Control plugin for Miranda. Bans a contact for a specific time
;Copyright (C) 2007 A. Chilaru
;
;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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\comctl32.inc
include \masm32\include\gdi32.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\IcoLib.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_clist.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_clc.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_database.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_langpack.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_options.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_protocols.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_protosvc.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\m_updater.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\newpluginapi.inc
include \masm32\FlexLabs\BanControl\Source\trunk\inc\statusmodes.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
includelib \masm32\lib\comctl32.lib
includelib \masm32\lib\gdi32.lib

				;function definitions for debuggers
				;hook functions are C-style functions. The rest are stdcall
OptInit proto C wParam:DWORD, lParam:DWORD
Message proto C wParam:DWORD, lParam:DWORD
MdLoaded proto C wParam:DWORD, lParam:DWORD
IcoLibEv proto C wParam:DWORD, lParam: DWORD

CallService proto lpServiceName:DWORD, wParam:DWORD, lParam:DWORD
ServiceExists proto lpServiceName:DWORD
HookEvent proto lpHookName:DWORD, lpFunction:DWORD
UnhookEvent proto HookHandle:DWORD

Translate proto lpStr:DWORD
Error proto lpStr:DWORD
ResetListOptions proto hCList:DWORD
SetAllContactIcons proto hCList:DWORD
SetListGroupIcons proto hCList:DWORD, hFirstItem:DWORD, hParentItem:DWORD, groupChildCount:DWORD
SetAllChildIcons proto hCList:DWORD, hFirstItem:DWORD, iColumn:DWORD, iImage:DWORD
MetaConClick proto hContact:DWORD, iCount:DWORD, iValue: DWORD
MetaSubClick proto hContact:DWORD

DLGHDR struct		;Structure to hold info about tabs
	hDlg		dd	?
    hTab		dd	?
    hDisplay	dd	?
    rcDisplay	RECT <>
    hTab1		dd	?
    hTab2		dd	?
DLGHDR ends

					;Macro to create lParam DWORD from two WORDs (low WORDs used if DWORD given)
MAKELPARAM macro lowWord, highWord
	push ebx
	mov ebx, lowWord
	mov eax, highWord
	shl eax, 16
	mov ax, bx
	pop ebx
	ENDM

.const
				;option controls
IDC_ENABLED				equ		40071			;Plugin enables checkbox ID
IDC_PROTO				equ		40081			;Protocol listbox ID
IDC_STARTTIME			equ		40088			;time selector
IDC_ENDTIME				equ		40089			;time selector
IDC_SHOWHIDDEN			equ		50071			;Show hidden contacts

IDC_CLIST				equ		3000			;CList component

IDI_BAN					equ		101				;PC icon

				;const from miranda resource file (small dot in CListControl)
IDI_SMALLDOT			equ		211

				;const from skin header
SKINICON_EVENT_MESSAGE	equ		100

WM_IMGLIST				equ		WM_USER+273

.data
hInstance				dd		0				;Plugin hWnd
hApp					dd		0				;Miranda .exe hWnd
hDlgWnd					dd		0				;Option dialog hWnd
hIcoLibEv				dd		0				;IcoLib dialog hWnd
hIml					dd		0				;Image list
hItemAll				dd		0				;CListControl "Group" ** All contacts **"
hItemUnk				dd		0				;CListControl "Group" ** Unknown contacts **"

ddRetAddr				dd		0				;Miranda proto ret. address
ddRetStack				dd		0				;Miranda proto EBP backup

hMsgHook				dd		0				;Message hook hWnd
hOptHook				dd		0				;Options hook hWnd
hMdLHook				dd		0				;Modules loaded hook

ddVar1					dd		0
ddVar2					dd		0				;Just three DWORD variables ;)
ddVar3					dd		0
ddInt					dd		0
ddFlags					dd		0				;Various flags. Descriptions (starting from the lower bit)
							;Reserved
							;OptEditing2		On while options are loaded from the DB
							;OptEditing2		On while options are loaded from the DB
							;noUniConvert		Don't convert to Unicode while translating

sPluginInfo				PLUGININFO <0>			;plugin info structure
sPluginLink				PLUGINLINK <>			;miranda procedure adresses structure
sOptionsPage			OPTIONSDIALOGPAGE <0>	;options struct
sDBVariant				DBVARIANT <>			;DB write/get variable struct
sDBGetSetting			DBCONTACTGETSETTING <>	;DB get struct
sDBWriteSetting			DBCONTACTWRITESETTING <>;DB write struct
sDBEventInfo			DBEVENTINFO <>			;Event ifnfo struct
sUpdate					UPDATE <0>				;Updater struct
sTCITEM					TCITEM	<0>				;struct for tabbing
sDlgHdr					DLGHDR <0>				;struct for tabbing
sSystemTime				SYSTEMTIME <>			;datetime struct
sLngPackDialog			LANGPACKTRANSLATEDIALOG <> ;langpack dialog struct
sClcInfoItem			CLCINFOITEM <>			;contact list child item info
sSkinIconDesc			SKINICONDESC <>			;IcoLib struct

				;plugin details
szShortName				db	"Ban Control",0
szShortNamev			db	"Ban Control "
IFDEF _UNICODE
	szPlVer				db	"(Unicode)",0
ELSE
	szPlVer				db	"(ANSI)",0
ENDIF
ddVersion				dd	00000100h
szDescription			db	"The plugin allows banning users for a certain time.",10,13,"Allows saving history, exporting it or disabling and other options.",0
szAuthor				db	"A. Chilaru",0
szAthorEmail			db	"flexlabs@gmail.com",0
szCopyright				db	"© 2007 A. Chilaru (FlexLabs Inc.)",0
szHomepage				db	"http://dev.mirandaim.ru/~flexer/",0

				;additional details for updater
IFDEF _UNICODE
	szURL				db	"http://addons.miranda-im.org/",0
	szURLKey			db	"<span class=",22h,"fileNameHeader",22h,">Ban Control (Unicode) ",0
	ddURLKeyLen			dd	33h
	szDnURL				db	"http://addons.miranda-im.org/",0
ELSE
	szURL				db	"http://addons.miranda-im.org/",0
	szURLKey			db	"<span class=",22h,"fileNameHeader",22h,">Ban Control (ANSI) ",0
	ddURLKeyLen			dd	30h
	szDnURL				db	"http://addons.miranda-im.org/",0
ENDIF
szBetaURL				db	"http://svn.mirandaim.ru/mainrepo/bancontrol/trunk/BETA",0
szBetaURLKey			db	"BanControl_v.",0
ddBetaURLKeyLen			dd	0Dh
IFDEF _UNICODE
	szBetaDnURL			db	"http://svn.mirandaim.ru/mainrepo/bancontrol/trunk/bin/BanControlW.dll",0
ELSE
	szBetaDnURL			db	"http://svn.mirandaim.ru/mainrepo/bancontrol/trunk/bin/BanControlA.dll",0
ENDIF
szVersion				db	"0.0.1.0",0
ddVersionLen			dd	07h
ddBeta					dd	1		;If plugin is beta - 1. Else - 0
szBetaChangelogURL		db	"http://svn.mirandaim.ru/mainrepo/bancontrol/trunk/CHANGELOG.txt",0

				;miranda functions
MS_DB_CONTACT_GETSETTING	db	"DB/Contact/GetSetting",0
MS_DB_CONTACT_WRITESETTING	db	"DB/Contact/WriteSetting",0
MS_DB_CONTACT_FINDFIRST		db	"DB/Contact/FindFirst",0
MS_DB_CONTACT_FINDNEXT		db	"DB/Contact/FindNext",0
MS_DB_EVENT_GET				db	"DB/Event/Get",0
MS_DB_EVENT_GETBLOBSIZE		db	"DB/Event/GetBlobSize",0
MS_DB_EVENT_GETCONTACT		db	"DB/Event/GetContact",0
MS_PROTO_GETCONTACTBASEPROTO db "Proto/GetContactBaseProto",0
MS_CLIST_ADDMAINMENUITEM	db	"CList/AddMainMenuItem",0
MS_CLIST_GETSTATUSMODE		db	"CList/GetStatusMode",0
MS_OPT_ADDPAGE				db	"Opt/AddPage",0
MS_PROTO_ENUMPROTOCOLS		db	"Proto/EnumProtocols",0
MS_MC_GETMETACONTACT		db	"MetaContacts/GetMeta",0
MS_MC_GETSUBCONTACT			db	"MetaContacts/GetSubContact",0
ME_SYSTEM_MODULESLOADED		db	"Miranda/System/ModulesLoaded",0
MS_SKIN_LOADICON			db	"Skin/Icons/Load",0
MS_SKIN2_ADDICON			db	"Skin2/Icons/AddIcon",0
MS_SKIN2_GETICON			db	"Skin2/Icons/GetIcon",0
MS_UPDATE_REGISTER			db	"Update/Register",0
MS_LANGPACK_TRANSLATESTRING	db	"LangPack/TranslateString",0
MS_LANGPACK_TRANSLATEDIALOG	db	"LangPack/TranslateDialog",0

PS_GETCAPS					db	"/GetCaps",0		;Takes form of "PROTO/GetCaps",0 (GetCapabilities of a PROTO)

				;miranda events
ME_DB_EVENT_ADDED			db	"DB/Event/Added",0
ME_OPT_INITIALISE			db	"Opt/Initialise",0
ME_SKIN2_ICONSCHANGED		db	"Skin2/IconsChanged",0

				;options
szOptGroup				db	"Events",0
szOptGroupW				db	"Unicod",0
szOptTitle				db	"Ban Control",0
szOptTitleW				db	"UnicodeUnic",0
szOModule				db	"BanControl",0
szOFlags				db	"Flags",0
szOTime					db	"Time",0
szOEnabled				db	"Enabled",0
szONotOnList			db	"NotOnList",0
szOCList				db	"CList",0
szOShowHidden			db	"ShowHidden",0
szOMetaContacts			db	"MetaContacts",0
szOMetaCount			db	"NumContacts",0
szOMetaLink				db	"MetaLink",0

				;tab captions
szTab1					db	"Options",0
szTab1W					db	"Unicode",0
szTab2					db	"Contacts",0
szTab2W					db	"UnicodeU",0

				;IcoLib
szIlDesc					db	"Enabled",0
szIlName					db	"banc_icon",0

				;other strings
szDtFormat				db	"HH':'mm",0
szError					db	"Default error message",0
szAllContacts			db	"** All contacts **",0
szAllContactsW			db	"UnicodeUnicodeUnic",0
szUnkContacts			db	"** Unknown contacts **",0
szUnkContactsW			db	"UnicodeUnicodeUnicodeU",0

				;option control info
IDC_ETYPE_MESSAGE		dd		40082
IDC_ETYPE_MESSAGE_V		dd		EVENTTYPE_MESSAGE
IDC_ETYPE_URL			dd		40083
IDC_ETYPE_URL_V			dd		EVENTTYPE_URL
IDC_ETYPE_CONTACTS		dd		40084
IDC_ETYPE_CONTACTS_V	dd		EVENTTYPE_CONTACTS
IDC_ETYPE_ADDED			dd		40085
IDC_ETYPE_ADDED_V		dd		EVENTTYPE_ADDED
IDC_ETYPE_AUTHREQUEST	dd		40086
IDC_ETYPE_AUTHREQUEST_V	dd		EVENTTYPE_AUTHREQUEST
IDC_ETYPE_FILE			dd		40087
IDC_ETYPE_FILE_V		dd		EVENTTYPE_FILE

.data?
				;option control hWnd's
HC_CLIST				dd		?
HC_ENABLED				dd		?
HC_STATUS_ONLINE		dd		?
HC_STATUS_AWAY			dd		?
HC_STATUS_DND			dd		?
HC_STATUS_NA			dd		?
HC_STATUS_OCCUPIED		dd		?
HC_STATUS_FREECHAT		dd		?
HC_STATUS_INVISIBLE 	dd		?
HC_STATUS_ONTHEPHONE	dd		?
HC_STATUS_OUTTOLUNCH	dd		?
HC_PROTO				dd		?
HC_ETYPE_MESSAGE		dd		?
HC_ETYPE_URL			dd		?
HC_ETYPE_CONTACTS		dd		?
HC_ETYPE_ADDED			dd		?
HC_ETYPE_AUTHREQUEST	dd		?
HC_ETYPE_FILE			dd		?
HC_STARTTIME			dd		?
HC_ENDTIME				dd		?

HC_SHOWHIDDEN			dd		?

				;empty string space
szGeneral				db		256 DUP(?)

.code
DllMain proc hInst:HINSTANCE, reason:DWORD, reserved1:DWORD
	push hInst
	pop hInstance										;save hWnd of the library
	invoke GetModuleHandle, NULL
	mov hApp, eax										;save hWnd of Miranda

	mov sPluginInfo.cbSize, sizeof PLUGININFO			;set the library's info
	mov sPluginInfo.shortName, offset szShortNamev
	push ddVersion
	pop sPluginInfo.version
	mov sPluginInfo.description, offset szDescription
	mov sPluginInfo.author, offset szAuthor
	mov sPluginInfo.authorEmail, offset szAthorEmail
	mov sPluginInfo.copyright, offset szCopyright
	mov sPluginInfo.homepage, offset szHomepage

    mov  eax, TRUE
    ret
DllMain Endp

MirandaPluginInfo proc
    mov eax, offset sPluginInfo		;return plugin info struct
    ret
MirandaPluginInfo Endp

					;internal miranda functions. Not necessary, but allow viewing them in the debugger
CallService proc lpServiceName:DWORD, wParam:DWORD, lParam:DWORD
	pop ddRetStack
	pop ddRetAddr
	call sPluginLink.lpCallService
	push ddRetAddr
	push ddRetStack
	ret
CallService endp

ServiceExists proc lpServiceName:DWORD
	pop ddRetStack
	pop ddRetAddr
	call sPluginLink.lpServiceExists
	push ddRetAddr
	push ddRetStack
	ret
ServiceExists endp

HookEvent proc lpHookName:DWORD, lpFunction:DWORD
	pop ddRetStack
	pop ddRetAddr
	call sPluginLink.lpHookEvent
	push ddRetAddr
	push ddRetStack
	ret
HookEvent endp

UnhookEvent proc HookHandle:DWORD
	pop ddRetStack
	pop ddRetAddr
	call sPluginLink.lpUnhookEvent
	push ddRetAddr
	push ddRetStack
	ret
UnhookEvent endp

					;call to Miranda translate module
Translate proc lpString:DWORD
	LOCAL ddLng:DWORD
	
	mov eax, ddFlags
	and eax, 8
	.IF eax==0
		IFDEF _UNICODE
			mov ddLng, LANG_UNICODE
		ELSE
			mov ddLng, 0
		ENDIF
	.ELSE
		and ddFlags, -9
		mov ddLng, 0
	.ENDIF
	invoke CallService, offset MS_LANGPACK_TRANSLATESTRING, ddLng, lpString		;translate the string
	ret
Translate endp

					;Just an info func. Shows a message or Default error if NULL
Error proc lpString:DWORD
	.IF lpString==0
		mov lpString, offset szError
	.ENDIF
	invoke MessageBox, NULL, lpString, addr szShortName, MB_OK
	ret
Error endp

Load proc link:DWORD
	invoke RtlMoveMemory, addr sPluginLink, link, sizeof PLUGINLINK		;get miranda proc struct

	IFDEF _UNICODE
		invoke lstrcpy, offset szGeneral, offset szOptGroup
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szOptGroup, 7
		invoke lstrcpy, offset szGeneral, offset szOptTitle
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szOptTitle, 12
		invoke lstrcpy, offset szGeneral, offset szTab1
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szTab1, 8
		invoke lstrcpy, offset szGeneral, offset szTab2
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szTab2, 9
		invoke lstrcpy, offset szGeneral, offset szAllContacts
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szAllContacts, 19
		invoke lstrcpy, offset szGeneral, offset szUnkContacts
		invoke MultiByteToWideChar, CP_ACP, MB_PRECOMPOSED, offset szGeneral, -1, offset szUnkContacts, 23
	ENDIF

	mov sSystemTime.wYear, 1870
	mov sSystemTime.wMonth, 1
	mov sSystemTime.wDay, 1
	mov sOptionsPage.cbSize, sizeof OPTIONSDIALOGPAGE					;set the option dialog's properties
	invoke Translate, offset szOptTitle
	IFDEF _UNICODE
		mov sOptionsPage.flags, ODPF_UNICODE
	ENDIF
	mov sOptionsPage.pszTitle, eax
	mov sOptionsPage.pfnDlgProc, offset TabOptions
	mov sOptionsPage.pszTemplate, 100
	push hInstance
	pop sOptionsPage.hInstance
	push offset szOptGroup
	pop sOptionsPage.pszGroup
	
	invoke HookEvent, offset ME_DB_EVENT_ADDED, offset Message			;set the message hook
	mov hMsgHook, eax													;save hook hWnd
	invoke HookEvent, offset ME_OPT_INITIALISE, offset OptInit			;set the options hook
	mov hOptHook, eax													;save hook hWnd
	invoke HookEvent, offset ME_SYSTEM_MODULESLOADED, offset MdLoaded	;set the ModulesLoaded hook
	mov hMdLHook, eax													;save hook hWnd

    xor eax, eax
    ret
Load Endp

IcoLibEv proc C wParam:DWORD, lParam: DWORD
	invoke SendMessage, hDlgWnd, WM_IMGLIST, 0, 0
	xor eax, eax
	ret
IcoLibEv endp

;##############################
;  Runs when all modules are loaded
;##############################
MdLoaded proc C wParam:DWORD, lParam:DWORD
	invoke UnhookEvent, hMdLHook				;unhook

	invoke ServiceExists, offset MS_UPDATE_REGISTER
	.IF eax!=0
		mov sUpdate.cbSize, sizeof UPDATE						;initialize updater struct
		mov sUpdate.szComponentName, offset szShortNamev
		.IF ddBeta==0
			mov sUpdate.szVersionURL, offset szURL			;check if is Beta
		.ENDIF
		mov sUpdate.pbVersionPrefix, offset szURLKey
		push ddURLKeyLen
		pop sUpdate.cpbVersionPrefix
		mov sUpdate.szUpdateURL, offset szDnURL
		mov sUpdate.szBetaVersionURL, offset szBetaURL
		mov sUpdate.pbBetaVersionPrefix, offset szBetaURLKey
		push ddBetaURLKeyLen
		pop sUpdate.cpbBetaVersionPrefix
		mov sUpdate.szBetaUpdateURL, offset szBetaDnURL
		mov sUpdate.pbVersion, offset szVersion
		push ddVersionLen
		pop sUpdate.cpbVersion
		mov sUpdate.szBetaChangelogURL, offset szBetaChangelogURL
		invoke CallService, offset MS_UPDATE_REGISTER, 0, offset sUpdate	;register updater service
	.ENDIF	

				;registering icon in IcoLib
	invoke ServiceExists, offset MS_SKIN2_ADDICON
	.IF eax!=0
		invoke GetModuleFileNameA, hInstance, offset szGeneral, 255
		mov sSkinIconDesc.cbSize, sizeof SKINICONDESC
		mov sSkinIconDesc.pszSection, offset szShortName
		or ddFlags, 8
		invoke Translate, offset szIlDesc
		mov sSkinIconDesc.pszDescription, eax
		mov sSkinIconDesc.pszName, offset szIlName
		mov sSkinIconDesc.pszDefaultFile, offset szGeneral
		mov sSkinIconDesc.iDefaultIndex, -IDI_BAN
		invoke CallService, offset MS_SKIN2_ADDICON, 0, offset sSkinIconDesc	
		invoke HookEvent, offset ME_SKIN2_ICONSCHANGED, offset IcoLibEv		;set the IconChanged hook
		mov hIcoLibEv, eax													;save hook hWnd
	.ENDIF

	xor eax, eax
	ret
MdLoaded endp

				;set CListControl style defaults
ResetListOptions proc hCList:DWORD
	LOCAL Col, i:DWORD

	invoke SendMessage, hCList, CLM_SETBKBITMAP, 0, 0
	invoke GetSysColor, COLOR_WINDOW
	mov Col, eax
	invoke SendMessage, hCList, CLM_SETBKCOLOR, Col, 0
	invoke SendMessage, hCList, CLM_SETGREYOUTFLAGS, 0, 0
	invoke SendMessage, hCList, CLM_SETLEFTMARGIN, 2, 0
	invoke SendMessage, hCList, CLM_SETINDENT, 10, 0
	invoke GetSysColor, COLOR_WINDOWTEXT
	mov Col, eax
	mov i, FONTID_MAX
	.WHILE i!=0
		invoke SendMessage, hCList, CLM_SETTEXTCOLOR, i, Col
		dec i
	.ENDW
    mov sDBGetSetting.szModule, offset szOModule			;load the flags
	mov sDBGetSetting.szSetting, offset szOShowHidden
    mov sDBGetSetting.pValue, offset sDBVariant
	mov sDBVariant.VAR1, DBVT_DWORD
	invoke CallService, offset MS_DB_CONTACT_GETSETTING, 0, offset sDBGetSetting
	.IF eax==0
		.IF sDBVariant.VAR2!=0
			invoke GetWindowLong, hCList, GWL_STYLE
			or eax, CLS_SHOWHIDDEN
			invoke SetWindowLong, hCList, GWL_STYLE, eax
		.ENDIF
	.ENDIF
	xor eax, eax
	ret
ResetListOptions endp

				;load per contact trigger settings from DB
SetAllContactIcons proc hCList:DWORD
	LOCAL hContact, hItem, szProto, flags, flag:DWORD

	invoke CallService, offset MS_DB_CONTACT_FINDFIRST, 0, 0
	mov hContact, eax
	invoke SendMessage, hCList, WM_TIMER, 14, 0		;Note: This is a hack. Wait for the CList To be ready. Thanks to FYR
	.WHILE hContact!=0
		invoke SendMessage, hCList, CLM_FINDCONTACT, hContact, 0
		mov hItem, eax
		.IF eax!=0
			invoke CallService, offset MS_PROTO_GETCONTACTBASEPROTO, hContact, 0	;get PROTO for each contact
			mov szProto, eax
			.IF eax==0
				mov flag, 1
				mov flags, 1
			.ELSE
				;invoke IgnoreCheck, szProto						;Check PROTO Capabilities
				mov flags, eax
				mov sDBGetSetting.szModule, offset szOModule		;load settings
				mov sDBGetSetting.szSetting, offset szOEnabled
				mov sDBGetSetting.pValue, offset sDBVariant
				mov sDBVariant.VAR1, DBVT_WORD
				invoke CallService, offset MS_DB_CONTACT_GETSETTING, hContact, offset sDBGetSetting
				.IF eax==0
					push sDBVariant.VAR2
					pop flag
				.ELSE
					mov flag, 1
				.ENDIF
			.ENDIF
			.IF flags!=0			;finally set the necesarry image
				xor eax, eax		;Use (MAKELPARAM "column", "image") instead of just 0
				invoke SendMessage, hCList, CLM_GETEXTRAIMAGE, hItem, eax
				.IF eax==0FFh
					.IF flag!=0
						mov flag, 1
					.ENDIF
					MAKELPARAM 0, flag
					invoke SendMessage, hCList, CLM_SETEXTRAIMAGE, hItem, eax
				.ENDIF
			.ENDIF
		.ENDIF
		invoke CallService, offset MS_DB_CONTACT_FINDNEXT, hContact, 0
		mov hContact, eax
	.ENDW
	ret
SetAllContactIcons endp

				;Set group icons according to the contacts inside
SetListGroupIcons proc hCList:DWORD, hFirstItem:DWORD, hParentItem:DWORD, groupChildCount:DWORD
	LOCAL typeOfFirst, iconOn, childCount, iImage, hItem, hChildItem:DWORD

	mov iconOn, 1						;assume all are enabled
	mov childCount, 0
	invoke SendMessage, hCList, CLM_GETITEMTYPE,hFirstItem, 0
	mov typeOfFirst, eax

	;check groups
	.IF typeOfFirst==CLCIT_GROUP
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=NULL
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_CHILD, hItem
		mov hChildItem, eax
		.IF eax!=0
			invoke SetListGroupIcons, hCList, hChildItem, hItem, addr childCount
		.ENDIF
		.IF iconOn!=0
			invoke SendMessage, hCList, CLM_GETEXTRAIMAGE, hItem, 0
			.IF eax==0
				mov iconOn, 0
			.ENDIF
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hItem
		mov hItem, eax
	.ENDW

	;check contacts
	.IF typeOfFirst==CLCIT_CONTACT
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=0
		invoke SendMessage, hCList, CLM_GETEXTRAIMAGE, hItem, 0
		.IF iconOn!=0
			.IF eax==0
				mov iconOn, 0
			.ENDIF
		.ENDIF
		.IF eax!=0FFh
			inc childCount
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hItem
		mov hItem, eax
	.ENDW

	;set group icons
	.IF childCount==0
		mov eax, 0FFh
	.ELSEIF iconOn==0
		mov eax, 0
	.ELSE
		mov eax, 1
	.ENDIF
	MAKELPARAM 0, eax
	invoke SendMessage, hCList, CLM_SETEXTRAIMAGE, hParentItem, eax
	.IF groupChildCount!=0
		mov eax, [groupChildCount]
		add eax, childCount
		mov [groupChildCount], eax
	.ENDIF
	ret
SetListGroupIcons endp

				;set group child icons
SetAllChildIcons proc hCList:DWORD, hFirstItem:DWORD, iColumn:DWORD, iImage:DWORD
	LOCAL typeOfFirst, iOldIcon, hItem, hChildItem:DWORD

	invoke SendMessage, hCList, CLM_GETITEMTYPE, hFirstItem, 0
	mov typeOfFirst, eax

	;set icons for groups
	.IF typeOfFirst==CLCIT_GROUP
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=0
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_CHILD, hItem
		mov hChildItem, eax
		.IF hChildItem!=0
			invoke SetAllChildIcons, hCList, hChildItem, iColumn, iImage
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hItem
		mov hItem, eax
	.ENDW

	;set icons for contacts
	.IF typeOfFirst==CLCIT_CONTACT
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=0
		invoke SendMessage, hCList, CLM_GETEXTRAIMAGE, hItem, iColumn
		mov iOldIcon, eax
		.IF iOldIcon!=0FFh
			mov eax, iImage
			.IF iOldIcon!=eax
				MAKELPARAM iColumn, iImage
				invoke SendMessage, hCList, CLM_SETEXTRAIMAGE, hItem, eax
				mov sDBGetSetting.szModule, offset szOMetaContacts		;load settings
				mov sDBGetSetting.szSetting, offset szOMetaCount
				mov sDBGetSetting.pValue, offset sDBVariant
				mov sDBVariant.VAR1, DBVT_WORD
				invoke CallService, offset MS_DB_CONTACT_GETSETTING, hItem, offset sDBGetSetting
				.IF eax==0
					invoke MetaConClick, hItem, sDBVariant.VAR2, iImage
				.ENDIF
			.ENDIF
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hItem
		mov hItem, eax
	.ENDW
	ret
SetAllChildIcons endp

				;set icons to contacts Not on list
SetUnkChildIcons proc hCList:DWORD, hFirstItem:DWORD, iColumn:DWORD, iImage:DWORD
	LOCAL typeOfFirst, iOldIcon, hItem, hChildItem:DWORD

	invoke SendMessage, hCList, CLM_GETITEMTYPE, hFirstItem, 0
	mov typeOfFirst, eax

	;set icons for groups
	.IF typeOfFirst==CLCIT_GROUP
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=0
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_CHILD, hItem
		mov hChildItem, eax
		.IF hChildItem!=0
			invoke SetUnkChildIcons, hCList, hChildItem, iColumn, iImage
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTGROUP, hItem
		mov hItem, eax
	.ENDW

	;set icons for contacts
	.IF typeOfFirst==CLCIT_CONTACT
		push hFirstItem
		pop hItem
	.ELSE
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hFirstItem
		mov hItem, eax
	.ENDIF
	.WHILE hItem!=0
		invoke SendMessage, hCList, CLM_GETEXTRAIMAGE, hItem, iColumn
		mov iOldIcon, eax
		.IF iOldIcon!=0FFh
			mov eax, iImage
			.IF iOldIcon!=eax
				mov sDBGetSetting.szModule, offset szOCList
				mov sDBGetSetting.szSetting, offset szONotOnList
				mov sDBGetSetting.pValue, offset sDBVariant
				mov sDBVariant.VAR1, DBVT_BYTE
				invoke CallService, offset MS_DB_CONTACT_GETSETTING, hItem, offset sDBGetSetting
				.IF eax==0
					lea esi, sDBVariant.VAR2
					mov al, [esi]
					.IF al!=0
						MAKELPARAM iColumn, iImage
						invoke SendMessage, hCList, CLM_SETEXTRAIMAGE, hItem, eax
					.ENDIF
				.ENDIF
			.ENDIF
		.ENDIF
		invoke SendMessage, hCList, CLM_GETNEXTITEM, CLGN_NEXTCONTACT, hItem
		mov hItem, eax
	.ENDW
	ret
SetUnkChildIcons endp

MetaConClick proc hContact:DWORD, iCount:DWORD, iValue: DWORD
	dec iCount
	.WHILE iCount!=-1
		invoke CallService, offset MS_MC_GETSUBCONTACT, hContact, iCount
		.IF eax!=0
			mov ebx, eax
			MAKELPARAM 0, 0
			invoke SendDlgItemMessage, hDlgWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, ebx, eax
			.IF eax!= 0FFh
				MAKELPARAM 0, iValue
				invoke SendDlgItemMessage, hDlgWnd, IDC_CLIST, CLM_SETEXTRAIMAGE, ebx, eax
			.ENDIF
		.ENDIF
		dec iCount
	.ENDW
	ret
MetaConClick endp

MetaSubClick proc hContact:DWORD
	LOCAL iVal, iCounter:DWORD

	invoke CallService, offset MS_MC_GETMETACONTACT, hContact, 0
	.IF eax!=0
		mov hContact, eax
		mov sDBGetSetting.szModule, offset szOMetaContacts		;load settings
		mov sDBGetSetting.szSetting, offset szOMetaCount
		mov sDBGetSetting.pValue, offset sDBVariant
		mov sDBVariant.VAR1, DBVT_WORD
		invoke CallService, offset MS_DB_CONTACT_GETSETTING, hContact, offset sDBGetSetting
		.IF eax==0
			push sDBVariant.VAR2
			pop iCounter
			dec iCounter
			mov iVal, 1
			.WHILE iCounter!=-1
				invoke CallService, offset MS_MC_GETSUBCONTACT, hContact, iCounter
				.IF eax!=0
					mov ebx, eax
					MAKELPARAM 0, 0
					invoke SendDlgItemMessage, hDlgWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, ebx, eax
					.IF eax==0
						mov iVal, 0
					.ENDIF
				.ENDIF
				dec iCounter
			.ENDW
		.ENDIF
	.ENDIF
	MAKELPARAM 0, iVal
	invoke SendDlgItemMessage, hDlgWnd, IDC_CLIST, CLM_SETEXTRAIMAGE, hContact, eax
	ret
MetaSubClick endp

;##############################
;	Options hook proc
;##############################
OptInit proc C wParam:DWORD, lParam:DWORD
	mov	eax, wParam			;init the options dialog
	invoke CallService, addr MS_OPT_ADDPAGE, eax, addr sOptionsPage
    xor eax, eax
    ret
OptInit Endp

;##############################
;	Message hook proc
;##############################
Message proc C wParam:DWORD, lParam:DWORD
	LOCAL hContact:DWORD

	xor eax, eax
    ret
Message Endp

;##############################
;	First tab event proc
;##############################
Options1 proc, dhWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
	.IF uMsg==WM_INITDIALOG								;if user opens options
		invoke SetWindowPos, dhWnd, HWND_TOP, sDlgHdr.rcDisplay.left, sDlgHdr.rcDisplay.top, 0, 0, SWP_NOSIZE
	.ELSEIF uMsg==WM_COMMAND							;if sth. happened in the dialog
	.ENDIF
	xor	eax,eax
	ret
Options1 endp

;##############################
;	Second tab event proc
;##############################
Options2 proc, dhWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
	LOCAL hContact, hItem, hitFlags, iImage, itemType, i:DWORD
	
	push dhWnd
	pop hDlgWnd
	.IF uMsg==WM_INITDIALOG
		invoke SetWindowPos, dhWnd, HWND_TOP, sDlgHdr.rcDisplay.left, sDlgHdr.rcDisplay.top, 0, 0, SWP_NOSIZE
		invoke	GetDlgItem, dhWnd, IDC_SHOWHIDDEN
		mov HC_SHOWHIDDEN, eax
	    mov sDBGetSetting.szModule, offset szOModule			;load the flags
		mov sDBGetSetting.szSetting, offset szOShowHidden
		mov sDBGetSetting.pValue, offset sDBVariant
		mov sDBVariant.VAR1, DBVT_DWORD
		invoke CallService, offset MS_DB_CONTACT_GETSETTING, 0, offset sDBGetSetting
		.IF eax==0
			.IF sDBVariant.VAR2!=0
				Invoke SendMessage, HC_SHOWHIDDEN, BM_SETCHECK, BST_CHECKED, 0
			.ENDIF
		.ENDIF
		invoke SendMessage, dhWnd, WM_IMGLIST, wParam, lParam
		invoke GetDlgItem, dhWnd, IDC_CLIST
		mov HC_CLIST, eax
		invoke ResetListOptions, HC_CLIST
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_SETEXTRACOLUMNS, 1, 0			;set no of icon columns
		mov sClcInfoItem.cbSize, sizeof CLCINFOITEM							;add additional items
		mov sClcInfoItem.flags, CLCIIF_GROUPFONT
		invoke Translate, offset szAllContacts								;** All contacts **
		mov sClcInfoItem.pszText, eax
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_ADDINFOITEM, 0, addr sClcInfoItem
		mov hItemAll, eax
		invoke Translate, offset szUnkContacts								;** Unknown contacts **
		mov sClcInfoItem.pszText, eax
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_ADDINFOITEM, 0, addr sClcInfoItem
		mov hItemUnk, eax
		
		invoke SetAllContactIcons, HC_CLIST									;Init control values
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
		invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
	    mov sDBGetSetting.szModule, offset szOModule			;load the flags
		mov sDBGetSetting.szSetting, offset szONotOnList
		mov sDBGetSetting.pValue, offset sDBVariant
		mov sDBVariant.VAR1, DBVT_DWORD
		invoke CallService, offset MS_DB_CONTACT_GETSETTING, 0, offset sDBGetSetting
		.IF eax==0
			.IF sDBVariant.VAR2!=0
				mov eax, 1
			.ENDIF
		.ELSE
			mov eax, 1
		.ENDIF
		push eax
		invoke SetUnkChildIcons, HC_CLIST, hItemUnk, 0, eax
		pop eax
		MAKELPARAM 0, eax
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_SETEXTRAIMAGE, hItemUnk, eax
	.ELSEIF uMsg==WM_IMGLIST
		invoke GetVersion			;check win version
		.IF al==5
			.IF ah!=0
				mov eax, ILC_COLOR32 or ILC_MASK
			.ELSEIF
				mov eax, ILC_COLOR16 or ILC_MASK
			.ENDIF
		.ENDIF
		invoke ImageList_Create, 16, 16, eax, 3, 3		;create image list
		mov hIml, eax
		invoke LoadIcon, hApp, IDI_SMALLDOT				;add icon
		push eax
		invoke ImageList_AddIcon, hIml, eax
		pop eax
		invoke DeleteObject, eax
		invoke CallService, offset MS_SKIN2_GETICON, 0, offset szIlName
		invoke ImageList_AddIcon, hIml, eax
		invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_SETEXTRAIMAGELIST, 0, hIml		;link image list to CList
	.ELSEIF uMsg==WM_SETFOCUS
		invoke SetFocus, HC_CLIST
	.ELSEIF uMsg==WM_COMMAND
		mov eax, wParam
		.IF lParam!=0
			.IF ax==IDC_SHOWHIDDEN
				mov sDBWriteSetting.szModule, offset szOModule
				mov sDBWriteSetting.szSetting, offset szOShowHidden
				mov sDBWriteSetting.VAR1, DBVT_DWORD
				invoke SendMessage, HC_SHOWHIDDEN, BM_GETCHECK, 0, 0
				.IF eax==BST_CHECKED
					mov sDBWriteSetting.VAR2, 1
					invoke GetWindowLong, HC_CLIST, GWL_STYLE
					or eax, CLS_SHOWHIDDEN
				.ELSE
					mov sDBWriteSetting.VAR2, 0
					invoke GetWindowLong, HC_CLIST, GWL_STYLE
					mov ebx, -CLS_SHOWHIDDEN
					dec ebx
					and eax, ebx
				.ENDIF
				invoke SetWindowLong, HC_CLIST, GWL_STYLE, eax
				invoke CallService, offset MS_DB_CONTACT_WRITESETTING, 0, offset sDBWriteSetting
				invoke SendMessage, HC_CLIST, CLM_SETUSEGROUPS, 1, 0
				invoke SetAllContactIcons, HC_CLIST
				invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
				invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
			.ENDIF
		.ENDIF
	.ELSEIF uMsg==WM_NOTIFY				;if sth changes
		mov edi, lParam
		assume edi:ptr NMHDR
		.IF [edi].idFrom==IDC_CLIST
			.IF [edi].code==CLN_NEWCONTACT
				invoke SetAllContactIcons, HC_CLIST
				invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
				invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
			.ELSEIF [edi].code==CLN_LISTREBUILT
				invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
				invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
			.ELSEIF [edi].code==CLN_CONTACTMOVED
				invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
				invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
			.ELSEIF [edi].code==CLN_OPTIONSCHANGED
				invoke ResetListOptions, HC_CLIST
			.ELSEIF [edi].code==NM_CLICK		;Click on the CList
				assume edi:ptr NMCLISTCONTROL
				.IF [edi].iColumn!=-1
					MAKELPARAM [edi].pt.x, [edi].pt.y
					invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_HITTEST, addr hitFlags, eax
					mov hItem, eax
					.IF hItem!=0
						and hitFlags, CLCHT_ONITEMEXTRA		;if click on icon, then analyze and edit
						.IF hitFlags!=0
							invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, hItem, [edi].iColumn
							.IF eax==0
								mov iImage, 1
							.ELSE
								mov iImage, 0
							.ENDIF
							MAKELPARAM [edi].iColumn, 0
							invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, hItem, eax
							.IF eax!= 0FFh
								MAKELPARAM [edi].iColumn, iImage
								invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_SETEXTRAIMAGE, hItem, eax
								invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETITEMTYPE, hItem, 0
								mov itemType, eax
								.IF itemType==CLCIT_CONTACT		;if click on contact
									MAKELPARAM [edi].iColumn, iImage
									invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_SETEXTRAIMAGE, hItem, eax
									mov sDBGetSetting.szModule, offset szOMetaContacts		;load settings
									mov sDBGetSetting.szSetting, offset szOMetaCount
									mov sDBGetSetting.pValue, offset sDBVariant
									mov sDBVariant.VAR1, DBVT_WORD
									invoke CallService, offset MS_DB_CONTACT_GETSETTING, hItem, offset sDBGetSetting
									.IF eax==0
										;is MetaContact
										invoke MetaConClick, hItem, sDBVariant.VAR2, iImage
									.ELSE
										mov sDBGetSetting.szSetting, offset szOMetaLink
										invoke CallService, offset MS_DB_CONTACT_GETSETTING, hItem, offset sDBGetSetting
										.IF eax==0
											;is subcontact
											invoke MetaSubClick, hItem
										.ENDIF
									.ENDIF
								.ENDIF
								.IF itemType==CLCIT_INFO		;if click on extra item
									mov eax, hItem
									.IF eax==hItemAll
										invoke SetAllChildIcons, HC_CLIST, hItem, [edi].iColumn, iImage
									.ELSEIF eax==hItemUnk
										invoke SetUnkChildIcons, HC_CLIST, hItem, [edi].iColumn, iImage
									.ENDIF
								.ENDIF
								.IF itemType==CLCIT_GROUP		;if click on group
									invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_CHILD, hItem
									mov hItem, eax
									.IF hItem!=0
										invoke SetAllChildIcons, HC_CLIST, hItem, [edi].iColumn, iImage
									.ENDIF
								.ENDIF
								invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETNEXTITEM, CLGN_ROOT, 0
								invoke SetListGroupIcons, HC_CLIST, eax, hItemAll, NULL
								invoke GetParent, dhWnd
								invoke GetParent, eax
								push eax
								invoke SendMessage, eax, PSM_CHANGED, 0, 0		;enable the Apply button
								pop eax
								invoke GetParent, eax
								invoke SendMessage, eax, PSM_CHANGED, 0, 0		;enable the Apply button
							.ENDIF
						.ENDIF
					.ENDIF
				.ENDIF
				assume edi:ptr NMHDR
			.ENDIF
		.ELSEIF [edi].idFrom==0
			.IF [edi].code==PSN_APPLY					;if OK/Apply pressed. Save options
				invoke CallService, addr MS_DB_CONTACT_FINDFIRST, 0, 0
				mov hContact, eax
				.WHILE hContact!=0
					invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_FINDCONTACT, hContact, 0
					mov hItem, eax
					.IF eax!=0
						xor eax, eax			;Use (MAKELPARAM "column", "image") instead of just 0
						invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, hItem, eax
						mov sDBWriteSetting.szModule, offset szOModule
						mov sDBWriteSetting.szSetting, offset szOEnabled
						mov sDBWriteSetting.VAR1, DBVT_DWORD
						mov sDBWriteSetting.VAR2, eax
						invoke CallService, offset MS_DB_CONTACT_WRITESETTING, hContact, offset sDBWriteSetting
					.ENDIF
					invoke CallService, addr MS_DB_CONTACT_FINDNEXT, hContact, 0
					mov hContact, eax
				.ENDW
				invoke SendDlgItemMessage, dhWnd, IDC_CLIST, CLM_GETEXTRAIMAGE, hItemUnk, 0
				mov sDBWriteSetting.szModule, offset szOModule
				mov sDBWriteSetting.szSetting, offset szONotOnList
				mov sDBWriteSetting.VAR1, DBVT_DWORD
				mov sDBWriteSetting.VAR2, eax
				invoke CallService, offset MS_DB_CONTACT_WRITESETTING, 0, offset sDBWriteSetting
			.ENDIF
		.ENDIF
		assume edi:nothing
	.ELSEIF uMsg==WM_DESTROY
		invoke ImageList_Destroy, hIml				;Free used resources
	.ENDIF
	xor eax, eax
	ret
Options2 endp

TabOptions proc, dhWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
	push dhWnd
	pop sDlgHdr.hDlg
	.IF uMsg==WM_INITDIALOG								;if user opens options
		mov ddInt, 0
		invoke InitCommonControls
		invoke	GetDlgItem, dhWnd, 1000					;get hWnd of tabs control
		mov sDlgHdr.hTab, eax
		mov edi, offset sTCITEM							;insert tabs
		mov eax, TCIF_TEXT
		mov [edi], eax
		invoke Translate, offset szTab1								;translate Tab name
		IFDEF _UNICODE
			mov sTCITEM.pszText, eax
			invoke SendMessage, sDlgHdr.hTab, TCM_INSERTITEMW, 0, addr sTCITEM
		ELSE
			mov sTCITEM.pszText, eax
			invoke SendMessage, sDlgHdr.hTab, TCM_INSERTITEMA, 0, addr sTCITEM
		ENDIF
		mov sDlgHdr.hDisplay, NULL
		
		invoke SetRectEmpty, addr sDlgHdr.rcDisplay
		invoke SendMessage, sDlgHdr.hTab, TCM_ADJUSTRECT, FALSE, addr sDlgHdr.rcDisplay

		invoke FindResource, hInstance, 101, RT_DIALOG	;load first tab in memory
		invoke LoadResource, hInstance, eax
		invoke LockResource, eax
		invoke CreateDialogIndirectParam, hInstance, eax, sDlgHdr.hTab, Options1, NULL
		mov sDlgHdr.hTab1, eax
		mov sDlgHdr.hDisplay, eax
		
		invoke Translate, offset szTab2								;translate Tab name
		IFDEF _UNICODE
			mov sTCITEM.pszText, eax
			invoke SendMessage, sDlgHdr.hTab, TCM_INSERTITEMW, 1, addr sTCITEM
		ELSE
			mov sTCITEM.pszText, eax
			invoke SendMessage, sDlgHdr.hTab, TCM_INSERTITEMA, 1, addr sTCITEM
		ENDIF
		invoke FindResource, hInstance, 102, RT_DIALOG	;load second tab in memory
		invoke LoadResource, hInstance, eax
		invoke LockResource, eax
		invoke CreateDialogIndirectParam, hInstance, eax, sDlgHdr.hTab, Options2, NULL
		mov sDlgHdr.hTab2, eax
		invoke ShowWindow, sDlgHdr.hTab2, SW_HIDE
		
		mov sLngPackDialog.cbSize, sizeof LANGPACKTRANSLATEDIALOG
		mov sLngPackDialog.flags, 0
		push dhWnd
		pop sLngPackDialog.hwndDlg
		mov sLngPackDialog.ignoreControls, 0
		invoke CallService, offset MS_LANGPACK_TRANSLATEDIALOG, LPTDF_NOTITLE, offset sLngPackDialog		;translate dialogs
		
	.ELSEIF uMsg==WM_NOTIFY
		.IF wParam==1000								;if message from tab control
			mov edi, lParam
			assume edi:ptr NMHDR
				.IF [edi].code==TCN_SELCHANGE			;if tab changed
					.IF sDlgHdr.hDisplay!=0
						invoke ShowWindow, sDlgHdr.hDisplay, SW_HIDE	;hide old tab
					.ENDIF
					invoke SendMessage, sDlgHdr.hTab, TCM_GETCURSEL, 0, 0	;get current tab and show it
					.IF eax==0
						mov eax, sDlgHdr.hTab1
					.ELSEIF eax==1
						mov eax, sDlgHdr.hTab2
					.ELSE
						invoke Error, 0
					.ENDIF
					mov sDlgHdr.hDisplay, eax
					invoke ShowWindow, sDlgHdr.hDisplay, SW_SHOW
				.ENDIF
			assume edi:nothing
		.ELSEIF
			invoke SendMessage, sDlgHdr.hTab1, WM_NOTIFY, wParam, lParam
			invoke SendMessage, sDlgHdr.hTab2, WM_NOTIFY, wParam, lParam
		.ENDIF
	.ENDIF
	xor eax, eax
	ret
TabOptions endp

Unload proc
    invoke UnhookEvent, hMsgHook			;unhook events and exit
    invoke UnhookEvent, hOptHook
    invoke UnhookEvent, hIcoLibEv
    xor eax, eax
    ret
Unload Endp

End DllMain