changeset 5913:d82c0383106a

mod_muc_restrict_pm: small rewrite. improves room config options
author Nicholas George <wirlaburla@worlio.com>
date Thu, 23 May 2024 01:05:56 -0500
parents 9d0270680d1f
children d3610fb965d6
files mod_muc_restrict_pm/mod_muc_restrict_pm.lua
diffstat 1 files changed, 36 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_restrict_pm/mod_muc_restrict_pm.lua	Wed May 22 21:11:24 2024 -0500
+++ b/mod_muc_restrict_pm/mod_muc_restrict_pm.lua	Thu May 23 01:05:56 2024 -0500
@@ -1,95 +1,59 @@
 local st = require "util.stanza";
 
-local affils = {
-	none = 0;
-	member = 1;
-	administrator = 2;
-	owner = 3;
-};
-
-local function get_restrict_pm(room)
-	return room._data.restrict_pm or 'none';
+local function get_allow_pm(room)
+	return room._data.allow_pm or 'everyone';
 end
 
-local function set_restrict_pm(room, affil)
-	if get_restrict_pm(room) == affil then return false; end
-	room._data.restrict_pm = affil;
+local function set_allow_pm(room, val)
+	if get_allow_pm(room) == val then return false; end
+	room._data.allow_pm = val;
 	return true;
 end
 
-local function can_pm_anyone(room)
-	return room._data.restrict_pm_to or false;
+local function get_allow_modpm(room)
+	return room._data.allow_modpm or false;
 end
 
-local function set_pm_anyone(room, val)
-	if can_pm_anyone(room) == val then return false; end
-	room._data.restrict_pm_to = val;
-	return true;
-end
-
-local function is_visitor_pm_off(room)
-	return room._data.restrict_pm_visitor or false;
-end
-
-local function set_visitor_pm_off(room, val)
-	if is_visitor_pm_off(room) == val then return false; end
-	room._data.restrict_pm_visitor = val;
+local function set_allow_modpm(room, val)
+	if get_allow_modpm(room) == val then return false; end
+	room._data.allow_modpm = val;
 	return true;
 end
 
 module:hook("muc-config-form", function(event)
-	local affilpm = get_restrict_pm(event.room);
+	local pmval = get_allow_pm(event.room);
 	table.insert(event.form, {
-		name = 'muc#restrict_pm';
+		name = 'muc#allow_pm';
 		type = 'list-single';
 		label = 'Allow PMs from';
 		options = {
-			{ value = 'owner', label = 'Owner', default = affilpm == 'owner' },
-			{ value = 'administrator', label = 'Administrators', default = affilpm == 'administrator' },
-			{ value = 'member', label = 'Members', default = affilpm == 'member' },
-			{ value = 'none', label = 'Everyone', default = affilpm == 'none' }
+			{ value = 'everyone', label = 'Everyone', default = pmval == 'everyone' },
+			{ value = 'participants', label = 'Participants', default = pmval == 'participants' },
+			{ value = 'members', label = 'Members', default = pmval == 'members' },
+			{ value = 'moderators', label = 'Moderators', default = pmval == 'moderators' },
+			{ value = 'none', label = 'No one', default = pmval == 'none' }
 		}
 	});
-
 	table.insert(event.form, {
-		name = 'muc#restrict_pm_to';
+		name = 'muc#allow_modpm';
 		type = 'boolean';
-		label = 'Allow PMs to everyone';
-		value = can_pm_anyone(event.room);
-	});
-
-	table.insert(event.form, {
-		name = 'muc#restrict_pm_visitor';
-		type = 'boolean';
-		label = 'Disable PMs from Visitors';
-		value = is_visitor_pm_off(event.room);
+		label = 'Allow PMs to moderators';
+		value = get_allow_modpm(event.room)
 	});
 end);
 
-module:hook("muc-config-submitted/muc#restrict_pm", function(event)
-	if set_restrict_pm(event.room, event.value) then
+module:hook("muc-config-submitted/muc#allow_pm", function(event)
+	if set_allow_pm(event.room, event.value) then
 		event.status_codes["104"] = true;
 	end
 end);
 
-module:hook("muc-config-submitted/muc#restrict_pm_to", function(event)
-	if set_pm_anyone(event.room, event.value) then
+module:hook("muc-config-submitted/muc#allow_modpm", function(event)
+	if set_allow_modpm(event.room, event.value) then
 		event.status_codes["104"] = true;
 	end
 end);
 
-module:hook("muc-config-submitted/muc#restrict_pm_visitor", function(event)
-	if set_visitor_pm_off(event.room, event.value) then
-		event.status_codes["104"] = true;
-	end
-end);
-
-local function can_user_pm(room, jid)
-	local affil, pmval = affils[room:get_affiliation(jid) or 'none'], affils[get_restrict_pm(room)];
-	if affil >= pmval then return true; end
-	return false;
-end
-
 module:hook("muc-private-message", function(event)
 	local stanza, room = event.stanza, event.room;
 	local from_occupant = room:get_occupant_by_nick(stanza.attr.from);
@@ -98,19 +62,21 @@
 	-- To self is always okay
 	if to_occupant.bare_jid == from_occupant.bare_jid then return; end
 
-	-- To moderation is okay
-	if to_occupant and to_occupant.role == 'moderator' then return; end
+	if get_allow_modpm(room) and to_occupant and to_occupant.role == 'moderator' then return; end
 
-	if not is_visitor_pm_off(room) or (from_occupant == nil or from_occupant.role ~= 'visitor') then
-		-- If visitors disabled
-		if can_user_pm(room, from_occupant.bare_jid) and (can_pm_anyone(room) or can_user_pm(room, to_occupant.bare_jid)) then
-			return;
-		end;
-	end
+	local pmval = get_allow_pm(room);
+	local from_affiliation = room:get_affiliation(from_occupant.bare_jid) or 'none';
+	local to_affiliation = room:get_affiliation(to_occupant.bare_jid) or 'none';
+	if pmval == 'everyone' then return;
+	elseif pmval == 'participants' and from_occupant.role ~= 'visitor' then
+		if to_occupant.role ~= 'visitor' or from_occupant.role == 'moderator' then return; end
+	elseif pmval == 'members' and from_affiliation ~= 'none' then
+		if to_affiliation ~= 'none' or from_occupant.role == 'moderator' then return; end
+	elseif pmval == 'moderators' and from_occupant.role == 'moderator' then return; end
 
 	room:route_to_occupant(
 		from_occupant,
-		st.error_reply(stanza, "cancel", "policy-violation", "Private messages are disabled", room.jid)
-	);
+		st.error_reply(stanza, "cancel", "policy-violation", "Private messages are restricted", room.jid)
+		);
 	return false;
 end, 1);