examples/remote.lua
changeset 44 bd66956cd397
child 49 95f3bf77c598
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/remote.lua	Sat Mar 21 05:05:14 2009 +0200
@@ -0,0 +1,106 @@
+
+-- REMOTE CONTROLLING CLIENTS (XEP-0146)
+
+-- library
+
+require 'lm'
+require 'iq'
+-- forms
+require 'disco'
+
+-- public
+
+remote = { }
+
+function remote.list ( conn, to, success, fail )
+	disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
+end
+
+function remote.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 x = c:child ( 'x' )
+				if x then
+					local sid = c:attribute ( 'sessionid' )
+					local id  = parse_form ( x ) -- FIXME
+					forms[id].send =
+						function ( form )
+							iq.send ( conn, to, 'set',
+								{
+									command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
+										x = { xmlns = 'jabber:x:data', type = 'form',
+											field = form.val,
+										},
+									},
+								},
+								function ( mess )
+									local c = mess:child ( 'command' )
+									if c and c:attribute ( 'status' ) == 'completed' then
+										success ()
+										main.print_info ( to, 'Now you can run /form del ' .. id .. ' to delete form from list' )
+										form.status = 'acquired'
+									else
+										print ( 'D: FIXME: nonsuccessful remote command: ' .. mess:xml () )
+									end
+								end,
+								function ( mesg )
+									main.print_info ( to, 'Got non-successful response to form: ' .. mesg )
+									form.status = 'rejected'
+								end )
+							form.status = 'sent'
+						end
+					forms[id].status = 'filling'
+					main.print_info ( to, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
+				end
+			end
+		end,
+		fail )
+end
+
+-- mcabber
+
+main.command ( 'remote',
+	function ( args )
+		local who
+		if args.t then
+			who = args.t
+		else
+			who = main.full_jid ()
+		end
+		local action = args[1]
+		local conn   = lm.connection.bless ( main.connection () )
+		if action then
+			remote.command ( conn, who, action,
+				function ()
+					main.print_info ( who, ('Command %s executed'):format ( action ) )
+				end,
+				function ( mesg )
+					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
+				end )
+		else
+			remote.list ( conn, who,
+				function ( items )
+					local text = ''
+					for index, item in ipairs ( items ) do
+						text = text .. '\n - ' .. item.node
+					end
+					if text ~= '' then
+						main.print_info ( who, 'Available commands:' .. text )
+					else
+						main.print_info ( who, 'No commands available.' )
+					end
+				end,
+				function ( mesg )
+					main.print_info ( who, ("Remote commands list for %s failed: %s"):format ( who, mesg ) )
+				end )
+		end
+	end, true, 'jid' )
+
+commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
+
+-- vim: se ts=4: --