xban2 = {} -- =================================================================== xban = { MP = minetest.get_modpath(minetest.get_current_modname()) } dofile(xban.MP.."/serialize.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.settings:get("xban.db_filename") local SAVE_INTERVAL = tonumber( minetest.settings: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 WARNING = make_logger("warning") 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 print(("Created new entry for '%s'"):format(player)) 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 -- =================================================================== -- 211109 RJK: "xban.ban_player" has been modified so as to add two -- features: -- -- (a) If there's no "expire" time but there's a "reason" string and -- it contains an integer followed by any of "smhDWMY", the opera- -- tion is aborted because the admin may have intended to do -- "xtempban" instead of "xban". An error message to this effect -- is returned. -- -- (b) If there's no "expire" time and the target player's IP address -- starts with "10." or "172." or "192.168.", an "expire" time of -- 30 minutes is set. This is done because those types of IP ad- -- dresses are LAN and may be used by multiple people within short -- periods of each other. Example: Minetest set up and running in -- a school class. -- =================================================================== -- Returns: bool, err function xban.ban_player (player, source, expires, reason) if xban.get_whitelist (player) then return nil, "Player is whitelisted; remove from whitelist first" end local e = xban.find_entry (player, true) if e.banned then return nil, "Already banned" end if expires == nil and reason ~= nil then if reason:match ("(%d+)([smhDWMY])") or reason:match ("^(%d+)$") then return nil, "The reason looks odd. Did you mean to do xtempban?" end end local ip = nil local pl = minetest.get_player_by_name (player) if pl then ip = minetest.get_player_ip (player) end if ip ~= nil and expires == nil then local sw = ocutil.starts_with if sw (ip, "10.") or sw (ip, "172.") or sw (ip, "192.168.") then expires = os.time() + 1800 end end local rec = { source = source , time = os.time() , expires = expires , reason = reason , } table.insert (e.record, rec) e.names [player] = true if pl then if ip then e.names [ip] = true end e.last_pos = pl:getpos() 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 function xban.get_whitelist(name_or_ip) return db.whitelist and db.whitelist[name_or_ip] end function xban.remove_whitelist(name_or_ip) if db.whitelist then db.whitelist[name_or_ip] = nil end end function xban.add_whitelist(name_or_ip, source) local wl = db.whitelist if not wl then wl = { } db.whitelist = wl end wl[name_or_ip] = { source=source, } return true end function xban.get_record(player) local e = xban.find_entry(player) if not e then return nil, ("No entry for `%s'"):format(player) elseif (not e.record) or (#e.record == 0) then return nil, ("`%s' has no ban records"):format(player) end local record = { } for _, rec in ipairs(e.record) do local msg = rec.reason or "No reason given." if rec.expires then msg = msg..(", Expires: %s"):format(os.date("%c", e.expires)) end if rec.source then msg = msg..", Source: "..rec.source end table.insert(record, ("[%s]: %s"):format(os.date("%c", e.time), msg)) end local last_pos if e.last_pos then last_pos = ("User was last seen at %s"):format( minetest.pos_to_string(e.last_pos)) end return record, last_pos end -- =================================================================== xban2.members_list = {} xban2.members_file = minetest.get_worldpath() .. "/xban2_members.lua" xban2.members_flag = false local file = io.open (xban2.members_file, "r") if file then file:close() xban2.members_flag = true dofile (xban2.members_file) xban2.members_list = ocutil.vtable_to_ktable (xban2.members_list) end -- =================================================================== minetest.register_on_prejoinplayer (function (name, ip) -- If you'd like to exclude particular nicknames from IP-address and -- some other processing, add additional statements of the following -- form here: if name == "OldCoder" then return end if name == "moocow" then return end if xban2.members_flag and not xban2.members_list [name] then return ("This world is limited to private members") end local wl = db.whitelist or { } if wl[name] or wl[ip] then return end local e = xban.find_entry(name) or xban.find_entry(ip) if not e then return end 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_on_joinplayer(function(player) local name = player:get_player_name() -- If you'd like to exclude particular nicknames from IP-address and -- some other processing, add additional statements of the following -- form here: if name == "OldCoder" then return end if name == "moocow" then return end local e = xban.find_entry(name) local ip = minetest.get_player_ip(name) if not e then if ip then e = xban.find_entry(ip, true) else return end end e.names [name] = true if ip then e.names [ip] = true end e.last_seen = os.time() end) minetest.register_chatcommand ("xban", { description = "XBan a player", params = " ", privs = { ban=true }, func = function(name, params) local plname, reason = params:match("(%S+)%s+(.+)") if not (plname and reason) then return false, "Usage: /xban " end local ok, e = xban.ban_player(plname, name, nil, reason) return ok, ok and ("Banned %s."):format(plname) or e end, }) minetest.register_chatcommand ("xtempban", { description = "XBan a player temporarily", params = "