examples/attention.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 111 5bcdb71ef2f2
equal deleted inserted replaced
98:59aeae623ac6 99:ed4676536ed9
     1 
     1 
     2 local lm        = require 'lm'
     2 local lm        = require 'lm'
     3 local attention = require 'lm.attention'
     3 local attention = require 'lm.attention'
     4 
     4 
     5 attention.handler (
     5 attention.handler (
     6 	function ( mesg )
     6 	function ( mesg, from )
     7 		local times = 0
     7 		local times = 0
       
     8 		if from then
       
     9 			main.print_info ( from, "Buddy wants your attention!" )
       
    10 		end
     8 		main.timer ( 1,
    11 		main.timer ( 1,
     9 			function ()
    12 			function ()
    10 				if times < 6 then
    13 				if times < 6 then
    11 					main.beep ()
    14 					main.beep ()
    12 					times = times + 1
    15 					times = times + 1
    16 			end )
    19 			end )
    17 	end )
    20 	end )
    18 
    21 
    19 main.command ( 'attention',
    22 main.command ( 'attention',
    20 	function ( args )
    23 	function ( args )
       
    24 		local connection = main.connection ()
       
    25 		if not connection then
       
    26 			print "You are not online!"
       
    27 			return
       
    28 		end
    21 		local who
    29 		local who
    22 		if args.t then
    30 		if args.t then
    23 			who = args.t
    31 			who = args.t
    24 		else
    32 		else
    25 			who = main.full_jid ()
    33 			who = main.full_jid ()
    26 		end
    34 		end
    27 		attention.send ( lm.connection.bless ( main.connection () ), who, args[1] )
    35 		attention.send ( lm.connection.bless ( connection ), who, args[1] )
    28 	end, true, 'jid' )
    36 	end, true, 'jid' )
    29 
    37 
    30 commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
    38 commands_help['attention'] = "[-t to] [message]\n\nTries to get buddy's attention."
    31 
    39 
    32 local attention_handler = lm.message_handler.new ( attention.message_handler )
    40 local attention_handler = lm.message_handler.new ( attention.message_handler )
    33 local attention_handler_registered = false
    41 local attention_handler_registered = false
    34 
    42 
    35 hooks_d['hook-post-connect'].attention =
    43 local attention_pc_handler =
    36 	function ( args )
    44 	function ( args )
    37 		lm.connection.bless( main.connection () ):handler ( attention_handler, 'message', 'normal' )
    45 		local connection = main.connection ()
    38 		attention_handler_registered = true
    46 		if connection then
    39 		hooks_d['hook-post-connect'].attention = nil
    47 			lm.connection.bless(connection):handler ( attention_handler, 'message', 'normal' )
    40 		hooks_d['hook-quit'].attention =
    48 			attention_handler_registered = true
    41 			function ( args )
    49 		end
    42 				if attention_handler_registered then
    50 	end
    43 					lm.connection.bless( main.connection () ):handler ( attention_handler, 'message' )
    51 main.hook ( 'hook-post-connect', attention_pc_handler )
    44 				end
    52 main.hook ( 'hook-pre-disconnect',
       
    53 	function ( args )
       
    54 		if attention_handler_registered then
       
    55 			local connection = main.connection ()
       
    56 			if connection then
       
    57 				lm.connection.bless(connection):handler ( attention_handler, 'message' )
    45 			end
    58 			end
    46 	end
    59 			attention_handler_registered = false
       
    60 		end
       
    61 	end )
    47 
    62 
    48 main.add_feature ( 'urn:xmpp:attention:0' )
    63 -- register handler, if we are already connected
       
    64 main.hook ( 'hook-lua-start',
       
    65 	function ( args )
       
    66 		main.add_feature ( 'urn:xmpp:attention:0' )
       
    67 		attention_pc_handler ()
       
    68 	end )
       
    69 main.hook ('hook-lua-quit',
       
    70 	function ( args )
       
    71 		main.del_feature ( 'urn:xmpp:attention:0' )
       
    72 	end )
    49 
    73 
    50 -- vim: se ts=4: --
    74 -- vim: se ts=4 sw=4: --