Add callbacks, wip moving jail stuff to jail.lua

This commit is contained in:
SX 2020-02-12 00:33:12 +02:00
parent 848e2ca292
commit 2b5bb853df
4 changed files with 119 additions and 22 deletions

View File

@ -153,9 +153,7 @@ local join_channel = {
description = "Join channel named <Channel Name>. " ..
"After joining you will see messages sent to that channel (in addition to the other channels you have joined)",
func = function(name, param)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no joining channels for you."
end
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel name as a minimum"
end
@ -181,6 +179,11 @@ local join_channel = {
end
end
local cb_result, cb_message = beerchat.execute_callbacks('before_join', name, channel_name)
if not cb_result then
return cb_message and (false, cb_message) or false
end
beerchat.playersChannels[name] = beerchat.playersChannels[name] or {}
beerchat.playersChannels[name][channel_name] = "joined"
minetest.get_player_by_name(name):get_meta():set_string(
@ -204,9 +207,7 @@ local leave_channel = {
"When you leave the channel you can no longer send/ receive messages from that channel. " ..
"NOTE: You can also leave the main channel",
func = function(name, param)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no leaving for you."
end
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the channel name"
end
@ -217,6 +218,11 @@ local leave_channel = {
return false, "ERROR: You are not member of "..channel_name..", no need to leave"
end
local cb_result, cb_message = beerchat.execute_callbacks('before_leave', name, channel_name)
if not cb_result then
return cb_message and (false, cb_message) or false
end
beerchat.playersChannels[name][channel_name] = nil
minetest.get_player_by_name(name):get_meta():set_string(
"beerchat:channels",
@ -273,8 +279,9 @@ local invite_channel = {
if not minetest.get_player_by_name(player_name) then
return false, "ERROR: "..player_name.." does not exist or is not online"
else
if beerchat.is_player_jailed(player_name) then
return false, player_name .. " is in chat-jail, no inviting."
local cb_result, cb_message = beerchat.execute_callbacks('before_invite', name, player_name, channel_name)
if not cb_result then
return cb_message and (false, cb_message) or false
end
if not beerchat.has_player_muted_player(player_name, name) then
if beerchat.enable_sounds then
@ -304,9 +311,12 @@ local mute_player = {
description = "Mute a player. After muting a player, you will no longer see chat messages of this user, " ..
"regardless of what channel his user sends messages to",
func = function(name, param)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no muting for you."
local cb_result, cb_message = beerchat.execute_callbacks('before_mute', name, param)
if not cb_result then
return cb_message and (false, cb_message) or false
end
if not param or param == "" then
return false, "ERROR: Invalid number of arguments. Please supply the name of the user to mute"
end
@ -409,13 +419,9 @@ beerchat.force_player_to_channel = function(name, param)
beerchat.currentPlayerChannel[player_name] = channel_name
meta:set_string("beerchat:current_channel", channel_name)
-- going to/from jail?
if channel_name == beerchat.jail_channel_name then
meta:set_int("beerchat:jailed", 1)
beerchat.jail_list[player_name] = true
elseif beerchat.is_player_jailed(player_name) then
meta:set_int("beerchat:jailed", 0)
beerchat.jail_list[player_name] = nil
local cb_result, cb_message = beerchat.execute_callbacks('on_forced_join', name, player_name, channel_name, meta)
if not cb_result then
return cb_message and (false, cb_message) or false
end
-- inform user

View File

@ -1,4 +1,52 @@
beerchat.cb = {} -- all custom callbacks
beerchat.cb.before_send = {} -- executed before sending message
beerchat.cb.before_receive = {} -- executed before receiving message
beerchat.cb.before_join = {} -- executed before channel is joined
beerchat.cb.before_leave = {} -- executed before channel is leaved
beerchat.cb.before_invite = {} -- excuted before channel invitation takes place
beerchat.cb.on_forced_join = {} -- executed right after player is forced to channel
beerchat.register_callback = function(trigger, fn)
if type(fn) ~= 'function' then
print('Error: Invalid fn argument for beerchat.register_callback, must be function')
return
end
if type(trigger) ~= 'string' then
print('Error: Invalid trigger argument for beerchat.register_callback, must be string')
return
end
local cb = beerchat.cb
local callback_key = trigger
if not cb[callback_key] then
print('Error: Invalid callback trigger event, possible triggers:')
for k,_ in pairs(cb) do
print(' -> ' .. k)
end
return
end
table.insert(cb[callback_key], fn)
end
beerchat.execute_callbacks = function(trigger)
local cb_list = beerchat.cb[trigger]
if not cb_list then
print('Error: Invalid trigger argument for beerchat.execute_callbacks')
-- This is internal error / dev error, stop processing current event
return false
end
for _,fn in ipairs(cb_list) do
if not fn(unpack(arg) then
return false
end
end
return true
end
-- called on every channel message
-- params: channel, playername, message
beerchat.on_channel_message = function()

View File

@ -9,9 +9,6 @@ beerchat = {
-- The main channel is the one you send messages to when no channel is specified
main_channel_name = minetest.settings:get("beerchat.main_channel_name"),
-- Jail channel is where you put annoying missbehaving users with /force2channel
jail_channel_name = minetest.settings:get("beerchat.jail_channel_name"),
-- The default color of channels when no color is specified
default_channel_color = "#ffffff",
@ -46,7 +43,6 @@ if nil == beerchat.main_channel_name or "" == beerchat.main_channel_name then
end
local MP = minetest.get_modpath("beerchat")
dofile(MP.."/jail.lua")
dofile(MP.."/common.lua")
dofile(MP.."/format_message.lua")
dofile(MP.."/hooks.lua")
@ -71,6 +67,9 @@ end
-- remove http ref
beerchat.http = nil
-- integrated extensions (could also be different mod)
if minetest.settings:get("beerchat.enable_jail") then
dofile(MP.."/jail.lua")
end
print("[OK] beerchat")

View File

@ -1,3 +1,7 @@
-- Jail channel is where you put annoying missbehaving users with /force2channel
beerchat.jail_channel_name = minetest.settings:get("beerchat.jail_channel_name") or "grounded"
beerchat.jail_channel_name = beerchat.jail_channel_name or "grounded"
beerchat.jail_list = {}
if nil == beerchat.jail_channel_name or "" == beerchat.jail_channel_name then
@ -7,3 +11,43 @@ end
beerchat.is_player_jailed = function(name)
return true == beerchat.jail_list[name]
end
beerchat.register_callback('before_invite', function(sender, recipient, channel)
if beerchat.is_player_jailed(player_name) then
return false, player_name .. " is in chat-jail, no inviting."
end
return true
end)
beerchat.register_callback('before_mute', function(name, target)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no muting for you."
end
return true
end)
beerchat.register_callback('before_join', function(name, channel)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no joining channels for you."
end
return true
end)
beerchat.register_callback('before_leave', function(name, channel)
if beerchat.is_player_jailed(name) then
return false, "You are in chat-jail, no leaving for you."
end
return true
end)
beerchat.register_callback('on_forced_join', function(name, target, channel, target_meta)
-- going to/from jail?
if channel == beerchat.jail_channel_name then
target_meta:set_int("beerchat:jailed", 1)
beerchat.jail_list[target] = true
elseif beerchat.is_player_jailed(target) then
target_meta:set_int("beerchat:jailed", 0)
beerchat.jail_list[target] = nil
end
return true
end)