aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgpcf <gpcf@gpcf.eu>2024-07-27 23:23:39 +0200
committergpcf <gpcf@gpcf.eu>2024-07-27 23:23:39 +0200
commit8d9fe6845ecd8a14de2c973547d3ae98107d635f (patch)
treef13b4d0ac285d660b129e1ea8cd5c818e57f76ee
parent9340e2441c778ce5ba20ad3d7fe56e771560cbbd (diff)
parente870b8d1d6963c762e980a3cccfd5effdec61df9 (diff)
downloadxban2-8d9fe6845ecd8a14de2c973547d3ae98107d635f.tar.gz
xban2-8d9fe6845ecd8a14de2c973547d3ae98107d635f.tar.bz2
xban2-8d9fe6845ecd8a14de2c973547d3ae98107d635f.zip
Merge branch 'master' of https://github.com/minetest-mods/xban2
-rw-r--r--LICENSE24
-rw-r--r--gui.lua5
-rw-r--r--init.lua30
-rw-r--r--serialize.lua39
4 files changed, 83 insertions, 15 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..aea08cc
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,24 @@
+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.
diff --git a/gui.lua b/gui.lua
index 99ae6ec..24b2e69 100644
--- a/gui.lua
+++ b/gui.lua
@@ -61,7 +61,8 @@ local function make_fs(name)
"size[16,12]",
"label[0,-.1;Filter]",
"field[1.5,0;12.8,1;filter;;"..ESC(filter).."]",
- "button[14,-.3;2,1;search;Search]",
+ "field_close_on_enter[filter;false]",
+ "button[14,-.3;2,1;search_submit;Search]",
}
local fsn = #fs
fsn=fsn+1 fs[fsn] = format("textlist[0,.8;4,9.3;player;%s;%d;0]",
@@ -122,7 +123,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
return
end
- if fields.search then
+ if fields.search_submit or fields.filter then
local filter = fields.filter or ""
state.filter = filter
state.list = make_list(filter)
diff --git a/init.lua b/init.lua
index 5a0e1cf..576e0d0 100644
--- a/init.lua
+++ b/init.lua
@@ -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"))
diff --git a/serialize.lua b/serialize.lua
index c159141..0a54212 100644
--- a/serialize.lua
+++ b/serialize.lua
@@ -27,5 +27,44 @@ local function my_serialize_2(t, level)
end
function xban.serialize(t)
+ minetest.log("warning", "[xban2] xban.serialize() is deprecated")
return "return {\n"..my_serialize_2(t, 1).."\n}"
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