Old ibb updates
authorMyhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Mar 2009 03:44:07 +0200
changeset 42 18d801679feb
parent 41 9e39fd8a20df
child 43 7c22b1f2c6e5
Old ibb updates
examples/xep0047.lua
--- a/examples/xep0047.lua	Sat Mar 21 03:42:49 2009 +0200
+++ b/examples/xep0047.lua	Sat Mar 21 03:44:07 2009 +0200
@@ -1,13 +1,7 @@
 
 receiving_files = {}
 ibb_block_size  = 4096
-current_sid_number = 0
-
--- FIXME: read from /dev/urandom?
-function gen_unique_sid ()
-	current_sid_number = current_sid_number + 1
-	return 'mc-' .. tostring ( current_sid_number )
-end
+local ibb_sid = 0
 
 ibb_incoming_iq_handler = lm.message_handler.new ( 
 	function ( conn, mess )
@@ -100,13 +94,17 @@
 		return false
 	end )
 
-function ibb_send_file ( conn, to, name )
+function ibb_send_file ( conn, to, name, id )
 	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 sid = id
+	if not sid then -- XXX: move out of here?
+		ibb_sid = ibb_sid + 1
+		sid = 'ibb_' .. ibb_sid
+	end
 	conn:send (
 		lm.message.create { to = to, mtype = 'iq-set',
 			open = { sid = sid, ['block-size'] = ibb_block_size, xmlns = 'http://jabber.org/protocol/ibb' }
@@ -117,7 +115,7 @@
 			else
 				main.print_info ( to, "Stream accepted, starting sequence" )
 				local buffer = ''
-				main.bgread ( string.format ( 'base64 -w 0 %q', name ),
+				main.bgread ( string.format ( 'base64 -w 0 %q', name ), -- FIXME: stream reader function? XXX: then we need in-place base64 encoder.
 					function ( data )
 						if data then
 							buffer = buffer .. data
@@ -126,10 +124,11 @@
 							local seq = 0
 							local msgbuf = buffer:sub ( 1, ibb_block_size )
 							buffer = buffer:sub ( ibb_block_size + 1 )
-							local function handler ( conn, message )
-								if message:child ( 'error' ) then
+							local function handler ( conn, mess )
+								local mtype, smtype = mess:type ()
+								if smtype == 'error' then
 									main.print_info ( to, "Stream error, transfer ceased at seq = " .. seq .. ": " .. message:child( 'error' ):children():name () )
-								else
+								elseif smtype == 'result' then
 									main.print_info ( to, " - acquired seq = " .. seq )
 									seq = seq + 1
 									if buffer:len () == 0 then
@@ -137,11 +136,15 @@
 											lm.message.create { to = to, mtype = 'iq-set',
 												close = { sid = sid, xmlns = 'http://jabber.org/protocol/ibb' }
 											},
-											function ( conn, message )
-												if message:child ( 'error' ) then
-													main.print_info ( to, "Error at closing stream: " .. message:child( 'error' ):children():name () )
+											function ( conn, mess )
+												local mtype, smtype = mess:type ()
+												if smtype == 'error' then
+													main.print_info ( to, "Error at closing stream: " .. mess:child( 'error' ):children():name () )
+												elseif smtype == 'result' then
+													main.print_info ( to, "File successfully transferred" )
 												else
-													main.print_info ( to, "File successfully transferred" )
+													print ( 'Weird response for close of ibb stream: ' .. mess:xml () )
+													return false
 												end
 												return true
 											end )
@@ -156,6 +159,9 @@
 											},
 											handler )
 									end
+								else
+									print ( 'Weird response for ibb block: ' .. mess:xml () )
+									return false
 								end
 								return true
 							end