OOB uses module interface
authorMyhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Mar 2009 16:47:56 +0200
changeset 48 d31ae73038f7
parent 47 aba4bbe32cba
child 49 95f3bf77c598
OOB uses module interface
examples/mcabberrc.lua
examples/oob.lua
--- a/examples/mcabberrc.lua	Sat Mar 21 15:43:35 2009 +0200
+++ b/examples/mcabberrc.lua	Sat Mar 21 16:47:56 2009 +0200
@@ -267,7 +267,7 @@
 
 -- OUT OF BAND DATA (XEP-0066)
 
-dopath 'xep0066'
+dopath 'oob'
 
 -- IN-BAND REGISTRATION (XEP-0077)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/oob.lua	Sat Mar 21 16:47:56 2009 +0200
@@ -0,0 +1,136 @@
+
+-- OUT OF BAND DATA (XEP-0066)
+
+-- library
+
+require 'lm'
+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: --