comparison mod_muc_restrict_media/mod_muc_restrict_media.lua @ 4787:df2246b15075

mod_muc_restrict_media: Allow hiding inline media from unaffiliated users in MUCs
author Matthew Wild <mwild1@gmail.com>
date Tue, 23 Nov 2021 17:14:25 +0000
parents
children f06d04cfea7d
comparison
equal deleted inserted replaced
4786:016e9c7733bc 4787:df2246b15075
1 module:depends"muc";
2
3 local restrict_by_default = not module:get_option_boolean("muc_room_default_restrict_media", true);
4
5 local function should_restrict_media(room)
6 local restrict_media = room._data.restrict_media;
7 if restrict_media == nil then
8 restrict_media = restrict_by_default;
9 end
10 return restrict_media;
11 end
12
13 module:hook("muc-config-form", function(event)
14 local room, form = event.room, event.form;
15 table.insert(form, {
16 name = "{xmpp:prosody.im}muc#roomconfig_unaffiliated_media",
17 type = "boolean",
18 label = "Display inline media (images, etc.) from non-members",
19 value = not should_restrict_media(room),
20 });
21 end);
22
23 module:hook("muc-config-submitted", function(event)
24 local room, fields, changed = event.room, event.fields, event.changed;
25 local new_restrict_media = not fields["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"];
26 if new_restrict_media ~= should_restrict_media(room) then
27 if new_restrict_media == restrict_by_default(room) then
28 room._data.restrict_media = nil;
29 else
30 room._data.restrict_media = new_restrict_media;
31 end
32 if type(changed) == "table" then
33 changed["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"] = true;
34 else
35 event.changed = true;
36 end
37 end
38 end);
39
40 module:hook("muc-disco#info", function (event)
41 local room, form, formdata = event.room, event.form, event.formdata;
42
43 local allow_unaffiliated_media = not should_restrict_media(room);
44 table.insert(form, {
45 name = "{xmpp:prosody.im}muc#roomconfig_unaffiliated_media",
46 });
47 formdata["{xmpp:prosody.im}muc#roomconfig_unaffiliated_media"] = allow_unaffiliated_media;
48 end);
49
50 local function filter_media_tags(tag)
51 local xmlns = tag.attr.xmlns;
52 if xmlns == "jabber:x:oob" then
53 return nil;
54 elseif xmlns == "urn:xmpp:reference:0" then
55 if tag:get_child("media-sharing", "urn:xmpp:sims:1") then
56 return nil;
57 end
58 end
59 return tag;
60 end
61
62 module:hook("muc-occupant-groupchat", function (event)
63 local stanza = event.stanza;
64 if stanza.attr.type ~= "groupchat" then return; end
65 if should_restrict_media(event.room) then
66 stanza:maptags(filter_media_tags);
67 end
68 end, 20);