annotate mod_muc_mav/mod_muc_mav.lua @ 6541:03c80b7308bf

mod_muc_mav: stanza attribute cannot be a boolean
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Tue, 19 May 2026 09:15:06 -0500
parents 7cb25e7d945d
children d7162714f8b2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
1 local st = require "util.stanza";
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
2 local hashes = require "util.hashes";
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
3 local base64 = require "util.encodings".base64;
6540
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
4 local util_jid = require "util.jid";
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
5
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
6 local mod_muc = module:depends"muc";
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
7
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
8 local xmlns_mav = "urn:xmpp:muc:affiliations:1";
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
9 local xmlns_muc = "http://jabber.org/protocol/muc";
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
10 local xmlns_muc_user = "http://jabber.org/protocol/muc#user";
6539
4d7e2d0db2ab mod_muc_mav: include occupant-id in affiliation list
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6530
diff changeset
11 local xmlns_occupant_id = "urn:xmpp:occupant-id:0";
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
12
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
13 local mav_versions = {}
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
14
6540
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
15 local function get_affiliations(from, room)
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
16 local jid_affiliations = {}
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
17 local x = st.stanza("x", {xmlns = xmlns_muc_user;});
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
18 for jid, affiliation, affiliation_data in room:each_affiliation() do
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
19 table.insert(jid_affiliations, jid.."\0"..affiliation);
6539
4d7e2d0db2ab mod_muc_mav: include occupant-id in affiliation list
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6530
diff changeset
20 local occupant_id = room:get_occupant_id_from_jid(jid);
6541
03c80b7308bf mod_muc_mav: stanza attribute cannot be a boolean
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6540
diff changeset
21 local nick = affiliation_data and affiliation_data.reserved_nickname or nil;
6540
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
22 local show_jid = not room:is_anonymous_for(from) or util_jid.bare(from) == jid and jid or nil
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
23 x:tag("item", {affiliation = affiliation or "none"; jid = show_jid; nick = nick;})
6539
4d7e2d0db2ab mod_muc_mav: include occupant-id in affiliation list
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6530
diff changeset
24 :tag("occupant-id", { xmlns = xmlns_occupant_id, id = occupant_id }):up():up();
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
25 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
26 table.sort(jid_affiliations);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
27 local hash_input = table.concat(jid_affiliations, "\0");
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
28 local hash = base64.encode(hashes.sha256(hash_input))
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
29 x:tag("mav", {xmlns = xmlns_mav; ["until"] = hash;}):up();
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
30 return hash, x;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
31 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
32
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
33 local function handle_stanza_with_x(room, stanza, mav_current)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
34 local muc_x = stanza:get_child("x", xmlns_muc_user);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
35 local is_initial = false;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
36 for child in muc_x:childtags("status") do
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
37 if child.attr.code == "110" then is_initial = true; end;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
38 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
39 if is_initial then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
40 muc_x:tag("mav", {xmlns = xmlns_mav; ["until"] = mav_current;}):up();
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
41 else
6540
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
42 local mav_new = get_affiliations(stanza.attr.from, room);
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
43 if mav_current ~= mav_new then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
44 module:log("info", "Update version of %s from %s to %s in affiliation update", stanza.attr.to, mav_current, mav_new);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
45 muc_x:tag("mav", {xmlns = xmlns_mav; ["since"] = mav_current; ["until"] = mav_new; }):up();
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
46 mav_versions[room.jid.."\0"..stanza.attr.to] = mav_new;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
47 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
48 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
49 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
50
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
51 local function room_route_stanza(room, stanza)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
52 local muc_x = stanza:get_child("x", xmlns_muc_user);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
53 if muc_x then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
54 local mav_current = mav_versions[room.jid.."\0"..stanza.attr.to];
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
55 if mav_current then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
56 if stanza.name == "presence" or stanza.name == "message" then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
57 handle_stanza_with_x(room, stanza, mav_current);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
58 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
59 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
60 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
61 module:send(stanza);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
62 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
63
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
64 -- TODO: Stop this hack
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
65 local original_room_route_stanza = mod_muc.room_mt.route_stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
66 mod_muc.room_mt.route_stanza = room_route_stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
67 function module.unload()
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
68 mod_muc.room_mt.route_stanza = original_room_route_stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
69 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
70
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
71
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
72 module:hook("muc-disco#info", function(event)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
73 event.reply:tag("feature", {var = xmlns_mav}):up();
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
74 end);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
75
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
76 module:hook("muc-occupant-pre-join", function(event)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
77 local room, stanza = event.room, event.stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
78 local muc_x = stanza:get_child("x", xmlns_muc);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
79 if not muc_x then return; end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
80 local mav_child = muc_x:get_child("mav", xmlns_mav);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
81 if not mav_child then return; end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
82 local mav_since = mav_child.attr["since"];
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
83 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_since;
6540
7cb25e7d945d mod_muc_mav: Prevent JIDs leaking to occupants who are not allowed
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6539
diff changeset
84 local mav_current, x = get_affiliations(stanza.attr.from, room);
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
85 if mav_current ~= mav_since then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
86 -- TODO: Support for diffs
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
87 -- Send full response
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
88 module:log("info", "Old version of %s is %s, sending full response for %s", stanza.attr.from, mav_since, mav_current);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
89 local announce_msg = st.message({ from = room.jid, to = stanza.attr.from }):add_child(x);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
90 room:route_stanza(announce_msg);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
91 mav_versions[room.jid.."\0"..stanza.attr.from] = mav_current;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
92 else
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
93 module:log("info", "Old version of %s is %s, still current", stanza.attr.from, mav_since);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
94 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
95 end, -100);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
96
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
97 module:hook("muc-occupant-left", function(event)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
98 local room, occupant = event.room, event.occupant;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
99 for jid in occupant:each_session() do
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
100 mav_versions[room.jid.."\0"..jid] = nil;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
101 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
102 end);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
103
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
104 module:hook("muc-occupant-pre-leave", function(event)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
105 local room, stanza = event.room, event.stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
106 mav_versions[room.jid.."\0"..stanza.attr.from] = nil;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
107 end);