This file is indexed.

/usr/share/eiskaltdcpp/luascripts/startup.lua is in eiskaltdcpp-scripts 2.2.9-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
-- startup.lua: version 1.1.99/07
--
-- NMDC Listeners:
--   chat		= normal chat message (you won't get your own messages)
--   			  f( hub, user, "message" )
--   			  DISCARDABLE:
--   			    return non-nil to hide the msg from BCDC++,
--   			    all discardable functions also get an optional last
--   			    argument stating whether some previous listener returned non-nil
--   unknownchat= incoming messages displayed without any nick
--   			  f( hub, "message" )
--   			  DISCARDABLE
--   ownChat	= normal chat message from yourself (incoming from the hub)
--   			  f( hub, "message" )
--   			  DISCARDABLE
--   pm			= normal pm message
--   			  f( hub, user, "message" )
--   			  DISCARDABLE
--   hubPm		= pm message with a different prefix than the nick in the From field
--   			  f( hub, user, "message which may include a <nickname>" )
--   			  DISCARDABLE
--   userConnected = a user connected (or rather.. the first getUser call was made)
--   			  f( hub, user )
--   userMyInfo	= a myinfo message from a user, use this as "userConnected"
--   			  f( hub, user, "$MyINFO $ALL ... 1234$|" )
--   			  DISCARDABLE
--   userQuit	= a quit message from a user
--   			  f( hub, nick )
--   			  DISCARDABLE
--   raw		= a full message
--   			  f( hub, "line" )
--   			  DISCARDABLE (message won't get parsed.. no other listeners will be called)
-- ADC Listeners:
--   adcChat	= normal chat message (you won't get your own messages).
--   			  f( hub, user, "message", me_msg )
--   			  DISCARDABLE:
--   			    * return non-nil to hide the msg from BCDC++,
--   			    * all discardable functions also get an optional last
--   			      argument stating whether some previous listener returned non-nil
--					* me_msg is true if a /me-style message is sent
--   adcOwnChat	= normal chat message from yourself (incoming from the hub)
--   			  f( hub, "message", me_msg )
--   			  DISCARDABLE
--   adcPm		= normal pm message
--   			  f( hub, user, "message", me_msg )
--   			  DISCARDABLE
--   groupPm	= pm message with a different reply-sid than the one who talks (probably chatroom or bot)
--   			  f( hub, user, "message", reply_sid, me_msg )
--   			  DISCARDABLE
--   userInf	= an INF message from a user
--   			  f( hub, user, flags )
--					flags is a table where all named flags are stored (ie: flags["NI"], flags["I4"], ...)
--   			  DISCARDABLE
--   adcUserCon = a user connected (or rather.. the first getUser call was made)
--   			  f( hub, user )
--   adcUserQui	= a quit message from a user
--   			  f( hub, sid, flags )
--					flags is a table where all named flags are stored (ie: flags["ID"], flags["MS"], ...)
-- Common Listeners:
--   ownChatOut	= normal chat message from yourself (outgoing to the hub)
--   			  f( hub, "message" )
--   			  DISCARDABLE
--   connected	= connecting to a hub (many functions may be unavailable (return nil))
--   			  f( hub )
--   disconnected = disconnected from a hub
--   			  f( hub )
--   timer		= called every second (if DC():RunTimer(1) is called), use
--   			  dcpp:getHubs()[k]:getAddress() or :getUrl() to select a hub
--   			  f()
--   clientIn	= peer to peer line oriented transfer control input, userp is a pointer
--   			  (lightuserdata)
--   			  f( userp, "line" )
--   			  DISCARDABLE (returning non-nil kills the connection)
--   clientOut	= peer to peer line oriented transfer control output, userp is a pointer
--   			  (lightuserdata)
--   			  f( userp, "line" )
--   			  DISCARDABLE (returning non-nil kills the connection)
--
-- Example listener (load _after_ this script):
--		dcpp:setListener( "chat", "bier",
--			function( hub, user, text )
--				local s = string.lower( text )
--				if string.find( s, "[^a-z]bier+[^a-z]" ) or string.find( s, "[^a-z]biertje[^a-z]" ) then
--					hub:sendChat( "bier? ja lekker! :)" )
--				end
--			end
--		)
--
-- If you want to remove the "bier" chat listener, simply type:
-- /lua dcpp:setListener( "chat", "bier" )
-- in any chat window


DC():PrintDebug( "** Started startup.lua **" )

--/////////////////////////////////////
--// Helper functions
--/////////////////////////////////////

if not dcu then
	dcu = {}
end

dcu.NmdcEscape = function(this, msg )
	msg = string.gsub( msg, "%$", "&#36;")
	msg = string.gsub( msg, "|", "&#124;")
	return msg
end

dcu.AdcEscape = function(this, msg, inverse)
	msg = string.gsub(msg, "\r", "")
	local ret = ""
	if inverse then
		local replacetable = {}
		replacetable["\\\\"] = [[\]]
		replacetable["\\s"] = [[ ]]
		replacetable["\\n"] = "\n"
		local skip = false
		for k = 1, string.len(msg) do
			if skip then
				skip = false
			else
				local c = string.sub( msg, k, k + 1)
				if replacetable[c] then
					ret = ret .. replacetable[c]
					skip = true
				else
					ret = ret .. string.sub(c, 1, 1)
				end
			end
		end
	else
		local replacetable = {}
		replacetable["\\"] = [[\\]]
		replacetable[" "] = [[\s]]
		replacetable["\n"] = [[\n]]

		for k = 1, string.len(msg) do
			local c = string.sub( msg, k, k)
			if replacetable[c] then
				ret = ret .. replacetable[c]
			else
				ret = ret .. c
			end
		end
	end
	return ret
end

--// Checks if the given decimal number has the 2^exp bit set
dcu.BBit = function(this, num, exp)
	local ret = false
	if string.find(tostring(math.floor(num / 2^exp)), "[13579]$") then
		ret = true
	end
	return ret
end

--/////////////////////////////////////
--// Hub manager
--/////////////////////////////////////

if not dcpp or dcpp._init_me_anyway == true then
	dcpp = {}
	dcpp._hubs = {}
	dcpp._listeners = {}
end

dcpp.addHub = function( this, hub, isitadc )
	if not this._hubs[hub] then
		-- DC():PrintDebug( "Hub added (id = "..tostring( hub )..")" )
		if isitadc then
			this._hubs[hub] = createAdcHub( hub )
			DC():PrintDebug( "ADC hub added (id = " .. tostring( hub ) ..")" )
		else
			this._hubs[hub] = createNmdcHub( hub )
		end
		for k,f in pairs(dcpp:getListeners( "connected" )) do
			f( this._hubs[hub] )
		end
		return this._hubs[hub]
	else
		--DC():PrintDebug( "Tried to add existing hub on: "..this._hubs[hub]:getHubName()..
		--						" (id = "..tostring( hub )..")" )
		return nil
	end
end

dcpp.getHub = function( this, hub )
	if this._hubs[hub] then
		return this._hubs[hub]
	else
		--DC():PrintDebug( "Tried to fetch an unknown hub (id = "..tostring( hub )..")" )
		return this:addHub( hub ):setPartial() -- tell the object that we missed the logon
	end
end

dcpp.getHubs = function( this )
	return this._hubs
end

dcpp.hasHub = function( this, hub )
	if this._hubs[hub] then
		return 1
	else
		return nil
	end
end

dcpp.findHub = function( this, url )
	for k, h in pairs(this._hubs) do
		if h:getUrl() == url then
			return h
		end
	end
	return false
end

dcpp.removeHub = function( this, hub )
	if this._hubs[hub] then
		--DC():PrintDebug( "Hub removed: "..this._hubs[hub]:getHubName().." (id = "..tostring( hub )..")" )
		for k,f in pairs(dcpp:getListeners( "disconnected" )) do
			f( this._hubs[hub] )
		end
		this._hubs[hub]:destroy()
		this._hubs[hub] = nil
	else
		--DC():PrintDebug( "Tried to remove non-existent hub (id = "..tostring( hub )..")" )
	end
end

dcpp.listHubs = function( this )
	DC():PrintDebug( "** Listing hubs **" )
	for k,v in pairs(this._hubs) do
		DC():PrintDebug( tostring( k ).." ("..v:getHubName()..")" )
	end
end

dcpp.setListener = function( this, ltype, id, func )
	if not dcpp._listeners[ltype] then
		dcpp._listeners[ltype] = {}
	end
	dcpp._listeners[ltype][id] = func
end

dcpp.getListeners = function( this, ltype )
	if dcpp._listeners[ltype] then
		return dcpp._listeners[ltype]
	else
		return {}
	end
end


--/////////////////////////////////////
--// Hub object
--/////////////////////////////////////

function createAdcHub( hubid )
	local hub = {}

	hub._id = hubid
	hub._mySID = nil
	hub._myNick = nil
	hub._myCID = nil
	hub._nick = nil
	hub._description = nil
	hub._users = {}
	hub._uptime = os.time()

	hub.getId = function( this )
		return this._id
	end

	hub.getUptime = function( this )
		return ( os.time() - hub._uptime ) / 60
	end

	hub.getProtocol = function( this )
		return "adc"
	end

	hub.getAddress = function( this )
		return DC():GetHubIpPort( this._id )
	end

	hub.getHubName = function( this )
		--// TODO: Shall we return with NI or DE? or both?
		--//       For now just returning the nick with the url
		if this._nick then
			return this._nick  .. " ("..this:getUrl()..")"
		else
			return this:getUrl()
		end
	end

	hub.getUrl = function( this )
		return DC():GetHubUrl( this._id )
	end

	hub.getOwnNick = function( this )
		return this._myNick
	end

	hub.setOwnNick = function( this, newnick )
		this._myNick = newnick
		DC():PrintDebug("[STATUS] Own nick set: " .. newnick )
	end

	hub.getOwnSid = function( this )
		if this._mySID then
			return this._mySID
		end
		DC():PrintDebug( "[" .. this:getUrl() .."] Own SID not set, your scripts are failing. Reconnect please." )
	end

	hub.getOwnCid = function( this )
		if this._myCID then
			return this._myCID
		end
		DC():PrintDebug( "[" .. this:getUrl() .."] Own CID not set, your scripts are failing. Reconnect please." )
	end

	hub.getUser = function( this, sid, inf )
		local newuser = false
		if sid == "HUB9" and inf then
			-- INF is about the hub itself
			local params = {}
			string.gsub(inf, "([^ ]+)", function(s) table.insert(params, s) end )
			for k in pairs(params) do
				local name = string.sub( params[k], 1, 2 )
				if name == "NI" then
					local NI = dcu:AdcEscape(string.sub( params[k], 3), true)
					this._nick = NI
					DC():PrintDebug("[STATUS] HUB nick set: " .. NI )
				elseif name == "DE" then
					local DE = dcu:AdcEscape(string.sub( params[k], 3), true)
					this._description = DE
					DC():PrintDebug("[STATUS] HUB description set: " .. DE )
					this:addLine("The current hub topic is: " .. DE)
				end
			end
		end

		if not this._users[sid] then
			this._users[sid] = createAdcUser( this, sid )
			newuser = true
			if this:getUptime() >= 2 then -- start sending messages AFTER logon
	            for k,f in pairs(dcpp:getListeners( "adcUserCon" )) do
					f( this, this._users[sid] )
				end
			end
		end
		local r = this._users[sid]
		if inf then
			local params = {}
			string.gsub(inf, "([^ ]+)", function(s) table.insert(params, s) end )
			for k in pairs(params) do
				local name = string.sub( params[k], 1, 2 )
				if name == "ID" then
					local ID = string.sub( params[k], 3)
					r:setCid(ID)
					if sid == this._mySID then
						this._myCID = ID
						DC():PrintDebug("[STATUS] Own CID set: " .. ID )
					end
				elseif name == "CT" then
					local CT = string.sub( params[k], 3)
					r:processCt(CT)
				elseif name == "I4" then
					local I4 = string.sub( params[k], 3)
					r:setIp(I4)
				elseif name == "NI" then
					local NI = dcu:AdcEscape(string.sub( params[k], 3), true)
					if not newuser then
						local oldNI = r:getNick()
						r:setNick(NI)
						this:addLine( oldNI .. " is now known as " .. NI )
					else
						r:setNick(NI)
					end
					if sid == this._mySID then
						this:setOwnNick(NI)
					end
				end
			end
		end
		return r
	end

	hub.removeUser = function( this, sid )
		this._users[sid] = nil
	end

	hub.isOp = function( this, sid )
		if this._users[sid] then
			return this._users[sid]._op
		end
		return nil
	end

	hub.getSidbyNick = function( this, nick )
		for k in pairs(this._users) do
			if this._users[k]._nick == nick then
				return k
			end
		end
		return nil
	end

	hub.getSidbyCid = function( this, cid )
		for k in pairs(this._users) do
			if this._users[k]._cid == cid then
				return k
			end
		end
		return nil
	end

	hub.getUserByCid = function( this, cid )
		for k in pairs(this._users) do
			if this._users[k]._cid == cid then
				return this._users[k]
			end
		end
		return nil
	end

	hub.sendChat = function( this, msg )
		local ownSid = this:getOwnSid()
		msg = dcu:AdcEscape( msg )
		DC():SendHubMessage( this:getId(), "BMSG " .. ownSid .. " " .. msg .. "\n" )
	end

	hub.injectChat = function( this, msg )
			DC():PrintDebug("[WARNING] Your scripts trying to use hub:injectChat on ADC hub. Please use hub:injectMessage() to inject an ADC message or hub:addLine() to inject a chat line." )
	end

	hub.injectMessage = function( this, msg )
		DC():InjectHubMessageADC( this:getId(), msg )
	end

	hub.addLine = function( this, msg, fmt )
		--// TODO: need a function which adds a chat line without nick
		msg = dcu:AdcEscape( msg )
		DC():InjectHubMessageADC( this:getId(), "ISTA 000 " .. msg )
	end

	hub.sendPrivMsgTo = function( this, victimSid, msg_unescaped, hideFromSelf )
		local ownSid = this:getOwnSid()
		local msg = dcu:AdcEscape( msg_unescaped )
		if ownSid then
			if hideFromSelf then
				DC():SendHubMessage( this:getId(), "DMSG ".. ownSid .. " " .. victimSid .." " .. msg .. " PM" .. ownSid .."\n" )
			else
				DC():SendHubMessage( this:getId(), "EMSG ".. ownSid .. " " .. victimSid .." " .. msg .. " PM" .. ownSid .."\n" )
			end
		end
	end

	hub.injectPrivMsg = function( this, victimSid, fromSid, msg )
		DC():InjectHubMessageADC( this:getId(), "DMSG " .. fromSid .." ".. victimSid .." ".. msg .. " PM" .. victimSid .. "\n" )
	end

	hub.findUsers = function( this, nick, notag )
		-- you get a possibly empty table of users
		if not notag then
			return { this._users[this:getSidbyNick(nick)] }
		else
			local list = {}
			for k in pairs(this._users) do
				local ret,c,n = string.find( this._users[k]._nick, "^%[.*%](.-)$" )
				if n == nick then
					table.insert( list, this._users[k] )
				end
			end
			return list
		end
	end

	hub.destroy = function( this )
	end

	--// events

	hub.onChatMessage = function( this, user, text, me_msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "adcChat" )) do
			ret = f( this, user, text, me_msg, ret ) or ret
		end
		return ret
	end

	hub.onChatFromSelf = function( this, text, me_msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "adcOwnChat" )) do
			ret = f( this, text, me_msg, ret ) or ret
		end
		return ret
	end

	hub.onINF = function( this, user, flags )
		local ret
		for k,f in pairs(dcpp:getListeners( "userInf" )) do
			ret = f( this, user, flags, ret ) or ret
		end
		return ret
	end

	hub.onQUI = function( this, sid, flags )
		for k,f in pairs(dcpp:getListeners( "adcUserQui" )) do
			f( this, sid, flags )
		end
	end

	hub.onPrivateMessage = function( this, user, targetSid, replySid, text, me_msg )
		if targetSid == this:getOwnSid() then
			if user:getSid() == replySid then
				local ret
				for k,f in pairs(dcpp:getListeners( "adcPm" )) do
					ret = f( this, user, text, me_msg, ret ) or ret
				end
				return ret
			else
				local ret
				for k,f in pairs(dcpp:getListeners( "groupPm" )) do
					ret = f( this, user, replySid, text, me_msg, ret ) or ret
				end
				return ret
			end
		end
	end

	hub.attention = function( this )
		DC():HubWindowAttention( this:getId() )
	end

	return hub
end

function createNmdcHub( hubid )
	local hub = {}

	hub._id = hubid
	hub._users = {}
	hub._name = nil
	hub._myNick = nil
	hub._partial = nil
	hub._gotOpList = nil
	hub._uptime = os.time()
	hub._hubUC = {}
	hub._customUC = {}

	hub.getId = function( this )
		return this._id
	end

	hub.getProtocol = function( this )
		return "nmdc"
	end

	hub.getUptime = function( this )
		return ( os.time() - hub._uptime ) / 60
	end

	hub.getUser = function( this, nick, op )
		if not this._users[nick] then
			this._users[nick] = createNmdcUser( this, nick )
			if this:getUptime() >= 2 then -- start sending messages AFTER logon
	            for k,f in pairs(dcpp:getListeners( "userConnected" )) do
					f( this, this._users[nick] )
				end
			end
		end
		local r = this._users[nick]
		if op then
			r:setOp( true )
			r:setClass( "op" )
			this._gotOpList = 1
		end
		return r
	end

	hub.findUsers = function( this, nick, notag )
		-- you get a possibly empty table of users
		if not notag then
			return { this._users[nick] }
		else
			local list = {}
			for k,v in pairs(this._users) do
				local ret,c,n = string.find( k, "^%[.*%](.-)$" )
				if n == nick then
					table.insert( list, v )
				end
			end
			return list
		end
	end

	hub.gotOpList = function( this )
		return this._gotOpList
	end

	hub.isOp = function( this, nick )
		if this._users[nick] and this._users[nick]:isOp() then
			return 1
		end
	end

	hub.removeUser = function( this, nick )
		this._users[nick] = nil
	end

	hub.setPartial = function( this )
		--// we're most likely missing logon info
		this._partial = 1
	end

	hub.isPartial = function( this )
		--// are we missing logon info?
		return this._partial
	end

	hub.setHubName = function( this, msg )
		this._name = msg
	end

	hub.getAddress = function( this )
		return DC():GetHubIpPort( this._id )
	end

	hub.getUrl = function( this )
		return DC():GetHubUrl( this._id )
	end

	hub.getHubName = function( this )
		if this._name then
			return this._name.." ("..this:getUrl()..")"
		else
			return this:getUrl()
		end
	end

	hub.setOwnNick = function( this, nick )
		this._myNick = nick
	end

	hub.getOwnNick = function( this )
		if not this:isPartial() then
 			return this._myNick
 		end
		DC():PrintDebug( "[" .. this:getUrl() .."] Your scripts are failing. "..
					"Own nick not set. Reconnect please." )
	end

	hub.destroy = function( this )
	end

	hub.sendChat = function( this, msg )
		local ownNick = this:getOwnNick()
		msg = dcu:NmdcEscape( msg )
		if ownNick then
			DC():SendHubMessage( this:getId(), "<"..ownNick.."> "..msg.."|" )
		end
	end

	hub.injectChat = function( this, msg, skipusercommand )
			DC():InjectHubMessage( this:getId(), msg )
			-- test if it's usercommand
			if ( string.sub(msg, 1, 13) == "$UserCommand " ) and (not skipusercommand) then
				this:customUC( msg )
			end
	end

	hub.addLine = function( this, msg, fmt )
		if not fmt then
			msg = "*** " .. msg
		end
		DC():InjectHubMessage( this:getId(), msg )
	end

	hub.sendPrivMsgTo = function( this, victim, msg, hideFromSelf )
		local ownNick = this:getOwnNick()
		msg = dcu:NmdcEscape( msg )
		if ownNick then
			DC():SendHubMessage( this:getId(), "$To: "..victim.." From: "..ownNick.." $"..msg.."|" )
			if not hideFromSelf then
				this:injectPrivMsg( victim, ownNick, msg )
			end
		end
	end

	hub.injectPrivMsg = function( this, from, to, msg )
			DC():InjectHubMessage( this:getId(), "$To: "..to.." From: "..from.." $"..msg )
	end

	hub.injectPrivMsgFmt = function( this, from, to, msg )
		this:injectPrivMsg( from, to, "<"..from.."> "..msg )
	end

	hub.attention = function( this )
		-- TODO old method was broken, probably worthwhile functionality though
	end

	--///////////////////////////
	--// $UserCommand manager
	--///////////////////////////
	-- Todo: context sensitivitiy at UC 255

	hub.hubUC = function( this, msg )
		local uc_type = string.gsub( msg, "%$UserCommand (%d+) .+", "%1")
		-- DC():PrintDebug( "UserCommand " .. uc_type .. " arrived from " .. this:getHubName() .. " : " .. msg)
		if uc_type == "255" then
			-- clear user command list and resend our own commands to prevent it from disappearing
			-- DC():PrintDebug( "Clearing Hub UC list.." )
			this:clearHubUCList()
			-- DC():PrintDebug( "Resending Custom commands.." )
			this:reSendCustomUC()
		else
			-- add usercommands to UClist
			-- DC():PrintDebug( "Adding hubUC.." )
			this:addHubUC( msg )
		end

		return true
	end

	hub.customUC = function ( this, msg )
		local uc_type = string.gsub( msg, "%$UserCommand (%d+) .+", "%1")
		-- DC():PrintDebug( "UserCommand " .. uc_type .. " arrived from a script: " .. msg)
		if uc_type == "255" then
			-- clear own user command list and resend hub usercommands to prevent it from disappearing
			this:clearCustomUCList()
			this:reSendHubUC()
		else
			-- add usercommands to UClist
			this:addCustomUC( msg )
		end
		return true
	end

	hub.addHubUC = function( this, msg )
		table.insert( this._hubUC, msg )
		return true
	end

	hub.addCustomUC = function( this, msg)
		table.insert( this._customUC, msg )
		return true
	end

	hub.clearHubUCList = function( this )
		this._hubUC = {}
		return true
	end

	hub.clearCustomUCList = function( this )
		this._customUC = {}
		return true
	end

	hub.reSendCustomUC = function( this )
		for k in pairs(this._customUC) do
			hub:injectChat( this._customUC[k] , true )
			-- DC():PrintDebug("Sending custom uc from stored table: " .. this._customUC[k] )
		end
		return true
	end

	hub.reSendHubUC = function( this )
		for k in pairs(this._hubUC) do
			hub:injectChat( this._hubUC[k] , true)
		end
		return true
	end

	--////////////////
	--// Own functions
	--////////////////

	hub.onRaw = function( this, msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "raw" )) do
			ret = f( this, msg ) or ret
		end
		return ret
	end

	hub.onSearch = function( this, msg )
		--DC():PrintDebug( this:getHubName().."> "..msg )
	end

	hub.onHello = function( this, user, msg )
	end

	hub.onMyInfo = function( this, user, msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "userMyInfo" )) do
			ret = f( this, user, msg, ret ) or ret
		end
		return ret
	end

	hub.onQuit = function( this, nick, msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "userQuit" )) do
			ret = f( this, nick, ret ) or ret
		end
		return ret
	end

	hub.onHubName = function( this, hubname, msg )
	end

	hub.onPrivateMessage = function( this, user, to, prefix, text, full )
		-- DC():PrintDebug("user: " .. tostring(user) .. " to: " .. tostring(to) .. " prefix: " .. tostring(prefix) .. " text: " .. tostring(text) .. " full: " .. tostring(full) )
		if to == this:getOwnNick() then
			if prefix == "<"..user:getNick().."> " then
				local ret
				for k,f in pairs(dcpp:getListeners( "pm" )) do
					ret = f( this, user, text, ret ) or ret
				end
				return ret
			elseif not prefix then
				local ret
				for k,f in pairs(dcpp:getListeners( "hubPm" )) do
					ret = f( this, user, text, ret ) or ret
				end
				return ret
			else
				local ret
				for k,f in pairs(dcpp:getListeners( "hubPm" )) do
					ret = f( this, user, prefix .. text, ret ) or ret
				end
				return ret
			end
		end
	end

	hub.onChatMessage = function( this, user, text )
		local ret
		for k,f in pairs(dcpp:getListeners( "chat" )) do
			ret = f( this, user, text, ret ) or ret
		end
		return ret
	end

	hub.onUnknownChatMessage = function( this, text )
		local ret
		for k,f in pairs(dcpp:getListeners( "unknownchat" )) do
			ret = f( this, text, ret ) or ret
		end
		return ret
	end

	hub.onChatFromSelf = function( this, text )
		local ret
		for k,f in pairs(dcpp:getListeners( "ownChat" )) do
			ret = f( this, text, ret ) or ret
		end
		return ret
	end

	return hub
end


--/////////////////////////////////////
--// User object
--/////////////////////////////////////

function createNmdcUser( hub, nick )
	local user = {}

	user._hub = hub
	user._nick = nick
	user._op = false
	user._ip = ""
	user._class = "user"
	user._handled_messages = {} -- flood protection

	user.getProtocol = function( this )
		return "nmdc"
	end

	user.setOp = function( this, op )
		this._op = op
		return this
	end

	user.isOp = function( this )
		return this._op
	end

	user.setClass = function( this, param )
		this._class = param
		return this
	end

	user.getClass = function( this )
		return this._class
	end

	user.setIp = function( this, ip )
		this._ip = ip
	end

	user.getIp = function( this )
		return this._ip
	end

	user.getNick = function( this )
		return this._nick
	end

	user.sendPrivMsgFmt = function( this, msg, hideFromSelf )
		local ownNick = this._hub:getOwnNick()
		if ownNick then
			this._hub:sendPrivMsgTo( this._nick, "<"..ownNick.."> "..msg, hideFromSelf )
		end
		return this
	end

	user.setMsgHandled = function( this, which )
		this._handled_messages[which] = 1
		return this
	end

	user.msgHandled = function( this, which )
		return this._handled_messages[which]
	end

	return user
end

function createAdcUser( hub, sid )
	local user = {}

	user._sid = sid
	user._cid = ""
	user._hub = hub
	user._nick = ""
	user._op = false
	user._bot = false
	user._registered = false
	user._hubitself = false
	user._class = "user"
	user._ip = ""
	user._handled_messages = {} -- flood protection

	user.getProtocol = function( this )
		return "adc"
	end

	user.setOp = function( this, op )
		this._op = op
		return this
	end

	user.isOp = function( this )
		return this._op
	end

	user.processCt = function( this, param )
		local num = tonumber(param)
		if num then

			--// Init
			this._class = "user"

			--// 2^0: bot
			if dcu:BBit(num, 0) then
				this:setBot(true)
				DC():PrintDebug("BOT set: " .. this:getSid())
			else
				this:setBot(false)
			end

			--// 2^1: registered
			if dcu:BBit(num, 1) then
				this:setReg(true)
			else
				this:setReg(false)
			end

			--// 2^2, 2^3, 2^4: some type of operator
			if dcu:BBit(num, 2) or dcu:BBit(num, 3) or dcu:BBit(num, 4) then
				this:setOp(true)
				if dcu:BBit(num, 2) then
					this._class = "op"
				elseif dcu:BBit(num, 3) then
					this._class = "su"
				else
					this._class = "owner"
				end
			else
				this:setOp(false)
			end

			if dcu:BBit(num, 5) then
				this:setHub(true)
				this._class = "hub"
			else
				this:setHub(false)
			end

		else
			--// Empty CT field should cause all properties gone
			this:setBot(false)
			this:setOp(false)
			this:setReg(false)
			this:setHub(false)
			this._class = "user"
		end

		--// DC():PrintDebug("CLASS: " .. this._class .. " [" .. tostring(param) .. "]" )
		return this
	end

	--// Possible values: "user", "op", "su", "owner", "hub"
	user.getClass = function( this )
		return this._class
	end

	user.setBot = function(this, bot)
		this._bot = bot
		return this
	end

	user.isBot = function(this)
		return this._bot
	end

	user.setReg = function(this, registered)
		this._registered = registered
		return this
	end

	user.isReg = function(this)
		return this._registered
	end

	user.setHub = function(this, hubitself)
		this._hubitself = hubitself
		return this
	end

	user.isHub = function(this)
		return this._hubitself
	end

	user.setIp = function( this, ip )
		this._ip = ip
	end

	user.getIp = function( this )
		return this._ip
	end

	user.setCid = function( this, cid )
		this._cid = cid
	end

	user.getCid = function( this )
		return this._cid
	end

	user.getSid = function( this )
		return this._sid
	end

	user.getNick = function( this )
		return this._nick
	end

	user.setNick = function( this, nick )
		this._nick = nick
		-- DC():PrintDebug("Nick set: " .. nick )
	end

	user.sendPrivMsg = function( this, msg, hideFromSelf )
		local victimSid = this:getSid()
		this._hub:sendPrivMsgTo( victimSid, msg, hideFromSelf )
		return this
	end

	-- Backward compatibility
	user.sendPrivMsgFmt = user.sendPrivMsg

	user.setMsgHandled = function( this, which )
		this._handled_messages[which] = 1
		return this
	end

	user.msgHandled = function( this, which )
		return this._handled_messages[which]
	end

	return user
end


--/////////////////////////////////////
--// Handlers
--/////////////////////////////////////

nmdch = {}

function nmdch.DataArrival( hub, msg )
	local h = dcpp:getHub( hub )

	--// If we missed the logon, we really have no business here,
	--// modify only if you really have to.
	--// Note that if not h:isPartial and not h:getOwnNick(),
	--// functions requiring an ownNick will silently fail.
	if h:isPartial() then
		return
	end

	--// raw/unparsed message
	if h:onRaw( msg ) then
		return 1
	end

	--// parse message and fire appropriate
	local ret,c,cmd = string.find( msg, "^%$([^ ]+)" )
	if ret then
		if cmd == "Search" then
			return h:onSearch( msg )
		elseif cmd == "Hello" then
			local nick = string.sub( msg, 8 )
			if not h:getOwnNick() then
				h:setOwnNick( nick ) -- don't trust this nick on h:isPartial()
			end
			return h:onHello( h:getUser( nick ), msg )
		elseif cmd == "MyINFO" and string.sub( msg, 1, 13 ) == "$MyINFO $ALL " then
			local nick = string.sub( msg, 14, string.find( msg, " ", 14, 1 ) - 1 )
			return h:onMyInfo( h:getUser( nick ), msg )
		elseif cmd == "Quit" then
			local nick = string.sub( msg, 7 )
			h:removeUser( nick )
			return h:onQuit( nick, msg )
		elseif cmd == "HubName" then
			local hubname = string.sub( msg, 10 )
			h:setHubName( hubname )
			return h:onHubName( hubname, msg )
		elseif cmd == "OpList" then
			for nick in string.gfind( string.sub( msg, 9 ), "[^$]+") do
				h:getUser( nick, 1 )
			end
			return nil
		elseif cmd == "UserIP" then
			local nick,ip
			for combo in string.gfind( string.sub( msg, 9 ), "[^$]+") do
				ret,c,nick,ip = string.find( combo, "^(%S+) (%S+)$" )
				if ret then
					h:getUser( nick ):setIp( ip )
				end
			end
			return nil
		elseif cmd == "UserCommand" then
			h:hubUC( msg )
		--elseif string.sub( msg, 1, 10 ) == "$NickList " then
		--	for nick in string.gfind( string.sub( msg, 9, -1), "[^$]+") do
		--		h:getUser( nick )
		--	end
		--	return nil
		elseif cmd == "To:" then
			local ret,c,to,from,fulltext = string.find( msg, "^%$To: ([^ ]+) From: ([^ ]+) %$(.*)$" )
			if ret then
				local ret,c,prefix,text = string.find( fulltext, "^(%b<> )(.*)$" )
				if ret then
					return h:onPrivateMessage( h:getUser( from ), to, prefix, text, msg )
				else
					return h:onPrivateMessage( h:getUser( from ), to, nil, fulltext, msg )
				end
			end
		end
	elseif string.sub( msg, 1, 1 ) == "<" then
		local ret,c,nick,text = string.find( msg, "^<([^>]+)> (.*)$" )
		if ret and h:getOwnNick() then
			if nick ~= h:getOwnNick() then -- don't be flooding mainchat now..
 				return h:onChatMessage( h:getUser( nick ), text )
			else
				return h:onChatFromSelf( text )
			end
		end
	elseif msg ~= "" then
		return h:onUnknownChatMessage(msg)
	end
end

function dcpp.UserDataIn( user, msg )
	local ret
	for k,f in pairs(dcpp:getListeners( "clientIn" )) do
		ret = f( user, msg ) or ret
	end
	return ret
end

function dcpp.UserDataOut( user, msg )
	local ret
	for k,f in pairs(dcpp:getListeners( "clientOut" )) do
		ret = f( user, msg ) or ret
	end
	return ret
end

function dcpp.OnCommandEnter( hub, text )
	local h = dcpp:getHub( hub )
	local ret
	for k,f in pairs(dcpp:getListeners( "ownChatOut" )) do
		ret = f( h, text, ret ) or ret
	end
	return ret
end

function dcpp.OnTimer()
	-- Called every second.
	for k,f in pairs(dcpp:getListeners( "timer" )) do
		f()
	end
end

function nmdch.OnHubAdded( hub )
	dcpp:addHub( hub, false )
end

function nmdch.OnHubRemoved( hub )
	dcpp:removeHub( hub )
end

adch = {}

function adch.DataArrival( hub, msg )
	if msg == "" then
		return nil
	end
	local h = dcpp:getHub( hub )

	local params = {}
	string.gsub(msg, "([^ ]+)", function(s) table.insert(params, s) end )

	local mtype = string.sub(params[1], 1, 1)
	local cmd = string.sub(params[1], 2, 4)
	local sid, targetSid, parameters = false, false, ""

	if mtype == "I" then
		parameters = string.sub( msg, 6)
		sid = "HUB9"
	elseif mtype == "B" then
		parameters = string.sub( msg, 11)
		sid = params[2]
	elseif mtype == "D" or mtype == "E" then
		parameters = string.sub( msg, 16 )
		sid = params[2]
		targetSid = params[3]
	end

	-- Building flags table
	local flags = {}
	string.gsub(parameters, "([^ ]+)", function(s) flags[string.sub(s, 1, 2)] = dcu:AdcEscape( string.sub(s, 3), true ) end )

	if cmd == "SID" then
		DC():PrintDebug( "[STATUS] SID received: " .. params[2] )
		h._mySID = params[2]
	elseif cmd == "QUI" then
		h:removeUser( params[2] )
		h:onQUI( params[2], flags )
	elseif cmd == "INF" then
		local u = h:getUser( sid, parameters )
		return h:onINF( u, flags )
	elseif cmd == "MSG" then
		local pm, replySid, me_msg = false, false, false
		local tmp = {}

		string.gsub(parameters, "([^ ]+)", function(s) table.insert(tmp, s) end )
		local text = dcu:AdcEscape(tmp[1], true)
		tmp[1] = nil

		-- Check for named parameters
		for k in pairs(tmp) do
			local name = string.sub(tmp[k], 1,2)
			local value = string.sub(tmp[k], 3)
			if name == "ME" then
				if value == "1" then
					me_msg = true
				end
			elseif name == "PM" then
				pm = true
				replySid = value
			end
		end

		if pm then
			return h:onPrivateMessage( h:getUser( sid ), targetSid, replySid, text, me_msg )
		else
			if sid ~= h:getOwnSid() then -- don't be flooding mainchat now..
				return h:onChatMessage( h:getUser( sid ), text, me_msg )
			else
				return h:onChatFromSelf( text, me_msg )
			end
		end

	end
end

function adch.OnHubAdded( hub )
	dcpp:addHub( hub, true )
end

function adch.OnHubRemoved( hub )
	dcpp:removeHub( hub )
end

--/////////////////////////////////////
--// Utility functions
--/////////////////////////////////////

function SendActiveSearchResult( hub, ip_port, search_nick, filename, filesize, open_slots,
		total_slots )
	DC():SendUDP( ip_port, "$SR "..search_nick.." "..filename.."\005".. filesize.." "..open_slots..
			"/"..total_slots.."\005"..dcpp:getHub( hub ):getHubName() ) -- no pipe in UDP $SR
end


--/////////////////////////////////////
--// Execute your own scripts
--/////////////////////////////////////

-- do you need the timer?
--DC():RunTimer(1)

--dofile( DC():GetScriptsPath() .. "bier.lua" )
--dofile( DC():GetScriptsPath() .. "slots.lua" )
--dofile( DC():GetScriptsPath() .. "formatting.lua" )
dofile( DC():GetScriptsPath() .. "uptime.lua" )
--dofile( DC():GetScriptsPath() .. "onjoin.lua" )
--dofile( DC():GetScriptsPath() .. "monologue.lua" )
dofile( DC():GetScriptsPath() .. "ignore.lua" )
--dofile( DC():GetScriptsPath() .. "p2pblock.lua" )
--dofile( DC():GetScriptsPath() .. "quiet_login.lua" )
--dofile( DC():GetScriptsPath() .. "log.lua" )
--dofile( DC():GetScriptsPath() .. "kickfilter.lua" )
dofile( DC():GetScriptsPath() .. "adccommands.lua" )