author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 28 Mar 2009 19:43:12 +0200 | |
changeset 67 | d33ca5572e91 |
parent 66 | 542f61e113cb |
permissions | -rw-r--r-- |
66 | 1 |
|
2 |
local lm = require 'lm' |
|
3 |
local pubsub = require 'pubsub' |
|
4 |
||
5 |
main.command ( 'node', |
|
6 |
function ( args ) |
|
7 |
local who, action, node = args.t, args[1], args[2] |
|
8 |
local conn = lm.connection.bless ( main.connection () ) |
|
9 |
if not who then |
|
10 |
who = main.current_buddy () |
|
11 |
end |
|
12 |
if action == 'subscribe' then |
|
13 |
pubsub.subscribe ( conn, who, node, |
|
14 |
function ( id ) |
|
15 |
if id then |
|
16 |
main.print_info ( who, 'Subscription succeeds with id ' .. id ) |
|
17 |
else |
|
18 |
main.print_info ( who, 'Subscription successful' ) |
|
19 |
end |
|
20 |
end, |
|
21 |
function ( mesg ) |
|
22 |
main.print_info ( who, 'Subscription unsuccessful: ' .. mesg ) |
|
23 |
end ) |
|
24 |
elseif action == 'unsubscribe' then |
|
25 |
pubsub.unsubscribe ( conn, who, node, |
|
26 |
function () |
|
27 |
main.print_info ( who, 'Unubscription successful' ) |
|
28 |
end, |
|
29 |
function ( mesg ) |
|
30 |
main.print_info ( who, 'Unsubscription unsuccessful: ' .. mesg ) |
|
31 |
end ) |
|
32 |
elseif action == 'retrieve' or action == 'items' or action == 'get' then |
|
33 |
pubsub.retrieve ( conn, who, node, |
|
34 |
function ( from, node, item ) |
|
35 |
main.print_info ( who, 'Item from ' .. from .. ', node ' .. node .. ':\n' .. item:xml () ) |
|
36 |
end, |
|
37 |
function ( mesg ) |
|
38 |
main.print_info ( who, 'Retrieval failed: ' .. mesg ) |
|
39 |
end, args.m ) |
|
40 |
elseif action == 'create' or action == 'new' then |
|
41 |
pubsub.create_node ( conn, who, node, |
|
42 |
function ( node ) |
|
43 |
if node then |
|
44 |
main.print_info ( who, 'Node ' .. node .. ' successfully created' ) |
|
45 |
else |
|
46 |
main.print_info ( who, 'Node successfully created' ) |
|
47 |
end |
|
48 |
end, |
|
49 |
function ( mesg ) |
|
50 |
main.print_info ( who, 'Creation failed: ' .. mesg ) |
|
51 |
end ) |
|
52 |
elseif action == 'delete' or action == 'del' then |
|
53 |
pubsub.delete_node ( conn, who, node, |
|
54 |
function () |
|
55 |
main.print_info ( who, 'Node deleted' ) |
|
56 |
end, |
|
57 |
function ( mesg ) |
|
58 |
main.print_info ( who, 'Node deletion failed: ' .. mesg ) |
|
59 |
end ) |
|
60 |
elseif action == 'purge' or action == 'del_items' then |
|
61 |
pubsub.purge_node ( conn, who, node, |
|
62 |
function () |
|
63 |
main.print_info ( who, 'Node purged' ) |
|
64 |
end, |
|
65 |
function ( mesg ) |
|
66 |
main.print_info ( who, 'Node purge failed: ' .. mesg ) |
|
67 |
end ) |
|
68 |
elseif action:sub ( 1, 4 ) == 'conf' then |
|
69 |
pubsub.configure_node ( conn, who, node, |
|
70 |
function ( form, submit, reject ) |
|
67
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
71 |
insert_form ( form, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
72 |
function ( form ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
73 |
submit ( form, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
74 |
function () |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
75 |
main.print_info ( who, 'Node configuration completed' ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
76 |
end, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
77 |
function ( mesg ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
78 |
main.print_info ( who, 'Node configuration failed: ' .. mesg ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
79 |
end ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
80 |
end, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
81 |
function ( form ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
82 |
reject ( form, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
83 |
function () |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
84 |
main.print_info ( who, 'Node configuration cancelled' ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
85 |
end, |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
86 |
function ( mesg ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
87 |
main.print_info ( who, 'Node configuration cancellation failed: ' .. mesg ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
88 |
end ) |
d33ca5572e91
Fully object forms interface (untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
66
diff
changeset
|
89 |
end ) |
66 | 90 |
end, |
91 |
function ( mesg ) |
|
92 |
main.print_info ( who, 'Node configuration failed: ' .. mesg ) |
|
93 |
end ) |
|
94 |
elseif action == 'subscriptions' or action == 'subscribers' then |
|
95 |
pubsub.list_subscriptions ( conn, who, node, |
|
96 |
function ( s ) |
|
97 |
local text = '' |
|
98 |
for i, v in ipairs ( s ) do |
|
99 |
local subid = v.subid |
|
100 |
if subid then |
|
101 |
subid = '(id ' .. subid .. ')' |
|
102 |
else |
|
103 |
subid = '' |
|
104 |
end |
|
105 |
text = text .. ('\n- [%s] %s %s'):format ( v.subscription, v.jid, subid ) |
|
106 |
end |
|
107 |
if text ~= '' then |
|
108 |
main.print_info ( who, 'Node subscriptions:' .. text ) |
|
109 |
else |
|
110 |
main.print_info ( who, 'No subscriptions' ) |
|
111 |
end |
|
112 |
end, |
|
113 |
function ( mesg ) |
|
114 |
main.print_info ( who, 'Node subscriptions retrieval failed: ' .. mesg ) |
|
115 |
end ) |
|
116 |
elseif action == 'subscription' or action == 'modify' then -- XXX |
|
117 |
pubsub.modify_subscription ( conn, args.t or main.current_buddy (), node, args[3], args[4], |
|
118 |
function () |
|
119 |
main.print_info ( who, 'Subscription modified' ) |
|
120 |
end, |
|
121 |
function ( mesg ) |
|
122 |
main.print_info ( who, 'Subsrciption modification failed: ' .. mesg ) |
|
123 |
end, args[5] ) |
|
124 |
else |
|
125 |
print ( 'Error: unknown action' ) |
|
126 |
end |
|
127 |
end, true ) |
|
128 |
||
129 |
-- FIXME |
|
130 |
commands_help['node'] = "[-t jid] [-m max_items] action [node_name]\n\nAction can be subscribe, unsubscribe, retrieve (items, get), create (new), delete (del), purge (del_items), configure (conf*), subscriptions (subscribers), subscription (modify?)" |
|
131 |
--[[ |
|
132 |
commands_help['subscribe'] = "[-t jid] node_name\n\nSends pubsub subscription request to specified node of jid or current buddy." |
|
133 |
commands_help['unsubscribe'] = "[-t jid] node_name\n\nSends pubsub unsubscription request to specified node of jid or current buddy." |
|
134 |
commands_help['retrieve'] = "[-t jid] [-m max_items] node_name\n\nSends pubsub items retrieval request to specified node of jid or current buddy.\nNote, that we cannot know, how to deal with these itemss, so, raw xml will be printed as a result." |
|
135 |
commands_help['create_node'] = "[-t jid] [node_name]\n\nSends pubsub node creation request to specified node of jid or current buddy. If node name is not specified, server can generate unique id for it, if supported." |
|
136 |
commands_help['configure_node'] = "[-t jid] node_name\n\nSends pubsub node configuration request to specified node of jid or current buddy." |
|
137 |
commands_help['delete_node'] = "[-t jid] node_name\n\nSends pubsub node deletion request to specified node of jid or current buddy." |
|
138 |
commands_help['purge_node'] = "[-t jid] node_name\n\nSends pubsub node items purging request to specified node of jid or current buddy." |
|
139 |
commands_help['subscriptions'] = "[-t jid] node_name\n\nSends pubsub subscription list request to specified node of jid or current buddy." |
|
140 |
commands_help['subscription'] = "[-t jid] node_name subscriber_jid state [subscription_id]\n\nSends pubsub subscription modification request to change subscription state of 'subscriber_jid' to 'state'. Optional id is used when multiple subscriptions for one jid are available." |
|
141 |
--]] |
|
142 |
||
143 |
local pubsub_handler = lm.message_handler.new ( pubsub.message_handler ) |
|
144 |
||
145 |
local pubsub_handler_registered = false |
|
146 |
||
147 |
hooks_d['hook-post-connect'].pubsub = |
|
148 |
function ( args ) |
|
149 |
lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message', 'normal' ) |
|
150 |
pubsub_handler_registered = true |
|
151 |
hooks_d['hook-post-connect'].pubsub = nil |
|
152 |
hooks_d['hook-quit'].pubsub = |
|
153 |
function ( args ) |
|
154 |
if pubsub_handler_registered then |
|
155 |
lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message' ) |
|
156 |
pubsub_handler_registered = false |
|
157 |
end |
|
158 |
end |
|
159 |
end |
|
160 |
||
161 |
-- vim: se ts=4: -- |