Mercurial > prosody-modules
annotate mod_muc_mav/mod_muc_mav.lua @ 6530:8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
| author | Marvin W <hg@larma.de> |
|---|---|
| date | Mon, 04 May 2026 20:55:39 +0200 |
| parents | |
| children | 4d7e2d0db2ab |
| 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"; |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
10 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
11 local mav_versions = {} |
|
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 function get_affiliations(room) |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
14 local jid_affiliations = {} |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
15 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
|
16 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
|
17 table.insert(jid_affiliations, jid.."\0"..affiliation); |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
18 local nick = affiliation_data and affiliation_data.reserved_nickname; |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
19 x:tag("item", {affiliation = affiliation or "none"; jid = jid; nick = nick;}):up(); |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
20 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
21 table.sort(jid_affiliations); |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 return hash, x; |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
26 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
27 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
28 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
|
29 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
|
30 local is_initial = false; |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
31 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
|
32 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
|
33 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
34 if is_initial then |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
35 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
|
36 else |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
37 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
|
38 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
|
39 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
|
40 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
|
41 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
|
42 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
43 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
44 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
45 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
46 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
|
47 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
|
48 if muc_x then |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
49 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
|
50 if mav_current then |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
51 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
|
52 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
|
53 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
54 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
55 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
56 module:send(stanza); |
|
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 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
59 -- TODO: Stop this hack |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
60 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
|
61 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
|
62 function module.unload() |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
63 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
|
64 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
65 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
66 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
67 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
|
68 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
|
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 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 -- TODO: Support for diffs |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
82 -- Send full response |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
83 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
|
84 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
|
85 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
|
86 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
|
87 else |
|
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, 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
|
89 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
90 end, -100); |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
91 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
92 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
|
93 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
|
94 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
|
95 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
|
96 end |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
97 end); |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
98 |
|
8e87fbbb6cd3
mod_muc_mav: Add module for XEP-0463: MUC Affiliations Versioning
Marvin W <hg@larma.de>
parents:
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 end); |
