examples/remote.lua
changeset 49 95f3bf77c598
parent 44 bd66956cd397
child 51 a95a3a73482c
equal deleted inserted replaced
48:d31ae73038f7 49:95f3bf77c598
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 require 'lm'
     7 require 'iq'
     7 require 'iq'
     8 -- forms
     8 require 'x_data'
     9 require 'disco'
     9 require 'disco'
    10 
    10 
    11 -- public
    11 -- public
    12 
    12 
    13 remote = { }
    13 remote = { }
    22 			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
    22 			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
    23 		},
    23 		},
    24 		function ( mess )
    24 		function ( mess )
    25 			local c = mess:child ( 'command' )
    25 			local c = mess:child ( 'command' )
    26 			if c then
    26 			if c then
    27 				local x = c:child ( 'x' )
    27 				local status = c:attribute ( 'status' )
    28 				if x then
    28 				if status == 'completed' then
    29 					local sid = c:attribute ( 'sessionid' )
    29 					success ()
    30 					local id  = parse_form ( x ) -- FIXME
    30 				else
    31 					forms[id].send =
    31 					local x = c:child ( 'x' )
    32 						function ( form )
    32 					if x then
    33 							iq.send ( conn, to, 'set',
    33 						local sid = c:attribute ( 'sessionid' )
    34 								{
    34 						success ( x_data.parse ( x ),
    35 									command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
    35 							function ( form, success, fail )
    36 										x = { xmlns = 'jabber:x:data', type = 'form',
    36 								form.type = 'submit' -- XXX in standard there is 'form' :/
    37 											field = form.val,
    37 								iq.send ( conn, to, 'set',
    38 										},
    38 									{
       
    39 										command = form.format ( form, { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid } )
    39 									},
    40 									},
    40 								},
    41 									function ( mess )
    41 								function ( mess )
    42 										local c = mess:child ( 'command' )
    42 									local c = mess:child ( 'command' )
    43 										if c and c:attribute ( 'status' ) == 'completed' then
    43 									if c and c:attribute ( 'status' ) == 'completed' then
    44 											success ()
    44 										success ()
    45 										else
    45 										main.print_info ( to, 'Now you can run /form del ' .. id .. ' to delete form from list' )
    46 											fail ( mess:xml () ) -- XXX more forms?
    46 										form.status = 'acquired'
    47 										end
    47 									else
    48 									end, fail )
    48 										print ( 'D: FIXME: nonsuccessful remote command: ' .. mess:xml () )
    49 							end,
    49 									end
    50 							function ( form, success, fail )
    50 								end,
    51 								success ()
    51 								function ( mesg )
    52 							end )
    52 									main.print_info ( to, 'Got non-successful response to form: ' .. mesg )
    53 					else
    53 									form.status = 'rejected'
    54 						fail ( mess:xml () ) -- XXX
    54 								end )
    55 					end
    55 							form.status = 'sent'
       
    56 						end
       
    57 					forms[id].status = 'filling'
       
    58 					main.print_info ( to, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
       
    59 				end
    56 				end
    60 			end
    57 			end
    61 		end,
    58 		end, fail )
    62 		fail )
       
    63 end
    59 end
    64 
    60 
    65 -- mcabber
    61 -- mcabber
    66 
    62 
    67 main.command ( 'remote',
    63 main.command ( 'remote',
    74 		end
    70 		end
    75 		local action = args[1]
    71 		local action = args[1]
    76 		local conn   = lm.connection.bless ( main.connection () )
    72 		local conn   = lm.connection.bless ( main.connection () )
    77 		if action then
    73 		if action then
    78 			remote.command ( conn, who, action,
    74 			remote.command ( conn, who, action,
    79 				function ()
    75 				function ( form, submit, reject )
    80 					main.print_info ( who, ('Command %s executed'):format ( action ) )
    76 					if not form then
       
    77 						main.print_info ( who, ('Command %s completed'):format ( action ) )
       
    78 					else
       
    79 						local id = #forms + 1
       
    80 						forms[id] = {
       
    81 							form = form,
       
    82 							submit =
       
    83 								function ( form )
       
    84 									submit ( form,
       
    85 										function ()
       
    86 											main.print_info ( who, ('Command %s completed'):format ( action ) )
       
    87 										end,
       
    88 										function ( mesg )
       
    89 											main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
       
    90 										end )
       
    91 								end,
       
    92 							reject =
       
    93 								function ( form )
       
    94 									reject ( form,
       
    95 										function ()
       
    96 											main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
       
    97 										end,
       
    98 										function ( mesg )
       
    99 											main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) )
       
   100 										end )
       
   101 								end,
       
   102 						}
       
   103 						print ( 'You have new form ' .. id )
       
   104 					end
    81 				end,
   105 				end,
    82 				function ( mesg )
   106 				function ( mesg )
    83 					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
   107 					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
    84 				end )
   108 				end )
    85 		else
   109 		else