Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- IP Banning, by H® Shaft
- -- Original Creators: AelitePrime and Wizard
- -- sendconsoletext overloaded by Nugget
- -- From Wizards Commands 2.3 and AelitePrimes Commands 4.3 scripts
- -- Extracted & Re-Written by H® Shaft 12/14/2014
- -- Updated 1/3/2015 - resolved ban and logging error, resolved issue with new phasor
- -- Console Commands: Description: Text Commands:
- -- sv_ipban [player#] IP Bans the specified player /ipban [player#]
- -- sv_ipbanlist Shows a list of all IP Bans /ipbanlist
- -- sv_ipunban [ID#] Unbans the specified player ip /ipunban [ID#]
- -- sv_banthisip [ip#] Bans the specified IP address [xxx.xxx.xxx.xxx] /banthisip [xxx.xxx.xxx.xxx]
- -- (none) Shows a list of all players and ips /list
- -- All IP Bans are saved to your servers cg directory in a file named ipbanlist.txt in
- -- format: [Name], [IP Address], [Ban Date] Example: H® Shaft,101.168.101.101,12/14/2014
- -- All console and text actions (and attempts) are recorded in the game log.
- -- Note: All IP Bans done by this script result in a permanent ban, until ip-unbanned.
- cur_players = 0
- ip_banid = 1
- players_list = {}
- ip_banlist = {}
- function GetRequiredVersion()
- return 200
- end
- function OnScriptLoad(process, game, persistent)
- profilepath = getprofilepath()
- for i=0,15 do
- if getplayer(i) then
- cur_players = cur_players + 1
- players_list[i] = {}
- end
- end
- local file = io.open(string.format("%s\\ipbanlist.txt", profilepath), "r")
- if file then
- for line in file:lines() do
- local words = tokenizestring(line, ",")
- ip_banlist[ip_banid] = {}
- ip_banlist[ip_banid].name = words[1]
- ip_banlist[ip_banid].ip = words[2]
- ip_banlist[ip_banid].bdate = words[3]
- ip_banid = ip_banid + 1
- end
- file:close()
- end
- end
- function OnPlayerJoin(player)
- if getplayer(player) then
- cur_players = cur_players + 1
- players_list[player] = {}
- end
- end
- function OnPlayerLeave(player)
- if getplayer(player) then
- cur_players = cur_players - 1
- players_list[player] = nil
- end
- end
- function OnScriptUnload()
- local file = io.open(profilepath .. "\\ipbanlist.txt", "w")
- for k = 0,ip_banid do
- if ip_banlist[k] then
- file:write(string.format("%s,%s,%s\n", ip_banlist[k].name, ip_banlist[k].ip, ip_banlist[k].bdate))
- end
- end
- file:close()
- end
- function OnServerChat(player, type, message)
- local response = nil
- if player then
- name = getname(player)
- hash = gethash(player)
- local t = tokenizecmdstring(message)
- local count = #t
- if t[1] == nil then
- return true
- end
- if string.lower(t[1]) == "/ipban" then
- if isadmin(player) then
- response = false
- if t[2] and rresolveplayer(t[2]) then
- local name = getname(rresolveplayer(t[2]))
- Command_Ipban(player, {t[1], t[2], 2})
- else
- sendresponse("Invalid Syntax: /ipban [player #]", "/", player)
- end
- else
- response = true
- sendresponse("You're not an admin. Logging attempt.", "/", player)
- log_msg(1, getname(player) .. " attempted use of the /ipban command.")
- end
- end
- if string.lower(t[1]) == "/ipunban" then
- if isadmin(player) then
- response = false
- if t[2] then
- if tonumber(t[2]) < ip_banid then
- Command_Ipunban(player, t[1], t[2], 2)
- end
- else
- sendresponse("Invalid Syntax: /ipunban [player #]", "/", player)
- end
- else
- response = true
- sendresponse("You're not an admin. Logging attempt.", "/", player)
- log_msg(1, getname(player) .. " attempted use of the /ipunban command.")
- end
- end
- if string.lower(t[1]) == "/ipbanlist" then
- if isadmin(player) then
- sendconsoletext(player, "[ID | Name | IP | Banned On ]", 10, 0)
- local bool = true
- for i = 0, ip_banid-1 do
- if ip_banlist[i] then
- bool = false
- sendconsoletext(player, i .. " | " .. ip_banlist[i].name .. " | " .. ip_banlist[i].ip .. " | " .. ip_banlist[i].bdate, 10, 0)
- end
- end
- if bool then sendconsoletext(player, "IP Ban list is empty", 5, 0) end
- log_msg(1, getname(player) .. " used the ipbanlist command.")
- else
- response = true
- sendresponse("You're not an admin. Logging attempt.", "/", player)
- log_msg(1, getname(player) .. " attempted use of the /ipunban command.")
- end
- end
- if string.lower(t[1]) == "/banthisip" then
- if isadmin(player) then
- response = false
- if t[2] then
- Command_BanThisIP(player, t[1], t[2], 2)
- log_msg(1, getname(player) .. " Banned the IP: " .. t[2])
- else
- sendresponse("Invalid Syntax: /banthisip [xxx.xxx.xxx.xxx]", "/", player)
- end
- else
- response = true
- sendresponse("You're not an admin. Logging attempt.", "/", player)
- log_msg(1, getname(player) .. " attempted use of the /banthisip command.")
- end
- end
- if string.lower(t[1]) == "/list" then
- if isadmin(player) then
- response = false
- sendconsoletext(player, "[ID# - Name - IP]", 10, 0)
- for i = 0,15 do
- if getplayer(i) then
- local name = getname(i)
- local id = resolveplayer(i)
- local ip = getip(i)
- sendconsoletext(player, id .. " - " .. name .. " - " .. ip, 10, 0)
- end
- end
- log_msg(1, getname(player) .. " used the list command.")
- else
- response = true
- sendresponse("You're not an admin. Logging attempt.", "/", player)
- log_msg(1, getname(player) .. " attempted use of the /list command.")
- end
- end
- end
- return response
- end
- function OnBanCheck(hash, ip)
- local response = nil
- for k,v in pairs(ip_banlist) do
- if ip_banlist[k] and ip_banlist[k].ip == ip then
- response = false
- end
- end
- return response
- end
- function OnServerCommand(player, command)
- local response = nil
- local temp = tokenizecmdstring(command)
- local cmd = temp[1]
- t = tokenizecmdstring(command)
- count = #t
- if t[1] == "sv_ipban" then
- response = false
- Command_Ipban(player, t[1], t[2], count)
- elseif t[1] == "sv_ipbanlist" then
- response = false
- Command_Ipbanlist(player, t[1], count)
- elseif t[1] == "sv_ipunban" then
- response = false
- Command_Ipunban(player, t[1], t[2], count)
- elseif t[1] == "sv_banthisip" then
- response = false
- Command_BanThisIP(player, t[1], t[2], count)
- end
- return response
- end
- function Command_Ipban(executor, arg)
- if arg[1] ~= nil and arg[2] ~= nil and arg[1] and arg[2] then
- local command = arg[1]
- local player = tonumber(arg[2])
- local count = arg[3]
- if count == 2 then
- if player then
- local ipbanlist = io.open(string.format("%s\\ipbanlist.txt", profilepath), "a")
- local id = rresolveplayer(player)
- local bandate = os.date("%m/%d/%Y")
- ip_banlist[player] = {}
- ip_banlist[player].name = getname(id)
- ip_banlist[player].ip = getip(id)
- ip_banlist[player].bdate = bandate
- ipbanlist:write(string.format("%s,%s,%s\n", getname(id), getip(id), bandate))
- ipbanlist:close()
- Ipban(rresolveplayer(player))
- say(getname(id) .. " was IP banned from the server!")
- sendresponse(getname(id) .. " has been IP banned from the server!", "/", executor)
- log_msg(1, getname(executor) .. " IP Banned " .. getname(id) .. " (player id#" .. resolveplayer(id) .. ")")
- else
- sendresponse("Invalid Player!", command, executor)
- end
- else
- sendresponse("Invalid Syntax: " .. command .. " [player]", command, executor)
- end
- end
- end
- function Command_BanThisIP(executor, command, ip, count)
- if count == 2 then
- for i = 1, 1 do
- local name = "UNKNOWN"
- local bandate = os.date("%m/%d/%Y")
- ip_banid = ip_banid + 1
- ip_banlist[ip_banid] = {}
- ip_banlist[ip_banid].name = name
- ip_banlist[ip_banid].ip = ip
- ip_banlist[ip_banid].bdate = bandate
- registertimer(0, "RefreshIPBanlist")
- end
- sendresponse(ip .. " was added to the IP ban list, name listed as UNKNOWN!", "/", executor)
- log_msg(1, getname(executor) .. " used the banthisip command.")
- registertimer(0, "RefreshIPBanlist")
- else
- sendresponse("Invalid Syntax: " .. command .. " [xxx.xxx.xxx.xxx]", command, executor)
- end
- end
- function Command_Ipbanlist(executor, command, count)
- if count == 1 then
- local response = "[ID | Name | IP | Banned On ]\n"
- local bool = true
- for i = 0, ip_banid-1 do
- if ip_banlist[i] then
- bool = false
- response = response .. i .. " | " .. ip_banlist[i].name .. " | " .. ip_banlist[i].ip .. " | " .. ip_banlist[i].bdate .. "\n"
- end
- end
- if bool then response = "IP Ban list is empty" end
- log_msg(1, getname(executor) .. " used the ipbanlist command.")
- sendresponse(response, command, executor)
- else
- sendresponse("Invalid Syntax: " .. command, command, executor)
- end
- end
- function Command_Ipunban(executor, command, ID, count)
- if count == 2 and tonumber(ID) then
- ID = tonumber(ID)
- local response = "Invalid ID"
- if ID < ip_banid then
- response = ip_banlist[ID].ip .. " has been un-ipbanned."
- table.remove(ip_banlist, ID)
- log_msg(1, getname(executor) .. " IP Un-Banned player id# " .. ID)
- sendresponse(response, command, executor)
- registertimer(0, "RefreshIPBanlist")
- else
- sendresponse(response, command, executor)
- end
- else
- sendresponse("Invalid Syntax: " .. command .. " [ID]", command, executor)
- end
- end
- function Ipban(player)
- svcmd("sv_ban " .. resolveplayer(player))
- local file = io.open(profilepath .. "\\banned.txt", "r")
- if file then
- local Lines = {}
- for line in file:lines() do
- if line and line ~= "" then
- table.insert(Lines, line)
- end
- end
- svcmd("sv_unban " .. #Lines - 2)
- file:close()
- end
- end
- function RefreshIPBanlist(id, count)
- local writefile = io.open(profilepath .. "\\ipbanlist.txt", "w")
- if writefile then
- for k = 0,ip_banid do
- if ip_banlist[k] then
- writefile:write(string.format("%s,%s,%s\n", ip_banlist[k].name, ip_banlist[k].ip, ip_banlist[k].bdate))
- end
- end
- writefile:close()
- end
- ip_banlist = {}
- ip_banid = 1
- local readfile = io.open(string.format("%s\\ipbanlist.txt", profilepath), "r")
- if readfile then
- for line in readfile:lines() do
- local words = tokenizestring(line, ",")
- ip_banlist[ip_banid] = {}
- ip_banlist[ip_banid].name = words[1]
- ip_banlist[ip_banid].ip = words[2]
- ip_banlist[ip_banid].bdate = words[3]
- ip_banid = ip_banid + 1
- end
- readfile:close()
- end
- return false
- end
- function getvalidplayers()
- if cur_players ~= 0 then
- local players = {}
- for i = 0,15 do
- if getplayer(i) then
- table.insert(players, i)
- end
- end
- if players[1] then
- return players
- end
- end
- return false
- end
- function delayMsg(id, count, arguments)
- local msg = arguments[1]
- local command = tostring(arguments[2])
- local player = arguments[3]
- if string.sub(command, 1, 1) == "/" then
- say(msg)
- elseif string.sub(command, 1, 1) == "\\" then
- privatesay(player, msg)
- elseif player ~= nil and player ~= -1 then
- sendconsoletext(player, msg, 5, 0)
- end
- return false
- end
- function sendresponse(message, command, player, log)
- local function sendmessage(msg, cmd, pl, logBool)
- local playerCheck = ( tonumber(pl) and pl ~= nil and pl ~= -1 and pl >= 0 and pl < 16 )
- if string.sub(cmd, 1, 1) == "/" then
- registertimer(0, "delayMsg", {msg, cmd, pl} )
- elseif string.sub(cmd, 1, 1) == "\\" then
- registertimer(0, "delayMsg", {msg, cmd, pl} )
- elseif playerCheck then
- sendconsoletext(pl, msg, 5, 0)
- end
- hprintf(msg)
- if logBool ~= false and playerCheck then
- cmdlog("Response to " .. getname(pl) .. ": " .. msg)
- end
- end
- if message == nil then
- hprintf("Internal Error Occured Check the Command Script Errors log")
- cmderrors("Error Occured at sendresponse: No message ")
- return
- end
- if command == nil then
- hprintf("Internal Error Occured Check the Command Script Errors log")
- cmderrors("Error Occured at sendresponse: No Command Helpful info: " .. message)
- return
- end
- if type(message) == "table" then
- for i = 0, #message do
- if message[i] then
- sendmessage(message[i] , command, player , log)
- end
- end
- elseif message ~= "" then
- sendmessage(message, command, player, log)
- end
- end
- function cmderrors(message)
- WriteLog(profilepath .. "\\logs\\IP_Ban_Errors.log", tostring(message))
- end
- function cmdlog(message)
- WriteLog(profilepath .. "\\logs\\IP_Ban_commands.log",tostring(message))
- end
- function WriteLog(filename, value)
- local file = io.open(filename, "a")
- if file then
- file:write(string.format("%s\t%s\n", os.date("%Y:%m:%d:%H:%M:%S"), tostring(value)))
- file:close()
- end
- end
- -- Start sendconsoletext overloaded by Nugget
- console = {}
- console.__index = console
- consoletimer = registertimer(100, "ConsoleTimer")
- phasor_sendconsoletext = sendconsoletext
- function sendconsoletext(player, message, time, order, align, height, func)
- if player then
- console[player] = console[player] or {}
- local temp = {}
- temp.player = player
- temp.id = nextid(player, order)
- temp.message = message or ""
- temp.time = time or 0.7
- temp.remain = temp.time
- temp.align = align or "left"
- temp.height = height or 0
- if type(func) == "function" then
- temp.func = func
- elseif type(func) == "string" then
- temp.func = _G[func]
- end
- console[player][temp.id] = temp
- setmetatable(console[player][temp.id], console)
- return console[player][temp.id]
- end
- end
- function nextid(player, order)
- if not order then
- local x = 0
- for k,v in pairs(console[player]) do
- if k > x + 1 then
- return x + 1
- end
- x = x + 1
- end
- return x + 1
- else
- local original = order
- while console[player][order] do
- order = order + 0.001
- if order == original + 0.999 then break end
- end
- return order
- end
- end
- function getmessage(player, order)
- if console[player] then
- if order then
- return console[player][order]
- end
- end
- end
- function getmessages(player)
- return console[player]
- end
- function getmessageblock(player, order)
- local temp = {}
- for k,v in opairs(console[player]) do
- if k >= order and k < order + 1 then
- table.insert(temp, console[player][k])
- end
- end
- return temp
- end
- function console:getmessage()
- return self.message
- end
- function console:append(message, reset)
- if console[self.player] then
- if console[self.player][self.id] then
- if getplayer(self.player) then
- if reset then
- if reset == true then
- console[self.player][self.id].remain = console[self.player][self.id].time
- elseif tonumber(reset) then
- console[self.player][self.id].time = tonumber(reset)
- console[self.player][self.id].remain = tonumber(reset)
- end
- end
- console[self.player][self.id].message = message or ""
- return true
- end
- end
- end
- end
- function console:shift(order)
- local temp = console[self.player][self.id]
- console[self.player][self.id] = console[self.player][order]
- console[self.player][order] = temp
- end
- function console:pause(time)
- console[self.player][self.id].pausetime = time or 5
- end
- function console:delete()
- console[self.player][self.id] = nil
- end
- function ConsoleTimer(id, count)
- for i,_ in opairs(console) do
- if tonumber(i) then
- if getplayer(i) then
- for k,v in opairs(console[i]) do
- if console[i][k].pausetime then
- console[i][k].pausetime = console[i][k].pausetime - 0.1
- if console[i][k].pausetime <= 0 then
- console[i][k].pausetime = nil
- end
- else
- if console[i][k].func then
- if not console[i][k].func(i) then
- console[i][k] = nil
- end
- end
- if console[i][k] then
- console[i][k].remain = console[i][k].remain - 0.1
- if console[i][k].remain <= 0 then
- console[i][k] = nil
- end
- end
- end
- end
- if table.len(console[i]) > 0 then
- local paused = 0
- for k,v in pairs(console[i]) do
- if console[i][k].pausetime then
- paused = paused + 1
- end
- end
- if paused < table.len(console[i]) then
- local str = ""
- for i = 0,30 do
- str = str .. " \n"
- end
- phasor_sendconsoletext(i, str)
- for k,v in opairs(console[i]) do
- if not console[i][k].pausetime then
- if console[i][k].align == "right" or console[i][k].align == "center" then
- phasor_sendconsoletext(i, consolecenter(string.sub(console[i][k].message, 1, 78)))
- else
- phasor_sendconsoletext(i, string.sub(console[i][k].message, 1, 78))
- end
- end
- end
- end
- end
- else
- console[i] = nil
- end
- end
- end
- return true
- end
- function consolecenter(text)
- if text then
- local len = string.len(text)
- for i = len + 1, 78 do
- text = " " .. text
- end
- return text
- end
- end
- function opairs(t)
- local keys = {}
- for k,v in pairs(t) do
- table.insert(keys, k)
- end
- table.sort(keys,
- function(a,b)
- if type(a) == "number" and type(b) == "number" then
- return a < b
- end
- an = string.lower(tostring(a))
- bn = string.lower(tostring(b))
- if an ~= bn then
- return an < bn
- else
- return tostring(a) < tostring(b)
- end
- end)
- local count = 1
- return function()
- if table.unpack(keys) then
- local key = keys[count]
- local value = t[key]
- count = count + 1
- return key,value
- end
- end
- end
- function table.len(t)
- local count = 0
- for k,v in pairs(t) do
- count = count + 1
- end
- return count
- end
- -- Stop sendconsoletext overloaded
- -- Created by H® Shaft thank you to Oxide, AelitePrime, Nugget & Wizard.
- -- Visit http://halorace.org/forum/index.php?topic=514.0 or
- -- Visit http://pastebin.com/u/HR_Shaft for more phasor scripts
Advertisement
Add Comment
Please, Sign In to add comment