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