examples/remote.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Mar 2009 05:05:14 +0200
changeset 44 bd66956cd397
child 49 95f3bf77c598
permissions -rw-r--r--
Disco and remote in new way
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
-- REMOTE CONTROLLING CLIENTS (XEP-0146)
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
-- library
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
require 'lm'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
require 'iq'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
-- forms
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     9
require 'disco'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
-- public
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
remote = { }
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
function remote.list ( conn, to, success, fail )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
	disco.items ( conn, to, success, fail, 'http://jabber.org/protocol/commands' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
function remote.command ( conn, to, command, success, fail )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
	iq.send ( conn, to, 'set',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
		{
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
			command = { xmlns = 'http://jabber.org/protocol/commands', action = 'execute', node = command },
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
		},
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
		function ( mess )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
			local c = mess:child ( 'command' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
			if c then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
				local x = c:child ( 'x' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
				if x then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
					local sid = c:attribute ( 'sessionid' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
					local id  = parse_form ( x ) -- FIXME
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
					forms[id].send =
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
						function ( form )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
							iq.send ( conn, to, 'set',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
								{
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
									command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
										x = { xmlns = 'jabber:x:data', type = 'form',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    37
											field = form.val,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    38
										},
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
									},
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
								},
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
								function ( mess )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
									local c = mess:child ( 'command' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
									if c and c:attribute ( 'status' ) == 'completed' then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
										success ()
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
										main.print_info ( to, 'Now you can run /form del ' .. id .. ' to delete form from list' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
										form.status = 'acquired'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
									else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
										print ( 'D: FIXME: nonsuccessful remote command: ' .. mess:xml () )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
									end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
								end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
								function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
									main.print_info ( to, 'Got non-successful response to form: ' .. mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
									form.status = 'rejected'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
								end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
							form.status = 'sent'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
						end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
					forms[id].status = 'filling'
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
					main.print_info ( to, 'You have new form. To fill it, use /form ' .. id .. ' fieldname value' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
				end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
			end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
		end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
		fail )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
-- mcabber
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
main.command ( 'remote',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
	function ( args )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
		local who
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
		if args.t then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
			who = args.t
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
			who = main.full_jid ()
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		local action = args[1]
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		local conn   = lm.connection.bless ( main.connection () )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		if action then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
			remote.command ( conn, who, action,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
				function ()
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
					main.print_info ( who, ('Command %s executed'):format ( action ) )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
				end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    83
					main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    85
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
			remote.list ( conn, who,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
				function ( items )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
					local text = ''
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    89
					for index, item in ipairs ( items ) do
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
						text = text .. '\n - ' .. item.node
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
					end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
					if text ~= '' then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
						main.print_info ( who, 'Available commands:' .. text )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
					else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
						main.print_info ( who, 'No commands available.' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
					end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
				end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
					main.print_info ( who, ("Remote commands list for %s failed: %s"):format ( who, mesg ) )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   100
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
	end, true, 'jid' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
commands_help['remote'] = "[-t target_jid] [remote_command]\n\nPrints list of available remote command or requests execution of specified command."
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
-- vim: se ts=4: --