examples/xep0066.lua
changeset 66 542f61e113cb
parent 65 f1be8dbb209c
child 67 d33ca5572e91
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
       
     2 -- OUT OF BAND DATA
       
     3 
       
     4 -- we can do more - use external command to put file on server,
       
     5 -- read it's output for link, and then send that link.
       
     6 -- that will be much more convenient.
       
     7 function oob_send_link ( conn, to, url, desc )
       
     8 	if desc then
       
     9 		desc = { desc }
       
    10 	end
       
    11 	conn:send (
       
    12 		lm.message.create { mtype = 'iq-set', to = to,
       
    13 			query = { xmlns = 'jabber:iq:oob',
       
    14 				url = { url },
       
    15 				desc = desc,
       
    16 			},
       
    17 		},
       
    18 		function ( conn, mess )
       
    19 			local mtype, smtype = mess:type ()
       
    20 			if smtype == 'result' then
       
    21 				main.print_info ( to, 'Url is successfully downloaded' )
       
    22 			elseif smtype == 'error' then
       
    23 				main.print_info ( to, 'Url is not accepted: ' .. mess:child( 'error' ):children():name () )
       
    24 			else
       
    25 				print ( 'Weird response to oob url: ' .. mess:xml () )
       
    26 				return false
       
    27 			end
       
    28 			return true
       
    29 		end )
       
    30 end
       
    31 
       
    32 oob_incoming_iq_handler = lm.message_handler.new (
       
    33 	function ( conn, mess )
       
    34 		local mtype, smtype = mess:type ()
       
    35 		if smtype == 'set' then
       
    36 			local query = mess:child ( 'query' )
       
    37 			if query and query:attribute ( 'xmlns' ) == 'jabber:iq:oob' then
       
    38 				local url  = query:child( 'url' ):value ()
       
    39 				local desc = query:child( 'desc' )
       
    40 				if desc then
       
    41 					desc = ('(%s)'):format ( desc:value () )
       
    42 				else
       
    43 					desc = ''
       
    44 				end
       
    45 				main.print_info ( mess:attribute ( 'from' ), ('Buddy wants you to download url: %s %s'):format ( url, desc ) )
       
    46 
       
    47 				-- FIXME: in fact, we need to register file and after downloading (or rejecting) should send a notification to sender,
       
    48 				-- but first we need to develop common file infrastructure (as with forms, though even that still needs redesign)
       
    49 				-- however, to be nice (in hope, that others also would be nice to us :), we'll send reply right now.
       
    50 				-- this saves memory and resources for pending handlers on the other side.
       
    51 				conn:send ( lm.message.create { mtype = 'iq-result', to = mess:attribute ( 'from' ), id = mess:attribute ( 'id' ) } )
       
    52 
       
    53 				return true
       
    54 			end
       
    55 		end
       
    56 		return false
       
    57 	end )
       
    58 oob_incoming_message_handler = lm.message_handler.new (
       
    59 	function ( conn, mess )
       
    60 		local x = mess:child ( 'x' )
       
    61 		if x and x:attribute ( 'xmlns' ) == 'jabber:x:oob' then
       
    62 			local url = x:child( 'url' ):value ()
       
    63 			local desc = x:child( 'desc' )
       
    64 			if desc then
       
    65 				desc = ('(%s)'):format ( desc:value () )
       
    66 			else
       
    67 				desc = ''
       
    68 			end
       
    69 			main.print_info ( mess:attribute ( 'from' ), ('Attached url: %s %s'):format ( url, desc ) )
       
    70 		end
       
    71 		return false
       
    72 	end )
       
    73 
       
    74 main.command ( 'oob',
       
    75 	function ( args )
       
    76 		local who
       
    77 		if args.t then
       
    78 			who = args.t
       
    79 		else
       
    80 			who = main.full_jid ()
       
    81 		end
       
    82 		oob_send_link ( lm.connection.bless ( main.connection () ), who, args[1], args[2] )
       
    83 	end, true )
       
    84 
       
    85 oob_handler_registered = false
       
    86 
       
    87 hooks_d['hook-post-connect'].xep0066 =
       
    88 	function ( args )
       
    89 		local conn = lm.connection.bless ( main.connection () )
       
    90 		conn:handler ( oob_incoming_iq_handler, 'iq', 'normal' )
       
    91 		conn:handler ( oob_incoming_message_handler, 'message', 'normal' )
       
    92 		conn:handler ( oob_incoming_message_handler, 'presence', 'normal' )
       
    93 		oob_handler_registered = true
       
    94 		hooks_d['hook-post-connect'].xep0066 = nil
       
    95 		hooks_d['hook-quit'].xep0066 =
       
    96 			function ( args )
       
    97 				if oob_handler_registered then
       
    98 					local conn = lm.connection.bless ( main.connection () )
       
    99 					conn:handler ( oob_incoming_iq_handler, 'iq' )
       
   100 					conn:handler ( oob_incoming_message_handler, 'message' )
       
   101 					conn:handler ( oob_incoming_message_handler, 'presence' )
       
   102 				end
       
   103 			end
       
   104 	end
       
   105 
       
   106 main.add_feature ( 'jabber:iq:oob' )
       
   107 main.add_feature ( 'jabber:x:oob' )
       
   108 
       
   109 -- vim: se ts=4: --