Changeset

5598:c7e532ac6bf7

mod_muc_block_pm: Update to 0.12+ API, use roles instead of affiliations The module was possibly broken with 0.12 before. This changes the behavior to allow only messages to or from moderators.
author Kim Alvefur <zash@zash.se>
date Wed, 12 Jul 2023 15:47:20 +0200
parents 5597:b681948a01f1
children 5599:b43ff0302204
files mod_muc_block_pm/README.markdown mod_muc_block_pm/mod_muc_block_pm.lua
diffstat 2 files changed, 17 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_block_pm/README.markdown	Mon Jul 10 16:10:57 2023 +0200
+++ b/mod_muc_block_pm/README.markdown	Wed Jul 12 15:47:20 2023 +0200
@@ -1,12 +1,11 @@
 ---
-summary: Prevent unaffiliated MUC participants from sending PMs
+summary: Prevent MUC participants from sending PMs
 ---
 
 # Introduction
 
-This module prevents unaffiliated users from sending private messages in
-chat rooms, unless someone with an affiliation (member, admin etc)
-messages them first.
+This module prevents *participants* from sending private messages to
+anyone except *moderators*.
 
 # Configuration
 
@@ -23,6 +22,5 @@
 
     Branch State
   -------- -----------------
-       0.9 Works
-      0.10 Should work
-      0.11 Should work
+      0.11 Will **not** work
+      0.12 Should work
--- a/mod_muc_block_pm/mod_muc_block_pm.lua	Mon Jul 10 16:10:57 2023 +0200
+++ b/mod_muc_block_pm/mod_muc_block_pm.lua	Wed Jul 12 15:47:20 2023 +0200
@@ -1,29 +1,18 @@
-local bare_jid = require"util.jid".bare;
-local st = require"util.stanza";
+local st = require "util.stanza";
 
--- Support both old and new MUC code
-local mod_muc = module:depends"muc";
-local rooms = rawget(mod_muc, "rooms");
-local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
-	function (jid)
-		return rooms[jid];
+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);
+
+	if from_occupant and from_occupant.role == "moderator" then
+		return -- moderators may message anyone
 	end
 
-module:hook("message/full", function(event)
-	local stanza, origin = event.stanza, event.origin;
-	if stanza.attr.type == "error" then
-		return
+	local to_occupant = room:get_occupant_by_nick(stanza.attr.to)
+	if to_occupant and to_occupant.role == "moderator" then
+		return -- messaging moderators is ok
 	end
-	local to, from = stanza.attr.to, stanza.attr.from;
-	local room = get_room_from_jid(bare_jid(to));
-	local to_occupant = room and room._occupants[to];
-	local from_occupant = room and room._occupants[room._jid_nick[from]]
-	if not ( to_occupant and from_occupant ) then return end
 
-	if from_occupant.affiliation then
-		to_occupant._pm_block_override = true;
-	elseif not from_occupant._pm_block_override then
-		origin.send(st.error_reply(stanza, "cancel", "not-authorized", "Private messages are disabled"));
-		return true;
-	end
+	room:route_to_occupant(from_occupant, st.error_reply(stanza, "cancel", "policy-violation", "Private messages are disabled", room.jid))
+	return false;
 end, 1);