examples/attention.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
child 99 ed4676536ed9
--- a/examples/attention.lua	Sat Mar 28 19:43:12 2009 +0200
+++ b/examples/attention.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -1,48 +1,50 @@
 
--- ATTENTION (XEP-0224)
+local lm        = require 'lm'
+local attention = require 'lm.attention'
 
--- library
-
-local lm = require 'lm'
-
---
+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 )
+	end )
 
-local O = {
-	handler =
-		function ( mesg )
-		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."
 
-local F = { }
+local attention_handler = lm.message_handler.new ( attention.message_handler )
+local attention_handler_registered = false
 
-function F.send ( conn, to, message )
-	local body = nil
-	if message then
-		body = { message }
+hooks_d['hook-post-connect'].attention =
+	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
+			end
 	end
-	conn:send (
-		lm.message.create { mtype = 'message-headline', to = to,
-			attention = { xmlns = 'urn:xmpp:attention:0' },
-			body = body,
-		} )
-end
 
-function F.handler ( handler )
-	O.handler = handler
-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
-	return false
-end
-
-return F
+main.add_feature ( 'urn:xmpp:attention:0' )
 
 -- vim: se ts=4: --