55
|
1 |
|
|
2 |
-- XMPP PING (XEP-0199) |
|
3 |
|
|
4 |
-- library |
|
5 |
|
|
6 |
require 'lm' |
|
7 |
require 'iq' |
|
8 |
|
|
9 |
-- public |
|
10 |
|
|
11 |
ping = { } |
|
12 |
|
|
13 |
function ping.send ( conn, to, success, fail ) |
|
14 |
iq.send ( conn, to, 'get', success, fail ) |
|
15 |
end |
|
16 |
|
|
17 |
-- private |
|
18 |
|
|
19 |
local ping_incoming_iq_handler = lm.message_handler.new ( |
|
20 |
function ( conn, mess ) |
|
21 |
local mtype, smtype = mess:type () |
|
22 |
if smtype == 'get' then |
|
23 |
local p = mess:child ( 'ping' ) |
|
24 |
if p and p:attribute ( 'xmlns' ) == 'urn:xmpp:ping' then |
|
25 |
conn:send ( lm.message.create { mtype = 'iq-result', to = mess:attribute ( 'from' ), id = mess:attribute ( 'id' ) } ) |
|
26 |
return true |
|
27 |
end |
|
28 |
end |
|
29 |
return false |
|
30 |
end ) |
|
31 |
|
|
32 |
-- mcabber |
|
33 |
|
|
34 |
main.command ( 'ping', |
|
35 |
function ( args ) |
|
36 |
local who |
|
37 |
if args[1] then |
|
38 |
who = args[1] |
|
39 |
else |
|
40 |
who = main.full_jid () |
|
41 |
end |
|
42 |
local time = os.time () |
|
43 |
ping.send ( lm.connection.bless ( main.connection () ), who, |
|
44 |
function () |
|
45 |
main.print_info ( ('Pong: %d seconds'):format ( os.time () - time ) ) |
|
46 |
end, |
|
47 |
function ( mesg ) |
|
48 |
main.print_info ( 'Ping failed: ' .. mesg ) |
|
49 |
end ) |
|
50 |
end, true, 'jid' ) |
|
51 |
|
|
52 |
local ping_handler_registered = false |
|
53 |
|
|
54 |
hooks_d['hook-post-connect'].ping = |
|
55 |
function ( args ) |
|
56 |
lm.connection.bless( main.connection () ):handler ( ping_incoming_iq_handler, 'iq', 'normal' ) |
|
57 |
ping_handler_registered = true |
|
58 |
hooks_d['hook-post-connect'].ping = nil |
|
59 |
hooks_d['hook-quit'].ping = |
|
60 |
function ( args ) |
|
61 |
if ping_handler_registered then |
|
62 |
lm.connection.bless( main.connection () ):handler ( ping_incoming_iq_handler, 'iq' ) |
|
63 |
end |
|
64 |
end |
|
65 |
end |
|
66 |
|
|
67 |
main.add_feature ( 'urn:xmpp:ping' ) |
|
68 |
|
|
69 |
-- vim: se ts=4: -- |