examples/remote.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Sun, 22 Mar 2009 04:14:36 +0200
changeset 49 95f3bf77c598
parent 44 bd66956cd397
child 51 a95a3a73482c
permissions -rw-r--r--
Forms in modular 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'
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
     8
require 'x_data'
44
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
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    27
				local status = c:attribute ( 'status' )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    28
				if status == 'completed' then
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    29
					success ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    30
				else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    31
					local x = c:child ( 'x' )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    32
					if x then
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    33
						local sid = c:attribute ( 'sessionid' )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    34
						success ( x_data.parse ( x ),
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    35
							function ( form, success, fail )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    36
								form.type = 'submit' -- XXX in standard there is 'form' :/
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    37
								iq.send ( conn, to, 'set',
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    38
									{
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    39
										command = form.format ( form, { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid } )
44
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
									},
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    41
									function ( mess )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    42
										local c = mess:child ( 'command' )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    43
										if c and c:attribute ( 'status' ) == 'completed' then
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    44
											success ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    45
										else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    46
											fail ( mess:xml () ) -- XXX more forms?
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    47
										end
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    48
									end, fail )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    49
							end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    50
							function ( form, success, fail )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    51
								success ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    52
							end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    53
					else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    54
						fail ( mess:xml () ) -- XXX
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    55
					end
44
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
			end
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    58
		end, fail )
44
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
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
-- mcabber
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
main.command ( 'remote',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
	function ( args )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
		local who
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
		if args.t then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
			who = args.t
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
			who = main.full_jid ()
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
		local action = args[1]
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
		local conn   = lm.connection.bless ( main.connection () )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
		if action then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
			remote.command ( conn, who, action,
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    75
				function ( form, submit, reject )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    76
					if not form then
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    77
						main.print_info ( who, ('Command %s completed'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    78
					else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    79
						local id = #forms + 1
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    80
						forms[id] = {
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    81
							form = form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    82
							submit =
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    83
								function ( form )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    84
									submit ( form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    85
										function ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    86
											main.print_info ( who, ('Command %s completed'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    87
										end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    88
										function ( mesg )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    89
											main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    90
										end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    91
								end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    92
							reject =
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    93
								function ( form )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    94
									reject ( form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    95
										function ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    96
											main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    97
										end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    98
										function ( mesg )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    99
											main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   100
										end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   101
								end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   102
						}
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   103
						print ( 'You have new form ' .. id )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   104
					end
44
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
				end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
					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
   108
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
			remote.list ( conn, who,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
				function ( items )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
					local text = ''
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
					for index, item in ipairs ( items ) do
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
						text = text .. '\n - ' .. item.node
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
					end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
					if text ~= '' then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
						main.print_info ( who, 'Available commands:' .. text )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
					else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
						main.print_info ( who, 'No commands available.' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
					end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
				end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
					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
   124
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
	end, true, 'jid' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
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
   129
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
-- vim: se ts=4: --