comparison mod_muc_restrict_pm/mod_muc_restrict_pm.lua @ 5914:d3610fb965d6

mod_muc_restrict_pm: Backport changes from upstream timber patch.
author Nicholas George <wirlaburla@worlio.com>
date Fri, 24 May 2024 18:30:47 -0500
parents d82c0383106a
children
comparison
equal deleted inserted replaced
5913:d82c0383106a 5914:d3610fb965d6
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 local muc_util = module:require "muc/util";
3 local valid_roles = muc_util.valid_roles;
4
5 -- Backported backwards compatibility map (Thanks MattJ)
6 local compat_map = {
7 everyone = "visitor";
8 participants = "participant";
9 moderators = "moderator";
10 members = "affiliated";
11 };
2 12
3 local function get_allow_pm(room) 13 local function get_allow_pm(room)
4 return room._data.allow_pm or 'everyone'; 14 local val = room._data.allow_pm;
15 return compat_map[val] or val or 'visitor';
5 end 16 end
6 17
7 local function set_allow_pm(room, val) 18 local function set_allow_pm(room, val)
8 if get_allow_pm(room) == val then return false; end 19 if get_allow_pm(room) == val then return false; end
9 room._data.allow_pm = val; 20 room._data.allow_pm = val;
25 table.insert(event.form, { 36 table.insert(event.form, {
26 name = 'muc#allow_pm'; 37 name = 'muc#allow_pm';
27 type = 'list-single'; 38 type = 'list-single';
28 label = 'Allow PMs from'; 39 label = 'Allow PMs from';
29 options = { 40 options = {
30 { value = 'everyone', label = 'Everyone', default = pmval == 'everyone' }, 41 { value = 'visitor', label = 'Everyone', default = pmval == 'visitor' },
31 { value = 'participants', label = 'Participants', default = pmval == 'participants' }, 42 { value = 'participant', label = 'Participants', default = pmval == 'participant' },
32 { value = 'members', label = 'Members', default = pmval == 'members' }, 43 { value = 'affiliated', label = 'Members', default = pmval == 'affiliated' },
33 { value = 'moderators', label = 'Moderators', default = pmval == 'moderators' }, 44 { value = 'moderator', label = 'Moderators', default = pmval == 'moderator' },
34 { value = 'none', label = 'No one', default = pmval == 'none' } 45 { value = 'none', label = 'No one', default = pmval == 'none' }
35 } 46 }
36 }); 47 });
37 table.insert(event.form, { 48 table.insert(event.form, {
38 name = 'muc#allow_modpm'; 49 name = 'muc#allow_modpm';
60 local to_occupant = room:get_occupant_by_nick(stanza.attr.to); 71 local to_occupant = room:get_occupant_by_nick(stanza.attr.to);
61 72
62 -- To self is always okay 73 -- To self is always okay
63 if to_occupant.bare_jid == from_occupant.bare_jid then return; end 74 if to_occupant.bare_jid == from_occupant.bare_jid then return; end
64 75
65 if get_allow_modpm(room) and to_occupant and to_occupant.role == 'moderator' then return; end 76 if get_allow_modpm(room) then
77 if to_occupant and to_occupant.role == 'moderator'
78 or from_occupant and from_occupant.role == "moderator" then
79 return; -- Allow to/from moderators
80 end
81 end
66 82
67 local pmval = get_allow_pm(room); 83 local pmval = get_allow_pm(room);
68 local from_affiliation = room:get_affiliation(from_occupant.bare_jid) or 'none'; 84
69 local to_affiliation = room:get_affiliation(to_occupant.bare_jid) or 'none'; 85 -- Backported improved handling (Thanks MattJ)
70 if pmval == 'everyone' then return; 86 if pmval ~= "none" then
71 elseif pmval == 'participants' and from_occupant.role ~= 'visitor' then 87 if pmval == "affiliated" and room:get_affiliation(from_occupant.bare_jid) then
72 if to_occupant.role ~= 'visitor' or from_occupant.role == 'moderator' then return; end 88 return; -- Allow from affiliated users
73 elseif pmval == 'members' and from_affiliation ~= 'none' then 89 elseif valid_roles[from_occupant.role] >= valid_roles[pmval] then
74 if to_affiliation ~= 'none' or from_occupant.role == 'moderator' then return; end 90 return; -- Allow from a permitted role
75 elseif pmval == 'moderators' and from_occupant.role == 'moderator' then return; end 91 end
92 end
76 93
77 room:route_to_occupant( 94 room:route_to_occupant(
78 from_occupant, 95 from_occupant,
79 st.error_reply(stanza, "cancel", "policy-violation", "Private messages are restricted", room.jid) 96 st.error_reply(stanza, "cancel", "policy-violation", "Private messages are restricted", room.jid)
80 ); 97 );