Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PtokaX <> AJAXChat bans script
- -- version 1.0.0a
- -- by deseven, 2011
- -- use !banchat <ip> <time> and !unbanchat <ip> to control users
- -- use !getchatbans to get the list of bans
- -- settings --
- DataFile = "/srv/ptokax/scripts/banchat.data"
- MysqlHost = "localhost"
- MysqlUser = "ajaxchat"
- MysqlPass = "ajaxchatpwd"
- MysqlDB = "ajaxchat"
- CheckBansTimer = 60000
- BotName = "Bot"
- -- lang --
- MsgWrongIP = "Wrong IP"
- MsgIPs = "Banned IPs:"
- MsgNoIPs = "n/a"
- MsgIPAdded = "Added IP "
- MsgIPRemoved = "Removed IP "
- MsgSyntax = "Use !banchat <ip> <time(m,h,d)> and !unbanchat <ip>"
- MsgBanned = "You are not allowed to use this chat..."
- -- split function taken from lua-users wiki
- function Split(str, delim, maxNb)
- -- Eliminate bad cases...
- if string.find(str, delim) == nil then
- return { str }
- end
- if maxNb == nil or maxNb < 1 then
- maxNb = 0 -- No limit
- end
- local result = {}
- local pat = "(.-)" .. delim .. "()"
- local nb = 0
- local lastPos
- for part, pos in string.gfind(str, pat) do
- nb = nb + 1
- result[nb] = part
- lastPos = pos
- if nb == maxNb then break end
- end
- -- Handle the last field
- if nb ~= maxNb then
- result[nb + 1] = string.sub(str, lastPos)
- end
- return result
- end
- function AddBan(ip,bantime)
- -- os.execute("echo '"..ip.."|"..bantime.."' > /srv/ptokax/scripts/banchat.log")
- if bantime == "ForEver" then
- AddIP = "echo 'insert into ajax_chat_bans (userID,userName,dateTime,ip) values (50000,'\"'\""..ip.."\"'\"',date_add(now(),interval 50 year),unhex(hex(inet_aton('\"'\""..ip.."\"'\"'))))' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
- os.execute(AddIP)
- CheckBans()
- return true
- else
- if string.find(bantime,"m") ~= nil then
- bantime = bantime:gsub("m","")
- AddIP = "echo 'insert into ajax_chat_bans (userID,userName,dateTime,ip) values (50000,'\"'\""..ip.."\"'\"',date_add(now(),interval "..bantime.." minute),unhex(hex(inet_aton('\"'\""..ip.."\"'\"'))))' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
- os.execute(AddIP)
- CheckBans()
- return true
- end
- if string.find(bantime,"h") ~= nil then
- bantime = bantime:gsub("h","")
- AddIP = "echo 'insert into ajax_chat_bans (userID,userName,dateTime,ip) values (50000,'\"'\""..ip.."\"'\"',date_add(now(),interval "..bantime.." hour),unhex(hex(inet_aton('\"'\""..ip.."\"'\"'))))' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
- os.execute(AddIP)
- CheckBans()
- return true
- end
- if string.find(bantime,"d") ~= nil then
- bantime = bantime:gsub("d","")
- AddIP = "echo 'insert into ajax_chat_bans (userID,userName,dateTime,ip) values (50000,'\"'\""..ip.."\"'\"',date_add(now(),interval "..bantime.." day),unhex(hex(inet_aton('\"'\""..ip.."\"'\"'))))' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
- os.execute(AddIP)
- CheckBans()
- return true
- end
- return false
- end
- end
- function RemBan(ip)
- RemIP = "echo 'delete from ajax_chat_bans where ip=unhex(hex(inet_aton('\"'\""..ip.."\"'\"')))' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB
- os.execute(RemIP)
- CheckBans()
- end
- function CheckDate(BanDate)
- CurDate = os.date("%Y%m%d%H%M")
- if tonumber(BanDate) < tonumber(CurDate) then
- return false
- else
- return true
- end
- end
- CheckBans = function()
- -- getting bans
- GetBans = "echo 'select inet_ntoa(conv(hex(ip),16,10)),date_format(dateTime,'\"'\"%Y%m%d%H%i\"'\"') from ajax_chat_bans' | mysql -B -N -s -h "..MysqlHost.." -u"..MysqlUser.." -p"..MysqlPass.." "..MysqlDB.." > "..DataFile
- os.execute(GetBans)
- local BansDataR = assert(io.open(DataFile,"r"))
- bans = {}
- if BansDataR:read("*line") == nil then
- BansDataR:close()
- else
- BansDataR:close()
- local BansDataR = assert(io.open(DataFile,"r"))
- for line in BansDataR:lines() do
- table.insert(bans,line)
- end
- BansDataR:close()
- -- if ban is expired - removing it
- for i,v in ipairs(bans) do
- CurBan = Split(bans[i],"\t",2)
- if CheckDate(CurBan[2]) == false then
- RemBan(CurBan[1])
- end
- end
- end
- end
- function IsIP(IPString)
- IPString = IPString:gsub("%.","|")
- local ip = Split(IPString,"|",4)
- if tonumber(ip[1]) == nil or tonumber(ip[2]) == nil or tonumber(ip[3]) == nil or tonumber(ip[4]) == nil then
- return false
- end
- if tonumber(ip[1]) > 255 or tonumber(ip[2]) > 255 or tonumber(ip[3]) > 255 or tonumber(ip[4]) > 255 then
- return false
- end
- return true
- end
- function OnStartup()
- AJAXChatTimer = TmrMan.AddTimer(CheckBansTimer,"CheckBans")
- CheckBans()
- end
- function ChatArrival(user,data)
- Core.GetUserAllData(user)
- if user.iProfile == 0 then
- local s,e,chat = string.find(data,"^%b<>%s(.*)$")
- if string.find(chat,"!banchat ") == 1 then
- local banstring = Split(chat," ")
- banstring[2] = banstring[2]:gsub("|","")
- if IsIP(banstring[2]) == true then
- if banstring[3] == nil then
- AddBan(banstring[2],"ForEver")
- else
- banstring[3] = banstring[3]:gsub("|","")
- if string.find(banstring[3],"m") ~= nil or string.find(banstring[3],"h") ~= nil or string.find(banstring[3],"d") ~= nil then
- AddBan(banstring[2],banstring[3])
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- end
- Core.SendToUser(user,"<"..BotName.."> "..MsgIPAdded..banstring[2])
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- return true
- end
- if string.find(chat,"!unbanchat ") == 1 then
- local ip = Split(chat," ")
- ip[2] = ip[2]:gsub("|","")
- if IsIP(ip[2]) == true then
- RemBan(ip[2])
- Core.SendToUser(user,"<"..BotName.."> "..MsgIPRemoved..ip[2])
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- return true
- end
- if string.find(chat,"!getchatbans") == 1 then
- Core.SendToUser(user,"<"..BotName.."> "..MsgIPs)
- if bans == {} or bans == nil then
- Core.SendToUser(user,"<"..BotName.."> "..MsgNoIPs)
- else
- for i,v in ipairs(bans) do
- Core.SendToUser(user,"<"..BotName.."> "..bans[i])
- end
- end
- return true
- end
- return false
- else
- for i,v in ipairs(bans) do
- CurIP = Split(bans[i],"\t",2)
- if user.sIP == CurIP[1] then
- Core.SendToUser(user,"<"..BotName.."> "..MsgBanned)
- return true
- end
- end
- end
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement