Mercurial > prosody-modules
annotate 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 |
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 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
3 local function get_allow_pm(room) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
4 return room._data.allow_pm or 'everyone'; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
5 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
6 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
7 local function set_allow_pm(room, val) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
8 if get_allow_pm(room) == val then return false; end |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
9 room._data.allow_pm = val; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
10 return true; |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
11 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
12 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
13 local function get_allow_modpm(room) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
14 return room._data.allow_modpm or false; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
15 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
16 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
17 local function set_allow_modpm(room, val) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
18 if get_allow_modpm(room) == val then return false; end |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
19 room._data.allow_modpm = val; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
20 return true; |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
21 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
22 |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
23 module:hook("muc-config-form", function(event) |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
24 local pmval = get_allow_pm(event.room); |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
25 table.insert(event.form, { |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
26 name = 'muc#allow_pm'; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
27 type = 'list-single'; |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
28 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
|
29 options = { |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
30 { value = 'everyone', label = 'Everyone', default = pmval == 'everyone' }, |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
31 { value = 'participants', label = 'Participants', default = pmval == 'participants' }, |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
32 { value = 'members', label = 'Members', default = pmval == 'members' }, |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
33 { value = 'moderators', label = 'Moderators', default = pmval == 'moderators' }, |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
34 { value = 'none', label = 'No one', default = pmval == 'none' } |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
35 } |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
36 }); |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
37 table.insert(event.form, { |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
38 name = 'muc#allow_modpm'; |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
39 type = 'boolean'; |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
40 label = 'Allow PMs to moderators'; |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
41 value = get_allow_modpm(event.room) |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
42 }); |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
43 end); |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
44 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
45 module:hook("muc-config-submitted/muc#allow_pm", function(event) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
46 if set_allow_pm(event.room, event.value) then |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
47 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
|
48 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
49 end); |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
50 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
51 module:hook("muc-config-submitted/muc#allow_modpm", function(event) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
52 if set_allow_modpm(event.room, event.value) then |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
53 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
|
54 end |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
55 end); |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
56 |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
57 module:hook("muc-private-message", function(event) |
5912
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
58 local stanza, room = event.stanza, event.room; |
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
59 local from_occupant = room:get_occupant_by_nick(stanza.attr.from); |
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
60 local to_occupant = room:get_occupant_by_nick(stanza.attr.to); |
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
61 |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
62 -- 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
|
63 if to_occupant.bare_jid == from_occupant.bare_jid then return; end |
5912
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
64 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
65 if get_allow_modpm(room) and to_occupant and to_occupant.role == 'moderator' then return; end |
5912
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
66 |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
67 local pmval = get_allow_pm(room); |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
68 local from_affiliation = room:get_affiliation(from_occupant.bare_jid) or 'none'; |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
69 local to_affiliation = room:get_affiliation(to_occupant.bare_jid) or 'none'; |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
70 if pmval == 'everyone' then return; |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
71 elseif pmval == 'participants' and from_occupant.role ~= 'visitor' then |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
72 if to_occupant.role ~= 'visitor' or from_occupant.role == 'moderator' then return; end |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
73 elseif pmval == 'members' and from_affiliation ~= 'none' then |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
74 if to_affiliation ~= 'none' or from_occupant.role == 'moderator' then return; end |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
75 elseif pmval == 'moderators' and from_occupant.role == 'moderator' then return; end |
5912
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
76 |
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
77 room:route_to_occupant( |
9d0270680d1f
mod_muc_restrict_pm: fix lua warnings
Nicholas George <wirlaburla@worlio.com>
parents:
5910
diff
changeset
|
78 from_occupant, |
5913
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
79 st.error_reply(stanza, "cancel", "policy-violation", "Private messages are restricted", room.jid) |
d82c0383106a
mod_muc_restrict_pm: small rewrite. improves room config options
Nicholas George <wirlaburla@worlio.com>
parents:
5912
diff
changeset
|
80 ); |
5910
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
81 return false; |
7358d1b64b1d
mod_muc_restrict_pm: Limit who may send and recieve MUC PMs
Nicholas George <wirlaburla@worlio.com>
parents:
diff
changeset
|
82 end, 1); |