Mercurial > prosody-modules
annotate mod_muc_auto_role/mod_muc_auto_role.lua @ 6496:5a01db53d680
mod_muc_auto_role: Also restrict default role assignment to unaffiliated JIDs
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Wed, 25 Mar 2026 09:47:22 +0000 |
| parents | fb7902f66c36 |
| children | 4226e6aadbd8 |
| rev | line source |
|---|---|
|
6422
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local jid = require "prosody.util.jid"; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local xmlns_aff = "urn:xmpp:raa:0"; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 local raa_servers = module:get_option_set("muc_auto_role_trusted_servers", {}); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 local trust_threshold = module:get_option_integer("muc_auto_role_trust_threshold", 51, 0, 100); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 local untrusted_servers = module:get_option_set("muc_auto_role_untrusted_servers", {}); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 local default_role = module:get_option_enum("muc_default_role", "visitor", "participant"); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 -- TODO: This should be a config option in mod_muc |
|
6496
5a01db53d680
mod_muc_auto_role: Also restrict default role assignment to unaffiliated JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6495
diff
changeset
|
13 module:hook("muc-get-default-role", function (event) |
|
5a01db53d680
mod_muc_auto_role: Also restrict default role assignment to unaffiliated JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6495
diff
changeset
|
14 if (event.affiliation or "none") == "none" then |
|
5a01db53d680
mod_muc_auto_role: Also restrict default role assignment to unaffiliated JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6495
diff
changeset
|
15 return default_role; |
|
5a01db53d680
mod_muc_auto_role: Also restrict default role assignment to unaffiliated JIDs
Matthew Wild <mwild1@gmail.com>
parents:
6495
diff
changeset
|
16 end |
|
6422
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end, 5); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 module:hook("muc-occupant-pre-join", function (event) |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 local stanza = event.stanza; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 |
|
6495
fb7902f66c36
mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
22 if event.room:get_affiliation(event.occupant.bare_jid) then |
|
fb7902f66c36
mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
23 -- Skip for already-affiliated users |
|
fb7902f66c36
mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
24 return; |
|
fb7902f66c36
mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
25 end |
|
fb7902f66c36
mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents:
6422
diff
changeset
|
26 |
|
6422
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 local from_host = jid.host(stanza.attr.from); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 if untrusted_servers:contains(from_host) then |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 event.occupant.role = "visitor"; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 end |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 if not raa_servers:contains(from_host) then |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 return; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 end |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 local raa = stanza:get_child("info", xmlns_aff); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 if not raa then |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 return; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 end |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 local aff = raa.attr.affiliation; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 local trust = tonumber(raa.attr.trust) or 50; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 if aff == "member" or aff == "admin" or trust >= trust_threshold then |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 event.occupant.role = "participant"; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 else |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 event.occupant.role = "visitor"; |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 end |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
51 module:log("debug", "Assigning <%s> role '%s' due to affiliation %s and trust score %d of <%s>", |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
52 event.occupant.nick, event.occupant.role, aff, trust, stanza.attr.from |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
53 ); |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
54 |
|
c41615686ce4
mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
55 end); |
