OOB
authorMyhailo Danylenko <isbear@ukrpost.net>
Thu, 19 Mar 2009 10:15:23 +0200
changeset 37 b438d630a556
parent 36 b156d7342ec1
child 38 1f141d9a081a
OOB
examples/mcabberrc.lua
examples/xep0066.lua
--- a/examples/mcabberrc.lua	Tue Mar 17 18:38:52 2009 +0200
+++ b/examples/mcabberrc.lua	Thu Mar 19 10:15:23 2009 +0200
@@ -265,6 +265,10 @@
 
 dopath 'xep0060'
 
+-- OUT OF BAND DATA (XEP-0066)
+
+dopath 'xep0066'
+
 -- IN-BAND REGISTRATION (XEP-0077)
 
 dopath 'xep0077'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/xep0066.lua	Thu Mar 19 10:15:23 2009 +0200
@@ -0,0 +1,109 @@
+
+-- 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: --