forked from Mirrorlandia_minetest/minetest
Add last_login field to auth.txt
Also shortens some related code and adds more parameters to string.split.
This commit is contained in:
parent
c40e993ce4
commit
90b6de173e
@ -155,13 +155,33 @@ function dump(o, indent, nested, level)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function string:split(sep)
|
function string.split(str, delim, include_empty, max_splits)
|
||||||
local sep, fields = sep or ",", {}
|
delim = delim or ","
|
||||||
local pattern = string.format("([^%s]+)", sep)
|
max_splits = max_splits or 0
|
||||||
self:gsub(pattern, function(c) fields[#fields+1] = c end)
|
local fields = {}
|
||||||
|
local num_splits = 0
|
||||||
|
local last_pos = 0
|
||||||
|
for part, pos in str:gmatch("(.-)[" .. delim .. "]()") do
|
||||||
|
last_pos = pos
|
||||||
|
if include_empty or part ~= "" then
|
||||||
|
num_splits = num_splits + 1
|
||||||
|
fields[num_splits] = part
|
||||||
|
if max_splits > 0 and num_splits + 1 >= max_splits then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
-- Handle the last field
|
||||||
|
if max_splits <= 0 or num_splits <= max_splits then
|
||||||
|
local last_part = str:sub(last_pos)
|
||||||
|
if include_empty or last_part ~= "" then
|
||||||
|
fields[num_splits + 1] = last_part
|
||||||
|
end
|
||||||
|
end
|
||||||
return fields
|
return fields
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
function file_exists(filename)
|
function file_exists(filename)
|
||||||
local f = io.open(filename, "r")
|
local f = io.open(filename, "r")
|
||||||
|
@ -41,12 +41,14 @@ local function read_auth_file()
|
|||||||
end
|
end
|
||||||
for line in file:lines() do
|
for line in file:lines() do
|
||||||
if line ~= "" then
|
if line ~= "" then
|
||||||
local name, password, privilegestring = string.match(line, "([^:]*):([^:]*):([^:]*)")
|
local fields = line:split(":", true)
|
||||||
if not name or not password or not privilegestring then
|
local name, password, privilege_string, last_login = unpack(fields)
|
||||||
|
last_login = tonumber(last_login)
|
||||||
|
if not (name and password and privilege_string) then
|
||||||
error("Invalid line in auth.txt: "..dump(line))
|
error("Invalid line in auth.txt: "..dump(line))
|
||||||
end
|
end
|
||||||
local privileges = core.string_to_privs(privilegestring)
|
local privileges = core.string_to_privs(privilege_string)
|
||||||
newtable[name] = {password=password, privileges=privileges}
|
newtable[name] = {password=password, privileges=privileges, last_login=last_login}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
io.close(file)
|
io.close(file)
|
||||||
@ -63,14 +65,16 @@ local function save_auth_file()
|
|||||||
assert(type(stuff) == "table")
|
assert(type(stuff) == "table")
|
||||||
assert(type(stuff.password) == "string")
|
assert(type(stuff.password) == "string")
|
||||||
assert(type(stuff.privileges) == "table")
|
assert(type(stuff.privileges) == "table")
|
||||||
|
assert(stuff.last_login == nil or type(stuff.last_login) == "number")
|
||||||
end
|
end
|
||||||
local file, errmsg = io.open(core.auth_file_path, 'w+b')
|
local file, errmsg = io.open(core.auth_file_path, 'w+b')
|
||||||
if not file then
|
if not file then
|
||||||
error(core.auth_file_path.." could not be opened for writing: "..errmsg)
|
error(core.auth_file_path.." could not be opened for writing: "..errmsg)
|
||||||
end
|
end
|
||||||
for name, stuff in pairs(core.auth_table) do
|
for name, stuff in pairs(core.auth_table) do
|
||||||
local privstring = core.privs_to_string(stuff.privileges)
|
local priv_string = core.privs_to_string(stuff.privileges)
|
||||||
file:write(name..":"..stuff.password..":"..privstring..'\n')
|
local parts = {name, stuff.password, priv_string, stuff.last_login or ""}
|
||||||
|
file:write(table.concat(parts, ":").."\n")
|
||||||
end
|
end
|
||||||
io.close(file)
|
io.close(file)
|
||||||
end
|
end
|
||||||
@ -111,6 +115,8 @@ core.builtin_auth_handler = {
|
|||||||
return {
|
return {
|
||||||
password = core.auth_table[name].password,
|
password = core.auth_table[name].password,
|
||||||
privileges = privileges,
|
privileges = privileges,
|
||||||
|
-- Is set to nil if unknown
|
||||||
|
last_login = core.auth_table[name].last_login,
|
||||||
}
|
}
|
||||||
end,
|
end,
|
||||||
create_auth = function(name, password)
|
create_auth = function(name, password)
|
||||||
@ -120,6 +126,7 @@ core.builtin_auth_handler = {
|
|||||||
core.auth_table[name] = {
|
core.auth_table[name] = {
|
||||||
password = password,
|
password = password,
|
||||||
privileges = core.string_to_privs(core.setting_get("default_privs")),
|
privileges = core.string_to_privs(core.setting_get("default_privs")),
|
||||||
|
last_login = os.time(),
|
||||||
}
|
}
|
||||||
save_auth_file()
|
save_auth_file()
|
||||||
end,
|
end,
|
||||||
@ -139,7 +146,9 @@ core.builtin_auth_handler = {
|
|||||||
assert(type(name) == "string")
|
assert(type(name) == "string")
|
||||||
assert(type(privileges) == "table")
|
assert(type(privileges) == "table")
|
||||||
if not core.auth_table[name] then
|
if not core.auth_table[name] then
|
||||||
core.builtin_auth_handler.create_auth(name, core.get_password_hash(name, core.setting_get("default_password")))
|
core.builtin_auth_handler.create_auth(name,
|
||||||
|
core.get_password_hash(name,
|
||||||
|
core.setting_get("default_password")))
|
||||||
end
|
end
|
||||||
core.auth_table[name].privileges = privileges
|
core.auth_table[name].privileges = privileges
|
||||||
core.notify_authentication_modified(name)
|
core.notify_authentication_modified(name)
|
||||||
@ -149,6 +158,11 @@ core.builtin_auth_handler = {
|
|||||||
read_auth_file()
|
read_auth_file()
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
|
record_login = function(name)
|
||||||
|
assert(type(name) == "string")
|
||||||
|
assert(core.auth_table[name]).last_login = os.time()
|
||||||
|
save_auth_file()
|
||||||
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
function core.register_authentication_handler(handler)
|
function core.register_authentication_handler(handler)
|
||||||
@ -160,29 +174,27 @@ function core.register_authentication_handler(handler)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function core.get_auth_handler()
|
function core.get_auth_handler()
|
||||||
if core.registered_auth_handler then
|
return core.registered_auth_handler or core.builtin_auth_handler
|
||||||
return core.registered_auth_handler
|
|
||||||
end
|
|
||||||
return core.builtin_auth_handler
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function core.set_player_password(name, password)
|
local function auth_pass(name)
|
||||||
if core.get_auth_handler().set_password then
|
return function(...)
|
||||||
core.get_auth_handler().set_password(name, password)
|
local auth_handler = core.get_auth_handler()
|
||||||
end
|
if auth_handler[name] then
|
||||||
end
|
return auth_handler[name](...)
|
||||||
|
|
||||||
function core.set_player_privs(name, privs)
|
|
||||||
if core.get_auth_handler().set_privileges then
|
|
||||||
core.get_auth_handler().set_privileges(name, privs)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function core.auth_reload()
|
|
||||||
if core.get_auth_handler().reload then
|
|
||||||
return core.get_auth_handler().reload()
|
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
core.set_player_password = auth_pass("set_password")
|
||||||
|
core.set_player_privs = auth_pass("set_privileges")
|
||||||
|
core.auth_reload = auth_pass("reload")
|
||||||
|
|
||||||
|
|
||||||
|
local record_login = auth_pass("record_login")
|
||||||
|
|
||||||
|
core.register_on_joinplayer(function(player)
|
||||||
|
record_login(player:get_player_name())
|
||||||
|
end)
|
||||||
|
|
||||||
|
@ -723,3 +723,20 @@ core.register_chatcommand("msg", {
|
|||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
core.register_chatcommand("last-login", {
|
||||||
|
params = "[name]",
|
||||||
|
description = "Get the last login time of a player",
|
||||||
|
func = function(name, param)
|
||||||
|
if param == "" then
|
||||||
|
param = name
|
||||||
|
end
|
||||||
|
local pauth = core.get_auth_handler().get_auth(param)
|
||||||
|
if pauth and pauth.last_login then
|
||||||
|
-- Time in UTC, ISO 8601 format
|
||||||
|
return true, "Last login time was " ..
|
||||||
|
os.date("!%Y-%m-%dT%H:%M:%SZ", pauth.last_login)
|
||||||
|
end
|
||||||
|
return false, "Last login time is unknown"
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user