examples/xep0066.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Mar 2009 05:04:46 +0200
changeset 43 7c22b1f2c6e5
parent 37 b438d630a556
permissions -rw-r--r--
Iq separation


-- OUT OF BAND DATA

-- we can do more - use external command to put file on server,
-- read it's output for link, and then send that link.
-- that will be much more convenient.
function oob_send_link ( conn, to, url, desc )
	if desc then
		desc = { desc }
	end
	conn:send (
		lm.message.create { mtype = 'iq-set', to = to,
			query = { xmlns = 'jabber:iq:oob',
				url = { url },
				desc = desc,
			},
		},
		function ( conn, mess )
			local mtype, smtype = mess:type ()
			if smtype == 'result' then
				main.print_info ( to, 'Url is successfully downloaded' )
			elseif smtype == 'error' then
				main.print_info ( to, 'Url is not accepted: ' .. mess:child( 'error' ):children():name () )
			else
				print ( 'Weird response to oob url: ' .. mess:xml () )
				return false
			end
			return true
		end )
end

oob_incoming_iq_handler = lm.message_handler.new (
	function ( conn, mess )
		local mtype, smtype = mess:type ()
		if smtype == 'set' then
			local query = mess:child ( 'query' )
			if query and query:attribute ( 'xmlns' ) == 'jabber:iq:oob' then
				local url  = query:child( 'url' ):value ()
				local desc = query:child( 'desc' )
				if desc then
					desc = ('(%s)'):format ( desc:value () )
				else
					desc = ''
				end
				main.print_info ( mess:attribute ( 'from' ), ('Buddy wants you to download url: %s %s'):format ( url, desc ) )

				-- FIXME: in fact, we need to register file and after downloading (or rejecting) should send a notification to sender,
				-- but first we need to develop common file infrastructure (as with forms, though even that still needs redesign)
				-- however, to be nice (in hope, that others also would be nice to us :), we'll send reply right now.
				-- this saves memory and resources for pending handlers on the other side.
				conn:send ( lm.message.create { mtype = 'iq-result', to = mess:attribute ( 'from' ), id = mess:attribute ( 'id' ) } )

				return true
			end
		end
		return false
	end )
oob_incoming_message_handler = lm.message_handler.new (
	function ( conn, mess )
		local x = mess:child ( 'x' )
		if x and x:attribute ( 'xmlns' ) == 'jabber:x:oob' then
			local url = x:child( 'url' ):value ()
			local desc = x:child( 'desc' )
			if desc then
				desc = ('(%s)'):format ( desc:value () )
			else
				desc = ''
			end
			main.print_info ( mess:attribute ( 'from' ), ('Attached url: %s %s'):format ( url, desc ) )
		end
		return false
	end )

main.command ( 'oob',
	function ( args )
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		oob_send_link ( lm.connection.bless ( main.connection () ), who, args[1], args[2] )
	end, true )

oob_handler_registered = false

hooks_d['hook-post-connect'].xep0066 =
	function ( args )
		local conn = lm.connection.bless ( main.connection () )
		conn:handler ( oob_incoming_iq_handler, 'iq', 'normal' )
		conn:handler ( oob_incoming_message_handler, 'message', 'normal' )
		conn:handler ( oob_incoming_message_handler, 'presence', 'normal' )
		oob_handler_registered = true
		hooks_d['hook-post-connect'].xep0066 = nil
		hooks_d['hook-quit'].xep0066 =
			function ( args )
				if oob_handler_registered then
					local conn = lm.connection.bless ( main.connection () )
					conn:handler ( oob_incoming_iq_handler, 'iq' )
					conn:handler ( oob_incoming_message_handler, 'message' )
					conn:handler ( oob_incoming_message_handler, 'presence' )
				end
			end
	end

main.add_feature ( 'jabber:iq:oob' )
main.add_feature ( 'jabber:x:oob' )

-- vim: se ts=4: --