plugins/ping.lua

1
local verse = require "verse";
2
local gettime = require"socket".gettime;
3
local new_id = require "prosody.util.id".short;
4
 
5
local xmlns_ping = "urn:xmpp:ping";
6
 
7
function verse.plugins.ping(stream)
8
	function stream:ping(jid, callback)
9
		local t = gettime();
10
		local id = new_id();
11
		local ping = verse.iq{ id = id, to = jid, type = "get" }:tag("ping", { xmlns = xmlns_ping });
12
		stream:send_iq(ping,
13
			function (reply)
14
				if reply.attr.type == "error" then
15
					local type, condition, text = reply:get_error();
16
					if condition ~= "service-unavailable" and condition ~= "feature-not-implemented" then
17
						callback(nil, jid, { type = type, condition = condition, text = text });
18
						return;
19
					end
20
				end
21
				callback(gettime()-t, jid);
22
			end);
23
		return id;
24
	end
25
	stream:hook("iq/"..xmlns_ping, function(stanza)
26
		return stream:send(verse.reply(stanza));
27
	end);
28
	return true;
29
end