Comparison

mod_muc_gateway_optimize/mod_muc_gateway_optimize.lua @ 6055:23c4c61a1068

mod_muc_gateway_optimize: New module to optimize muc presence to remote gateways Some gateways are happy to receive presence for each participant in MUCs that they are in only once, to any one of their joined JIDs.
author Stephen Paul Weber <singpolyma@singpolyma.net>
date Sun, 17 Nov 2024 22:32:52 -0500
comparison
equal deleted inserted replaced
6054:d39ddf13ff0a 6055:23c4c61a1068
1 local jid = require("util.jid")
2 local mod_muc = module:depends("muc")
3
4 local gateway_hosts = module:get_option_array("gateway_hosts", {})
5
6 function optimize(remote_host, event)
7 local stanza = event.stanza
8 module:log("debug", "optimize presence event destined for " .. remote_host)
9
10 local muc_x = stanza:get_child("x", "http://jabber.org/protocol/muc#user")
11 if muc_x then
12 for status in muc_x:childtags("status") do
13 if status.attr.status == "110" then
14 module:log("debug", "optimize delivering 110")
15 -- Always deliver self-presence
16 return
17 end
18 end
19 end
20
21 local bare_jid = jid.bare(stanza.attr.to)
22 local room = mod_muc.get_room_from_jid(jid.bare(stanza.attr.from))
23 if not room then return end
24 for nick, occupant in room:each_occupant() do
25 local occupant_host = jid.host(occupant.bare_jid)
26 if occupant_host == remote_host then
27 -- This is the "first" occupant from the host
28 -- which is the only one we will route non-110
29 -- presence to
30 if occupant.bare_jid == bare_jid then
31 module:log("debug", "optimize found first occupant, so route")
32 return
33 else
34 module:log("debug", "optimize found non-first occupant, so drop")
35 return true
36 end
37 end
38 end
39 -- If we get here we found no occupants for this host
40 module:log("debug", "optimize found no occupants for host " .. remote_host)
41 end
42
43 -- Note this will only affect gateways over s2s for now
44 module:hook("route/remote", function (event)
45 if event.stanza.name ~= "presence" then
46 return
47 end
48
49 local remote_host = jid.host(event.stanza.attr.to)
50 for _, gateway_host in pairs(gateway_hosts) do
51 if remote_host == gateway_host then
52 return optimize(remote_host, event)
53 end
54 end
55 end, 1000)