# HG changeset patch # User Matthew Wild # Date 1637687665 0 # Node ID df2246b15075abbe0c103fc95e20dc8ff79d1e27 # Parent 016e9c7733bc9d8fb5bf39fcf9838e5497945390 mod_muc_restrict_media: Allow hiding inline media from unaffiliated users in MUCs diff -r 016e9c7733bc -r df2246b15075 mod_muc_hide_media/README.markdown --- a/mod_muc_hide_media/README.markdown Fri Jul 09 18:31:45 2021 +0200 +++ b/mod_muc_hide_media/README.markdown Tue Nov 23 17:14:25 2021 +0000 @@ -6,6 +6,10 @@ This can be useful in public channels where content posted by users should not be shown by default. +**Note:** You could consider the more useful [mod_muc_restrict_media] instead, +which allows affiliated users (e.g. members, admins, owners) to still send +inline media. + # Configuring ## Enabling diff -r 016e9c7733bc -r df2246b15075 mod_muc_restrict_media/README.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_muc_restrict_media/README.markdown Tue Nov 23 17:14:25 2021 +0000 @@ -0,0 +1,26 @@ +# Introduction + +This module adds a room configuration option to hide inline media from +unaffiliated users in MUCs and display them as links instead. + +This can be useful in public channels where content posted by users should not +be shown by default. + +# Configuring + +## Enabling + +``` {.lua} +Component "rooms.example.net" "muc" +modules_enabled = { + "muc_restrict_media"; +} +``` + +## Settings + +A default setting can be provided in the config file: + +``` {.lua} +muc_room_default_restrict_media = true +``` diff -r 016e9c7733bc -r df2246b15075 mod_muc_restrict_media/mod_muc_restrict_media.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mod_muc_restrict_media/mod_muc_restrict_media.lua Tue Nov 23 17:14:25 2021 +0000 @@ -0,0 +1,68 @@ +module:depends"muc"; + +local restrict_by_default = not module:get_option_boolean("muc_room_default_restrict_media", true); + +local function should_restrict_media(room) + local restrict_media = room._data.restrict_media; + if restrict_media == nil then + restrict_media = restrict_by_default; + end + return restrict_media; +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_media", + type = "boolean", + label = "Display inline media (images, etc.) from non-members", + value = not should_restrict_media(room), + }); +end); + +module:hook("muc-config-submitted", function(event) + local room, fields, changed = event.room, event.fields, event.changed; + local new_restrict_media = not fields["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"]; + if new_restrict_media ~= should_restrict_media(room) then + if new_restrict_media == restrict_by_default(room) then + room._data.restrict_media = nil; + else + room._data.restrict_media = new_restrict_media; + end + if type(changed) == "table" then + changed["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"] = 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_media = not should_restrict_media(room); + table.insert(form, { + name = "{xmpp:prosody.im}muc#roomconfig_unaffiliated_media", + }); + formdata["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"] = allow_unaffiliated_media; +end); + +local function filter_media_tags(tag) + local xmlns = tag.attr.xmlns; + if xmlns == "jabber:x:oob" then + return nil; + elseif xmlns == "urn:xmpp:reference:0" then + if tag:get_child("media-sharing", "urn:xmpp:sims:1") then + return nil; + end + end + return tag; +end + +module:hook("muc-occupant-groupchat", function (event) + local stanza = event.stanza; + if stanza.attr.type ~= "groupchat" then return; end + if should_restrict_media(event.room) then + stanza:maptags(filter_media_tags); + end +end, 20);