Advertisement
deseven

ajaxchatsync.lua

May 18th, 2011
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.28 KB | None | 0 0
  1. -- PtokaX <> AJAXChat sync
  2. -- version 2.0.2
  3. -- by deseven, 2010-2011
  4.  
  5. require "luasql.mysql"
  6.  
  7. -- main settings
  8. DebugMode = true
  9. BotName = "Bot"
  10.  
  11. -- transport settings
  12. MysqlHost = "localhost"
  13. MysqlUser = "ajaxchat"
  14. MysqlPass = "ajaxchatpwd"
  15. MysqlDB = "ajaxchat"
  16. AJAXChatCodepage = "utf8"
  17. PtokaXCodepage = "latin1"
  18.  
  19. -- AJAX Chat > PtokaX settings
  20. RecEnabled = true
  21. IDFile = "/srv/ptokax/scripts/receivechat.id"
  22. CheckMsgTimer = 3000
  23. ReplaceWhat = { "%[b]","%[/b]","%[u]","%[/u]","%[i]","%[/i]","%[code]","%[/code]","%[quote]","%[/quote]","%[color.-]","%[/color]","%[url.-]","%[/url]","\\n", "|"      }
  24. ReplaceWith = { "",    "",     "",    "",     "",    "",     "",       "",        "",        "",         "",          "",         "",        "",       "\r\n","&#124;" }
  25.  
  26. -- PtokaX > AJAX Chat settings
  27. SendEnabled = true
  28. BadChars = {".","?","!","+","-",}
  29.  
  30. -- bans settings
  31. BansEnabled = true
  32. CheckBansTimer = 60000
  33. AddBanCommand = "!banchat"
  34. RemBanCommand = "!unbanchat"
  35. GetBansCommand = "!getchatbans"
  36. MsgWrongIP = "Wrong IP"
  37. MsgIPs = "Banned IPs:"
  38. MsgNoIPs = "n/a"
  39. MsgIPAdded = "Banned IP "
  40. MsgIPRemoved = "Removed IP "
  41. MsgSyntax = "Use !banchat <ip> <time(m,h,d)> and !unbanchat <ip>"
  42. MsgBanned = "You are not allowed to use this chat until "
  43.  
  44. -- split function taken from lua-users wiki
  45. function Split(str, delim, maxNb)
  46.     -- Eliminate bad cases...
  47.     if string.find(str, delim) == nil then
  48.         return { str }
  49.     end
  50.     if maxNb == nil or maxNb < 1 then
  51.         maxNb = 0    -- No limit
  52.     end
  53.     local result = {}
  54.     local pat = "(.-)" .. delim .. "()"
  55.     local nb = 0
  56.     local lastPos
  57.     for part, pos in string.gfind(str, pat) do
  58.         nb = nb + 1
  59.         result[nb] = part
  60.         lastPos = pos
  61.         if nb == maxNb then break end
  62.     end
  63.     -- Handle the last field
  64.     if nb ~= maxNb then
  65.         result[nb + 1] = string.sub(str, lastPos)
  66.     end
  67.     return result
  68. end
  69.  
  70. ReceiveMsg = function()
  71.     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"
  72.     local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  73.     local res = assert(con:execute(Query))
  74.     if res == nil and DebugMode == true then
  75.         Core.SendToOps("<"..BotName.."> ACSync: failed to receive messages, check your script.log")
  76.     else
  77.         local row = res:fetch({},"a")
  78.         while row do
  79.             if string.find(row.text,"/privmsg") ~= 1 then
  80.                 for r,w in ipairs(ReplaceWhat) do
  81.                     row.userName = row.userName:gsub(w,ReplaceWith[r])
  82.                     row.text = row.text:gsub(w,ReplaceWith[r])
  83.                 end
  84.                 Core.SendToAll("<"..row.userName.."> "..row.text)
  85.             end
  86.             LastMsgID = row.id
  87.             row = res:fetch(row,"a")
  88.         end
  89.     end
  90.     res:close()
  91.     con:close()
  92.     return true
  93. end
  94.  
  95. function AddBan(ip,bantime)
  96.     local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  97.     if bantime == "ForEver" then
  98.         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.."'))))"
  99.         if con:execute(Query) == nil and DebugMode == true then
  100.             Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
  101.         end
  102.         CheckBans()
  103.         con:close()
  104.         return true
  105.     else
  106.         if string.find(bantime,"m") ~= nil then
  107.             bantime = bantime:gsub("m","")
  108.             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.."'))))"
  109.             if con:execute(Query) == nil and DebugMode == true then
  110.                 Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
  111.             end
  112.             CheckBans()
  113.             con:close()
  114.             return true
  115.         end
  116.         if string.find(bantime,"h") ~= nil then
  117.             bantime = bantime:gsub("h","")
  118.             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.."'))))"
  119.             if con:execute(Query) == nil and DebugMode == true then
  120.                 Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
  121.             end
  122.             CheckBans()
  123.             con:close()
  124.             return true
  125.         end
  126.         if string.find(bantime,"d") ~= nil then
  127.             bantime = bantime:gsub("d","")
  128.             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.."'))))"
  129.             if con:execute(Query) == nil and DebugMode == true then
  130.                 Core.SendToOps("<"..BotName.."> ACSync: failed to add ban for ip "..ip..", check your script.log")
  131.             end
  132.             CheckBans()
  133.             con:close()
  134.             return true
  135.         end
  136.         con:close()
  137.         return false
  138.     end
  139. end
  140.  
  141. function RemBan(ip)
  142.     local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  143.     local Query = "delete from ajax_chat_bans where ip=unhex(hex(inet_aton('"..ip.."')))"
  144.     if con:execute(Query) == nil and DebugMode == true then
  145.         Core.SendToOps("<"..BotName.."> ACSync: failed to delete ban for ip "..ip..", check your script.log")
  146.     end
  147.     con:close()
  148.     CheckBans()
  149. end
  150.  
  151. function CheckDate(BanDate)
  152.     CurDate = os.date("%Y%m%d%H%M")
  153.     if tonumber(BanDate) < tonumber(CurDate) then
  154.         return false
  155.     else
  156.         return true
  157.     end
  158. end
  159.  
  160. function IsIP(IPString)
  161.     IPString = IPString:gsub("%.","|")
  162.     local ip = Split(IPString,"|",4)
  163.     if tonumber(ip[1]) == nil or tonumber(ip[2]) == nil or tonumber(ip[3]) == nil or tonumber(ip[4]) == nil then
  164.         return false
  165.     end
  166.     if tonumber(ip[1]) > 255 or tonumber(ip[2]) > 255 or tonumber(ip[3]) > 255 or tonumber(ip[4]) > 255 then
  167.         return false
  168.     end
  169.     return true
  170. end
  171.  
  172. CheckBans = function()
  173.     -- getting bans
  174.     local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  175.     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"
  176.     local res = assert(con:execute(Query))
  177.     if res == nil and DebugMode == true then
  178.         Core.SendToOps("<"..BotName.."> ACSync: failed to check bans, check your script.log")
  179.     else
  180.         local row = res:fetch({},"a")
  181.         while row do
  182.             if CheckDate(row.dateTime) == false then
  183.                 if DebugMode == true then
  184.                     Core.SendToOps("<"..BotName.."> ACSync: removing ban for ip "..row.ip.." because it's expired")
  185.                 end
  186.                 RemBan(row.ip)
  187.             end
  188.             row = res:fetch(row,"a")
  189.         end
  190.     end
  191.     res:close()
  192.     con:close()
  193. end
  194.  
  195. function OnStartup()
  196.         env = assert(luasql.mysql())
  197.         if RecEnabled == true then
  198.             -- reading file to get LastMsgID after restart (or first start)
  199.             local IDFileR = assert(io.open(IDFile,"r"))
  200.             LastMsgID = IDFileR:read("*line")
  201.             IDFileR:close()
  202.         end
  203.        
  204.         if RecEnabled == false or LastMsgID == nil then
  205.             if DebugMode == true then
  206.                 Core.SendToOps("<"..BotName.."> ACSync: not receiving messages from AJAX Chat - disabled or no last message ID defined")
  207.             end
  208.         else
  209.             -- setting timer
  210.             AJAXChatTimer = TmrMan.AddTimer(CheckMsgTimer,"ReceiveMsg")
  211.             if DebugMode == true then
  212.                 Core.SendToOps("<"..BotName.."> ACSync: added timer for receiving messages from AJAX Chat - "..CheckMsgTimer.."ms")
  213.             end
  214.         end
  215.        
  216.         if BansEnabled == true then
  217.             AJAXChatTimer2 = TmrMan.AddTimer(CheckBansTimer,"CheckBans")
  218.             if DebugMode == true then
  219.                 Core.SendToOps("<"..BotName.."> ACSync: added timer for bans system - "..CheckBansTimer.."ms")
  220.             end
  221.             CheckBans()
  222.         end
  223. end
  224.  
  225. function OnExit()
  226.     if RecEnabled == true then
  227.         -- writing LastMsgID to file
  228.         if LastMsgID == nil then
  229.             if DebugMode == true then
  230.                 Core.SendToOps("<"..BotName.."> ACSync: trying to write the last message ID to file, but no ID defined")
  231.             end
  232.         else
  233.             if DebugMode == true then
  234.                 Core.SendToOps("<"..BotName.."> ACSync: writing the last message ID to file")
  235.             end
  236.             local IDFileW = assert(io.open(IDFile,"w"))
  237.             IDFileW:write(LastMsgID)
  238.             IDFileW:close()
  239.         end
  240.     end
  241.     if DebugMode == true then
  242.         Core.SendToOps("<"..BotName.."> ACSync: exiting")
  243.     end
  244.     env:close()
  245. end
  246.  
  247. function ChatArrival(user,data)
  248.     Core.GetUserAllData(user)
  249.     local s,e,chat = string.find(data,"^%b<>%s(.*)$")
  250.     local nick = user.sNick
  251.     chat = string.sub(chat,1,-2)
  252.     if BansEnabled == true then
  253.         if user.iProfile == 0 then
  254.             if string.find(chat,AddBanCommand.." ") == 1 then
  255.                 local banstring = Split(chat," ")
  256.                 banstring[2] = banstring[2]:gsub("|","")
  257.                 if IsIP(banstring[2]) == true then
  258.                     if banstring[3] == nil then
  259.                         AddBan(banstring[2],"ForEver")
  260.                         Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPAdded..banstring[2])
  261.                     else
  262.                         banstring[3] = banstring[3]:gsub("|","")
  263.                         if string.find(banstring[3],"m") ~= nil or string.find(banstring[3],"h") ~= nil or string.find(banstring[3],"d") ~= nil then
  264.                             AddBan(banstring[2],banstring[3])
  265.                             Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPAdded..banstring[2])
  266.                         else
  267.                             Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  268.                         end
  269.                     end
  270.                 else
  271.                     Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
  272.                     Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  273.                 end
  274.                 return true
  275.             end
  276.             if string.find(chat,RemBanCommand.." ") == 1 then
  277.                 local ip = Split(chat," ")
  278.                 ip[2] = ip[2]:gsub("|","")
  279.                 if IsIP(ip[2]) == true then
  280.                     RemBan(ip[2])
  281.                     Core.SendToOps("<"..BotName.."> * "..user.sNick.." "..MsgIPRemoved..ip[2])
  282.                 else
  283.                     Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
  284.                     Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  285.                 end
  286.                 return true
  287.             end
  288.             if string.find(chat,GetBansCommand) == 1 then
  289.                 Core.SendToUser(user,"<"..BotName.."> "..MsgIPs)
  290.                 local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  291.                 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"
  292.                 local res = assert(con:execute(Query))
  293.                 local row = res:fetch({},"a")
  294.                 local HaveData = false
  295.                 while row do
  296.                     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)
  297.                     Core.SendToUser(user,"<"..BotName.."> "..row.ip.." => "..row.dateTime)
  298.                     row = res:fetch(row,"a")
  299.                     HaveData = true
  300.                 end
  301.                 if HaveData == false then
  302.                     Core.SendToUser(user,"<"..BotName.."> "..MsgNoIPs)
  303.                 end
  304.                 res:close()
  305.                 con:close()
  306.                 return true
  307.             end
  308.         else
  309.             local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  310.             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"
  311.             local res = assert(con:execute(Query))
  312.             if res == nil then
  313.                     res:close()
  314.                     con:close()
  315.                     return false
  316.             else
  317.                 local row = res:fetch({},"a")
  318.                 while row do
  319.                     if user.sIP == row.ip then
  320.                         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)
  321.                         res:close()
  322.                         con:close()
  323.                         return Core.SendToUser(user,"<"..BotName.."> "..MsgBanned..row.dateTime),true
  324.                     end
  325.                     row = res:fetch(row,"a")
  326.                 end
  327.                 res:close()
  328.                 con:close()
  329.             end
  330.         end
  331.     end
  332.    
  333.     if SendEnabled == true then
  334.         local when = os.date("%Y-%m-%d %H:%M:%S")
  335.         -- checking for BadChars
  336.         for k,v in pairs(BadChars) do
  337.             if string.sub(chat,1,1) == v then
  338.                 return false
  339.             end
  340.         end
  341.         nick = string.gsub(nick,"\\","\\\\")
  342.         chat = string.gsub(chat,"\\","\\\\")
  343.         chat = string.gsub(chat,"&#124;","|")
  344.         chat = string.gsub(chat,"&#36;","$")
  345.         chat = string.gsub(chat,"'","\\'")
  346.         nick = string.gsub(nick,"&#124;","|")
  347.         nick = string.gsub(nick,"&#36;","$")
  348.         nick = string.gsub(nick,"'","\\'")
  349.         local Query = "insert into ajax_chat_messages (userName,dateTime,text,userID) values (convert(_"..PtokaXCodepage.."'"..nick.."' using "..AJAXChatCodepage.."),'"..when.."','"..chat.."','50000')"
  350.         -- sending message to AJAX chat
  351.         local con = assert(env:connect(MysqlDB,MysqlUser,MysqlPass,MysqlHost))
  352.         if con:execute(Query) == nil and DebugMode == true then
  353.             Core.SendToOps("<"..BotName.."> ACSync: failed to send incoming message, check your script.log")
  354.         end
  355.         con:close()
  356.         return false
  357.     end
  358. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement