examples/xep0146.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 27 Feb 2009 00:19:33 +0200
changeset 10 73f4c12b6ffb
parent 9 c2517f8bf647
child 24 25552b21d3fb
permissions -rw-r--r--
Full jid accessor * full_jid * current_buddy sets * fix memleak in buddy_info


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.command ( 'remote',
	function ( args )
		args = parse_args ( args )
		local who
		if args.t then
			who = args.t
			args.t = nil
		else
			who = main.full_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, 'jid' )

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: --