examples/attention.lua
changeset 66 542f61e113cb
parent 50 12d8dd774fcc
child 68 742878c74b8e
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
     1 
     2 -- ATTENTION (XEP-0224)
     2 -- ATTENTION (XEP-0224)
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 local lm = require 'lm'
     7 
     7 
     8 -- public
     8 --
     9 
     9 
    10 attention = {
    10 local O = {
    11 	handler =
    11 	handler =
    12 		function ( mesg )
    12 		function ( mesg )
    13 		end,
    13 		end,
    14 }
    14 }
    15 
    15 
    16 function attention.send ( conn, to, message )
    16 local F = { }
       
    17 
       
    18 function F.send ( conn, to, message )
    17 	local body = nil
    19 	local body = nil
    18 	if message then
    20 	if message then
    19 		body = { message }
    21 		body = { message }
    20 	end
    22 	end
    21 	conn:send (
    23 	conn:send (
    23 			attention = { xmlns = 'urn:xmpp:attention:0' },
    25 			attention = { xmlns = 'urn:xmpp:attention:0' },
    24 			body = body,
    26 			body = body,
    25 		} )
    27 		} )
    26 end
    28 end
    27 
    29 
    28 -- private
    30 function F.handler ( handler )
       
    31 	O.handler = handler
       
    32 end
    29 
    33 
    30 local attention_incoming_message_handler = lm.message_handler.new (
    34 function F.message_handler ( conn, mess )
    31 	function ( conn, mess )
    35 	local a = mess:child ( 'attention' )
    32 		local a = mess:child ( 'attention' )
    36 	if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
    33 		if a and a:attribute ( 'xmlns' ) == 'urn:xmpp:attention:0' then
    37 		local body = mess:child ( 'body' )
    34 			local body = mess:child ( 'body' )
    38 		if body then
    35 			if body then
    39 			body = body:value ()
    36 				body = body:value ()
       
    37 			end
       
    38 			attention.handler ( body )
       
    39 		end
    40 		end
    40 		return false
    41 		O.handler ( body )
    41 	end )
    42 	end
       
    43 	return false
       
    44 end
    42 
    45 
    43 -- mcabber
    46 return F
    44 
       
    45 attention.handler =
       
    46 	function ( mesg )
       
    47 		local times = 0
       
    48 		main.timer ( 1,
       
    49 			function ()
       
    50 				if times < 6 then
       
    51 					main.beep ()
       
    52 					times = times + 1
       
    53 					return true
       
    54 				end
       
    55 				return false
       
    56 			end )
       
    57 	end
       
    58 
       
    59 main.command ( 'attention',
       
    60 	function ( args )
       
    61 		local who
       
    62 		if args.t then
       
    63 			who = args.t
       
    64 		else
       
    65 			who = main.full_jid ()
       
    66 		end
       
    67 		attention.send ( lm.connection.bless ( main.connection () ), who, args[1] )
       
    68 	end, true, 'jid' )
       
    69 
       
    70 commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
       
    71 
       
    72 local attention_handler_registered = false
       
    73 
       
    74 hooks_d['hook-post-connect'].attention =
       
    75 	function ( args )
       
    76 		lm.connection.bless( main.connection () ):handler ( attention_incoming_message_handler, 'message', 'normal' )
       
    77 		attention_handler_registered = true
       
    78 		hooks_d['hook-post-connect'].attention = nil
       
    79 		hooks_d['hook-quit'].attention =
       
    80 			function ( args )
       
    81 				if attention_handler_registered then
       
    82 					lm.connection.bless( main.connection () ):handler ( attention_incoming_message_handler, 'message' )
       
    83 				end
       
    84 			end
       
    85 	end
       
    86 
       
    87 main.add_feature ( 'urn:xmpp:attention:0' )
       
    88 
    47 
    89 -- vim: se ts=4: --
    48 -- vim: se ts=4: --