Advertisement
deseven

banchat.lua

May 10th, 2011
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.49 KB | None | 0 0
  1. -- PtokaX <> AJAXChat bans script
  2. -- version 1.0.0a
  3. -- by deseven, 2011
  4.  
  5. -- use !banchat <ip> <time> and !unbanchat <ip> to control users
  6. -- use !getchatbans to get the list of bans
  7.  
  8. -- settings --
  9. DataFile = "/srv/ptokax/scripts/banchat.data"
  10. MysqlHost = "localhost"
  11. MysqlUser = "ajaxchat"
  12. MysqlPass = "ajaxchatpwd"
  13. MysqlDB = "ajaxchat"
  14. CheckBansTimer = 60000
  15. BotName = "Bot"
  16.  
  17. -- lang --
  18. MsgWrongIP = "Wrong IP"
  19. MsgIPs = "Banned IPs:"
  20. MsgNoIPs = "n/a"
  21. MsgIPAdded = "Added IP "
  22. MsgIPRemoved = "Removed IP "
  23. MsgSyntax = "Use !banchat <ip> <time(m,h,d)> and !unbanchat <ip>"
  24. MsgBanned = "You are not allowed to use this chat..."
  25.  
  26. -- split function taken from lua-users wiki
  27. function Split(str, delim, maxNb)
  28.     -- Eliminate bad cases...
  29.     if string.find(str, delim) == nil then
  30.         return { str }
  31.     end
  32.     if maxNb == nil or maxNb < 1 then
  33.         maxNb = 0    -- No limit
  34.     end
  35.     local result = {}
  36.     local pat = "(.-)" .. delim .. "()"
  37.     local nb = 0
  38.     local lastPos
  39.     for part, pos in string.gfind(str, pat) do
  40.         nb = nb + 1
  41.         result[nb] = part
  42.         lastPos = pos
  43.         if nb == maxNb then break end
  44.     end
  45.     -- Handle the last field
  46.     if nb ~= maxNb then
  47.         result[nb + 1] = string.sub(str, lastPos)
  48.     end
  49.     return result
  50. end
  51.  
  52. function AddBan(ip,bantime)
  53.     -- os.execute("echo '"..ip.."|"..bantime.."' > /srv/ptokax/scripts/banchat.log")
  54.     if bantime == "ForEver" then
  55.         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
  56.         os.execute(AddIP)
  57.         CheckBans()
  58.         return true
  59.     else
  60.         if string.find(bantime,"m") ~= nil then
  61.             bantime = bantime:gsub("m","")
  62.             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
  63.             os.execute(AddIP)
  64.             CheckBans()
  65.             return true
  66.         end
  67.         if string.find(bantime,"h") ~= nil then
  68.             bantime = bantime:gsub("h","")
  69.             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
  70.             os.execute(AddIP)
  71.             CheckBans()
  72.             return true
  73.         end
  74.         if string.find(bantime,"d") ~= nil then
  75.             bantime = bantime:gsub("d","")
  76.             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
  77.             os.execute(AddIP)
  78.             CheckBans()
  79.             return true
  80.         end
  81.         return false
  82.     end
  83. end
  84.  
  85. function RemBan(ip)
  86.     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
  87.     os.execute(RemIP)
  88.     CheckBans()
  89. end
  90.  
  91. function CheckDate(BanDate)
  92.     CurDate = os.date("%Y%m%d%H%M")
  93.     if tonumber(BanDate) < tonumber(CurDate) then
  94.         return false
  95.     else
  96.         return true
  97.     end
  98. end
  99.  
  100. CheckBans = function()
  101.     -- getting bans
  102.     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
  103.     os.execute(GetBans)
  104.     local BansDataR = assert(io.open(DataFile,"r"))
  105.     bans = {}
  106.     if BansDataR:read("*line") == nil then
  107.         BansDataR:close()
  108.     else
  109.         BansDataR:close()
  110.         local BansDataR = assert(io.open(DataFile,"r"))
  111.         for line in BansDataR:lines() do
  112.             table.insert(bans,line)
  113.         end
  114.         BansDataR:close()
  115.         -- if ban is expired - removing it
  116.         for i,v in ipairs(bans) do
  117.             CurBan = Split(bans[i],"\t",2)
  118.             if CheckDate(CurBan[2]) == false then
  119.                 RemBan(CurBan[1])
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. function IsIP(IPString)
  126.     IPString = IPString:gsub("%.","|")
  127.     local ip = Split(IPString,"|",4)
  128.     if tonumber(ip[1]) == nil or tonumber(ip[2]) == nil or tonumber(ip[3]) == nil or tonumber(ip[4]) == nil then
  129.         return false
  130.     end
  131.     if tonumber(ip[1]) > 255 or tonumber(ip[2]) > 255 or tonumber(ip[3]) > 255 or tonumber(ip[4]) > 255 then
  132.         return false
  133.     end
  134.     return true
  135. end
  136.  
  137. function OnStartup()
  138.     AJAXChatTimer = TmrMan.AddTimer(CheckBansTimer,"CheckBans")
  139.     CheckBans()
  140. end
  141.  
  142. function ChatArrival(user,data)
  143.     Core.GetUserAllData(user)
  144.     if user.iProfile == 0 then
  145.         local s,e,chat = string.find(data,"^%b<>%s(.*)$")
  146.         if string.find(chat,"!banchat ") == 1 then
  147.             local banstring = Split(chat," ")
  148.             banstring[2] = banstring[2]:gsub("|","")
  149.             if IsIP(banstring[2]) == true then
  150.                 if banstring[3] == nil then
  151.                     AddBan(banstring[2],"ForEver")
  152.                 else
  153.                     banstring[3] = banstring[3]:gsub("|","")
  154.                     if string.find(banstring[3],"m") ~= nil or string.find(banstring[3],"h") ~= nil or string.find(banstring[3],"d") ~= nil then
  155.                         AddBan(banstring[2],banstring[3])
  156.                     else
  157.                         Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  158.                     end
  159.                 end
  160.                 Core.SendToUser(user,"<"..BotName.."> "..MsgIPAdded..banstring[2])
  161.             else
  162.                 Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
  163.                 Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  164.             end
  165.             return true
  166.         end
  167.         if string.find(chat,"!unbanchat ") == 1 then
  168.             local ip = Split(chat," ")
  169.             ip[2] = ip[2]:gsub("|","")
  170.             if IsIP(ip[2]) == true then
  171.                 RemBan(ip[2])
  172.                 Core.SendToUser(user,"<"..BotName.."> "..MsgIPRemoved..ip[2])
  173.             else
  174.                 Core.SendToUser(user,"<"..BotName.."> "..MsgWrongIP)
  175.                 Core.SendToUser(user,"<"..BotName.."> "..MsgSyntax)
  176.             end
  177.             return true
  178.         end
  179.         if string.find(chat,"!getchatbans") == 1 then
  180.             Core.SendToUser(user,"<"..BotName.."> "..MsgIPs)
  181.             if bans == {} or bans == nil then
  182.                 Core.SendToUser(user,"<"..BotName.."> "..MsgNoIPs)
  183.             else
  184.                 for i,v in ipairs(bans) do
  185.                     Core.SendToUser(user,"<"..BotName.."> "..bans[i])
  186.                 end
  187.             end
  188.             return true
  189.         end
  190.         return false
  191.     else
  192.         for i,v in ipairs(bans) do
  193.             CurIP = Split(bans[i],"\t",2)
  194.             if user.sIP == CurIP[1] then
  195.                 Core.SendToUser(user,"<"..BotName.."> "..MsgBanned)
  196.                 return true
  197.             end
  198.         end
  199.     end
  200.     return false
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement