comparison mod_muc_restrict_pm/mod_muc_restrict_pm.lua @ 5910:7358d1b64b1d

mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
author Nicholas George <wirlaburla@worlio.com>
date Tue, 21 May 2024 00:40:06 -0500
parents
children 9d0270680d1f
comparison
equal deleted inserted replaced
5909:070b0db6c4a0 5910:7358d1b64b1d
1 local st = require "util.stanza";
2
3 local affils = {
4 none = 0;
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
13
14 local function set_restrict_pm(room, affil)
15 if get_restrict_pm(room) == affil then return false; end
16 room._data.restrict_pm = affil;
17 return true;
18 end
19
20 local function can_pm_anyone(room)
21 return room._data.restrict_pm_to or false;
22 end
23
24 local function set_pm_anyone(room, val)
25 if can_pm_anyone(room) == val then return false; end
26 room._data.restrict_pm_to = 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;
38 end
39
40 module:hook("muc-config-form", function(event)
41 local affilpm = get_restrict_pm(event.room);
42 table.insert(event.form, {
43 name = 'muc#restrict_pm';
44 type = 'list-single';
45 label = 'Allow PMs from';
46 options = {
47 { value = 'owner', label = 'Owner', default = affilpm == 'owner' },
48 { value = 'administrator', label = 'Administrators', default = affilpm == 'administrator' },
49 { value = 'member', label = 'Members', default = affilpm == 'member' },
50 { value = 'none', label = 'Everyone', default = affilpm == 'none' }
51 }
52 });
53
54 table.insert(event.form, {
55 name = 'muc#restrict_pm_to';
56 type = 'boolean';
57 label = 'Allow PMs to everyone';
58 value = can_pm_anyone(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 });
67 end);
68
69 module:hook("muc-config-submitted/muc#restrict_pm", function(event)
70 if set_restrict_pm(event.room, event.value) then
71 event.status_codes["104"] = true;
72 end
73 end);
74
75 module:hook("muc-config-submitted/muc#restrict_pm_to", function(event)
76 if set_pm_anyone(event.room, event.value) then
77 event.status_codes["104"] = true;
78 end
79 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
93 module:hook("muc-private-message", function(event)
94 local from_occupant, to_occupant = event.room:get_occupant_by_nick(event.stanza.attr.from), event.room:get_occupant_by_nick(event.stanza.attr.to);
95
96 -- To self is always okay
97 if to_occupant.bare_jid == from_occupant.bare_jid then return; end
98
99 -- To moderation is okay
100 if to_occupant and to_occupant.role == 'moderator' then return; end
101
102 if not is_visitor_pm_off(event.room) or (from_occupant == nil or from_occupant.role ~= 'visitor') then
103 -- If visitors disabled
104 if can_user_pm(event.room, from_occupant.bare_jid) and (can_pm_anyone(event.room) or can_user_pm(event.room, to_occupant.bare_jid)) then return; end;
105 end
106
107 event.room:route_to_occupant(from_occupant, st.error_reply(event.stanza, "cancel", "policy-violation", "Private messages are disabled", event.room.jid));
108 return false;
109 end, 1);