examples/attention.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 111 5bcdb71ef2f2
--- a/examples/attention.lua	Wed Mar 31 00:28:04 2010 +0300
+++ b/examples/attention.lua	Wed Mar 31 01:25:19 2010 +0300
@@ -3,8 +3,11 @@
 local attention = require 'lm.attention'
 
 attention.handler (
-	function ( mesg )
+	function ( mesg, from )
 		local times = 0
+		if from then
+			main.print_info ( from, "Buddy wants your attention!" )
+		end
 		main.timer ( 1,
 			function ()
 				if times < 6 then
@@ -18,13 +21,18 @@
 
 main.command ( 'attention',
 	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
 		else
 			who = main.full_jid ()
 		end
-		attention.send ( lm.connection.bless ( main.connection () ), who, args[1] )
+		attention.send ( lm.connection.bless ( connection ), who, args[1] )
 	end, true, 'jid' )
 
 commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
@@ -32,19 +40,35 @@
 local attention_handler = lm.message_handler.new ( attention.message_handler )
 local attention_handler_registered = false
 
-hooks_d['hook-post-connect'].attention =
+local attention_pc_handler =
+	function ( args )
+		local connection = main.connection ()
+		if connection then
+			lm.connection.bless(connection):handler ( attention_handler, 'message', 'normal' )
+			attention_handler_registered = true
+		end
+	end
+main.hook ( 'hook-post-connect', attention_pc_handler )
+main.hook ( 'hook-pre-disconnect',
 	function ( args )
-		lm.connection.bless( main.connection () ):handler ( attention_handler, 'message', 'normal' )
-		attention_handler_registered = true
-		hooks_d['hook-post-connect'].attention = nil
-		hooks_d['hook-quit'].attention =
-			function ( args )
-				if attention_handler_registered then
-					lm.connection.bless( main.connection () ):handler ( attention_handler, 'message' )
-				end
+		if attention_handler_registered then
+			local connection = main.connection ()
+			if connection then
+				lm.connection.bless(connection):handler ( attention_handler, 'message' )
 			end
-	end
+			attention_handler_registered = false
+		end
+	end )
 
-main.add_feature ( 'urn:xmpp:attention:0' )
+-- register handler, if we are already connected
+main.hook ( 'hook-lua-start',
+	function ( args )
+		main.add_feature ( 'urn:xmpp:attention:0' )
+		attention_pc_handler ()
+	end )
+main.hook ('hook-lua-quit',
+	function ( args )
+		main.del_feature ( 'urn:xmpp:attention:0' )
+	end )
 
--- vim: se ts=4: --
+-- vim: se ts=4 sw=4: --