examples/attention.lua
changeset 66 542f61e113cb
parent 50 12d8dd774fcc
child 68 742878c74b8e
--- a/examples/attention.lua	Fri Mar 27 09:48:17 2009 +0200
+++ b/examples/attention.lua	Fri Mar 27 12:06:19 2009 +0200
@@ -3,17 +3,19 @@
 
 -- library
 
-require 'lm'
+local lm = require 'lm'
 
--- public
+--
 
-attention = {
+local O = {
 	handler =
 		function ( mesg )
 		end,
 }
 
-function attention.send ( conn, to, message )
+local F = { }
+
+function F.send ( conn, to, message )
 	local body = nil
 	if message then
 		body = { message }
@@ -25,65 +27,22 @@
 		} )
 end
 
--- private
-
-local attention_incoming_message_handler = lm.message_handler.new (
-	function ( conn, mess )
-		local a = mess:child ( 'attention' )
-		if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
-			local body = mess:child ( 'body' )
-			if body then
-				body = body:value ()
-			end
-			attention.handler ( body )
-		end
-		return false
-	end )
+function F.handler ( handler )
+	O.handler = handler
+end
 
--- mcabber
-
-attention.handler =
-	function ( mesg )
-		local times = 0
-		main.timer ( 1,
-			function ()
-				if times < 6 then
-					main.beep ()
-					times = times + 1
-					return true
-				end
-				return false
-			end )
+function F.message_handler ( conn, mess )
+	local a = mess:child ( 'attention' )
+	if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
+		local body = mess:child ( 'body' )
+		if body then
+			body = body:value ()
+		end
+		O.handler ( body )
 	end
-
-main.command ( 'attention',
-	function ( args )
-		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] )
-	end, true, 'jid' )
-
-commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
+	return false
+end
 
-local attention_handler_registered = false
-
-hooks_d['hook-post-connect'].attention =
-	function ( args )
-		lm.connection.bless( main.connection () ):handler ( attention_incoming_message_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_incoming_message_handler, 'message' )
-				end
-			end
-	end
-
-main.add_feature ( 'urn:xmpp:attention:0' )
+return F
 
 -- vim: se ts=4: --