examples/remote.lua
changeset 67 d33ca5572e91
parent 66 542f61e113cb
child 68 742878c74b8e
--- a/examples/remote.lua	Fri Mar 27 12:06:19 2009 +0200
+++ b/examples/remote.lua	Sat Mar 28 19:43:12 2009 +0200
@@ -3,20 +3,19 @@
 
 -- library
 
-local lm    = require 'lm'
-local iq    = require 'iq'
-require 'x_data'
-local disco = require 'disco'
+local iq     = require 'iq'
+local x_data = require 'x_data'
+local disco  = require 'disco'
 
 -- public
 
-remote = { }
+local F = { }
 
-function remote.list ( conn, to, success, fail )
+function F.list ( conn, to, success, fail )
 	disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
 end
 
-function remote.command ( conn, to, command, success, fail )
+function F.command ( conn, to, command, success, fail )
 	iq.send ( conn, to, 'set',
 		{
 			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
@@ -33,26 +32,23 @@
 						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 } )
+										command = form:format ( { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid }, 'submit' ),
 									},
 									function ( mess )
 										local c = mess:child ( 'command' )
 										if c and c:attribute ( 'status' ) == 'completed' then
 											success ()
 										else
-											fail ( mess:xml () ) -- XXX more forms?
+											fail ( mess:xml () ) -- XXX more forms? results?
 										end
 									end, fail )
 							end,
 							function ( form, success, fail )
 								iq.send ( conn, to, 'set',
 									{
-										command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
-											x = { xmlns = 'jabber:x:data', type = 'cancel' }, -- FIXME
-										},
+										command = form:format ( { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid }, 'cancel' ),
 									}, success, fail )
 							end )
 					else
@@ -63,73 +59,6 @@
 		end, fail )
 end
 
--- mcabber
-
-main.command ( 'remote',
-	function ( args )
-		local who
-		if args.t then
-			who = args.t
-		else
-			who = main.full_jid ()
-		end
-		local action = args[1]
-		local conn   = lm.connection.bless ( main.connection () )
-		if action then
-			remote.command ( conn, who, 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 ) )
-				end )
-		else
-			remote.list ( conn, who,
-				function ( items )
-					local text = ''
-					for index, item in ipairs ( items ) do
-						text = text .. '\n - ' .. item.node
-					end
-					if text ~= '' then
-						main.print_info ( who, 'Available commands:' .. text )
-					else
-						main.print_info ( who, 'No commands available.' )
-					end
-				end,
-				function ( mesg )
-					main.print_info ( who, ("Remote commands list for %s failed: %s"):format ( who, mesg ) )
-				end )
-		end
-	end, true, 'jid' )
-
-commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
+return F
 
 -- vim: se ts=4: --