examples/oob.lua
changeset 66 542f61e113cb
parent 64 bf7521ed96eb
child 68 742878c74b8e
--- a/examples/oob.lua	Fri Mar 27 09:48:17 2009 +0200
+++ b/examples/oob.lua	Fri Mar 27 12:06:19 2009 +0200
@@ -3,19 +3,21 @@
 
 -- library
 
-require 'lm'
+local lm = require 'lm'
 local iq = require 'iq'
 
--- public
+--
 
-oob = {
+local O = {
 	handler =
 		function ( from, url, desc, success, fail )
 			fail ()
 		end,
 }
 
-function oob.send ( conn, to, url, success, fail, desc )
+local F = { }
+
+function F.send ( conn, to, url, success, fail, desc )
 	if desc then
 		desc = { desc }
 	end
@@ -28,109 +30,58 @@
 		}, success, fail )
 end
 
--- private
+function F.handler ( handler )
+	O.handler = handler
+end
 
-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
+function F.iq_handler ( 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  = x:child( 'url' ):value ()
-			local desc = x:child( 'desc' )
+			local url  = query:child( 'url' ):value ()
+			local desc = query:child( 'desc' )
 			if desc then
 				desc = desc:value ()
 			end
-			oob.handler ( from, url, desc,
+			O.handler ( from, url, desc,
 				function ()
+					conn:send ( lm.message.create { mtype = 'iq-result', to = from, id = mess:attribute ( 'id' ) } )
 				end,
-				function ()
+				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 )
-		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 ()
+			return true
 		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
+	end
+	return false
+end
 
-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
+function F.message_handler ( 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
+		O.handler ( from, url, desc,
+			function ()
+			end,
+			function ()
+			end )
 	end
+	return false
+end
 
-main.add_feature ( 'jabber:iq:oob' )
-main.add_feature ( 'jabber:x:oob' )
+return F
 
 -- vim: se ts=4: --