History 2.0b

Post finished Clientside Scripts here, known supporting clients for LUA scripting.
Note: Client must be ADC 1.0

BCDC++ | RSX++
Locked
Toast

History 2.0b

Post by Toast » 11 Feb 2008, 19:28

Code: Select all

--// history.lua -- For BCDC++ 0.69a and newer
--// history.lua -- This script stores the last x lines of the main chat and send it in PM if -history command is sent into PM or main chat
--// history.lua -- Version 2.0b Rev 10: Jun 06, 2006
--// history.lua -- FleetCommand, [email protected]

-----------------------------------
-- Settings / Beállítások:
-----------------------------------

-- historymaxlines változó adja meg, hogy hány sort tároljon a script (hubonként)
-- the historymaxlines variable sets how many messages to store (per hub)
historymaxlines = 500

-- historydefault adja meg, hogy hány sort küldjön el priviben, ha nincs megadva paraméter
-- historydefault sets how many lines should be sent in PM if no parameter is given
historydefault = 50

------------------------------------
-- Functions
------------------------------------

if not historyfunctions then
	historyfunctions = {}
end

if not historyhubs then
	historyhubs = {}
end

function historyfunctions.tokenize(text)
	local ret = {}
	string.gsub(text, "([^%s]+)", function(s) table.insert(ret, s) end )
	return ret
end

function historyfunctions.addhistory(hub, nick, text, me_msg)
		local message = ""
		local url = hub:getUrl()
		local histnum = #historyhubs[url].chat + 1
		if histnum > historymaxlines then
			histnum = historymaxlines
		end
		while (histnum > 1) do
			historyhubs[url].chat[histnum] = historyhubs[url].chat[histnum-1]
			histnum = histnum - 1
		end
		if me_msg then -- only on ADC hubs
			message = message .. "["..os.date("%H:%M:%S") .."] * " .. nick .. " " .. text
		elseif nick then
			message = message .. "["..os.date("%H:%M:%S") .."] <".. nick .. "> " .. text
		else
			message = message .. "["..os.date("%H:%M:%S") .."] " .. text
		end
		historyhubs[url].chat[1] = message
end

function historyfunctions.sendhistory(hub, user, num)
	local allofthem = ""
	local url = hub:getUrl()
	num = tonumber(num)
	if (not num) then
		num = historydefault
	elseif (num < 1) or (num > historymaxlines) then
		allofthem = allofthem .. "\nWrong parameter (should be between 1 and ".. historymaxlines .."), using " .. historydefault .. " as default.\n"
		num = historydefault
	end
	local histnum = num
	if histnum > #historyhubs[url].chat then
		histnum = #historyhubs[url].chat
	end
	allofthem = allofthem .. "\nHistory of the latest ".. num .. " messages:"
	while (histnum > 0) do
		allofthem = allofthem .. "\n" .. historyhubs[url].chat[histnum]
		histnum = histnum - 1
	end
	user:sendPrivMsgFmt( allofthem, true)
end

dcpp:setListener("connected", "historyconn", 
	function (hub)
		local url = hub:getUrl()
		if  not historyhubs[url] then
			historyhubs[url] = {}
			historyhubs[url].chat = {}
			historyhubs[url].started = os.date("%Y. %m. %d - %H:%M:%S")
		end
		historyfunctions.addhistory(hub, "BCDC++", "Connected to " .. url)
	end
)

dcpp:setListener("disconnected", "historydisc",
	function (hub)
		local url = hub:getUrl()
		historyfunctions.addhistory(hub, "BCDC++", "Disconnected from " .. url)
	end
)

dcpp:setListener( "chat", "nmdc_history",
	function( hub, user, text )
		local params = historyfunctions.tokenize(text)
		if params[1] == "-history" then
			historyfunctions.sendhistory(hub, user, params[2])
			DC():PrintDebug("History requested on " .. hub:getUrl() .. " by " .. user:getNick() .. " [mainchat]")
			return true
		elseif string.find(text, "is kicking .+ because:") then
			-- don't save kick-messages
		elseif ( params[1] == "-scripts" ) and ( user:isOp() ) then
			local url = hub:getUrl()
			hub:sendChat("history.lua: Running on this hub since: " .. historyhubs[url].started)
		else
			local nick = user:getNick()
			historyfunctions.addhistory(hub, nick, text)
		end
		return nil
	end																			
)

dcpp:setListener( "adcChat", "adc_history",
	function( hub, user, text, me_msg )
		local params = historyfunctions.tokenize(text)
		if params[1] == "-history" then
			historyfunctions.sendhistory(hub, user, params[2])
			DC():PrintDebug("History requested on " .. hub:getUrl() .. " by " .. user:getNick() .. " [mainchat]")
			return true
		elseif string.find(text, "is kicking .+ because:") then
			-- don't save kick-messages
		elseif ( params[1] == "-scripts" ) and ( user:isOp() ) then
			local url = hub:getUrl()
			hub:sendChat("history.lua: Running on this hub since: " .. historyhubs[url].started)
		else
			local nick = user:getNick()
			historyfunctions.addhistory(hub, nick, text, me_msg)
		end
		return nil
	end																			
)

dcpp:setListener( "unknownchat", "historyuk",
	function( hub, text )
		historyfunctions.addhistory(hub, nil, text)
		return nil
	end																			
)

dcpp:setListener( "ownChatOut", "historyown",
	function( hub, text )
		local s = string.lower( text )
		if string.sub(s, 1,8) == "-history" or string.find(text, "is kicking .+ because:") then
			--
		else
			local nick = hub:getOwnNick()
			if hub:getProtocol() == "nmdc" then
				text = DC():FromUtf8(text)
			end
			historyfunctions.addhistory(hub, nick, text)
		end
		return nil
	end
)

dcpp:setListener( "pm", "nmdc_historypm",
	function( hub, user, text )
		local params = historyfunctions.tokenize(text)
		if params[1] == "-history" then
			DC():PrintDebug("History requested on " .. hub:getUrl() .. " by " .. user:getNick() .. " [pm]")
			historyfunctions.sendhistory(hub, user, params[2])
			return 1
		end
		return nil
	end
)

dcpp:setListener( "adcPm", "adc_historypm",
	function( hub, user, text, me_msg )
		local params = historyfunctions.tokenize(text)
		if params[1] == "-history" then
			DC():PrintDebug("History requested on " .. hub:getUrl() .. " by " .. user:getNick() .. " [pm]")
			historyfunctions.sendhistory(hub, user, params[2])
			return 1
		end
		return nil
	end
)

DC():PrintDebug( "  ** Loaded history.lua **" )

Locked