examples/mc_remote.lua
changeset 67 d33ca5572e91
equal deleted inserted replaced
66:542f61e113cb 67:d33ca5572e91
       
     1 
       
     2 local lm     = require 'lm'
       
     3 local remote = require 'remote'
       
     4 
       
     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' )
       
    63 
       
    64 commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
       
    65 
       
    66 -- vim: se ts=4: --