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-- |
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 | 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 | 27 |
local status = c:attribute ( 'status' ) |
28 |
if status == 'completed' then |
|
29 |
success () |
|
30 |
else |
|
31 |
local x = c:child ( 'x' ) |
|
32 |
if x then |
|
33 |
local sid = c:attribute ( 'sessionid' ) |
|
34 |
success ( x_data.parse ( x ), |
|
35 |
function ( form, success, fail ) |
|
36 |
form.type = 'submit' -- XXX in standard there is 'form' :/ |
|
37 |
iq.send ( conn, to, 'set', |
|
38 |
{ |
|
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 | 41 |
function ( mess ) |
42 |
local c = mess:child ( 'command' ) |
|
43 |
if c and c:attribute ( 'status' ) == 'completed' then |
|
44 |
success () |
|
45 |
else |
|
46 |
fail ( mess:xml () ) -- XXX more forms? |
|
47 |
end |
|
48 |
end, fail ) |
|
49 |
end, |
|
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 | 57 |
end ) |
58 |
else |
|
59 |
fail ( mess:xml () ) -- XXX |
|
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 | 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 | 80 |
function ( form, submit, reject ) |
81 |
if not form then |
|
82 |
main.print_info ( who, ('Command %s completed'):format ( action ) ) |
|
83 |
else |
|
84 |
local id = #forms + 1 |
|
85 |
forms[id] = { |
|
86 |
form = form, |
|
87 |
submit = |
|
88 |
function ( form ) |
|
89 |
submit ( form, |
|
90 |
function () |
|
91 |
main.print_info ( who, ('Command %s completed'):format ( action ) ) |
|
92 |
end, |
|
93 |
function ( mesg ) |
|
94 |
main.print_info ( who, ('Command %s execution failed: %s'):format ( action, mesg ) ) |
|
95 |
end ) |
|
96 |
end, |
|
97 |
reject = |
|
98 |
function ( form ) |
|
99 |
reject ( form, |
|
100 |
function () |
|
101 |
main.print_info ( who, ('Command %s execution cancelled'):format ( action ) ) |
|
102 |
end, |
|
103 |
function ( mesg ) |
|
104 |
main.print_info ( who, ('Command %s execution cancellation failed: %s'):format ( action, mesg ) ) |
|
105 |
end ) |
|
106 |
end, |
|
107 |
} |
|
108 |
print ( 'You have new form ' .. id ) |
|
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: -- |