Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make things go forward
Browse files Browse the repository at this point in the history
S-S-X committed May 7, 2023

Verified

This commit was signed with the committer’s verified signature.
torkelrogstad Torkel Rogstad
1 parent 14e978d commit 59e4555
Showing 3 changed files with 98 additions and 107 deletions.
67 changes: 0 additions & 67 deletions chatcommands.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
local channel_created_string = "|#${channel_name}| Channel created"
local channel_updated_string = "|#${channel_name}| Channel updated"
local channel_invitation_string = "|#${channel_name}| Channel invite from (${from_player}), "
.. "to join the channel, do /jc ${channel_name},${channel_password} after "
.. "which you can send messages to the channel via #${channel_name}: message"
local channel_invited_string = "|#${channel_name}| Invite sent to ${to_player}"
local channel_deleted_string = "|#${channel_name}| Channel deleted"
local channel_left_string = "|#${channel_name}| Left channel"
local channel_already_deleted_string = "|#${channel_name}| Channel seems to have already "
.. "been deleted, will unregister channel from your list of channels"

local leave_channel_sound = "beerchat_chirp" -- Sound when you leave a channel
local channel_invite_sound = "beerchat_chirp" -- Sound when sending/ receiving an invite to a channel

local create_channel = {
params = "<Channel Name>,<Password (optional)>,<Color (optional, default is #ffffff)>",
@@ -216,66 +211,6 @@ local leave_channel = {
end
}

local invite_channel = {
params = "<Channel Name>,<Player Name>",
description = "Invite player named <Player Name> to channel named <Channel Name>. "
.. "You must be the owner of the channel in order to invite others.",
func = function(name, param)
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel "
.. "name and the player name."
end

local channel_name, player_name = string.match(param, "#?(.*),(.*)")

if not channel_name or channel_name == "" then
return false, "ERROR: Channel name is empty."
end

if not player_name or player_name == "" then
return false, "ERROR: Player name not supplied or empty."
end

if not beerchat.channels[channel_name] then
return false, "ERROR: Channel " .. channel_name .. " does not exist."
end

if name ~= beerchat.channels[channel_name].owner then
return false, "ERROR: You are not the owner of channel " .. param .. "."
end

if not minetest.get_player_by_name(player_name) then
return false, "ERROR: " .. player_name .. " does not exist or is not online."
else
if not beerchat.execute_callbacks('before_invite', name, player_name, channel_name) then
return false
end
if not beerchat.has_player_muted_player(player_name, name) then
if beerchat.enable_sounds then
minetest.sound_play(channel_invite_sound,
{ to_player = player_name, gain = beerchat.sounds_default_gain })
end
-- Sending the message
minetest.chat_send_player(
player_name,
beerchat.format_message(channel_invitation_string,
{ channel_name = channel_name, from_player = name })
)
end
if beerchat.enable_sounds then
minetest.sound_play(channel_invite_sound,
{ to_player = name, gain = beerchat.sounds_default_gain })
end
minetest.chat_send_player(
name,
beerchat.format_message(channel_invited_string,
{ channel_name = channel_name, to_player = player_name })
)
end
return true
end
}

local mute_player = {
params = "<Player Name>",
description = "Mute a player. After muting a player, you will no longer see chat "
@@ -363,8 +298,6 @@ minetest.register_chatcommand("jc", join_channel)
minetest.register_chatcommand("join_channel", join_channel)
minetest.register_chatcommand("lc", leave_channel)
minetest.register_chatcommand("leave_channel", leave_channel)
minetest.register_chatcommand("ic", invite_channel)
minetest.register_chatcommand("invite_channel", invite_channel)

minetest.register_chatcommand("mute", mute_player)
minetest.register_chatcommand("ignore", mute_player)
5 changes: 5 additions & 0 deletions common.lua
Original file line number Diff line number Diff line change
@@ -82,6 +82,11 @@ beerchat.is_player_subscribed_to_channel = function(name, channel)
and (nil ~= beerchat.playersChannels[name][channel])
end

-- TODO: add third argument `options` table for possible overrides.
beerchat.sound_play = beerchat.enable_sounds and function (target, sound)
minetest.sound_play(sound, { to_player = target, gain = beerchat.sounds_default_gain })
end or function() end

beerchat.send_message = function(name, message, data)
if beerchat.execute_callbacks('before_send', name, message or data.message, data) then
if type(data) == "table" then
133 changes: 93 additions & 40 deletions plugin/acl.lua
Original file line number Diff line number Diff line change
@@ -1,40 +1,93 @@
--luacheck: no_unused_args

--[[ TODO:
* Move rest of password stuff from chatcommands.
* Possibly also move channel invitation commands here.
* Add simple channel ACLs. Invite command to add users, kick command to remove users.
* Build simple RBAC on top of that to allow multiple managers / role based permissions for users.
--]]

beerchat.register_callback('after_joinplayer', function(player)
-- CHECK ACTIVE CHANNEL ACL HERE AND TAKE ACTION IF NEEDED
end)

beerchat.register_callback('before_invite', function(name, recipient, channel)
-- INJECT EVERYTHING THAT IS REQUIRED TO HAVE FULL ACCESS TO CHANNEL SO THAT
-- PLAYERS WITH THE FORCE CAN MOVE ANYONE TO ANY CHANNEL, ALSO TO LOCKED CHANNELS.
end)

beerchat.register_callback('before_join', function(name, channel_name, options)
local channel = beerchat.channels[channel_name]
if channel.password and channel.password ~= "" then
if not options or not options.password or options.password == "" then
return false, "ERROR: This channel requires that you supply a password. "
.. "Supply it in the following format: /jc my channel,password01"
end
if options.password ~= channel.password then
return false, "ERROR: Invalid password."
end
end
end)

beerchat.register_callback('before_switch_chan', function(name, oldchannel, newchannel)
-- COULD BE USED TO REVALIDATE ACCESS IF NEEDED, PROBABLY NOT NEEDED
-- IF KICKING PLAYERS FROM CHANNELS IS ALSO ADDED.
end)

beerchat.register_callback('on_forced_join', function(name, target, channel, from_channel)
-- INJECT EVERYTHING THAT IS REQUIRED TO HAVE FULL ACCESS TO CHANNEL SO THAT
-- PLAYERS WITH THE FORCE CAN MOVE ANYONE TO ANY CHANNEL, ALSO TO LOCKED CHANNELS.
end)
--luacheck: no_unused_args

--[[ TODO:
* Move rest of password stuff from chatcommands.
* Possibly also move channel invitation commands here.
* Add simple channel ACLs. Invite command to add users, kick command to remove users.
* Build simple RBAC on top of that to allow multiple managers / role based permissions for users.
--]]

local channel_invitation_string = "|#${channel_name}| Channel invite from ${from_player}, to join the channel, "
.. "do /jc ${channel_name} after which you can send messages to the channel via #${channel_name} message"
local channel_invited_string = "|#${channel_name}| Invite sent to ${to_player}"
local channel_invite_sound = "beerchat_chirp" -- Sound when sending / receiving an invite to a channel

local invite_channel = {
params = "<Channel Name>,<Player Name>",
description = "Invite player named <Player Name> to channel named <Channel Name>. "
.. "You must be the owner of the channel in order to invite others.",
func = function(name, param)
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel "
.. "name and the player name."
end

local channel_name, recipient = string.match(param, "#?(.*),(.*)")
if not channel_name or channel_name == "" then
return false, "ERROR: Channel name is empty."
elseif not recipient or recipient == "" then
return false, "ERROR: Player name not supplied or empty."
elseif not beerchat.execute_callbacks('before_invite', name, recipient, channel_name) then
return true -- Assume that callback handler already handled error messages
end

if beerchat.allow_private_message(name, recipient) then
-- Message to player who was invited to channel
beerchat.sound_play(recipient, channel_invite_sound)
minetest.chat_send_player(recipient, beerchat.format_message(channel_invitation_string, {
channel_name = channel_name,
from_player = name
}))
end
-- Feedback to player who ran command and invited other player
beerchat.sound_play(name, channel_invite_sound)
minetest.chat_send_player(name, beerchat.format_message(channel_invited_string, {
channel_name = channel_name,
to_player = recipient
}))
end
}

minetest.register_chatcommand("ic", invite_channel)
minetest.register_chatcommand("invite_channel", invite_channel)

--
-- ACL core functionality
--

beerchat.register_callback('after_joinplayer', function(player)
-- CHECK ACTIVE CHANNEL ACL HERE AND TAKE ACTION IF NEEDED
end)

beerchat.register_callback('before_invite', function(name, recipient, channel)
-- INJECT EVERYTHING THAT IS REQUIRED TO HAVE FULL ACCESS TO CHANNEL SO THAT
-- PLAYERS WITH THE FORCE CAN MOVE ANYONE TO ANY CHANNEL, ALSO TO LOCKED CHANNELS.
if not beerchat.channels[channel] then
return false, "ERROR: Channel #" .. channel .. " does not exist."
elseif name ~= beerchat.channels[channel].owner then
return false, "ERROR: You are not the owner of channel " .. channel .. "."
end
end)

beerchat.register_callback('before_join', function(name, channel_name, options)
local channel = beerchat.channels[channel_name]
if channel.password and channel.password ~= "" then
if not options or not options.password or options.password == "" then
return false, "ERROR: This channel requires that you supply a password. "
.. "Supply it in the following format: /jc my channel,password01"
end
if options.password ~= channel.password then
return false, "ERROR: Invalid password."
end
end
end)

beerchat.register_callback('before_switch_chan', function(name, oldchannel, newchannel)
-- COULD BE USED TO REVALIDATE ACCESS IF NEEDED, PROBABLY NOT NEEDED
-- IF KICKING PLAYERS FROM CHANNELS IS ALSO ADDED.
end)

beerchat.register_callback('on_forced_join', function(name, target, channel, from_channel)
-- INJECT EVERYTHING THAT IS REQUIRED TO HAVE FULL ACCESS TO CHANNEL SO THAT
-- PLAYERS WITH THE FORCE CAN MOVE ANYONE TO ANY CHANNEL, ALSO TO LOCKED CHANNELS.
end)

0 comments on commit 59e4555

Please sign in to comment.