comparison mod_muc_restrict_pm/mod_muc_restrict_pm.lua @ 5913:d82c0383106a

mod_muc_restrict_pm: small rewrite. improves room config options
author Nicholas George <wirlaburla@worlio.com>
date Thu, 23 May 2024 01:05:56 -0500
parents 9d0270680d1f
children d3610fb965d6
comparison
equal deleted inserted replaced
5912:9d0270680d1f 5913:d82c0383106a
1 local st = require "util.stanza"; 1 local st = require "util.stanza";
2 2
3 local affils = { 3 local function get_allow_pm(room)
4 none = 0; 4 return room._data.allow_pm or 'everyone';
5 member = 1;
6 administrator = 2;
7 owner = 3;
8 };
9
10 local function get_restrict_pm(room)
11 return room._data.restrict_pm or 'none';
12 end 5 end
13 6
14 local function set_restrict_pm(room, affil) 7 local function set_allow_pm(room, val)
15 if get_restrict_pm(room) == affil then return false; end 8 if get_allow_pm(room) == val then return false; end
16 room._data.restrict_pm = affil; 9 room._data.allow_pm = val;
17 return true; 10 return true;
18 end 11 end
19 12
20 local function can_pm_anyone(room) 13 local function get_allow_modpm(room)
21 return room._data.restrict_pm_to or false; 14 return room._data.allow_modpm or false;
22 end 15 end
23 16
24 local function set_pm_anyone(room, val) 17 local function set_allow_modpm(room, val)
25 if can_pm_anyone(room) == val then return false; end 18 if get_allow_modpm(room) == val then return false; end
26 room._data.restrict_pm_to = val; 19 room._data.allow_modpm = val;
27 return true;
28 end
29
30 local function is_visitor_pm_off(room)
31 return room._data.restrict_pm_visitor or false;
32 end
33
34 local function set_visitor_pm_off(room, val)
35 if is_visitor_pm_off(room) == val then return false; end
36 room._data.restrict_pm_visitor = val;
37 return true; 20 return true;
38 end 21 end
39 22
40 module:hook("muc-config-form", function(event) 23 module:hook("muc-config-form", function(event)
41 local affilpm = get_restrict_pm(event.room); 24 local pmval = get_allow_pm(event.room);
42 table.insert(event.form, { 25 table.insert(event.form, {
43 name = 'muc#restrict_pm'; 26 name = 'muc#allow_pm';
44 type = 'list-single'; 27 type = 'list-single';
45 label = 'Allow PMs from'; 28 label = 'Allow PMs from';
46 options = { 29 options = {
47 { value = 'owner', label = 'Owner', default = affilpm == 'owner' }, 30 { value = 'everyone', label = 'Everyone', default = pmval == 'everyone' },
48 { value = 'administrator', label = 'Administrators', default = affilpm == 'administrator' }, 31 { value = 'participants', label = 'Participants', default = pmval == 'participants' },
49 { value = 'member', label = 'Members', default = affilpm == 'member' }, 32 { value = 'members', label = 'Members', default = pmval == 'members' },
50 { value = 'none', label = 'Everyone', default = affilpm == 'none' } 33 { value = 'moderators', label = 'Moderators', default = pmval == 'moderators' },
34 { value = 'none', label = 'No one', default = pmval == 'none' }
51 } 35 }
52 }); 36 });
53
54 table.insert(event.form, { 37 table.insert(event.form, {
55 name = 'muc#restrict_pm_to'; 38 name = 'muc#allow_modpm';
56 type = 'boolean'; 39 type = 'boolean';
57 label = 'Allow PMs to everyone'; 40 label = 'Allow PMs to moderators';
58 value = can_pm_anyone(event.room); 41 value = get_allow_modpm(event.room)
59 });
60
61 table.insert(event.form, {
62 name = 'muc#restrict_pm_visitor';
63 type = 'boolean';
64 label = 'Disable PMs from Visitors';
65 value = is_visitor_pm_off(event.room);
66 }); 42 });
67 end); 43 end);
68 44
69 module:hook("muc-config-submitted/muc#restrict_pm", function(event) 45 module:hook("muc-config-submitted/muc#allow_pm", function(event)
70 if set_restrict_pm(event.room, event.value) then 46 if set_allow_pm(event.room, event.value) then
71 event.status_codes["104"] = true; 47 event.status_codes["104"] = true;
72 end 48 end
73 end); 49 end);
74 50
75 module:hook("muc-config-submitted/muc#restrict_pm_to", function(event) 51 module:hook("muc-config-submitted/muc#allow_modpm", function(event)
76 if set_pm_anyone(event.room, event.value) then 52 if set_allow_modpm(event.room, event.value) then
77 event.status_codes["104"] = true; 53 event.status_codes["104"] = true;
78 end 54 end
79 end); 55 end);
80
81 module:hook("muc-config-submitted/muc#restrict_pm_visitor", function(event)
82 if set_visitor_pm_off(event.room, event.value) then
83 event.status_codes["104"] = true;
84 end
85 end);
86
87 local function can_user_pm(room, jid)
88 local affil, pmval = affils[room:get_affiliation(jid) or 'none'], affils[get_restrict_pm(room)];
89 if affil >= pmval then return true; end
90 return false;
91 end
92 56
93 module:hook("muc-private-message", function(event) 57 module:hook("muc-private-message", function(event)
94 local stanza, room = event.stanza, event.room; 58 local stanza, room = event.stanza, event.room;
95 local from_occupant = room:get_occupant_by_nick(stanza.attr.from); 59 local from_occupant = room:get_occupant_by_nick(stanza.attr.from);
96 local to_occupant = room:get_occupant_by_nick(stanza.attr.to); 60 local to_occupant = room:get_occupant_by_nick(stanza.attr.to);
97 61
98 -- To self is always okay 62 -- To self is always okay
99 if to_occupant.bare_jid == from_occupant.bare_jid then return; end 63 if to_occupant.bare_jid == from_occupant.bare_jid then return; end
100 64
101 -- To moderation is okay 65 if get_allow_modpm(room) and to_occupant and to_occupant.role == 'moderator' then return; end
102 if to_occupant and to_occupant.role == 'moderator' then return; end
103 66
104 if not is_visitor_pm_off(room) or (from_occupant == nil or from_occupant.role ~= 'visitor') then 67 local pmval = get_allow_pm(room);
105 -- If visitors disabled 68 local from_affiliation = room:get_affiliation(from_occupant.bare_jid) or 'none';
106 if can_user_pm(room, from_occupant.bare_jid) and (can_pm_anyone(room) or can_user_pm(room, to_occupant.bare_jid)) then 69 local to_affiliation = room:get_affiliation(to_occupant.bare_jid) or 'none';
107 return; 70 if pmval == 'everyone' then return;
108 end; 71 elseif pmval == 'participants' and from_occupant.role ~= 'visitor' then
109 end 72 if to_occupant.role ~= 'visitor' or from_occupant.role == 'moderator' then return; end
73 elseif pmval == 'members' and from_affiliation ~= 'none' then
74 if to_affiliation ~= 'none' or from_occupant.role == 'moderator' then return; end
75 elseif pmval == 'moderators' and from_occupant.role == 'moderator' then return; end
110 76
111 room:route_to_occupant( 77 room:route_to_occupant(
112 from_occupant, 78 from_occupant,
113 st.error_reply(stanza, "cancel", "policy-violation", "Private messages are disabled", room.jid) 79 st.error_reply(stanza, "cancel", "policy-violation", "Private messages are restricted", room.jid)
114 ); 80 );
115 return false; 81 return false;
116 end, 1); 82 end, 1);