view mod_muc_restrict_attention/mod_muc_restrict_attention.lua @ 6423:d748985de48f

mod_muc_restrict_attention: New Module based on mod_muc_restrict_media to strip Attention. Untested.
author Menel <menel@snikket.de>
date Sun, 08 Mar 2026 10:59:40 +0100
parents
children 23e3edb6f9ea
line wrap: on
line source

module:depends"muc";

local restrict_by_default = module:get_option_boolean("muc_room_default_restrict_attention", 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_unaffiliated_attention",
		type = "boolean",
		label = "Allow \"XEP-0224: Attention\" from non-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_unaffiliated_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_unaffiliated_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_unaffiliated_attention = not should_restrict_attention(room);
	table.insert(form, {
		name = "{xmpp:prosody.im}muc#roomconfig_unaffiliated_attention",
		type = "boolean",
	});
	formdata["{xmpp:prosody.im}muc#roomconfig_unaffiliated_attention"] = allow_unaffiliated_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
	if event.room:get_affiliation(stanza.attr.from) then return end
	if should_restrict_attention(event.room) then
		stanza:maptags(filter_attention_tags);
	end
end, 20);