examples/remote.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 121 75a7d595817c
permissions -rw-r--r--
Fix module loading problem


local lm     = require 'lm'
local remote = require 'lm.remote'

main.command ( 'remote',
	function ( args )
		local connection = main.connection ()
		if not connection then
			print "You are not online!"
			return
		end
		local who
		if args.t then
			who = args.t
		else
			who = main.full_jid ()
		end
		local action = args[1]
		local conn   = lm.connection.bless ( connection )
		if action then
			remote.command ( conn, who, action,
				function ( form, submit, reject )
					if not form then
						main.print_info ( who, ('Command %s completed'):format ( action ) )
					else
						insert_form ( form, -- XXX
							function ( form )
								submit ( form,
									function ()
										main.print_info ( who, ('Command %s completed'):format ( action ) )
									end,
									function ( mesg )
										main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
									end )
							end,
							function ( form )
								reject ( form,
									function ()
										main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
									end,
									function ( mesg )
										main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) )
									end )
							end )
					end
				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' )

-- vim: se ts=4 sw=4: --