Add simple GUI to check records interactively.

This commit is contained in:
Diego Martinez 2014-07-09 06:32:24 -03:00
parent 19d92df3bc
commit 7923c393e9
2 changed files with 127 additions and 22 deletions

92
gui.lua Normal file
View File

@ -0,0 +1,92 @@
local FORMNAME = "xban2:main"
local states = { }
local table_insert, table_concat =
table.insert, table.concat
local ESC = minetest.formspec_escape
local function make_fs(name)
local state = states[name]
if not state then return end
local list, index, filter = state.list, state.index, state.filter
if index > #list then
index = #list
end
local fs = {
"size[10,8]",
"label[0.5,0.6;Filter]",
"field[1.5,0.5;6,2;filter;;"..ESC(filter).."]",
"button[7.5,0.5;2,1;search;Search]",
}
table_insert(fs,
("textlist[0.5,2;3,5.5;player;%s;%d;0]"):
format(table_concat(list, ","), index))
local record_name = list[index]
if record_name then
local record, err = xban.get_record(record_name)
if record then
local reclist = { }
for _, r in ipairs(record) do
table_insert(reclist, ESC(r))
end
table_insert(fs,
("textlist[4,2;5,5.5;entry;%s;0;0]"):
format(table_concat(reclist, ",")))
else
table_insert(fs,
"textlist[4,2;5,5.5;entry;"..ESC(err)..";0]")
end
end
fs = table_concat(fs)
print(fs)
return fs
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if formname ~= FORMNAME then return end
local name = player:get_player_name()
local state = states[name]
if fields.player then
local t = minetest.explode_textlist_event(fields.player)
if (t.type == "CHG") or (t.type == "DCL") then
state.index = t.index
minetest.show_formspec(name, FORMNAME, make_fs(name))
end
return
end
if fields.search then
local filter = fields.filter or ""
state.filter = filter
local list = { }
state.list = list
for k in pairs(minetest.auth_table) do
if k:find(filter, 1, true) then
table_insert(list, k)
end
end
table.sort(list)
minetest.show_formspec(name, FORMNAME, make_fs(name))
end
end)
minetest.register_chatcommand("xban_gui", {
description = "Show XBan GUI",
params = "",
func = function(name, params)
local state = states[name]
if not state then
state = { index=1, filter="" }
states[name] = state
local list = { }
state.list = list
for k in pairs(minetest.auth_table) do
table_insert(list, k)
end
table.sort(list)
end
minetest.show_formspec(name, FORMNAME, make_fs(name))
end,
})

View File

@ -131,6 +131,32 @@ function xban.unban_player(player, source) --> bool, err
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
if rec.expires then
msg = ("%s, Expires: %s"):format(
rec.reason, os.date("%c", e.expires))
else
msg = rec.reason
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
minetest.register_on_prejoinplayer(function(name, ip)
local e = xban.find_entry(name) or xban.find_entry(ip)
if not e then return end
@ -220,31 +246,17 @@ minetest.register_chatcommand("xban_record", {
"Usage: /xban_record <player_or_ip>")
return
end
local e = xban.find_entry(params)
if not e then
minetest.chat_send_player(name,
("[xban_record] No entry for `%s'"):format(params))
return
elseif (not e.record) or (#e.record == 0) then
minetest.chat_send_player(name,
("[xban_record] `%s' has no ban records"):format(params))
local record, last_pos = xban.get_record(plname)
if not record then
local err = last_pos
minetest.chat_send_player(name, "[xban] "..err)
return
end
for _, rec in ipairs(e.record) do
local msg
if rec.expires then
msg = ("%s, Expires: %s"):format(
rec.reason, os.date("%c", rec.expires))
else
msg = rec.reason
end
minetest.chat_send_player(name,
("[%s]: %s"):format(os.date("%c", rec.time), msg))
for _, e in ipairs(record) do
minetest.chat_send_player(name, "[xban] "..e)
end
if e.last_pos then
minetest.chat_send_player(name,
("User was last seen at %s"):format(
minetest.pos_to_string(e.last_pos)))
if last_pos then
minetest.chat_send_player(name, "[xban] "..last_pos)
end
end,
})
@ -311,3 +323,4 @@ load_db()
xban.db = db
dofile(xban.MP.."/dbimport.lua")
dofile(xban.MP.."/gui.lua")