Skip to content

Commit

Permalink
Rework password protected join
Browse files Browse the repository at this point in the history
  • Loading branch information
S-S-X committed Jan 7, 2024
1 parent 117cb99 commit 1376a3b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 38 deletions.
47 changes: 9 additions & 38 deletions plugin/acl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,24 @@
-- ACL core functionality / access level checks
--

local acls = dofile(minetest.get_modpath("beerchat").."/plugin/acl/acls.lua")(
local srcdir = minetest.get_modpath("beerchat").."/plugin/acl"

local acls = dofile(srcdir .. "/acls.lua")(
minetest.deserialize(beerchat.mod_storage:get("acl.acls")),
function (data) beerchat.mod_storage:set_string("acl.acls", minetest.serialize(data)) end
)

local password_protected_join = dofile(srcdir .. "/password.lua")

--
-- Access level / authorization checks for player actions
--

local password_requests = {}
beerchat.register_callback('before_join', function(name, _, data)
return password_protected_join(name, data)
end)

local function handle_password_protected_join(name, password)
local data = password_requests[name]
if type(data) ~= "table" or type(data.channel) ~= "string" or data.channel == "" then
minetest.log("warning", "Invalid password_requests data for player '" .. name .. "'")
minetest.chat_send_player(name, "ERROR: Something went wrong with authorization, please try joining again.")
return
end
local channel = beerchat.channels[data.channel]
if type(channel) ~= "table" then
minetest.chat_send_player(name, "ERROR: Channel #"..data.channel.." disappeared while joining.")
return
end
if password == channel.password then
minetest.chat_send_player(name, "OK: Channel #"..data.channel.." password accepted.")
;(data.set_default and beerchat.set_player_channel or beerchat.add_player_channel)(name, data.channel)
else
minetest.chat_send_player(name, "ERROR: Invalid password, please verify password and try joining again.")
end
end

beerchat.register_callback('before_join', function(name, channel_name, data)
local channel = beerchat.channels[channel_name]
if channel.password and channel.password ~= "" then
if not data or not data.password or data.password == "" then
-- Channel has password but nothing has provided any password so far, ask player to provide password
password_requests[name] = { channel = channel_name, set_default = data.set_default }
beerchat.capture_message(name, handle_password_protected_join)
return false, minetest.colorize("#f00d00", "ATTENTION:") .. "This channel requires that you supply"
.. " a password. Your next message will be used as a password and hidden from other players.\n"
.. minetest.colorize("#f00d00", "Please enter your password:")
end
-- External password handling mechanism has already provided password for this channel, verify it
if data.password ~= channel.password then
return false, "ERROR: Invalid password."
end
end
beerchat.register_callback('before_join', function(name, _, data)
return acls:check_access(data.channel, name)
end)

Expand Down
39 changes: 39 additions & 0 deletions plugin/acl/password.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- Channel password handler

local function protected_join(name, password, data)
if type(data) ~= "table" or type(data.channel) ~= "string" or data.channel == "" then
minetest.log("warning", "Invalid password_requests data for player '" .. name .. "'")
minetest.chat_send_player(name, "ERROR: Something went wrong with authorization, please try joining again.")
return
end
local channel = beerchat.channels[data.channel]
if type(channel) ~= "table" then
minetest.chat_send_player(name, "ERROR: Channel #"..data.channel.." disappeared while joining.")
return
end
if password == channel.password then
minetest.chat_send_player(name, "OK: Channel #"..data.channel.." password accepted.")
;(data.set_default and beerchat.set_player_channel or beerchat.add_player_channel)(name, data.channel)
else
minetest.chat_send_player(name, "ERROR: Invalid password, please verify password and try joining again.")
end
end

return function(name, data)
local channel = beerchat.channels[data.channel]
if channel and channel.password and channel.password ~= "" then
if not data or not data.password or data.password == "" then
-- Channel has password but nothing has provided any password so far, ask player to provide password
beerchat.capture_message(name, function(playername, password)
protected_join(playername, password, { channel = data.channel, set_default = data.set_default })
end)
return false, minetest.colorize("#f00d00", "ATTENTION:") .. "This channel requires that you supply"
.. " a password. Your next message will be used as a password and hidden from other players.\n"
.. minetest.colorize("#f00d00", "Please enter your password:")
end
-- External password handling mechanism has already provided password for this channel, verify it
if data.password ~= channel.password then
return false, "ERROR: Invalid password."
end
end
end

0 comments on commit 1376a3b

Please sign in to comment.