xban2/init.lua

272 lines
6.2 KiB
Lua
Raw Normal View History

2014-05-20 04:23:43 +02:00
xban = { }
local MP = minetest.get_modpath(minetest.get_current_modname())
dofile(MP.."/serialize.lua")
dofile(MP.."/shutil.lua")
local db = { }
local tempbans = { }
local DEF_SAVE_INTERVAL = 300 -- 5 minutes
local DEF_DB_FILENAME = minetest.get_worldpath().."/xban.db"
local DB_FILENAME = minetest.setting_get("xban.db_filename")
local SAVE_INTERVAL = tonumber(
minetest.setting_get("xban.db_save_interval")) or DEF_SAVE_INTERVAL
if (not DB_FILENAME) or (DB_FILENAME == "") then
DB_FILENAME = DEF_DB_FILENAME
end
local function make_logger(level)
return function(text, ...)
minetest.log(level, "[xban] "..text:format(...))
end
end
local ACTION = make_logger("action")
local INFO = make_logger("info")
local WARNING = make_logger("warning")
local ERROR = make_logger("error")
local unit_to_secs = {
s = 1, m = 60, h = 3600,
D = 86400, W = 604800, M = 2592000, Y = 31104000,
[""] = 1,
}
local function parse_time(t) --> secs
local secs = 0
for num, unit in t:gmatch("(%d+)([smhDWMY]?)") do
secs = secs + (tonumber(num) * (unit_to_secs[unit] or 1))
end
return secs
end
function xban.find_entry(player, create) --> entry, index
for index, e in ipairs(db) do
for name in pairs(e.names) do
if name == player then
return e, index
end
end
end
if create then
local e = {
names = { [player]=true },
banned = false,
record = { },
}
table.insert(db, e)
return e, #db
end
return nil
end
function xban.get_info(player) --> ip_name_list, banned, last_record
local e = xban.find_entry(player)
if not e then
return nil, "No such entry"
end
return e.names, e.banned, e.record[#e.record]
end
function xban.ban_player(player, source, expires, reason) --> bool, err
local e = xban.find_entry(player, true)
local rec = {
source = source,
time = os.time(),
expires = expires,
reason = reason,
}
table.insert(e.record, rec)
e.names[player] = true
local pl = minetest.get_player_by_name(player)
if pl then
local ip = minetest.get_player_ip(player)
if ip then
e.names[ip] = true
end
end
e.reason = reason
e.time = rec.time
e.expires = expires
e.banned = true
local msg
local date = (expires and os.date("%c", expires)
or "the end of time")
if expires then
table.insert(tempbans, e)
msg = ("Banned: Expires: %s, Reason: %s"):format(date, reason)
else
msg = ("Banned: Reason: %s"):format(reason)
end
for nm in pairs(e.names) do
minetest.kick_player(nm, msg)
end
ACTION("%s bans %s until %s for reason: %s", source, player,
date, reason)
ACTION("Banned Names/IPs: %s", table.concat(e.names, ", "))
return true
end
function xban.unban_player(player, source) --> bool, err
local e = xban.find_entry(player)
if not e then
return nil, "No such entry"
end
local rec = {
source = source,
time = os.time(),
reason = "Unbanned",
}
table.insert(e.record, rec)
e.banned = false
e.reason = nil
e.expires = nil
e.time = nil
ACTION("%s unbans %s", source, player)
ACTION("Unbanned Names/IPs: %s", table.concat(e.names, ", "))
return true
end
minetest.register_on_prejoinplayer(function(name, ip)
local e = xban.find_entry(name) or xban.find_entry(ip, true)
e.names[name] = true
e.names[ip] = true
if e.banned then
local date = (e.expires and os.date("%c", e.expires)
or "the end of time")
return ("Banned: Expires: %s, Reason: %s"):format(
date, e.reason)
end
end)
minetest.register_chatcommand("xban", {
description = "XBan a player",
params = "<player> <reason>",
privs = { ban=true },
func = function(name, params)
local plname, reason = params:match("(%S+)%s+(.+)")
if not (plname and reason) then
minetest.chat_send_player(name,
"Usage: /xban <player> <reason>")
return
end
xban.ban_player(plname, name, nil, reason)
minetest.chat_send_player(name,
("Banned %s."):format(plname))
end,
})
minetest.register_chatcommand("xtempban", {
description = "XBan a player temporarily",
params = "<player> <time> <reason>",
privs = { ban=true },
func = function(name, params)
local plname, time, reason = params:match("(%S+)%s+(%S+)%s+(.+)")
if not (plname and time and reason) then
minetest.chat_send_player(name,
"Usage: /xtempban <player> <time> <reason>")
return
end
time = parse_time(time)
if time < 60 then
minetest.chat_send_player(name,
"You must ban for at least 60 seconds.")
return
end
local expires = os.time() + time
xban.ban_player(plname, name, expires, reason)
minetest.chat_send_player(name,
("Banned %s until %s."):format(
plname, os.date("%c", expires)))
end,
})
minetest.register_chatcommand("xunban", {
description = "XUnBan a player",
params = "<player_or_ip>",
privs = { ban=true },
func = function(name, params)
local plname = params:match("%S+")
if not plname then
minetest.chat_send_player(name,
"Usage: /xunban <player_or_ip>")
return
end
local ok, e = xban.unban_player(plname, name)
minetest.chat_send_player(name,
("Unbanned %s."):format(plname))
end,
})
local function check_temp_bans()
minetest.after(60, check_temp_bans)
local to_rm = { }
local now = os.time()
for i, e in ipairs(tempbans) do
if e.expires and (e.expires <= now) then
table.insert(to_rm, i)
e.banned = false
e.expires = nil
e.reason = nil
e.time = nil
end
end
for _, i in ipairs(to_rm) do
table.remove(tempbans, i)
end
end
local function save_db()
minetest.after(SAVE_INTERVAL, save_db)
local ok
local f, e = io.open(DB_FILENAME, "wt")
db.timestamp = os.time()
if f then
ok, e = f:write(xban.serialize(db))
WARNING("Unable to save database: %s", e)
end
if f then f:close() end
return
end
local function load_db()
local f, e = io.open(DB_FILENAME, "rt")
if not f then
WARNING("Unable to load database: %s", e)
return
end
local cont
cont, e = f:read("*a")
if not cont then
WARNING("Unable to load database: %s", e)
return
end
local t = minetest.deserialize(cont)
if not t then
WARNING("Unable to load database: %s",
"Deserialization failed")
return
end
db = t
tempbans = { }
for _, entry in ipairs(db) do
if entry.banned and entry.expires then
table.insert(tempbans, entry)
end
end
end
-- Backup database
shutil.copy_file(DB_FILENAME,
DB_FILENAME.."."..os.date("%Y%m%d%H%M%S"))
minetest.register_on_shutdown(save_db)
minetest.after(SAVE_INTERVAL, save_db)
load_db()
xban.db = db