examples/remote.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 28 Mar 2009 19:43:12 +0200
changeset 67 d33ca5572e91
parent 66 542f61e113cb
child 68 742878c74b8e
permissions -rw-r--r--
Fully object forms interface (untested)


-- REMOTE CONTROLLING CLIENTS (XEP-0146)

-- library

local iq     = require 'iq'
local x_data = require 'x_data'
local disco  = require 'disco'

-- public

local F = { }

function F.list ( conn, to, success, fail )
	disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
end

function F.command ( conn, to, command, success, fail )
	iq.send ( conn, to, 'set',
		{
			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
		},
		function ( mess )
			local c = mess:child ( 'command' )
			if c then
				local status = c:attribute ( 'status' )
				if status == 'completed' then
					success ()
				else
					local x = c:child ( 'x' )
					if x then
						local sid = c:attribute ( 'sessionid' )
						success ( x_data.parse ( x ),
							function ( form, success, fail )
								iq.send ( conn, to, 'set',
									{
										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? results?
										end
									end, fail )
							end,
							function ( form, success, fail )
								iq.send ( conn, to, 'set',
									{
										command = form:format ( { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid }, 'cancel' ),
									}, success, fail )
							end )
					else
						fail ( mess:xml () ) -- XXX
					end
				end
			end
		end, fail )
end

return F

-- vim: se ts=4: --