Emoji only mode WIP

This commit is contained in:
John Livingston
2024-09-05 18:28:54 +02:00
parent 2f78b901e3
commit 1a75b30c50
20 changed files with 375 additions and 6 deletions

View File

@ -16,6 +16,9 @@ module:depends"http";
local mod_muc_peertubelivechat_terms = module:depends"muc_peertubelivechat_terms";
local set_muc_terms = rawget(mod_muc_peertubelivechat_terms, "set_muc_terms");
local mod_muc_peertubelivechat_restrict_message = module:depends"muc_peertubelivechat_restrict_message";
local set_peertubelivechat_emoji_only_mode = rawget(mod_muc_peertubelivechat_restrict_message, "set_peertubelivechat_emoji_only_mode");
local set_peertubelivechat_emoji_only_regexp = rawget(mod_muc_peertubelivechat_restrict_message, "set_peertubelivechat_emoji_only_regexp");
function check_auth(routes)
local function check_request_auth(event)
@ -102,6 +105,16 @@ local function update_room(event)
room._data.moderation_delay = config.moderation_delay;
end
end
if type(config.livechat_emoji_only) == "boolean" then
if set_peertubelivechat_emoji_only_mode then
set_peertubelivechat_emoji_only_mode(room, config.livechat_emoji_only)
end
end
if type(config.livechat_emoji_only_regexp) == "string" then
if set_peertubelivechat_emoji_only_mode then
set_peertubelivechat_emoji_only_regexp(room, config.livechat_emoji_only_regexp)
end
end
if (type(config.livechat_muc_terms) == "string") then
-- to easily detect if the value is given or not, we consider that the caller passes "" when terms must be deleted.
if set_muc_terms then

View File

@ -9,6 +9,9 @@
-- * "mute_anonymous"
-- * "moderation_delay"
-- * "anonymize_moderation_actions"
-- * "livechat_emoji_only"
-- * "livechat_emoji_only_regexp"
-- * "livechat_muc_terms"
-- These options are introduced in the Peertube livechat plugin.
--
-- The "slow_mode_duration" comes with mod_muc_slow_mode.
@ -128,6 +131,12 @@ local function apply_config(room, settings)
if (type(config.mute_anonymous) == "boolean") then
room._data.x_peertubelivechat_mute_anonymous = config.mute_anonymous;
end
if (type(config.livechat_emoji_only) == "boolean") then
room._data.x_peertubelivechat_emoji_only_mode = config.livechat_emoji_only;
end
if (type(config.livechat_emoji_only_regexp) == "string" and config.livechat_emoji_only_regexp ~= "") then
room._data.x_peertubelivechat_emoji_only_regexp = config.emoji_only_regexp;
end
if (type(config.livechat_muc_terms) == "string") then
-- we don't need to use set_muc_terms here, as this is called for a newly created room
-- (and thus we don't need to broadcast changes)

View File

@ -0,0 +1,14 @@
<!--
SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
SPDX-License-Identifier: AGPL-3.0-only
-->
# mod_muc_peertubelivechat_restrict_message
This module is a custom module designed for the peertube-plugin-livechat project, that can restrict message content to
given regular expression.
This module is part of peertube-plugin-livechat, and is under the same LICENSE (AGPL-v3).
## Prerequisites
This modules needs lrexlib instlaled (available as lua-rex-pcre2 package on Debian).

View File

@ -0,0 +1,131 @@
-- mod_muc_peertubelivechat_roles
--
-- SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
-- SPDX-License-Identifier: AGPL-3.0-only
--
-- This file is AGPL-v3 licensed.
-- Please see the Peertube livechat plugin copyright information.
-- https://livingston.frama.io/peertube-plugin-livechat/credits/
--
local st = require "util.stanza";
local jid_bare = require "util.jid".bare;
local rex = require "rex_pcre2"; -- We are using PCRE2 (Perl Compatible Regular Expression)
-- Plugin dependencies
local mod_muc = module:depends "muc";
local muc_util = module:require "muc/util";
local valid_roles = muc_util.valid_roles;
function get_peertubelivechat_emoji_only_mode(room)
return room._data.x_peertubelivechat_emoji_only_mode;
end
function set_peertubelivechat_emoji_only_mode(room, emoji_only)
emoji_only = emoji_only and true or nil;
if get_peertubelivechat_emoji_only_mode(room) == emoji_only then return false; end
room._data.x_peertubelivechat_emoji_only_mode = emoji_only;
return true;
end
function get_peertubelivechat_emoji_only_regexp(room)
return room._data.x_peertubelivechat_emoji_only_regexp;
end
function set_peertubelivechat_emoji_only_regexp(room, emoji_only_regexp)
if (emoji_only_regexp ~= nil and type(emoji_only_regexp) ~= "string") then
return false;
end
if emoji_only_regexp == "" then emoji_only_regexp = nil; end
if get_peertubelivechat_emoji_only_regexp(room) == emoji_only_regexp then return false; end
room._data.x_peertubelivechat_emoji_only_regexp = emoji_only_regexp;
-- and we must decache the compile regexp
room.x_peertubelivechat_emoji_only_compiled_regexp = nil;
return true;
end
module:hook("muc-disco#info", function(event)
if get_peertubelivechat_emoji_only_mode(event.room) and get_peertubelivechat_emoji_only_regexp(event.room) ~= nil then
event.reply:tag("feature", {var = "x_peertubelivechat_emoji_only_mode"}):up();
end
end);
module:hook("muc-config-form", function(event)
if (get_peertubelivechat_emoji_only_regexp(event.room) ~= nil) then
table.insert(event.form, {
name = "muc#roomconfig_x_peertubelivechat_emoji_only_mode";
type = "boolean";
label = "Emoji only mode";
desc = "Occupants will only be able to send emoji. This does not affect moderators.";
value = get_peertubelivechat_emoji_only_mode(event.room);
});
end
end, 121);
module:hook("muc-config-submitted/muc#roomconfig_x_peertubelivechat_emoji_only_mode", function(event)
if get_peertubelivechat_emoji_only_regexp(event.room) ~= nil and set_peertubelivechat_emoji_only_mode(event.room, event.value) then
event.status_codes["104"] = true;
end
end);
-- handling groupchat messages
function handle_groupchat(event)
local origin, stanza = event.origin, event.stanza;
local room = event.room;
if (not get_peertubelivechat_emoji_only_mode(room)) then
return;
end
if not room.x_peertubelivechat_emoji_only_compiled_regexp then
-- compute the regexp on first access
local r = get_peertubelivechat_emoji_only_regexp(room);
if (r == nil) then
return;
end
room.x_peertubelivechat_emoji_only_compiled_regexp = rex.new(r, "i");
end
-- only consider messages with body (ie: ignore chatstate and other non-text xmpp messages)
local body = stanza:get_child_text("body")
if not body or #body < 1 then
-- module:log("debug", "No body, message accepted");
return;
end
-- Checking user's permissions (moderators are not subject to restrictions)
local actor = stanza.attr.from;
local actor_nick = room:get_occupant_jid(actor);
local actor_jid = jid_bare(actor);
-- Only checking role, not affiliation (restrictions only applies on users currently connected to the room)
local role = room:get_role(actor_nick);
if valid_roles[role or "none"] >= valid_roles.moderator then
-- user bypasses
-- module:log("debug", "User is moderator, bypassing restrictions");
return;
end
-- testing the content
if (room.x_peertubelivechat_emoji_only_compiled_regexp:match(body) ~= nil) then
-- module:log("debug", "Message accepted");
return;
end
module:log("debug", "Bouncing message for user %s", actor_nick);
local reply = st.error_reply(
stanza,
-- error_type = 'modify' (see descriptions in RFC 6120 https://xmpp.org/rfcs/rfc6120.html#stanzas-error-syntax)
"modify",
-- error_condition = 'policy-violation' (see RFC 6120 Defined Error Conditions https://xmpp.org/rfcs/rfc6120.html#stanzas-error-conditions)
"policy-violation",
"Emoji only mode enabled"
);
origin.send(reply);
return true; -- stoping propagation
end
module:hook("muc-occupant-groupchat", handle_groupchat);