diff options
Diffstat (limited to 'init.lua')
-rw-r--r-- | init.lua | 30 |
1 files changed, 17 insertions, 13 deletions
@@ -26,6 +26,7 @@ end local ACTION = make_logger("action") local WARNING = make_logger("warning") +local ERROR = make_logger("error") local unit_to_secs = { s = 1, m = 60, h = 3600, @@ -41,11 +42,20 @@ local function parse_time(t) --> secs return secs end + function xban.is_ip(name) -- checks if name is an ipv4 or ipv6 address return string.match(name, "%.") or string.match(name, "%:") end +local function concat_keys(t, sep) + local keys = {} + for k, _ in pairs(t) do + keys[#keys + 1] = k + end + return table.concat(keys, sep) +end + function xban.find_entry(player, create) --> entry, index for index, e in ipairs(db) do for name in pairs(e.names) do @@ -135,7 +145,7 @@ function xban.ban_player(player, source, expires, reason) --> bool, err end ACTION("%s bans %s until %s for reason: %s", source, player, date, reason) - ACTION("Banned Names/IPs: %s", table.concat(e.names, ", ")) + ACTION("Banned Names/IPs: %s", concat_keys(e.names, ", ")) return true end @@ -156,7 +166,7 @@ function xban.unban_player(player, source, reason) --> bool, err e.expires = nil e.time = nil ACTION("%s unbans %s", source, player) - ACTION("Unbanned Names/IPs: %s", table.concat(e.names, ", ")) + ACTION("Unbanned Names/IPs: %s", concat_keys(e.names, ", ")) return true end @@ -579,18 +589,12 @@ end local function save_db() minetest.after(SAVE_INTERVAL, save_db) - local f, e = io.open(DB_FILENAME, "wt") db.timestamp = os.time() - if f then - local ok, err = f:write(xban.serialize(db)) - if not ok then - WARNING("Unable to save database: %s", err) - end - else - WARNING("Unable to save database: %s", e) + local contents = assert(xban.serialize_db(db)) + local ok = minetest.safe_file_write(DB_FILENAME, contents) + if not ok then + ERROR("Unable to save database") end - if f then f:close() end - return end local function load_db() @@ -604,7 +608,7 @@ local function load_db() WARNING("Unable to load database: %s", "Read failed") return end - local t, e2 = minetest.deserialize(cont) + local t, e2 = xban.deserialize_db(cont) if not t then WARNING("Unable to load database: %s", "Deserialization failed: "..(e2 or "unknown error")) |