Conversion - Nick Watchdog 1.0 LUA 5.0-5.1
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

Conversion - Nick Watchdog 1.0 LUA 5.0-5.1

Started by rEALx, 12 February, 2008, 07:27:43

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

rEALx

Hi All

Nick Watchdog 1.0 LUA 5.0-5.1 by Mutor

I would like to have this script converted to New API.
Also, is it possible to include removal  of nick / ip numbers from the saved log file from a RC menu ?

Thx

rEALx

--[[
	Nick Watchdog 1.0 LUA 5.0/5.1
	By Mutor	02/13/07
	
	Requested by tom_cruise4g
	
	Logs user nick names and IP addresses
	-Disconnect users with changed nick names.
	-Reports status/errors to OpNick
	-Saves to file for script/hub restart
	
]]

Cfg = {
-- Botname pulled from the hub or use "CustomName"
Bot = frmHub:GetHubBotName(),
-- Bot description
Desc = "I watch for changes in user nicknames",
-- Bot email address
Mail = "user@domain.com",
-- Admins nick for status / error messages
OpNick = "Mutor",
--Filename for user data
File="NickWatchdog.dat",
--//--Set your profiles permissions here.
--profile_idx, Check this Profiles IP [0=no 1=yes], "Profile Name"
Profiles = {
	[-1] = 1,
	[0] = 0,
	[1] = 1,
	[2] = 1,
	[3] = 1,
	[4] = 1,
	[5] = 1,
	[6] = 1,
	},
Exclude = {
	["Mutor"] = 1,
	["OtherNick"] = 1,
	},
}

Main = function()
	if Cfg.Bot ~= frmHub:GetHubBotName() or frmHub:GetHubBot()== 0 then
		frmHub:RegBot(Cfg.Bot, 1, Cfg.Desc.." PtokaX ".._VERSION, Cfg.Mail)
	end
	local LuaVer = string.sub(_VERSION,1,7)
	if LuaVer == "Lua 5.1" then
		Cfg.Gc,Cfg.Mem = "collect",collectgarbage("count")
	elseif LuaVer == "Lua 5.0" then
		Cfg.Gc,Cfg.Mem = nil,gcinfo()
	else
		OnError("This script is incompatible with ".._VERSION)
	end
	if loadfile(Cfg.File) then
		dofile(Cfg.File)
	else
		Cfg.IPs = {}
		Save_File(Cfg.File,Cfg.IPs,"Cfg.IPs")
	end
	for a,b in ipairs(frmHub:GetOnlineUsers()) do
		local user = GetItemByName(b.sName)
		if user then
			CheckIP(user)
		end
	end
	Cfg.Mem = string.format("%-.2f Kb.",Cfg.Mem)
	OnError("Nick Watchdog 1.0 for ".._VERSION.." by Mutor has been started, using "..Cfg.Mem.." of memory.")
	
end

OnExit = function()
	OnError("Nick Watchdog 1.0 for ".._VERSION.." by Mutor has been stopped, freeing "..Cfg.Mem.." of memory.")
end

OnError = function(msg)
	SendToNick(Cfg.OpNick,"<"..Cfg.Bot.."> "..msg)
end

function NewUserConnected(user)
	CheckIP(user)
end
OpConnected = NewUserConnected

CheckIP = function(user)
	if Cfg.Profiles[user.iProfile] and Cfg.Profiles[user.iProfile] == 1 then
		if Cfg.IPs[user.sIP] then
			if not Cfg.Exclude[user.sName] or Cfg.Exclude[user.sName] ~= 1 then
				if Cfg.IPs[user.sIP] ~= user.sName then
					local profile = GetProfileName(user.iProfile) or "Unregistered User"
					local msg = "Sorry "..user.sName.." you may not change your nickname. "..
					"You will be disconnected from the hub. You may return with the nick "..
					Cfg.IPs[user.sIP].." after "..frmHub:GetDefaultTempBanTime()..
					" minute(s) has elapsed."
					local reason = "Nickname changed from "..Cfg.IPs[user.sIP].." to "..user.sName
					user:SendData(Cfg.Bot,msg)
					user:SendPM(Cfg.Bot,msg)
					user:Kick(Cfg.Bot,reason)
					return 1
				end
			end
		else
			Cfg.IPs[user.sIP] = user.sName
			Save_File(Cfg.File,Cfg.IPs,"Cfg.IPs")
		end
	end
end

Save_Serialize = function(tTable, sTableName, hFile, sTab)
	sTab = sTab or "";
	hFile:write(sTab..sTableName.." = {\n" )
	for key, value in pairs(tTable) do
		local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
		if(type(value) == "table") then
			Save_Serialize(value, sKey, hFile, sTab.."\t")
		else
			local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
			hFile:write( sTab.."\t"..sKey.." = "..sValue)
		end
		hFile:write( ",\n")
	end
	hFile:write( sTab.."}")
end

Save_File = function(file,table, tablename )
	local hFile = io.open (file , "wb")
	Save_Serialize(table, tablename, hFile)
	hFile:close()
	collectgarbage(Cfg.Gc)
end

SMF spam blocked by CleanTalk