view mod_muc_restrict_attention/mod_muc_restrict_attention.lua @ 6429:66d7da6a9032

mod_muc_restrict_attention: change variable names to fit the new logic to allow restricting members. Adhere to stylesheet.
author Menel <menel@snikket.de>
date Tue, 10 Mar 2026 15:38:39 +0100
parents 23e3edb6f9ea
children af8577933ddf
line wrap: on
line source

module:depends"muc";

local restrict_by_default = module:get_option_boolean("muc_restrict_attention_from_members", true);

local function should_restrict_attention(room)
	local restrict_attention = room._data.restrict_attention;
		if restrict_attention == nil then
			restrict_attention = restrict_by_default;
		end
	return restrict_attention;
end

module:hook("muc-config-form", function(event)
	local room, form = event.room, event.form;
	table.insert(form, {
		name = "{xmpp:prosody.im}muc#roomconfig_allow_members_attention",
		type = "boolean",
		label = "Allow \"XEP-0224: Attention\" from members",
		value = not should_restrict_attention(room),
	});
end);

module:hook("muc-config-submitted", function(event)
	local room, fields, changed = event.room, event.fields, event.changed;
	local new_restrict_attention = not fields["{xmpp:prosody.im}muc#roomconfig_allow_members_attention"];
	if new_restrict_attention ~= should_restrict_attention(room) then
		if new_restrict_attention == restrict_by_default then
			room._data.restrict_attention = nil;
		else
			room._data.restrict_attention = new_restrict_attention;
		end
		if type(changed) == "table" then
			changed["{xmpp:prosody.im}muc#roomconfig_allow_members_attention"] = true;
		else
			event.changed = true;
		end
	end
end);

module:hook("muc-disco#info", function (event)
	local room, form, formdata = event.room, event.form, event.formdata;
	local allow_members_attention = not should_restrict_attention(room);
	table.insert(form, {
		name = "{xmpp:prosody.im}muc#roomconfig_allow_members_attention",
		type = "boolean",
	});
	formdata["{xmpp:prosody.im}muc#roomconfig_allow_members_attention"] = allow_members_attention;
end);

local function filter_attention_tags(tag)
	local xmlns = tag.attr.xmlns;
	if xmlns == "urn:xmpp:attention:0" then
		return nil;
	end
	return tag;
end

module:hook("muc-occupant-groupchat", function (event)
	local stanza = event.stanza;
	if stanza.attr.type ~= "groupchat" then return; end -- not relevant
	if event.room:get_affiliation(stanza.attr.from) == 'admin'
	or event.room:get_affiliation(stanza.attr.from) == 'owner' then
		return
	end
	if should_restrict_attention(event.room)
	or not event.room:get_affiliation(stanza.attr.from) then
		stanza:maptags(filter_attention_tags);
	end
end, 9);