examples/attention.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
child 99 ed4676536ed9
equal deleted inserted replaced
67:d33ca5572e91 68:742878c74b8e
     1 
     1 
     2 -- ATTENTION (XEP-0224)
     2 local lm        = require 'lm'
       
     3 local attention = require 'lm.attention'
     3 
     4 
     4 -- library
     5 attention.handler (
       
     6 	function ( mesg )
       
     7 		local times = 0
       
     8 		main.timer ( 1,
       
     9 			function ()
       
    10 				if times < 6 then
       
    11 					main.beep ()
       
    12 					times = times + 1
       
    13 					return true
       
    14 				end
       
    15 				return false
       
    16 			end )
       
    17 	end )
     5 
    18 
     6 local lm = require 'lm'
    19 main.command ( 'attention',
       
    20 	function ( args )
       
    21 		local who
       
    22 		if args.t then
       
    23 			who = args.t
       
    24 		else
       
    25 			who = main.full_jid ()
       
    26 		end
       
    27 		attention.send ( lm.connection.bless ( main.connection () ), who, args[1] )
       
    28 	end, true, 'jid' )
     7 
    29 
     8 --
    30 commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
     9 
    31 
    10 local O = {
    32 local attention_handler = lm.message_handler.new ( attention.message_handler )
    11 	handler =
    33 local attention_handler_registered = false
    12 		function ( mesg )
       
    13 		end,
       
    14 }
       
    15 
    34 
    16 local F = { }
    35 hooks_d['hook-post-connect'].attention =
       
    36 	function ( args )
       
    37 		lm.connection.bless( main.connection () ):handler ( attention_handler, 'message', 'normal' )
       
    38 		attention_handler_registered = true
       
    39 		hooks_d['hook-post-connect'].attention = nil
       
    40 		hooks_d['hook-quit'].attention =
       
    41 			function ( args )
       
    42 				if attention_handler_registered then
       
    43 					lm.connection.bless( main.connection () ):handler ( attention_handler, 'message' )
       
    44 				end
       
    45 			end
       
    46 	end
    17 
    47 
    18 function F.send ( conn, to, message )
    48 main.add_feature ( 'urn:xmpp:attention:0' )
    19 	local body = nil
       
    20 	if message then
       
    21 		body = { message }
       
    22 	end
       
    23 	conn:send (
       
    24 		lm.message.create { mtype = 'message-headline', to = to,
       
    25 			attention = { xmlns = 'urn:xmpp:attention:0' },
       
    26 			body = body,
       
    27 		} )
       
    28 end
       
    29 
       
    30 function F.handler ( handler )
       
    31 	O.handler = handler
       
    32 end
       
    33 
       
    34 function F.message_handler ( conn, mess )
       
    35 	local a = mess:child ( 'attention' )
       
    36 	if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
       
    37 		local body = mess:child ( 'body' )
       
    38 		if body then
       
    39 			body = body:value ()
       
    40 		end
       
    41 		O.handler ( body )
       
    42 	end
       
    43 	return false
       
    44 end
       
    45 
       
    46 return F
       
    47 
    49 
    48 -- vim: se ts=4: --
    50 -- vim: se ts=4: --