examples/remote.lua
changeset 68 742878c74b8e
parent 67 d33ca5572e91
child 99 ed4676536ed9
equal deleted inserted replaced
67:d33ca5572e91 68:742878c74b8e
     1 
     1 
     2 -- REMOTE CONTROLLING CLIENTS (XEP-0146)
     2 local lm     = require 'lm'
       
     3 local remote = require 'lm.remote'
     3 
     4 
     4 -- library
     5 main.command ( 'remote',
       
     6 	function ( args )
       
     7 		local who
       
     8 		if args.t then
       
     9 			who = args.t
       
    10 		else
       
    11 			who = main.full_jid ()
       
    12 		end
       
    13 		local action = args[1]
       
    14 		local conn   = lm.connection.bless ( main.connection () )
       
    15 		if action then
       
    16 			remote.command ( conn, who, action,
       
    17 				function ( form, submit, reject )
       
    18 					if not form then
       
    19 						main.print_info ( who, ('Command %s completed'):format ( action ) )
       
    20 					else
       
    21 						insert_form ( form, -- XXX
       
    22 							function ( form )
       
    23 								submit ( form,
       
    24 									function ()
       
    25 										main.print_info ( who, ('Command %s completed'):format ( action ) )
       
    26 									end,
       
    27 									function ( mesg )
       
    28 										main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
       
    29 									end )
       
    30 							end,
       
    31 							function ( form )
       
    32 								reject ( form,
       
    33 									function ()
       
    34 										main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
       
    35 									end,
       
    36 									function ( mesg )
       
    37 										main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) )
       
    38 									end )
       
    39 							end )
       
    40 					end
       
    41 				end,
       
    42 				function ( mesg )
       
    43 					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
       
    44 				end )
       
    45 		else
       
    46 			remote.list ( conn, who,
       
    47 				function ( items )
       
    48 					local text = ''
       
    49 					for index, item in ipairs ( items ) do
       
    50 						text = text .. '\n - ' .. item.node
       
    51 					end
       
    52 					if text ~= '' then
       
    53 						main.print_info ( who, 'Available commands:' .. text )
       
    54 					else
       
    55 						main.print_info ( who, 'No commands available.' )
       
    56 					end
       
    57 				end,
       
    58 				function ( mesg )
       
    59 					main.print_info ( who, ("Remote commands list for %s failed: %s"):format ( who, mesg ) )
       
    60 				end )
       
    61 		end
       
    62 	end, true, 'jid' )
     5 
    63 
     6 local iq     = require 'iq'
    64 commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
     7 local x_data = require 'x_data'
       
     8 local disco  = require 'disco'
       
     9 
       
    10 -- public
       
    11 
       
    12 local F = { }
       
    13 
       
    14 function F.list ( conn, to, success, fail )
       
    15 	disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
       
    16 end
       
    17 
       
    18 function F.command ( conn, to, command, success, fail )
       
    19 	iq.send ( conn, to, 'set',
       
    20 		{
       
    21 			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
       
    22 		},
       
    23 		function ( mess )
       
    24 			local c = mess:child ( 'command' )
       
    25 			if c then
       
    26 				local status = c:attribute ( 'status' )
       
    27 				if status == 'completed' then
       
    28 					success ()
       
    29 				else
       
    30 					local x = c:child ( 'x' )
       
    31 					if x then
       
    32 						local sid = c:attribute ( 'sessionid' )
       
    33 						success ( x_data.parse ( x ),
       
    34 							function ( form, success, fail )
       
    35 								iq.send ( conn, to, 'set',
       
    36 									{
       
    37 										command = form:format ( { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid }, 'submit' ),
       
    38 									},
       
    39 									function ( mess )
       
    40 										local c = mess:child ( 'command' )
       
    41 										if c and c:attribute ( 'status' ) == 'completed' then
       
    42 											success ()
       
    43 										else
       
    44 											fail ( mess:xml () ) -- XXX more forms? results?
       
    45 										end
       
    46 									end, fail )
       
    47 							end,
       
    48 							function ( form, success, fail )
       
    49 								iq.send ( conn, to, 'set',
       
    50 									{
       
    51 										command = form:format ( { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid }, 'cancel' ),
       
    52 									}, success, fail )
       
    53 							end )
       
    54 					else
       
    55 						fail ( mess:xml () ) -- XXX
       
    56 					end
       
    57 				end
       
    58 			end
       
    59 		end, fail )
       
    60 end
       
    61 
       
    62 return F
       
    63 
    65 
    64 -- vim: se ts=4: --
    66 -- vim: se ts=4: --