plugins/ping.lua

changeset 457
73d4eb93657b
parent 393
69229fa1d24f
child 490
6b2f31da9610
equal deleted inserted replaced
456:6a65142052c8 457:73d4eb93657b
1 local verse = require "verse"; 1 local verse = require "verse";
2 local gettime = require"socket".gettime; 2 local gettime = require"socket".gettime;
3 local new_id = require "util.id".short;
3 4
4 local xmlns_ping = "urn:xmpp:ping"; 5 local xmlns_ping = "urn:xmpp:ping";
5 6
6 function verse.plugins.ping(stream) 7 function verse.plugins.ping(stream)
7 function stream:ping(jid, callback) 8 function stream:ping(jid, callback)
8 local t = gettime(); 9 local t = gettime();
9 stream:send_iq(verse.iq{ to = jid, type = "get" }:tag("ping", { xmlns = xmlns_ping }), 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,
10 function (reply) 13 function (reply)
11 if reply.attr.type == "error" then 14 if reply.attr.type == "error" then
12 local type, condition, text = reply:get_error(); 15 local type, condition, text = reply:get_error();
13 if condition ~= "service-unavailable" and condition ~= "feature-not-implemented" then 16 if condition ~= "service-unavailable" and condition ~= "feature-not-implemented" then
14 callback(nil, jid, { type = type, condition = condition, text = text }); 17 callback(nil, jid, { type = type, condition = condition, text = text });
15 return; 18 return;
16 end 19 end
17 end 20 end
18 callback(gettime()-t, jid); 21 callback(gettime()-t, jid);
19 end); 22 end);
23 return id;
20 end 24 end
21 stream:hook("iq/"..xmlns_ping, function(stanza) 25 stream:hook("iq/"..xmlns_ping, function(stanza)
22 return stream:send(verse.reply(stanza)); 26 return stream:send(verse.reply(stanza));
23 end); 27 end);
24 return true; 28 return true;

mercurial