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