scripts/xep0146.lua
changeset 5 cba039bd6f13
parent 4 bca17e4a9851
child 6 90dceae3ed1f
equal deleted inserted replaced
4:bca17e4a9851 5:cba039bd6f13
     1 
       
     2 function remote_command ( who, command )
       
     3 	lm.connection.bless( main.connection () ):send (
       
     4 		lm.message.create { mtype = 'iq-set', to = who,
       
     5 			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command }
       
     6 		},
       
     7 		function ( conn, mess )
       
     8 			if mess:child ( 'command' ) and mess:child( 'command' ):child ( 'x' ) then
       
     9 				local id = parse_form ( mess:child( 'command' ):child ( 'x' ) )
       
    10 				local sid = mess:child( 'command' ):attribute ( 'sessionid' )
       
    11 				forms[id].send =
       
    12 					function ( form )
       
    13 						conn:send (
       
    14 							lm.message.create { mtype = 'iq-set', to = who,
       
    15 								command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
       
    16 									x = { xmlns = 'jabber:x:data', type = 'form',
       
    17 										field = form.val,
       
    18 									},
       
    19 								},
       
    20 							},
       
    21 							function ( conn, mess )
       
    22 								if mess:child ( 'command' ) and mess:child( 'command' ):attribute ( 'status' ) == 'completed' then
       
    23 									main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
       
    24 									forms[id].status = 'acquired'
       
    25 								else
       
    26 									main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
       
    27 									forms[id].status = 'rejected'
       
    28 								end
       
    29 								return true
       
    30 							end )
       
    31 					end
       
    32 				forms[id].status = 'filling'
       
    33 				main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
       
    34 			else
       
    35 				main.print_info ( who, 'Got response to command request:\n' .. mess:xml () )
       
    36 			end
       
    37 			return true
       
    38 		end )
       
    39 end
       
    40 
       
    41 main.add_command ( 'remote',
       
    42 	function ( args )
       
    43 		args = parse_args ( args )
       
    44 		local who
       
    45 		if args.t then
       
    46 			who = args.t
       
    47 			args.t = nil
       
    48 		else
       
    49 			who = full_current_jid ()
       
    50 		end
       
    51 		if args[1] == 'list' or not args[1] then
       
    52 			disco_items (
       
    53 				function ( items )
       
    54 					if type ( items ) == 'string' then
       
    55 						main.print_info ( who, string.format ( "Service items discovery for %s (http://jabber.org/protocol/commands) failed: %s", who, items ) )
       
    56 					else
       
    57 						main.print_info ( who, 'Available commands:' )
       
    58 						for index, item in ipairs ( items ) do
       
    59 							main.print_info ( who, ' - ' .. item.node )
       
    60 						end
       
    61 					end
       
    62 				end, who, 'http://jabber.org/protocol/commands' )
       
    63 		elseif args[1] then
       
    64 			remote_command ( who, args[1] )
       
    65 		end
       
    66 	end )
       
    67 
       
    68 commands_help['remote'] = "[-t target_jid] [list | command]\n\nPrints list of available remote command or requests execution of specified command."
       
    69 
       
    70 -- vim: se ts=4: --