-- Startup.lua version 1.1.1
--
-- vim:ts=4:sw=4:noet
--
--
-- 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, "All inf-flags separated by spaces" )
--   			  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 separated by spaces (if any available)" )
-- 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 dcUtil then
	dcUtil = {}
end

-- This function is intended to remove the $ and pipe characters from private and chat messages
-- because they can cause a lot of problem.
function dcUtil.escaping( message )
	message = string.gsub( message, "%$", "&#36;")
	message = string.gsub( message, "|", "&#124;")
	return message
end

function dcUtil.escapeAdc( msg, inverse )
	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

--/////////////////////////////////////
--// 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.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 )
		-- shall we return with NI or DE? or both?
		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.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 )
		if sid == "_HUB" and inf then
			-- INF is about the hub itself
			local params = {}
			string.gsub(inf, "([%S\r]+)", function(s) table.insert(params, s) end )
			local NI, DE = nil, nil
			for k in pairs(params) do
				local name = string.sub( params[k], 1, 2 )
				if name == "NI" then
					NI = dcUtil.escapeAdc(string.sub( params[k], 3), true)
					this._nick = NI
					DC():PrintDebug("[STATUS] HUB nick set: " .. NI )
				elseif name == "DE" then
					DE = dcUtil.escapeAdc(string.sub( params[k], 3), true)
					this._description = DE
					DC():PrintDebug("[STATUS] HUB description set: " .. DE )
				end
			end
		end
		
		if not this._users[sid] then
			this._users[sid] = createAdcUser( this, sid )
			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, "([%S\r]+)", function(s) table.insert(params, s) end )
			local ID, OP, I4, NI = nil, nil, nil, nil
			for k in pairs(params) do
				local name = string.sub( params[k], 1, 2 )
				if name == "ID" then
					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 == "OP" then
					OP = string.sub( params[k], 3)
					if OP == "1" then
						r:setOp(true)
						-- DC():PrintDebug("[STATUS] OP set: " .. sid )
					else
						r:setOp(false)
						-- DC():PrintDebug("[STATUS] OP removed: " .. sid )
					end
				elseif name == "I4" then
					I4 = string.sub( params[k], 3)
					r:setIp(I4)
				elseif name == "NI" then
					NI = dcUtil.escapeAdc(string.sub( params[k], 3), true)
					r:setNick(NI)
					-- DC():PrintDebug("[STATUS] Nick set (" .. sid .."): " .. NI )
					if sid == this._mySID then
						this._myNick = NI
						DC():PrintDebug("[STATUS] Own nick set: " .. NI )
					end
				end
			end
			-- DC():PrintDebug("NI: " .. NI .. " OP: " .. tostring(OP) .. " I4: " .. I4 .. " ID: " .. ID )
		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.getSid = 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.sendChat = function( this, msg )
		local ownSid = this:getOwnSid()
		msg = dcUtil.escapeAdc( 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 )
		if not fmt then
			msg = "*** " .. msg
		end
		msg = dcUtil.escapeAdc( msg )
		-- todo: need a function which adds a chat line without nick
		DC():InjectHubMessageADC( this:getId(), "IMSG " .. msg )
	end
	
	hub.sendPrivMsgTo = function( this, victimSid, msg_unescaped, hideFromSelf )
		local ownSid = this:getOwnSid()
		local msg = dcUtil.escapeAdc( 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:getSid(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, msg )
		local ret
		for k,f in pairs(dcpp:getListeners( "userInf" )) do
			ret = f( this, user, msg, ret ) or ret
		end
		return ret
	end
	
	hub.onQUI = function( this, sid, msg )
		for k,f in pairs(dcpp:getListeners( "adcUserQui" )) do
			f( this, sid, msg )
		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( 1 ) ; 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 = dcUtil.escaping( 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 = dcUtil.escaping( 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 )
		DC():HubWindowAttention( this:getId() )
	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 )
		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
			else
				local ret
				for k,f in pairs(dcpp:getListeners( "hubPm" )) do
					ret = f( this, user, 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 = nil
	user._ip = ""
	user._handled_messages = {} -- flood protection

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

	user.isOp = function( this )
		return this._op
	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 = nil
	user._op = false
	user._ip = ""
	user._handled_messages = {} -- flood protection

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

	user.isOp = function( this )
		return this._op
	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 )
	local h = dcpp:getHub( hub )
	local params = {}
	string.gsub(msg, "([%S\r]+)", function(s) table.insert(params, s) end )
	local ctype = string.sub(params[1], 1, 1)
	local cmd = string.sub(params[1], 2, 4)
	local sid, targetSid, parameters = nil, nil, ""
	if ctype == "I" then
		parameters = string.sub( msg, 6)
		sid = "_HUB"
	elseif ctype == "B" then
		parameters = string.sub( msg, 11)
		sid = params[2]
	elseif ctype == "D" or ctype == "E" then
		parameters = string.sub( msg, 16 )
		sid = params[2]
		targetSid = params[3]
	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(hub, sid, parameters)
	elseif cmd == "INF" then
		local u = h:getUser( sid, parameters )
		return h:onINF( u, parameters )
	elseif cmd == "MSG" then
		local pm, replySid, me_msg = false, nil, false
		local tmp = {}
		string.gsub(parameters, "([%S\r]+)", function(s) table.insert(tmp, s) end )
		local text = dcUtil.escapeAdc(tmp[1], true)
		tmp[1] = nil
		-- DC():PrintDebug("MSG triggered: " .. text )
		-- 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():GetAppPath() .. "/scripts/bier.lua" )
--dofile( DC():GetAppPath() .. "/scripts/slots.lua" )
dofile( DC():GetAppPath() .. "/scripts/uptime.lua" )
--dofile( DC():GetAppPath() .. "/scripts/onjoin.lua" )
--dofile( DC():GetAppPath() .. "/scripts/monologue.lua" )
dofile( DC():GetAppPath() .. "/scripts/ignore.lua" )
--dofile( DC():GetAppPath() .. "/scripts/p2pblock.lua" )
--dofile( DC():GetAppPath() .. "/scripts/quiet_login.lua" )
--dofile( DC():GetAppPath() .. "/scripts/log.lua" )
dofile( DC():GetAppPath() .. "/scripts/kickfilter.lua" )