examples/ping.lua
changeset 66 542f61e113cb
parent 64 bf7521ed96eb
child 68 742878c74b8e
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
     1 
     2 -- XMPP PING (XEP-0199)
     2 -- XMPP PING (XEP-0199)
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 local lm = require 'lm'
     7 local iq = require 'iq'
     7 local iq = require 'iq'
     8 
     8 
     9 -- public
     9 --
    10 
    10 
    11 ping = { }
    11 local F = { }
    12 
    12 
    13 function ping.send ( conn, to, success, fail )
    13 function F.send ( conn, to, success, fail )
    14 	iq.send ( conn, to, 'get',
    14 	iq.send ( conn, to, 'get',
    15 		{
    15 		{
    16 			ping = { xmlns = 'urn:xmpp:ping' },
    16 			ping = { xmlns = 'urn:xmpp:ping' },
    17 		}, success, fail )
    17 		}, success, fail )
    18 end
    18 end
    19 
    19 
    20 -- private
    20 function F.iq_handler ( 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
    21 
    31 
    22 local ping_incoming_iq_handler = lm.message_handler.new (
    32 return F
    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 ()
       
    48 				main.print_info ( who, ('Pong: %d seconds'):format ( os.time () - time ) )
       
    49 			end,
       
    50 			function ( mesg )
       
    51 				main.print_info ( who, 'Ping failed: ' .. mesg )
       
    52 			end )
       
    53 	end, true, 'jid' )
       
    54 
       
    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' )
       
    72 --]]
       
    73 
    33 
    74 -- vim: se ts=4: --
    34 -- vim: se ts=4: --