Changeset

5905:66e7d46b1d4b

mod_muc_restrict_avatars: Allow MUC admin to control restriction Thanks, Strix!
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Tue, 09 Apr 2024 14:44:52 -0500
parents 5904:eb1c524a5150
children 5906:563c2c70cb9f 5920:5b95e06d75d5
files mod_muc_restrict_avatars/mod_muc_restrict_avatars.lua
diffstat 1 files changed, 37 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mod_muc_restrict_avatars/mod_muc_restrict_avatars.lua	Sat Apr 06 17:55:23 2024 +0200
+++ b/mod_muc_restrict_avatars/mod_muc_restrict_avatars.lua	Tue Apr 09 14:44:52 2024 -0500
@@ -9,11 +9,46 @@
 	return tag;
 end
 
+-- Function to determine if avatar restriction is enabled
+local function is_avatar_restriction_enabled(room)
+	return room._data.restrict_avatars;
+end
+
+-- Add MUC configuration form option for avatar restriction
+module:hook("muc-config-form", function(event)
+	local room, form = event.room, event.form;
+	table.insert(form, {
+		name = "restrict_avatars",
+		type = "boolean",
+		label = "Restrict avatars to members only",
+		value = is_avatar_restriction_enabled(room)
+	});
+end);
+
+-- Handle MUC configuration form submission
+module:hook("muc-config-submitted", function(event)
+	local room, fields, changed = event.room, event.fields, event.changed;
+	local restrict_avatars = fields["restrict_avatars"];
+
+	if restrict_avatars ~= is_avatar_restriction_enabled(room) then
+		-- Update room settings based on the submitted value
+		room._data.restrict_avatars = restrict_avatars;
+		-- Mark the configuration as changed
+		if type(changed) == "table" then
+			changed["restrict_avatars"] = true;
+		else
+			event.changed = true;
+		end
+	end
+end);
+
+-- Handle presence/full events to filter avatar advertisements
 module:hook("presence/full", function(event)
 	local stanza = event.stanza;
 	local room = mod_muc.get_room_from_jid(bare_jid(stanza.attr.to));
-
 	if not room:get_affiliation(stanza.attr.from) then
-		stanza:maptags(filter_avatar_advertisement);
+		if is_avatar_restriction_enabled(room) then
+			stanza:maptags(filter_avatar_advertisement);
+		end
 	end
 end, 1);