# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1488297870 -3600
# Node ID 69d3e00374352163fdf729f0eac078ef6fbe994f
# Parent  8c879948a2cfe219fecfd830ddd50a5ae2cef75c
mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC

diff -r 8c879948a2cf -r 69d3e0037435 mod_muc_block_pm/README.markdown
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_block_pm/README.markdown	Tue Feb 28 17:04:30 2017 +0100
@@ -0,0 +1,28 @@
+---
+summary: Prevent unaffiliated 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.
+
+# Configuration
+
+The module doesn't have any options, just load it onto a MUC component.
+
+``` lua
+Component "muc"
+modules_enabled = {
+    "muc_block_pm";
+}
+```
+
+# Compatibility
+
+    Branch State
+  -------- -----------------
+       0.9 Works
+      0.10 Should work
+     trunk *Does not work*
diff -r 8c879948a2cf -r 69d3e0037435 mod_muc_block_pm/mod_muc_block_pm.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mod_muc_block_pm/mod_muc_block_pm.lua	Tue Feb 28 17:04:30 2017 +0100
@@ -0,0 +1,20 @@
+local bare_jid = require"util.jid".bare;
+local st = require"util.stanza";
+
+local muc_rooms = module:depends"muc".rooms;
+
+module:hook("message/full", function(event)
+	local stanza, origin = event.stanza, event.origin;
+	local to, from = stanza.attr.to, stanza.attr.from;
+	local room = muc_rooms[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
+end, 1);