mod_muc_block_pm/mod_muc_block_pm.lua
author Matthew Wild <mwild1@gmail.com>
Fri, 23 Sep 2022 22:41:15 +0100
changeset 5058 62480053c87b
parent 4031 291a45919988
child 5595 c7e532ac6bf7
permissions -rw-r--r--
mod_cloud_notify_encrypted: Additional debug logging when enabling/skipping
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2592
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local bare_jid = require"util.jid".bare;
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
local st = require"util.stanza";
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
3640
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     4
-- Support both old and new MUC code
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     5
local mod_muc = module:depends"muc";
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     6
local rooms = rawget(mod_muc, "rooms");
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     7
local get_room_from_jid = rawget(mod_muc, "get_room_from_jid") or
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     8
	function (jid)
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
     9
		return rooms[jid];
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
    10
	end
2592
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    12
module:hook("message/full", function(event)
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
	local stanza, origin = event.stanza, event.origin;
4031
291a45919988 mod_muc_block_pm: Don't respond to error stanzas
JC Brand <jc@opkode.com>
parents: 3640
diff changeset
    14
	if stanza.attr.type == "error" then
291a45919988 mod_muc_block_pm: Don't respond to error stanzas
JC Brand <jc@opkode.com>
parents: 3640
diff changeset
    15
		return
291a45919988 mod_muc_block_pm: Don't respond to error stanzas
JC Brand <jc@opkode.com>
parents: 3640
diff changeset
    16
	end
2592
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
	local to, from = stanza.attr.to, stanza.attr.from;
3640
afedc2430b0d mod_muc_block_pm: Add support for Prosody 0.11
JC Brand <jc@opkode.com>
parents: 2592
diff changeset
    18
	local room = get_room_from_jid(bare_jid(to));
2592
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	local to_occupant = room and room._occupants[to];
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	local from_occupant = room and room._occupants[room._jid_nick[from]]
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	if not ( to_occupant and from_occupant ) then return end
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	if from_occupant.affiliation then
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
		to_occupant._pm_block_override = true;
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	elseif not from_occupant._pm_block_override then
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
		origin.send(st.error_reply(stanza, "cancel", "not-authorized", "Private messages are disabled"));
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
		return true;
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
	end
69d3e0037435 mod_muc_block_pm: Prevent unaffiliated users from sending private messages in MUC
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
end, 1);