examples/oob.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 100 521c27baa387
--- a/examples/oob.lua	Wed Mar 31 00:28:04 2010 +0300
+++ b/examples/oob.lua	Wed Mar 31 01:25:19 2010 +0300
@@ -14,6 +14,11 @@
 
 main.command ( 'oob',
 	function ( args )
+		local connection = main.connection ()
+		if not connection then
+			print "You are not online!"
+			return
+		end
 		local who
 		if args.t then
 			who = args.t
@@ -21,7 +26,7 @@
 			who = main.full_jid ()
 		end
 		-- here we can run something external to put file on server and obtain link to it
-		oob.send ( lm.connection.bless ( main.connection () ), who, args[1],
+		oob.send ( lm.connection.bless ( connection ), who, args[1],
 			function ()
 				main.print_info ( who, 'OOB link accepted' )
 			end,
@@ -30,30 +35,47 @@
 			end, args[2] )
 	end, true )
 
-local oob_iq_handler = lm.message_handler.new ( oob.iq_handler )
+local oob_iq_handler      = lm.message_handler.new ( oob.iq_handler )
 local oob_message_handler = lm.message_handler.new ( oob.message_handler )
 local oob_handler_registered = false
 
-hooks_d['hook-post-connect'].oob =
+local oob_pc_handler =
 	function ( args )
-		local conn = lm.connection.bless ( main.connection () )
-		conn:handler ( oob_iq_handler, 'iq', 'normal' )
-		conn:handler ( oob_message_handler, 'message', 'normal' )
-		conn:handler ( oob_message_handler, 'presence', 'normal' )
-		oob_handler_registered = true
-		hooks_d['hook-post-connect'].oob = nil
-		hooks_d['hook-quit'].oob =
-			function ( args )
-				if oob_handler_registered then
-					local conn = lm.connection.bless ( main.connection () )
-					conn:handler ( oob_iq_handler, 'iq' )
-					conn:handler ( oob_message_handler, 'message' )
-					conn:handler ( oob_message_handler, 'presence' )
-				end
+		local connection = main.connection ()
+		if connection then
+			local conn = lm.connection.bless ( connection )
+			conn:handler ( oob_iq_handler, 'iq', 'normal' )
+			conn:handler ( oob_message_handler, 'message', 'normal' )
+			conn:handler ( oob_message_handler, 'presence', 'normal' )
+			oob_handler_registered = true
+		end
+	end
+main.hook ( 'hook-post-connect', oob_pc_handler )
+main.hook ( 'hook-pre-disconnect',
+	function ( args )
+		if oob_handler_registered then
+			local connection = main.connection ()
+			if connection then
+				local conn = lm.connection.bless ( connection )
+				conn:handler ( oob_iq_handler, 'iq' )
+				conn:handler ( oob_message_handler, 'message' )
+				conn:handler ( oob_message_handler, 'presence' )
 			end
+			oob_handler_registered = false
+		end
 	end
 
-main.add_feature ( 'jabber:iq:oob' )
-main.add_feature ( 'jabber:x:oob' )
 
--- vim: se ts=4: --
+main.hook ( 'hook-lua-start',
+	function ( args )
+		main.add_feature ( 'jabber:iq:oob' )
+		main.add_feature ( 'jabber:x:oob' )
+		oob_pc_handler ()
+	end )
+main.hook ( 'hook-lua-quit',
+	function ( args )
+		main.del_feature ( 'jabber:iq:oob' )
+		main.del_feature ( 'jabber:x:oob' )
+	end )
+
+-- vim: se ts=4 sw=4: --