Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PtokaX <> AJAXChat sync
- -- version 2.0.2
- -- by deseven, 2010-2011
- require "luasql.mysql"
- -- main settings
- DebugMode = true
- BotName = "Bot"
- -- transport settings
- MysqlHost = "localhost"
- MysqlUser = "ajaxchat"
- MysqlPass = "ajaxchatpwd"
- MysqlDB = "ajaxchat"
- AJAXChatCodepage = "utf8"
- PtokaXCodepage = "latin1"
- -- AJAX Chat > PtokaX settings
- RecEnabled = true
- IDFile = "/srv/ptokax/scripts/receivechat.id"
- CheckMsgTimer = 3000
- ReplaceWhat = { "%[b]","%[/b]","%[u]","%[/u]","%[i]","%[/i]","%[code]","%[/code]","%[quote]","%[/quote]","%[color.-]","%[/color]","%[url.-]","%[/url]","\\n", "|" }
- ReplaceWith = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "\r\n","|" }
- -- PtokaX > AJAX Chat settings
- SendEnabled = true
- BadChars = {".","?","!","+","-",}
- -- bans settings
- BansEnabled = true
- CheckBansTimer = 60000
- AddBanCommand = "!banchat"
- RemBanCommand = "!unbanchat"
- GetBansCommand = "!getchatbans"
- MsgWrongIP = "Wrong IP"
- MsgIPs = "Banned IPs:"
- MsgNoIPs = "n/a"
- MsgIPAdded = "Banned IP "
- MsgIPRemoved = "Removed IP "
- MsgSyntax = "Use !banchat <ip> <time(m,h,d)> and !unbanchat <ip>"
- MsgBanned = "You are not allowed to use this chat until "
- -- 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
- ReceiveMsg = function()
- local Query = "select id,convert(userName using "..PtokaXCodepage..") as userName,convert(text using "..PtokaXCodepage..") as text from ajax_chat_messages where id > "..LastMsgID.." and userID != 50000 and userID != 2147483647 order by id"
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- local res = assert(con:execute(Query))
- if res == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to receive messages, check your script.log")
- else
- local row = res:fetch({},"a")
- while row do
- if string.find(row.text,"/privmsg") ~= 1 then
- for r,w in ipairs(ReplaceWhat) do
- row.userName = row.userName:gsub(w,ReplaceWith[r])
- row.text = row.text:gsub(w,ReplaceWith[r])
- end
- Core.SendToAll("<"..row.userName.."> "..row.text)
- end
- LastMsgID = row.id
- row = res:fetch(row,"a")
- end
- end
- res:close()
- con:close()
- return true
- end
- function AddBan(ip,bantime)
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- if bantime == "ForEver" then
- local Query = "insert into ajax_chat_bans (userID,userName,dateTime,ip) values ('50000','"..ip.."',date_add(now(),interval 50 year),unhex(hex(inet_aton('"..ip.."'))))"
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
- end
- CheckBans()
- con:close()
- return true
- else
- if string.find(bantime,"m") ~= nil then
- bantime = bantime:gsub("m","")
- local Query = "insert into ajax_chat_bans (userID,userName,dateTime,ip) values ('50000','"..ip.."',date_add(now(),interval "..bantime.." minute),unhex(hex(inet_aton('"..ip.."'))))"
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
- end
- CheckBans()
- con:close()
- return true
- end
- if string.find(bantime,"h") ~= nil then
- bantime = bantime:gsub("h","")
- local Query = "insert into ajax_chat_bans (userID,userName,dateTime,ip) values ('50000','"..ip.."',date_add(now(),interval "..bantime.." hour),unhex(hex(inet_aton('"..ip.."'))))"
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
- end
- CheckBans()
- con:close()
- return true
- end
- if string.find(bantime,"d") ~= nil then
- bantime = bantime:gsub("d","")
- local Query = "insert into ajax_chat_bans (userID,userName,dateTime,ip) values ('50000','"..ip.."',date_add(now(),interval "..bantime.." day),unhex(hex(inet_aton('"..ip.."'))))"
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
- end
- CheckBans()
- con:close()
- return true
- end
- con:close()
- return false
- end
- end
- function RemBan(ip)
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- local Query = "delete from ajax_chat_bans where ip=unhex(hex(inet_aton('"..ip.."')))"
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to delete ban for ip "..ip..", check your script.log")
- end
- con:close()
- 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
- 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
- CheckBans = function()
- -- getting bans
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- local Query = "select inet_ntoa(conv(hex(ip),16,10)) as ip,date_format(dateTime,'%Y%m%d%H%i') as dateTime from ajax_chat_bans"
- local res = assert(con:execute(Query))
- if res == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to check bans, check your script.log")
- else
- local row = res:fetch({},"a")
- while row do
- if CheckDate(row.dateTime) == false then
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: removing ban for ip "..row.ip.." because it's expired")
- end
- RemBan(row.ip)
- end
- row = res:fetch(row,"a")
- end
- end
- res:close()
- con:close()
- end
- function OnStartup()
- env = assert(luasql.mysql())
- if RecEnabled == true then
- -- reading file to get LastMsgID after restart (or first start)
- local IDFileR = assert(io.open(IDFile,"r"))
- LastMsgID = IDFileR:read("*line")
- IDFileR:close()
- end
- if RecEnabled == false or LastMsgID == nil then
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: not receiving messages from AJAX Chat - disabled or no last message ID defined")
- end
- else
- -- setting timer
- AJAXChatTimer = TmrMan.AddTimer(CheckMsgTimer,"ReceiveMsg")
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: added timer for receiving messages from AJAX Chat - "..CheckMsgTimer.."ms")
- end
- end
- if BansEnabled == true then
- AJAXChatTimer2 = TmrMan.AddTimer(CheckBansTimer,"CheckBans")
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: added timer for bans system - "..CheckBansTimer.."ms")
- end
- CheckBans()
- end
- end
- function OnExit()
- if RecEnabled == true then
- -- writing LastMsgID to file
- if LastMsgID == nil then
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: trying to write the last message ID to file, but no ID defined")
- end
- else
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: writing the last message ID to file")
- end
- local IDFileW = assert(io.open(IDFile,"w"))
- IDFileW:write(LastMsgID)
- IDFileW:close()
- end
- end
- if DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: exiting")
- end
- env:close()
- end
- function ChatArrival(user,data)
- Core.GetUserAllData(user)
- local s,e,chat = string.find(data,"^%b<>%s(.*)$")
- local nick = user.sNick
- chat = string.sub(chat,1,-2)
- if BansEnabled == true then
- if user.iProfile == 0 then
- if string.find(chat,AddBanCommand.." ") == 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")
- Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPAdded..banstring[2])
- 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])
- Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPAdded..banstring[2])
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- end
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- return true
- end
- if string.find(chat,RemBanCommand.." ") == 1 then
- local ip = Split(chat," ")
- ip[2] = ip[2]:gsub("|","")
- if IsIP(ip[2]) == true then
- RemBan(ip[2])
- Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPRemoved..ip[2])
- else
- Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
- Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
- end
- return true
- end
- if string.find(chat,GetBansCommand) == 1 then
- Core.SendToUser(user,"<"..BotName.."> "..MsgIPs)
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- local Query = "select inet_ntoa(conv(hex(ip),16,10)) as ip,date_format(dateTime,'%Y%m%d%H%i') as dateTime from ajax_chat_bans"
- local res = assert(con:execute(Query))
- local row = res:fetch({},"a")
- local HaveData = false
- while row do
- row.dateTime = row.dateTime:sub(7,8).."."..row.dateTime:sub(5,6).."."..row.dateTime:sub(1,4).." "..row.dateTime:sub(9,10)..":"..row.dateTime:sub(11,12)
- Core.SendToUser(user,"<"..BotName.."> "..row.ip.." => "..row.dateTime)
- row = res:fetch(row,"a")
- HaveData = true
- end
- if HaveData == false then
- Core.SendToUser(user,"<"..BotName.."> "..MsgNoIPs)
- end
- res:close()
- con:close()
- return true
- end
- else
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- local Query = "select inet_ntoa(conv(hex(ip),16,10)) as ip,date_format(dateTime,'%Y%m%d%H%i') as dateTime from ajax_chat_bans"
- local res = assert(con:execute(Query))
- if res == nil then
- res:close()
- con:close()
- return false
- else
- local row = res:fetch({},"a")
- while row do
- if user.sIP == row.ip then
- row.dateTime = row.dateTime:sub(7,8).."."..row.dateTime:sub(5,6).."."..row.dateTime:sub(1,4).." "..row.dateTime:sub(9,10)..":"..row.dateTime:sub(11,12)
- res:close()
- con:close()
- return Core.SendToUser(user,"<"..BotName.."> "..MsgBanned..row.dateTime),true
- end
- row = res:fetch(row,"a")
- end
- res:close()
- con:close()
- end
- end
- end
- if SendEnabled == true then
- local when = os.date("%Y-%m-%d %H:%M:%S")
- -- checking for BadChars
- for k,v in pairs(BadChars) do
- if string.sub(chat,1,1) == v then
- return false
- end
- end
- nick = string.gsub(nick,"\\","\\\\")
- chat = string.gsub(chat,"\\","\\\\")
- chat = string.gsub(chat,"|","|")
- chat = string.gsub(chat,"$","$")
- chat = string.gsub(chat,"'","\\'")
- nick = string.gsub(nick,"|","|")
- nick = string.gsub(nick,"$","$")
- nick = string.gsub(nick,"'","\\'")
- local Query = "insert into ajax_chat_messages (userName,dateTime,text,userID) values (convert(_"..PtokaXCodepage.."'"..nick.."' using "..AJAXChatCodepage.."),'"..when.."','"..chat.."','50000')"
- -- sending message to AJAX chat
- local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
- if con:execute(Query) == nil and DebugMode == true then
- Core.SendToOps("<"..BotName.."> ACSync: failed to send incoming message, check your script.log")
- end
- con:close()
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement