comparison mod_ping_muc/mod_ping_muc.lua @ 4804:a7c0c70e64b6

mod_ping_muc: Yet another attempt to improve MUC reliability This time by moving XEP-0410 to the server. Will this save the users from the s2s timeouts? What horrifying unintentional side-effects will this have? Tune in next week to find out!
author Kim Alvefur <zash@zash.se>
date Thu, 02 Dec 2021 23:43:11 +0100
parents
children 806f7c8d830b
comparison
equal deleted inserted replaced
4803:f74c7c518bb2 4804:a7c0c70e64b6
1 local id = require "util.id";
2 local jid = require "util.jid";
3 local set = require "util.set";
4 local st = require "util.stanza";
5
6 module:depends "track_muc_joins";
7 module:add_feature("https://modules.prosody.im/mod_" .. module.name);
8
9 local local_sessions = prosody.hosts[module.host].sessions;
10
11 module:hook_global("s2s-destroyed", function(event)
12 local s2s_session = event.session;
13 if s2s_session.direction == "outgoing" and s2s_session.from_host ~= module.host then
14 return
15 elseif s2s_session.direction == "incoming" and s2s_session.to_host ~= module.host then
16 return
17 end
18
19 local related_hosts = set.new({ s2s_session.direction == "outgoing" and s2s_session.to_host or s2s_session.from_host });
20
21 if s2s_session.hosts then
22 -- While rarely used, multiplexing is still supported
23 for host, state in pairs(s2s_session.hosts) do if state.authed then related_hosts:add(host); end end
24 end
25
26 for _, user_session in pairs(local_sessions) do
27 for _, session in pairs(user_session.sessions) do
28 if session.rooms_joined then
29 for room, info in pairs(session.rooms_joined) do
30 local nick = info.nick or info;
31 local room_nick = room .. "/" .. nick;
32 if related_hosts:contains(jid.host(room)) then
33 -- User is in a MUC room for which the s2s connection was lost. Now what?
34
35 -- Self-ping
36 -- =========
37 --
38 -- Response of <iq type=result> means the user is still in the room
39 -- (and self-ping is supported), so we do nothing.
40 --
41 -- An error reply either means the user has fallen out of the room,
42 -- or that self-ping is unsupported. In the later case, whether the
43 -- user is still joined is indeterminate and we might as well
44 -- pretend they fell out.
45 module:send_iq(st.iq({ type = "get"; id = id.medium(); from = session.full_jid; to = room_nick })
46 :tag("ping", { xmlns = "urn:xmpp:ping"; }))
47 :catch(function(err)
48 module:send(
49 st.presence({ type = "unavailable"; id = id.medium(); to = session.full_jid; from = room_nick })
50 :tag("x", { xmlns = "http://jabber.org/protocol/muc#user" })
51 :tag("item", { affiliation = "none"; role = "none" })
52 :text_tag("reason", err.text or "Connection to remote server lost")
53 :up()
54 :tag("status", { code = "110" }):up()
55 :tag("status", { code = "307" }):up()
56 :tag("status", { code = "333" }):up()
57 :reset());
58 end);
59 -- TODO do this with some delay?
60 end
61 end
62
63 end
64 end
65 end
66 end);