annotate mod_muc_auto_role/mod_muc_auto_role.lua @ 6562:5da6fb562df9 default tip

mod_unified_push: Fix push error handling (fixes #2000) Use the error object that send_iq() passes as an argument to it's reject callback instead of attempting and failing to do the parsing in the callback itself.
author kmq
date Mon, 06 Jul 2026 14:23:57 +0200
parents 4226e6aadbd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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";
6498
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
2 local datetime = require "prosody.util.datetime";
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 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
5
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 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
7 local trust_threshold = module:get_option_integer("muc_auto_role_trust_threshold", 51, 0, 100);
6498
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
8 local age_threshold = module:get_option_period("muc_auto_role_age_threshold", "1d");
6422
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 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
11
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 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
13
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 -- 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
15 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
16 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
17 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
18 end
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end, 5);
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 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
22 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
23
6495
fb7902f66c36 mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 6422
diff changeset
24 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
25 -- 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
26 return;
fb7902f66c36 mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 6422
diff changeset
27 end
fb7902f66c36 mod_muc_auto_role: Skip processing for users already affiliated with the MUC
Matthew Wild <mwild1@gmail.com>
parents: 6422
diff changeset
28
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 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
30
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 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
32 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
33 end
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 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
36 return;
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 end
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 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
40 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
41 return;
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 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
45 local trust = tonumber(raa.attr.trust) or 50;
6498
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
46 local since = raa.attr.since and datetime.parse(raa.attr.since);
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
47 local age = since and (os.time() - since);
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
6498
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
49 if (aff == "member" or aff == "admin") or (trust >= trust_threshold and (not age or age >= age_threshold)) then
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 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
51 else
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.role = "visitor";
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 end
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
6498
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
55 module:log("debug", "Assigning <%s> role '%s' due to affiliation %s, trust score %d, age %d of <%s>",
4226e6aadbd8 mod_muc_auto_role: Add account age threshold
Matthew Wild <mwild1@gmail.com>
parents: 6496
diff changeset
56 event.occupant.nick, event.occupant.role, aff, trust, age or -1, stanza.attr.from
6422
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 );
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58
c41615686ce4 mod_muc_auto_role: Set default role for MUC occupants based on policies
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end);