mod_muc_restrict_pm: Backport changes from upstream timber patch.
authorNicholas George <wirlaburla@worlio.com>
Fri, 24 May 2024 18:30:47 -0500
changeset 5918 d3610fb965d6
parent 5917 d82c0383106a
child 5919 db5128d1a16c
mod_muc_restrict_pm: Backport changes from upstream timber patch.
mod_muc_restrict_pm/mod_muc_restrict_pm.lua
--- a/mod_muc_restrict_pm/mod_muc_restrict_pm.lua	Thu May 23 01:05:56 2024 -0500
+++ b/mod_muc_restrict_pm/mod_muc_restrict_pm.lua	Fri May 24 18:30:47 2024 -0500
@@ -1,7 +1,18 @@
 local st = require "util.stanza";
+local muc_util = module:require "muc/util";
+local valid_roles = muc_util.valid_roles;
+
+-- Backported backwards compatibility map (Thanks MattJ)
+local compat_map = {
+	everyone = "visitor";
+	participants = "participant";
+	moderators = "moderator";
+	members = "affiliated";
+};
 
 local function get_allow_pm(room)
-	return room._data.allow_pm or 'everyone';
+	local val = room._data.allow_pm;
+	return compat_map[val] or val or 'visitor';
 end
 
 local function set_allow_pm(room, val)
@@ -27,10 +38,10 @@
 		type = 'list-single';
 		label = 'Allow PMs from';
 		options = {
-			{ 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 = 'visitor', label = 'Everyone', default = pmval == 'visitor' },
+			{ value = 'participant', label = 'Participants', default = pmval == 'participant' },
+			{ value = 'affiliated', label = 'Members', default = pmval == 'affiliated' },
+			{ value = 'moderator', label = 'Moderators', default = pmval == 'moderator' },
 			{ value = 'none', label = 'No one', default = pmval == 'none' }
 		}
 	});
@@ -62,17 +73,23 @@
 	-- To self is always okay
 	if to_occupant.bare_jid == from_occupant.bare_jid then return; end
 
-	if get_allow_modpm(room) and to_occupant and to_occupant.role == 'moderator' then return; end
+	if get_allow_modpm(room) then
+		if to_occupant and to_occupant.role == 'moderator'
+		or from_occupant and from_occupant.role == "moderator" then
+			return; -- Allow to/from moderators
+		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
+
+	-- Backported improved handling (Thanks MattJ)
+	if pmval ~= "none" then
+		if pmval == "affiliated" and room:get_affiliation(from_occupant.bare_jid) then
+			return; -- Allow from affiliated users
+		elseif valid_roles[from_occupant.role] >= valid_roles[pmval] then
+			return; -- Allow from a permitted role
+		end
+	end
 
 	room:route_to_occupant(
 		from_occupant,