examples/xep0146.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Mon, 16 Mar 2009 00:47:44 +0200
changeset 27 92b254b64360
parent 25 38c68c285e41
child 34 8206d7cb1447
permissions -rw-r--r--
Full C args parsing


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.status = 'acquired'
								else
									main.print_info ( who, 'Got non-successful response to form:\n' .. mess:xml () )
									forms.status = 'rejected'
								end
								return true
							end )
						form.status = 'sent'
					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 = main.parse_args ( args )
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		local action = args[1]
		if not action 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
						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
				end, who, 'http://jabber.org/protocol/commands' )
		elseif action then
			remote_command ( who, action )
		end
	end, '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: --