annotate mod_muc_restrict_pm/mod_muc_restrict_pm.lua @ 5911:e7584fd5b191

mod_muc_restrict_pm: fix table in README
author Nicholas George <wirlaburla@worlio.com>
date Tue, 21 May 2024 01:09:12 -0500
parents 7358d1b64b1d
children 9d0270680d1f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5910
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
1 local st = require "util.stanza";
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
2
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
3 local affils = {
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
4 none = 0;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
5 member = 1;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
6 administrator = 2;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
7 owner = 3;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
8 };
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
9
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
10 local function get_restrict_pm(room)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
11 return room._data.restrict_pm or 'none';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
12 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
13
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
14 local function set_restrict_pm(room, affil)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
15 if get_restrict_pm(room) == affil then return false; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
16 room._data.restrict_pm = affil;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
17 return true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
18 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
19
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
20 local function can_pm_anyone(room)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
21 return room._data.restrict_pm_to or false;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
22 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
23
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
24 local function set_pm_anyone(room, val)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
25 if can_pm_anyone(room) == val then return false; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
26 room._data.restrict_pm_to = val;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
27 return true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
28 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
29
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
30 local function is_visitor_pm_off(room)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
31 return room._data.restrict_pm_visitor or false;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
32 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
33
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
34 local function set_visitor_pm_off(room, val)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
35 if is_visitor_pm_off(room) == val then return false; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
36 room._data.restrict_pm_visitor = val;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
37 return true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
38 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
39
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
40 module:hook("muc-config-form", function(event)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
41 local affilpm = get_restrict_pm(event.room);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
42 table.insert(event.form, {
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
43 name = 'muc#restrict_pm';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
44 type = 'list-single';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
45 label = 'Allow PMs from';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
46 options = {
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
47 { value = 'owner', label = 'Owner', default = affilpm == 'owner' },
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
48 { value = 'administrator', label = 'Administrators', default = affilpm == 'administrator' },
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
49 { value = 'member', label = 'Members', default = affilpm == 'member' },
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
50 { value = 'none', label = 'Everyone', default = affilpm == 'none' }
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
51 }
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
52 });
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
53
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
54 table.insert(event.form, {
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
55 name = 'muc#restrict_pm_to';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
56 type = 'boolean';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
57 label = 'Allow PMs to everyone';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
58 value = can_pm_anyone(event.room);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
59 });
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
60
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
61 table.insert(event.form, {
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
62 name = 'muc#restrict_pm_visitor';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
63 type = 'boolean';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
64 label = 'Disable PMs from Visitors';
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
65 value = is_visitor_pm_off(event.room);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
66 });
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
67 end);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
68
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
69 module:hook("muc-config-submitted/muc#restrict_pm", function(event)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
70 if set_restrict_pm(event.room, event.value) then
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
71 event.status_codes["104"] = true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
72 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
73 end);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
74
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
75 module:hook("muc-config-submitted/muc#restrict_pm_to", function(event)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
76 if set_pm_anyone(event.room, event.value) then
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
77 event.status_codes["104"] = true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
78 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
79 end);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
80
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
81 module:hook("muc-config-submitted/muc#restrict_pm_visitor", function(event)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
82 if set_visitor_pm_off(event.room, event.value) then
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
83 event.status_codes["104"] = true;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
84 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
85 end);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
86
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
87 local function can_user_pm(room, jid)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
88 local affil, pmval = affils[room:get_affiliation(jid) or 'none'], affils[get_restrict_pm(room)];
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
89 if affil >= pmval then return true; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
90 return false;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
91 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
92
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
93 module:hook("muc-private-message", function(event)
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
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);
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
95
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
96 -- To self is always okay
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
97 if to_occupant.bare_jid == from_occupant.bare_jid then return; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
98
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
99 -- To moderation is okay
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
100 if to_occupant and to_occupant.role == 'moderator' then return; end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
101
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
102 if not is_visitor_pm_off(event.room) or (from_occupant == nil or from_occupant.role ~= 'visitor') then
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
103 -- If visitors disabled
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
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;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
105 end
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
106
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
107 event.room:route_to_occupant(from_occupant, st.error_reply(event.stanza, "cancel", "policy-violation", "Private messages are disabled", event.room.jid));
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
108 return false;
7358d1b64b1d mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff changeset
109 end, 1);