examples/ping.lua
changeset 66 542f61e113cb
parent 64 bf7521ed96eb
child 68 742878c74b8e
--- a/examples/ping.lua	Fri Mar 27 09:48:17 2009 +0200
+++ b/examples/ping.lua	Fri Mar 27 12:06:19 2009 +0200
@@ -3,72 +3,32 @@
 
 -- library
 
-require 'lm'
+local lm = require 'lm'
 local iq = require 'iq'
 
--- public
+--
 
-ping = { }
+local F = { }
 
-function ping.send ( conn, to, success, fail )
+function F.send ( conn, to, success, fail )
 	iq.send ( conn, to, 'get',
 		{
 			ping = { xmlns = 'urn:xmpp:ping' },
 		}, success, fail )
 end
 
--- private
-
-local ping_incoming_iq_handler = lm.message_handler.new (
-	function ( 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
-		end
-		return false
-	end )
-
--- mcabber
-
-main.command ( 'ping',
-	function ( args )
-		local who
-		if args[1] then
-			who = args[1]
-		else
-			who = main.full_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 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' )
-
---[[
-local ping_handler_registered = false
+	end
+	return false
+end
 
-hooks_d['hook-post-connect'].ping =
-	function ( args )
-		lm.connection.bless( main.connection () ):handler ( ping_incoming_iq_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_incoming_iq_handler, 'iq' )
-				end
-			end
-	end
-
-main.add_feature ( 'urn:xmpp:ping' )
---]]
+return F
 
 -- vim: se ts=4: --