2017-07-05 14:31:20 -07:00
|
|
|
--[[
|
|
|
|
original code provided by tenplus1
|
|
|
|
This mod instigates an effective border for new players joining a server
|
2018-04-18 14:37:24 -07:00
|
|
|
with persistence of the last state and visa records across server restarts
|
2017-07-05 14:31:20 -07:00
|
|
|
]]
|
|
|
|
|
|
|
|
local mod_data = minetest.get_mod_storage()
|
2018-04-18 14:37:24 -07:00
|
|
|
local border = mod_data:get_string("status")
|
|
|
|
local visa = mod_data:get_string("visa")
|
2019-09-23 13:07:07 -07:00
|
|
|
local duration = minetest.settings:get("border.visa_duration") or 86400 -- 1 day
|
|
|
|
local msg = minetest.settings:get("border.msg") or "\nSorry, no new players being admitted at this time!"
|
2017-07-05 14:31:20 -07:00
|
|
|
|
2018-04-18 14:37:24 -07:00
|
|
|
local function update_visa_cache(name)
|
2019-09-23 13:07:07 -07:00
|
|
|
-- If name exists in cache remove it
|
2018-04-18 14:37:24 -07:00
|
|
|
if visa[name] then
|
|
|
|
visa[name] = nil
|
|
|
|
collectgarbage()
|
|
|
|
else
|
|
|
|
-- cache visa
|
|
|
|
visa[name] = os.time() + duration
|
|
|
|
end
|
|
|
|
mod_data:set_string("visa", minetest.serialize(visa))
|
|
|
|
end
|
|
|
|
|
|
|
|
if border == "" then
|
2019-09-23 13:07:07 -07:00
|
|
|
-- Initialise
|
2018-04-18 15:46:32 -07:00
|
|
|
mod_data:set_string("status", "OPEN")
|
|
|
|
border = "OPEN"
|
2017-07-05 14:31:20 -07:00
|
|
|
end
|
|
|
|
|
2018-04-18 14:37:24 -07:00
|
|
|
if visa == "" then
|
|
|
|
-- initialise
|
|
|
|
visa = {}
|
|
|
|
else
|
|
|
|
-- load visa table
|
|
|
|
visa = minetest.deserialize(visa)
|
|
|
|
-- iterate
|
|
|
|
for name,expires in pairs(visa) do
|
|
|
|
local t_remains = expires - os.time()
|
|
|
|
if t_remains > 0 then
|
|
|
|
--set
|
|
|
|
minetest.after(t_remains, update_visa_cache, name)
|
|
|
|
else
|
|
|
|
-- remove
|
|
|
|
update_visa_cache(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-07-05 14:31:20 -07:00
|
|
|
|
2017-07-05 16:30:14 -07:00
|
|
|
-- announce status
|
|
|
|
minetest.after(5, function()
|
|
|
|
minetest.chat_send_all("[border:info] border is "..border)
|
|
|
|
end)
|
|
|
|
|
2018-04-18 14:37:24 -07:00
|
|
|
-- chat command to toggle the border status
|
2017-07-05 14:31:20 -07:00
|
|
|
minetest.register_chatcommand("border", {
|
|
|
|
params = "",
|
|
|
|
description = "Toggles if new players are allowed",
|
|
|
|
privs = {server = true},
|
|
|
|
func = function (name, param)
|
2017-07-05 16:30:14 -07:00
|
|
|
if border == "CLOSED" then
|
|
|
|
border = "OPEN"
|
|
|
|
minetest.chat_send_player(name, "[border:info] allowing new players.")
|
2017-07-05 14:31:20 -07:00
|
|
|
else
|
2017-07-05 16:30:14 -07:00
|
|
|
border = "CLOSED"
|
|
|
|
minetest.chat_send_player(name, "[border:info] refusing new players.")
|
2017-07-05 14:31:20 -07:00
|
|
|
end
|
2018-04-18 14:37:24 -07:00
|
|
|
mod_data:set_string("status", border) -- save current state
|
2017-07-05 14:31:20 -07:00
|
|
|
end
|
2019-09-23 13:07:07 -07:00
|
|
|
})
|
2017-07-05 14:31:20 -07:00
|
|
|
|
2018-04-18 14:37:24 -07:00
|
|
|
-- add visa
|
2018-01-28 17:02:42 -08:00
|
|
|
minetest.register_chatcommand("visa", {
|
|
|
|
params = "player",
|
|
|
|
description = "Adds a temporary visa allowing a new player to create an account",
|
|
|
|
privs = {server = true},
|
|
|
|
func = function (name, param)
|
|
|
|
if not param then
|
|
|
|
minetest.chat_send_player(name, "Use: /visa <name>")
|
|
|
|
end
|
2018-04-18 14:37:24 -07:00
|
|
|
update_visa_cache(param)
|
|
|
|
minetest.after(duration, update_visa_cache, param)
|
|
|
|
minetest.chat_send_player(name, "A visa was issued to "..param)
|
2018-01-28 17:02:42 -08:00
|
|
|
end
|
|
|
|
})
|
|
|
|
|
2018-04-18 14:37:24 -07:00
|
|
|
-- register callback
|
2017-07-05 14:31:20 -07:00
|
|
|
minetest.register_on_prejoinplayer(function(name, ip)
|
2017-10-22 10:19:14 -07:00
|
|
|
-- owner exception
|
2017-07-05 16:20:51 -07:00
|
|
|
if minetest.setting_get("name") == name then
|
2017-10-22 10:19:14 -07:00
|
|
|
return
|
2017-07-05 16:20:51 -07:00
|
|
|
end
|
2018-04-18 14:37:24 -07:00
|
|
|
-- stop NEW players from joining unless they have a visa
|
|
|
|
local player_exists = minetest.get_auth_handler().get_auth(name)
|
|
|
|
if border == "CLOSED" and not player_exists and not visa[name] then
|
|
|
|
return msg
|
2017-10-22 10:19:14 -07:00
|
|
|
end
|
2018-04-18 14:37:24 -07:00
|
|
|
if visa[name] then update_visa_cache(name) end -- remove visa
|
2017-10-22 10:19:14 -07:00
|
|
|
end
|
2017-07-05 14:31:20 -07:00
|
|
|
)
|