examples/oob.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 27 Mar 2009 01:46:53 +0200
changeset 64 bf7521ed96eb
parent 48 d31ae73038f7
child 66 542f61e113cb
permissions -rw-r--r--
Rewrite of ibb in object style


-- OUT OF BAND DATA (XEP-0066)

-- library

require 'lm'
local iq = require 'iq'

-- public

oob = {
	handler =
		function ( from, url, desc, success, fail )
			fail ()
		end,
}

function oob.send ( conn, to, url, success, fail, desc )
	if desc then
		desc = { desc }
	end
	iq.send ( conn, to, 'set',
		{
			query = { xmlns = 'jabber:iq:oob',
				url  = { url },
				desc = desc,
			},
		}, success, fail )
end

-- private

local 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 from = mess:attribute ( 'from' )
				local url  = query:child( 'url' ):value ()
				local desc = query:child( 'desc' )
				if desc then
					desc = desc:value ()
				end
				oob.handler ( from, url, desc,
					function ()
						conn:send ( lm.message.create { mtype = 'iq-result', to = from, id = mess:attribute ( 'id' ) } )
					end,
					function () -- XXX distinguish download error and reject
						conn:send (
							lm.message.create { mype = 'iq-error', to = from, id = mess:attribute ( 'id' ),
								-- XXX must we include query here?
								error = { code = '406', type = 'modify',
									['not-acceptable'] = { xmlns = 'urn:ietf:params:xml:ns:xmpp-stanzas' },
								},
							} )
					end )
				return true
			end
		end
		return false
	end )
local 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 from = mess:attribute ( 'from' )
			local url  = x:child( 'url' ):value ()
			local desc = x:child( 'desc' )
			if desc then
				desc = desc:value ()
			end
			oob.handler ( from, url, desc,
				function ()
				end,
				function ()
				end )
		end
		return false
	end )

-- mcabber

oob.handler =
	function ( from, url, desc, success, fail )
		if desc then
			main.print_info ( from, 'Buddy wants you to download link: ' .. url .. ' (' .. desc .. ')' )
		else
			main.print_info ( from, 'Buddy wants you to download link: ' .. url )
		end
		success ()
	end

main.command ( 'oob',
	function ( args )
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		-- here we can run something external to put file on server and obtain link to it
		oob.send ( lm.connection.bless ( main.connection () ), who, args[1],
			function ()
				main.print_info ( who, 'OOB link accepted' )
			end,
			function ( mesg )
				main.print_info ( who, 'OOB link refused: ' .. mesg )
			end, args[2] )
	end, true )

local oob_handler_registered = false

hooks_d['hook-post-connect'].oob =
	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'].oob = nil
		hooks_d['hook-quit'].oob =
			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: --