scripts/xep0146.lua
changeset 4 bca17e4a9851
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/xep0146.lua	Mon Feb 23 23:16:46 2009 +0200
@@ -0,0 +1,70 @@
+
+function remote_command ( who, command )
+	lm.connection.bless( main.connection () ):send (
+		lm.message.create { mtype = 'iq-set', to = who,
+			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command }
+		},
+		function ( conn, mess )
+			if mess:child ( 'command' ) and mess:child( 'command' ):child ( 'x' ) then
+				local id = parse_form ( mess:child( 'command' ):child ( 'x' ) )
+				local sid = mess:child( 'command' ):attribute ( 'sessionid' )
+				forms[id].send =
+					function ( form )
+						conn:send (
+							lm.message.create { mtype = 'iq-set', to = who,
+								command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
+									x = { xmlns = 'jabber:x:data', type = 'form',
+										field = form.val,
+									},
+								},
+							},
+							function ( conn, mess )
+								if mess:child ( 'command' ) and mess:child( 'command' ):attribute ( 'status' ) == 'completed' then
+									main.print_info ( who, 'Now you can run /form del ' .. id .. ' to delete form from list' )
+									forms[id].status = 'acquired'
+								else
+									main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
+									forms[id].status = 'rejected'
+								end
+								return true
+							end )
+					end
+				forms[id].status = 'filling'
+				main.print_info ( who, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
+			else
+				main.print_info ( who, 'Got response to command request:\n' .. mess:xml () )
+			end
+			return true
+		end )
+end
+
+main.add_command ( 'remote',
+	function ( args )
+		args = parse_args ( args )
+		local who
+		if args.t then
+			who = args.t
+			args.t = nil
+		else
+			who = full_current_jid ()
+		end
+		if args[1] == 'list' or not args[1] then
+			disco_items (
+				function ( items )
+					if type ( items ) == 'string' then
+						main.print_info ( who, string.format ( "Service items discovery for %s (http://jabber.org/protocol/commands) failed: %s", who, items ) )
+					else
+						main.print_info ( who, 'Available commands:' )
+						for index, item in ipairs ( items ) do
+							main.print_info ( who, ' - ' .. item.node )
+						end
+					end
+				end, who, 'http://jabber.org/protocol/commands' )
+		elseif args[1] then
+			remote_command ( who, args[1] )
+		end
+	end )
+
+commands_help['remote'] = "[-t target_jid] [list | command]\n\nPrints list of available remote command or requests execution of specified command."
+
+-- vim: se ts=4: --