comparison plugins/mod_admin_shell.lua @ 13996:8d90a83603d6 13.0

mod_admin_shell: Allow pinging any JID with xmpp:ping() Feature requested by MattJ. Argument that users don't expect s2s-only pings.
author Kim Alvefur <zash@zash.se>
date Mon, 01 Dec 2025 16:35:11 +0100
parents f44f2a8a8c37
children 015c8c75f8cf 5ad86978ae78
comparison
equal deleted inserted replaced
13992:9cfc62b4625b 13996:8d90a83603d6
2049 return true, "Showing "..(pat and (matches.." of ") or "all " )..total.." users"; 2049 return true, "Showing "..(pat and (matches.." of ") or "all " )..total.." users";
2050 end 2050 end
2051 2051
2052 def_env.xmpp = new_section("Commands for sending XMPP stanzas"); 2052 def_env.xmpp = new_section("Commands for sending XMPP stanzas");
2053 2053
2054 describe_command [[xmpp:ping(localhost, remotehost) - Sends a ping to a remote XMPP server and reports the response]] 2054 describe_command [[xmpp:ping(localjid, remotejid) - Sends a ping to an XMPP entity and reports the response]]
2055 local new_id = require "prosody.util.id".medium; 2055 local new_id = require "prosody.util.id".medium;
2056 function def_env.xmpp:ping(localhost, remotehost, timeout) 2056 function def_env.xmpp:ping(localjid, remotejid, timeout)
2057 localhost = select(2, jid_split(localhost)); 2057 local localhost, localresource = select(2, jid_split(localjid));
2058 remotehost = select(2, jid_split(remotehost)); 2058 local remotehost = select(2, jid_split(remotejid));
2059 if not localhost then 2059 if not localhost then
2060 return nil, "Invalid sender hostname"; 2060 return nil, "Invalid sender JID";
2061 elseif not prosody.hosts[localhost] then 2061 elseif not prosody.hosts[localhost] then
2062 return nil, "No such local host"; 2062 return nil, "No such local host";
2063 elseif localresource then
2064 return nil, "Server can't send ping from local resource"; -- Would require creating a temporary session with bound resource and everything
2063 end 2065 end
2064 if not remotehost then 2066 if not remotehost then
2065 return nil, "Invalid destination hostname"; 2067 return nil, "Invalid destination JID";
2066 elseif prosody.hosts[remotehost] then 2068 end
2067 return nil, "Both hosts are local"; 2069 local iq = st.iq{ from=localjid, to=remotejid, type="get", id=new_id()}
2068 end
2069 local iq = st.iq{ from=localhost, to=remotehost, type="get", id=new_id()}
2070 :tag("ping", {xmlns="urn:xmpp:ping"}); 2070 :tag("ping", {xmlns="urn:xmpp:ping"});
2071 local time_start = time.now(); 2071 local time_start = time.now();
2072 local print = self.session.print; 2072 local print = self.session.print;
2073 local function onchange(what) 2073 local function onchange(what)
2074 return function(event) 2074 return function(event)
2080 time.now() - time_start)); 2080 time.now() - time_start));
2081 elseif s2s_session.type == "s2sin_unauthed" and s2s_session.to_host == nil and s2s_session.from_host == nil then 2081 elseif s2s_session.type == "s2sin_unauthed" and s2s_session.to_host == nil and s2s_session.from_host == nil then
2082 print(("Session %s %s (%gs)"):format(s2s_session.id, what, time.now() - time_start)); 2082 print(("Session %s %s (%gs)"):format(s2s_session.id, what, time.now() - time_start));
2083 end 2083 end
2084 end 2084 end
2085 end
2086 if localhost == remotehost or prosody.hosts[remotehost] then
2087 -- No s2s connections will be triggered
2088 return module:context(localhost):send_iq(iq, nil, timeout):next(function(pong)
2089 return ("pong from %s in %gs"):format(pong.stanza.attr.from, time.now() - time_start);
2090 end);
2085 end 2091 end
2086 local onconnected = onchange("connected"); 2092 local onconnected = onchange("connected");
2087 local onauthenticated = onchange("authenticated"); 2093 local onauthenticated = onchange("authenticated");
2088 local onestablished = onchange("established"); 2094 local onestablished = onchange("established");
2089 local ondestroyed = onchange("destroyed"); 2095 local ondestroyed = onchange("destroyed");