examples/ping.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
child 70 e43e386c8a33
--- a/examples/ping.lua	Sat Mar 28 19:43:12 2009 +0200
+++ b/examples/ping.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -1,34 +1,43 @@
 
--- XMPP PING (XEP-0199)
-
--- library
-
-local lm = require 'lm'
-local iq = require 'iq'
-
---
-
-local F = { }
+local lm   = require 'lm'
+local ping = require 'lm.ping'
 
-function F.send ( conn, to, success, fail )
-	iq.send ( conn, to, 'get',
-		{
-			ping = { xmlns = 'urn:xmpp:ping' },
-		}, success, fail )
-end
+main.command ( 'ping',
+	function ( args )
+		local who
+		if args[1] then
+			who = args[1]
+		else
+			who = main.full_jid ()
+		end
+		local time = os.time ()
+		ping.send ( lm.connection.bless ( main.connection () ), who,
+			function ()
+				main.print_info ( who, ('Pong: %d seconds'):format ( os.time () - time ) )
+			end,
+			function ( mesg )
+				main.print_info ( who, 'Ping failed: ' .. mesg )
+			end )
+	end, true, 'jid' )
 
-function F.iq_handler ( conn, mess )
-	local mtype, smtype = mess:type ()
-	if smtype == 'get' then
-		local p = mess:child ( 'ping' )
-		if p and p:attribute ( 'xmlns' ) == 'urn:xmpp:ping' then
-			conn:send ( lm.message.create { mtype = 'iq-result', to = mess:attribute ( 'from' ), id = mess:attribute ( 'id' ) } )
-			return true
-		end
+--[[
+local ping_handler = lm.message_handler.new ( ping.iq_handler )
+local ping_handler_registered = false
+
+hooks_d['hook-post-connect'].ping =
+	function ( args )
+		lm.connection.bless( main.connection () ):handler ( ping_handler, 'iq', 'normal' )
+		ping_handler_registered = true
+		hooks_d['hook-post-connect'].ping = nil
+		hooks_d['hook-quit'].ping =
+			function ( args )
+				if ping_handler_registered then
+					lm.connection.bless( main.connection () ):handler ( ping_handler, 'iq' )
+				end
+			end
 	end
-	return false
-end
 
-return F
+main.add_feature ( 'urn:xmpp:ping' )
+--]]
 
 -- vim: se ts=4: --