examples/oob.lua
changeset 99 ed4676536ed9
parent 68 742878c74b8e
child 100 521c27baa387
equal deleted inserted replaced
98:59aeae623ac6 99:ed4676536ed9
    12 		success ()
    12 		success ()
    13 	end )
    13 	end )
    14 
    14 
    15 main.command ( 'oob',
    15 main.command ( 'oob',
    16 	function ( args )
    16 	function ( args )
       
    17 		local connection = main.connection ()
       
    18 		if not connection then
       
    19 			print "You are not online!"
       
    20 			return
       
    21 		end
    17 		local who
    22 		local who
    18 		if args.t then
    23 		if args.t then
    19 			who = args.t
    24 			who = args.t
    20 		else
    25 		else
    21 			who = main.full_jid ()
    26 			who = main.full_jid ()
    22 		end
    27 		end
    23 		-- here we can run something external to put file on server and obtain link to it
    28 		-- here we can run something external to put file on server and obtain link to it
    24 		oob.send ( lm.connection.bless ( main.connection () ), who, args[1],
    29 		oob.send ( lm.connection.bless ( connection ), who, args[1],
    25 			function ()
    30 			function ()
    26 				main.print_info ( who, 'OOB link accepted' )
    31 				main.print_info ( who, 'OOB link accepted' )
    27 			end,
    32 			end,
    28 			function ( mesg )
    33 			function ( mesg )
    29 				main.print_info ( who, 'OOB link refused: ' .. mesg )
    34 				main.print_info ( who, 'OOB link refused: ' .. mesg )
    30 			end, args[2] )
    35 			end, args[2] )
    31 	end, true )
    36 	end, true )
    32 
    37 
    33 local oob_iq_handler = lm.message_handler.new ( oob.iq_handler )
    38 local oob_iq_handler      = lm.message_handler.new ( oob.iq_handler )
    34 local oob_message_handler = lm.message_handler.new ( oob.message_handler )
    39 local oob_message_handler = lm.message_handler.new ( oob.message_handler )
    35 local oob_handler_registered = false
    40 local oob_handler_registered = false
    36 
    41 
    37 hooks_d['hook-post-connect'].oob =
    42 local oob_pc_handler =
    38 	function ( args )
    43 	function ( args )
    39 		local conn = lm.connection.bless ( main.connection () )
    44 		local connection = main.connection ()
    40 		conn:handler ( oob_iq_handler, 'iq', 'normal' )
    45 		if connection then
    41 		conn:handler ( oob_message_handler, 'message', 'normal' )
    46 			local conn = lm.connection.bless ( connection )
    42 		conn:handler ( oob_message_handler, 'presence', 'normal' )
    47 			conn:handler ( oob_iq_handler, 'iq', 'normal' )
    43 		oob_handler_registered = true
    48 			conn:handler ( oob_message_handler, 'message', 'normal' )
    44 		hooks_d['hook-post-connect'].oob = nil
    49 			conn:handler ( oob_message_handler, 'presence', 'normal' )
    45 		hooks_d['hook-quit'].oob =
    50 			oob_handler_registered = true
    46 			function ( args )
    51 		end
    47 				if oob_handler_registered then
    52 	end
    48 					local conn = lm.connection.bless ( main.connection () )
    53 main.hook ( 'hook-post-connect', oob_pc_handler )
    49 					conn:handler ( oob_iq_handler, 'iq' )
    54 main.hook ( 'hook-pre-disconnect',
    50 					conn:handler ( oob_message_handler, 'message' )
    55 	function ( args )
    51 					conn:handler ( oob_message_handler, 'presence' )
    56 		if oob_handler_registered then
    52 				end
    57 			local connection = main.connection ()
       
    58 			if connection then
       
    59 				local conn = lm.connection.bless ( connection )
       
    60 				conn:handler ( oob_iq_handler, 'iq' )
       
    61 				conn:handler ( oob_message_handler, 'message' )
       
    62 				conn:handler ( oob_message_handler, 'presence' )
    53 			end
    63 			end
       
    64 			oob_handler_registered = false
       
    65 		end
    54 	end
    66 	end
    55 
    67 
    56 main.add_feature ( 'jabber:iq:oob' )
       
    57 main.add_feature ( 'jabber:x:oob' )
       
    58 
    68 
    59 -- vim: se ts=4: --
    69 main.hook ( 'hook-lua-start',
       
    70 	function ( args )
       
    71 		main.add_feature ( 'jabber:iq:oob' )
       
    72 		main.add_feature ( 'jabber:x:oob' )
       
    73 		oob_pc_handler ()
       
    74 	end )
       
    75 main.hook ( 'hook-lua-quit',
       
    76 	function ( args )
       
    77 		main.del_feature ( 'jabber:iq:oob' )
       
    78 		main.del_feature ( 'jabber:x:oob' )
       
    79 	end )
       
    80 
       
    81 -- vim: se ts=4 sw=4: --