mirror of
https://github.com/minetest-mods/xban2.git
synced 2025-06-30 15:30:24 +02:00
Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
58e77ad16c | |||
af26ae75bd | |||
8fde3c240f | |||
d200b342e9 |
24
LICENSE
24
LICENSE
@ -1,24 +0,0 @@
|
|||||||
BSD 2-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2014-2023, Diego Martínez
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@ -104,9 +104,3 @@ the supported import plugins at the time of writing:
|
|||||||
* `v2`: Old format used by xban (`players.iplist.v2`).
|
* `v2`: Old format used by xban (`players.iplist.v2`).
|
||||||
|
|
||||||
**Example:** `/xban_dbi minetest`
|
**Example:** `/xban_dbi minetest`
|
||||||
|
|
||||||
### `xban_cleanup`
|
|
||||||
|
|
||||||
Removes all non-banned entries from the xban db.
|
|
||||||
|
|
||||||
**Usage:** `/xban_cleanup`
|
|
||||||
|
9
gui.lua
9
gui.lua
@ -9,7 +9,9 @@ local ESC = minetest.formspec_escape
|
|||||||
local function make_list(filter)
|
local function make_list(filter)
|
||||||
filter = filter or ""
|
filter = filter or ""
|
||||||
local list, n, dropped = { }, 0, false
|
local list, n, dropped = { }, 0, false
|
||||||
for k in minetest.get_auth_handler().iterate() do
|
local enumerate, e1, e2, e3 = minetest.get_auth_handler().enumerate_auths
|
||||||
|
if enumerate then e1 = enumerate() else e1, e2, e3 = pairs(minetest.auth_table) end
|
||||||
|
for k in e1, e2, e3 do
|
||||||
if strfind(k, filter, 1, true) then
|
if strfind(k, filter, 1, true) then
|
||||||
if n >= MAXLISTSIZE then
|
if n >= MAXLISTSIZE then
|
||||||
dropped = true
|
dropped = true
|
||||||
@ -61,8 +63,7 @@ local function make_fs(name)
|
|||||||
"size[16,12]",
|
"size[16,12]",
|
||||||
"label[0,-.1;Filter]",
|
"label[0,-.1;Filter]",
|
||||||
"field[1.5,0;12.8,1;filter;;"..ESC(filter).."]",
|
"field[1.5,0;12.8,1;filter;;"..ESC(filter).."]",
|
||||||
"field_close_on_enter[filter;false]",
|
"button[14,-.3;2,1;search;Search]",
|
||||||
"button[14,-.3;2,1;search_submit;Search]",
|
|
||||||
}
|
}
|
||||||
local fsn = #fs
|
local fsn = #fs
|
||||||
fsn=fsn+1 fs[fsn] = format("textlist[0,.8;4,9.3;player;%s;%d;0]",
|
fsn=fsn+1 fs[fsn] = format("textlist[0,.8;4,9.3;player;%s;%d;0]",
|
||||||
@ -123,7 +124,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if fields.search_submit or fields.filter then
|
if fields.search then
|
||||||
local filter = fields.filter or ""
|
local filter = fields.filter or ""
|
||||||
state.filter = filter
|
state.filter = filter
|
||||||
state.list = make_list(filter)
|
state.list = make_list(filter)
|
||||||
|
56
init.lua
56
init.lua
@ -25,7 +25,6 @@ end
|
|||||||
|
|
||||||
local ACTION = make_logger("action")
|
local ACTION = make_logger("action")
|
||||||
local WARNING = make_logger("warning")
|
local WARNING = make_logger("warning")
|
||||||
local ERROR = make_logger("error")
|
|
||||||
|
|
||||||
local unit_to_secs = {
|
local unit_to_secs = {
|
||||||
s = 1, m = 60, h = 3600,
|
s = 1, m = 60, h = 3600,
|
||||||
@ -41,14 +40,6 @@ local function parse_time(t) --> secs
|
|||||||
return secs
|
return secs
|
||||||
end
|
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
|
function xban.find_entry(player, create) --> entry, index
|
||||||
for index, e in ipairs(db) do
|
for index, e in ipairs(db) do
|
||||||
for name in pairs(e.names) do
|
for name in pairs(e.names) do
|
||||||
@ -120,7 +111,7 @@ function xban.ban_player(player, source, expires, reason) --> bool, err
|
|||||||
end
|
end
|
||||||
ACTION("%s bans %s until %s for reason: %s", source, player,
|
ACTION("%s bans %s until %s for reason: %s", source, player,
|
||||||
date, reason)
|
date, reason)
|
||||||
ACTION("Banned Names/IPs: %s", concat_keys(e.names, ", "))
|
ACTION("Banned Names/IPs: %s", table.concat(e.names, ", "))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -140,7 +131,7 @@ function xban.unban_player(player, source) --> bool, err
|
|||||||
e.expires = nil
|
e.expires = nil
|
||||||
e.time = nil
|
e.time = nil
|
||||||
ACTION("%s unbans %s", source, player)
|
ACTION("%s unbans %s", source, player)
|
||||||
ACTION("Unbanned Names/IPs: %s", concat_keys(e.names, ", "))
|
ACTION("Unbanned Names/IPs: %s", table.concat(e.names, ", "))
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -323,7 +314,6 @@ minetest.register_chatcommand("xban_wl", {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
local function check_temp_bans()
|
local function check_temp_bans()
|
||||||
minetest.after(60, check_temp_bans)
|
minetest.after(60, check_temp_bans)
|
||||||
local to_rm = { }
|
local to_rm = { }
|
||||||
@ -344,12 +334,18 @@ end
|
|||||||
|
|
||||||
local function save_db()
|
local function save_db()
|
||||||
minetest.after(SAVE_INTERVAL, save_db)
|
minetest.after(SAVE_INTERVAL, save_db)
|
||||||
|
local f, e = io.open(DB_FILENAME, "wt")
|
||||||
db.timestamp = os.time()
|
db.timestamp = os.time()
|
||||||
local contents = assert(xban.serialize_db(db))
|
if f then
|
||||||
local ok = minetest.safe_file_write(DB_FILENAME, contents)
|
local ok, err = f:write(xban.serialize(db))
|
||||||
if not ok then
|
if not ok then
|
||||||
ERROR("Unable to save database")
|
WARNING("Unable to save database: %s", err)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
WARNING("Unable to save database: %s", e)
|
||||||
end
|
end
|
||||||
|
if f then f:close() end
|
||||||
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local function load_db()
|
local function load_db()
|
||||||
@ -363,7 +359,7 @@ local function load_db()
|
|||||||
WARNING("Unable to load database: %s", "Read failed")
|
WARNING("Unable to load database: %s", "Read failed")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
local t, e2 = xban.deserialize_db(cont)
|
local t, e2 = minetest.deserialize(cont)
|
||||||
if not t then
|
if not t then
|
||||||
WARNING("Unable to load database: %s",
|
WARNING("Unable to load database: %s",
|
||||||
"Deserialization failed: "..(e2 or "unknown error"))
|
"Deserialization failed: "..(e2 or "unknown error"))
|
||||||
@ -378,30 +374,6 @@ local function load_db()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_chatcommand("xban_cleanup", {
|
|
||||||
description = "Removes all non-banned entries from the xban db",
|
|
||||||
privs = { server=true },
|
|
||||||
func = function(name, params)
|
|
||||||
local old_count = #db
|
|
||||||
|
|
||||||
local i = 1
|
|
||||||
while i <= #db do
|
|
||||||
if not db[i].banned then
|
|
||||||
-- not banned, remove from db
|
|
||||||
table.remove(db, i)
|
|
||||||
else
|
|
||||||
-- banned, hold entry back
|
|
||||||
i = i + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- save immediately
|
|
||||||
save_db()
|
|
||||||
|
|
||||||
return true, "Removed " .. (old_count - #db) .. " entries, new db entry-count: " .. #db
|
|
||||||
end,
|
|
||||||
})
|
|
||||||
|
|
||||||
minetest.register_on_shutdown(save_db)
|
minetest.register_on_shutdown(save_db)
|
||||||
minetest.after(SAVE_INTERVAL, save_db)
|
minetest.after(SAVE_INTERVAL, save_db)
|
||||||
load_db()
|
load_db()
|
||||||
@ -411,3 +383,5 @@ minetest.after(1, check_temp_bans)
|
|||||||
|
|
||||||
dofile(xban.MP.."/dbimport.lua")
|
dofile(xban.MP.."/dbimport.lua")
|
||||||
dofile(xban.MP.."/gui.lua")
|
dofile(xban.MP.."/gui.lua")
|
||||||
|
|
||||||
|
minetest.log("action", "[xban2] loaded.")
|
||||||
|
@ -27,44 +27,5 @@ local function my_serialize_2(t, level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function xban.serialize(t)
|
function xban.serialize(t)
|
||||||
minetest.log("warning", "[xban2] xban.serialize() is deprecated")
|
|
||||||
return "return {\n"..my_serialize_2(t, 1).."\n}"
|
return "return {\n"..my_serialize_2(t, 1).."\n}"
|
||||||
end
|
end
|
||||||
|
|
||||||
-- JSON doesn't allow combined string+number keys, this function moves any
|
|
||||||
-- number keys into an "entries" table
|
|
||||||
function xban.serialize_db(t)
|
|
||||||
local res = {}
|
|
||||||
local entries = {}
|
|
||||||
for k, v in pairs(t) do
|
|
||||||
if type(k) == "number" then
|
|
||||||
entries[k] = v
|
|
||||||
else
|
|
||||||
res[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
res.entries = entries
|
|
||||||
return minetest.write_json(res, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
function xban.deserialize_db(s)
|
|
||||||
if s:sub(1, 1) ~= "{" then
|
|
||||||
-- Load legacy databases
|
|
||||||
return minetest.deserialize(s)
|
|
||||||
end
|
|
||||||
|
|
||||||
local res, err = minetest.parse_json(s)
|
|
||||||
if not res then
|
|
||||||
return nil, err
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Remove all "null"s added by empty tables
|
|
||||||
for i, entry in ipairs(res.entries or {}) do
|
|
||||||
entry.names = entry.names or {}
|
|
||||||
entry.record = entry.record or {}
|
|
||||||
res[i] = entry
|
|
||||||
end
|
|
||||||
res.entries = nil
|
|
||||||
|
|
||||||
return res
|
|
||||||
end
|
|
||||||
|
Reference in New Issue
Block a user