examples/remote.lua
changeset 49 95f3bf77c598
parent 44 bd66956cd397
child 51 a95a3a73482c
--- a/examples/remote.lua	Sat Mar 21 16:47:56 2009 +0200
+++ b/examples/remote.lua	Sun Mar 22 04:14:36 2009 +0200
@@ -5,7 +5,7 @@
 
 require 'lm'
 require 'iq'
--- forms
+require 'x_data'
 require 'disco'
 
 -- public
@@ -24,42 +24,38 @@
 		function ( mess )
 			local c = mess:child ( 'command' )
 			if c then
-				local x = c:child ( 'x' )
-				if x then
-					local sid = c:attribute ( 'sessionid' )
-					local id  = parse_form ( x ) -- FIXME
-					forms[id].send =
-						function ( form )
-							iq.send ( conn, to, 'set',
-								{
-									command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
-										x = { xmlns = 'jabber:x:data', type = 'form',
-											field = form.val,
-										},
+				local status = c:attribute ( 'status' )
+				if status == 'completed' then
+					success ()
+				else
+					local x = c:child ( 'x' )
+					if x then
+						local sid = c:attribute ( 'sessionid' )
+						success ( x_data.parse ( x ),
+							function ( form, success, fail )
+								form.type = 'submit' -- XXX in standard there is 'form' :/
+								iq.send ( conn, to, 'set',
+									{
+										command = form.format ( form, { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid } )
 									},
-								},
-								function ( mess )
-									local c = mess:child ( 'command' )
-									if c and c:attribute ( 'status' ) == 'completed' then
-										success ()
-										main.print_info ( to, 'Now you can run /form del ' .. id .. ' to delete form from list' )
-										form.status = 'acquired'
-									else
-										print ( 'D: FIXME: nonsuccessful remote command: ' .. mess:xml () )
-									end
-								end,
-								function ( mesg )
-									main.print_info ( to, 'Got non-successful response to form: ' .. mesg )
-									form.status = 'rejected'
-								end )
-							form.status = 'sent'
-						end
-					forms[id].status = 'filling'
-					main.print_info ( to, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
+									function ( mess )
+										local c = mess:child ( 'command' )
+										if c and c:attribute ( 'status' ) == 'completed' then
+											success ()
+										else
+											fail ( mess:xml () ) -- XXX more forms?
+										end
+									end, fail )
+							end,
+							function ( form, success, fail )
+								success ()
+							end )
+					else
+						fail ( mess:xml () ) -- XXX
+					end
 				end
 			end
-		end,
-		fail )
+		end, fail )
 end
 
 -- mcabber
@@ -76,8 +72,36 @@
 		local conn   = lm.connection.bless ( main.connection () )
 		if action then
 			remote.command ( conn, who, action,
-				function ()
-					main.print_info ( who, ('Command %s executed'):format ( action ) )
+				function ( form, submit, reject )
+					if not form then
+						main.print_info ( who, ('Command %s completed'):format ( action ) )
+					else
+						local id = #forms + 1
+						forms[id] = {
+							form = form,
+							submit =
+								function ( form )
+									submit ( form,
+										function ()
+											main.print_info ( who, ('Command %s completed'):format ( action ) )
+										end,
+										function ( mesg )
+											main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
+										end )
+								end,
+							reject =
+								function ( form )
+									reject ( form,
+										function ()
+											main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
+										end,
+										function ( mesg )
+											main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) )
+										end )
+								end,
+						}
+						print ( 'You have new form ' .. id )
+					end
 				end,
 				function ( mesg )
 					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )