examples/remote.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Fri, 27 Mar 2009 01:46:53 +0200
changeset 64 bf7521ed96eb
parent 51 a95a3a73482c
child 66 542f61e113cb
permissions -rw-r--r--
Rewrite of ibb in object style
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'
64
bf7521ed96eb Rewrite of ibb in object style
Myhailo Danylenko <isbear@ukrpost.net>
parents: 51
diff changeset
     7
local iq = 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 )
51
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    51
								iq.send ( conn, to, 'set',
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    52
									{
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    53
										command = { xmlns = 'http://jabber.org/protocol/commands', node = command, sessionid = sid,
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    54
											x = { xmlns = 'jabber:x:data', type = 'cancel' }, -- FIXME
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    55
										},
a95a3a73482c Pubsub uses new forms
Myhailo Danylenko <isbear@ukrpost.net>
parents: 49
diff changeset
    56
									}, success, fail )
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    57
							end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    58
					else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    59
						fail ( mess:xml () ) -- XXX
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    60
					end
44
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
			end
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    63
		end, fail )
44
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
-- mcabber
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
main.command ( 'remote',
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
	function ( args )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
		local who
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
		if args.t then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
			who = args.t
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
			who = main.full_jid ()
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		local action = args[1]
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		local conn   = lm.connection.bless ( main.connection () )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
		if action then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
			remote.command ( conn, who, action,
49
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    80
				function ( form, submit, reject )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    81
					if not form then
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    82
						main.print_info ( who, ('Command %s completed'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    83
					else
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    84
						local id = #forms + 1
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    85
						forms[id] = {
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    86
							form = form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    87
							submit =
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    88
								function ( form )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    89
									submit ( form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    90
										function ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    91
											main.print_info ( who, ('Command %s completed'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    92
										end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    93
										function ( mesg )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    94
											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
    95
										end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    96
								end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    97
							reject =
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    98
								function ( form )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
    99
									reject ( form,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   100
										function ()
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   101
											main.print_info ( who, ('Command %s execution cancelled'):format ( action ) )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   102
										end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   103
										function ( mesg )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   104
											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
   105
										end )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   106
								end,
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   107
						}
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   108
						print ( 'You have new form ' .. id )
95f3bf77c598 Forms in modular way
Myhailo Danylenko <isbear@ukrpost.net>
parents: 44
diff changeset
   109
					end
44
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
				end,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   111
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
					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
   113
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
		else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
			remote.list ( conn, who,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
				function ( items )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
					local text = ''
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
					for index, item in ipairs ( items ) do
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
						text = text .. '\n - ' .. item.node
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
					if text ~= '' then
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
						main.print_info ( who, 'Available commands:' .. text )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
					else
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
						main.print_info ( who, 'No commands available.' )
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,
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
				function ( mesg )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
					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
   129
				end )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
		end
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
	end, true, 'jid' )
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
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
   134
bd66956cd397 Disco and remote in new way
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
-- vim: se ts=4: --