PtokaX forum

Archive => Archived 5.1 boards => Conversion Requests => Topic started by: rEALx on 12 February, 2008, 07:27:43

Title: Conversion - Nick Watchdog 1.0 LUA 5.0-5.1
Post by: rEALx on 12 February, 2008, 07:27:43
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