plugins/ping.lua

Thu, 27 Aug 2015 14:22:25 +0200

author
Kim Alvefur <zash@zash.se>
date
Thu, 27 Aug 2015 14:22:25 +0200
changeset 393
69229fa1d24f
parent 380
0891b4e27766
child 457
73d4eb93657b
permissions
-rw-r--r--

plugins.ping: Import socket.gettime to a local, LuaSocket stopped setting globals

250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 147
diff changeset
1 local verse = require "verse";
393
69229fa1d24f plugins.ping: Import socket.gettime to a local, LuaSocket stopped setting globals
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
2 local gettime = require"socket".gettime;
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 147
diff changeset
3
36
fc2cd2f36cdd plugins.ping: Define xmlns_ping namespace
Matthew Wild <mwild1@gmail.com>
parents: 34
diff changeset
4 local xmlns_ping = "urn:xmpp:ping";
32
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 function verse.plugins.ping(stream)
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 function stream:ping(jid, callback)
393
69229fa1d24f plugins.ping: Import socket.gettime to a local, LuaSocket stopped setting globals
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
8 local t = gettime();
380
0891b4e27766 Discard trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 339
diff changeset
9 stream:send_iq(verse.iq{ to = jid, type = "get" }:tag("ping", { xmlns = xmlns_ping }),
32
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function (reply)
41
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
11 if reply.attr.type == "error" then
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
12 local type, condition, text = reply:get_error();
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
13 if condition ~= "service-unavailable" and condition ~= "feature-not-implemented" then
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
14 callback(nil, jid, { type = type, condition = condition, text = text });
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
15 return;
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
16 end
1a1bd8cd4bdb plugins.ping: Don't handle all errors as successful pongs, call callback with nil time and 3rd parameter a table with error info
Matthew Wild <mwild1@gmail.com>
parents: 36
diff changeset
17 end
393
69229fa1d24f plugins.ping: Import socket.gettime to a local, LuaSocket stopped setting globals
Kim Alvefur <zash@zash.se>
parents: 380
diff changeset
18 callback(gettime()-t, jid);
32
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end);
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
339
72fbfb0367e9 plugins.ping: Reply to pings
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
21 stream:hook("iq/"..xmlns_ping, function(stanza)
72fbfb0367e9 plugins.ping: Reply to pings
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
22 return stream:send(verse.reply(stanza));
72fbfb0367e9 plugins.ping: Reply to pings
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
23 end);
34
dd5899412e3f plugins.ping: Return true on module load to indicate load success
Matthew Wild <mwild1@gmail.com>
parents: 32
diff changeset
24 return true;
32
391048601d54 plugins.ping: Add ping plugin to XMPP ping a JID
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end

mercurial