IBB uses user connection, optimization
authorMyhailo Danylenko <isbear@ukrpost.net>
Thu, 19 Mar 2009 10:16:20 +0200
changeset 38 1f141d9a081a
parent 37 b438d630a556
child 39 0d43f33243cc
IBB uses user connection, optimization
examples/xep0047.lua
--- a/examples/xep0047.lua	Thu Mar 19 10:15:23 2009 +0200
+++ b/examples/xep0047.lua	Thu Mar 19 10:16:20 2009 +0200
@@ -13,8 +13,10 @@
 	function ( conn, mess )
 		local id   = mess:attribute ( 'id' )
 		local from = mess:attribute ( 'from' )
-		if mess:child ( 'open' ) and mess:child( 'open' ):attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
-			local sid  = mess:child( 'open' ):attribute ( 'sid' )
+
+		local open = mess:child ( 'open' )
+		if open and open:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
+			local sid  = open:attribute ( 'sid' )
 			if not receiving_files[sid] then
 				local buffer = ''
 				receiving_files[sid] = { from = from, status = 'pending' }
@@ -44,11 +46,15 @@
 						}
 					} )
 			end
-		elseif mess:child ( 'data' ) and mess:child( 'data' ):attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
-			local sid  = mess:child( 'data' ):attribute ( 'sid' )
-			local seq  = mess:child( 'data' ):attribute ( 'seq' )
+			return true
+		end
+
+		local qdata = mess:child ( 'data' )
+		if qdata and qdata:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
+			local sid  = qdata:attribute ( 'sid' )
+			local seq  = qdata:attribute ( 'seq' )
 			if receiving_files[sid] and from == receiving_files[sid].from and not receiving_files[sid][tonumber(seq)+1] then
-				local data = mess:child( 'data' ):value ()
+				local data = qdata:value ()
 				main.print_info ( from, string.format ( " - stream part %s, id %s, %d bytes", seq, sid, data:len() ) )
 				conn:send ( lm.message.create { to = from, mtype = 'iq-result', id = id } )
 				receiving_files[sid][tonumber(seq)+1] = data
@@ -61,8 +67,12 @@
 						}
 					} )
 			end
-		elseif mess:child ( 'close' ) and mess:child( 'close' ):attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
-			local sid  = mess:child( 'close' ):attribute ( 'sid' )
+			return true
+		end
+
+		local close = mess:child ( 'close' )
+		if close and close:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/ibb' then
+			local sid  = close:attribute ( 'sid' )
 			if receiving_files[sid] and from == receiving_files[sid].from then
 				main.print_info ( from, "Done with stream id " .. sid )
 				conn:send ( lm.message.create { to = from, mtype = 'iq-result', id = id } )
@@ -84,20 +94,19 @@
 						}
 					} )
 			end
-		else
-			return false
+			return true
 		end
-		return true
+
+		return false
 	end )
 
-function send_file ( to, name )
+function ibb_send_file ( conn, to, name )
 	if not to then
 		to = main.full_jid ()
 	elseif not to:match ( "/" ) then
 		to = main.full_jid ( to )
 	end
 	local sid = gen_unique_sid ()
-	local conn = lm.connection.bless ( main.connection () )
 	conn:send (
 		lm.message.create { to = to, mtype = 'iq-set',
 			open = { sid = sid, ['block-size'] = ibb_block_size, xmlns = 'http://jabber.org/protocol/ibb' }
@@ -175,7 +184,7 @@
 			else
 				who = main.full_jid ()
 			end
-			send_file ( who, args[2] )
+			ibb_send_file ( lm.connection.bless ( main.connection () ), who, args[2] )
 		elseif action == 'accept' then
 			local id = args[2]
 			if receiving_files[id] then
@@ -207,7 +216,6 @@
 hooks_d['hook-post-connect'].xep0047 =
 	function ( args )
 		lm.connection.bless( main.connection () ):handler ( ibb_incoming_iq_handler, 'iq', 'normal' )
-		main.add_feature ( 'http://jabber.org/protocol/ibb' )
 		ibb_handler_registered = true
 		hooks_d['hook-post-connect'].xep0047 = nil
 		hooks_d['hook-quit'].xep0047 =
@@ -218,4 +226,6 @@
 			end
 	end
 
+main.add_feature ( 'http://jabber.org/protocol/ibb' )
+
 -- vim: se ts=4: --