examples/lm/remote.lua
changeset 68 742878c74b8e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/lm/remote.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -0,0 +1,64 @@
+
+-- REMOTE CONTROLLING CLIENTS (XEP-0146)
+
+-- library
+
+local iq     = require 'lm.iq'
+local x_data = require 'lm.x_data'
+local disco  = require 'lm.disco'
+
+--
+
+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: --