annotate mod_muc_mav/mod_muc_mav.lua @ 6539:4d7e2d0db2ab

mod_muc_mav: include occupant-id in affiliation list
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Mon, 18 May 2026 10:23:12 -0500
parents 8e87fbbb6cd3
children 7cb25e7d945d
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;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
4
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
5 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
6
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
7 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
8 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
9 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
10 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
11
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
12 local mav_versions = {}
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
13
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
14 local function get_affiliations(room)
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
15 local jid_affiliations = {}
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
16 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
17 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
18 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
19 local occupant_id = room:get_occupant_id_from_jid(jid);
6530
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
20 local nick = affiliation_data and affiliation_data.reserved_nickname;
6539
4d7e2d0db2ab mod_muc_mav: include occupant-id in affiliation list
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6530
diff changeset
21 x:tag("item", {affiliation = affiliation or "none"; jid = jid; nick = nick;})
4d7e2d0db2ab mod_muc_mav: include occupant-id in affiliation list
Stephen Paul Weber <singpolyma@singpolyma.net>
parents: 6530
diff changeset
22 :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
23 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
24 table.sort(jid_affiliations);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
25 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
26 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
27 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
28 return hash, x;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
29 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
30
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
31 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
32 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
33 local is_initial = false;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
34 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
35 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
36 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
37 if is_initial then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
38 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
39 else
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
40 local mav_new = get_affiliations(room);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
41 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
42 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
43 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
44 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
45 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
46 end
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
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
49 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
50 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
51 if muc_x then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
52 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
53 if mav_current then
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
54 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
55 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
56 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
57 end
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 module:send(stanza);
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
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
62 -- TODO: Stop this hack
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
63 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
64 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
65 function module.unload()
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 = original_room_route_stanza;
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
67 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
68
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
69
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
70 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
71 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
72 end);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
73
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 mav_versions[room.jid.."\0"..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
82 local mav_current, x = get_affiliations(room);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
83 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
84 -- TODO: Support for diffs
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
85 -- Send full response
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
86 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
87 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
88 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
89 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
90 else
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
91 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
92 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
93 end, -100);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
94
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
95 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
96 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
97 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
98 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
99 end
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
100 end);
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
101
8e87fbbb6cd3 mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff changeset
102 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
103 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
104 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
105 end);