From 51d605154037fb1f679b561e90db71163fe8a9f8 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Sun, 21 Jan 2024 20:46:32 +0100 Subject: [PATCH] `set_join_password` handler --- handlers/joinpassword.lua | 63 +++++++++++++++++++++++++++++++++++++++ init.lua | 1 + 2 files changed, 64 insertions(+) create mode 100644 handlers/joinpassword.lua diff --git a/handlers/joinpassword.lua b/handlers/joinpassword.lua new file mode 100644 index 0000000..4663b4d --- /dev/null +++ b/handlers/joinpassword.lua @@ -0,0 +1,63 @@ +local function get_store() + return minetest.parse_json(mtui.mod_storage:get_string("join_passwords")) or {} +end + +local function set_store(e) + mtui.mod_storage:set_string("join_passwords", minetest.write_json(e)) +end + +-- sets a temporary join-password (to be set via web, for the wasm client) +mtui.register_on_command("set_join_password", function(data) + + -- store old password + local store = get_store() + local auth = minetest.get_auth_handler().get_auth(data.username) + if not auth then + return false + end + store[data.username] = { + time = os.time(), + password = auth.password + } + set_store(store) + + -- set new join password + minetest.log("action", "[mtui] setting join-password for '" .. data.username .."'") + minetest.get_auth_handler().set_password(data.username, data.password) + + return true +end) + + +-- replace previously set join-passwords with original value +local function restore_join_passwords() + local store = get_store() + local now = os.time() + for playername, entry in pairs(store) do + local diff = now - entry.time + if diff > 60 then + -- restore password and clear entry + minetest.log("action", "[mtui] restoring join-password for '" .. playername .."' (timeout)") + minetest.get_auth_handler().set_password(playername, entry.password) + store[playername] = nil + end + end + set_store(store) + + minetest.after(10, restore_join_passwords) +end +minetest.after(10, restore_join_passwords) + +-- restore old password on login +minetest.register_on_joinplayer(function(player) + local playername = player:get_player_name() + + local store = get_store() + if not store[playername] then + return + end + minetest.log("action", "[mtui] restoring join-password for '" .. playername .."' (successful login)") + minetest.get_auth_handler().set_password(playername, store[playername].password) + store[playername] = nil + set_store(store) +end) \ No newline at end of file diff --git a/init.lua b/init.lua index 522dfe2..ccf39e8 100644 --- a/init.lua +++ b/init.lua @@ -33,6 +33,7 @@ dofile(MP.."/controls/restart_if_empty.lua") dofile(MP.."/handlers/ping.lua") dofile(MP.."/handlers/items.lua") dofile(MP.."/handlers/chat.lua") +dofile(MP.."/handlers/joinpassword.lua") dofile(MP.."/handlers/execute_command.lua") dofile(MP.."/handlers/lua.lua") dofile(MP.."/handlers/controls.lua")