1 |
1 |
2 -- PUBLISH-SUBSCRIBE (BEP-0060) |
2 local lm = require 'lm' |
|
3 local pubsub = require 'lm.pubsub' |
3 |
4 |
4 -- library |
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 ) |
|
71 insert_form ( form, |
|
72 function ( form ) |
|
73 submit ( form, |
|
74 function () |
|
75 main.print_info ( who, 'Node configuration completed' ) |
|
76 end, |
|
77 function ( mesg ) |
|
78 main.print_info ( who, 'Node configuration failed: ' .. mesg ) |
|
79 end ) |
|
80 end, |
|
81 function ( form ) |
|
82 reject ( form, |
|
83 function () |
|
84 main.print_info ( who, 'Node configuration cancelled' ) |
|
85 end, |
|
86 function ( mesg ) |
|
87 main.print_info ( who, 'Node configuration cancellation failed: ' .. mesg ) |
|
88 end ) |
|
89 end ) |
|
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 ) |
5 |
128 |
6 local iq = require 'iq' |
129 -- FIXME |
7 local x_data = require 'x_data' |
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 --]] |
8 |
142 |
9 -- |
143 local pubsub_handler = lm.message_handler.new ( pubsub.message_handler ) |
10 |
144 |
11 local O = { |
145 local pubsub_handler_registered = false |
12 handlers = { }, |
|
13 } |
|
14 |
146 |
15 local F = { } |
147 hooks_d['hook-post-connect'].pubsub = |
16 |
148 function ( args ) |
17 function F.handler ( xmlns, handler ) |
149 lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message', 'normal' ) |
18 O.handlers[xmlns] = handler |
150 pubsub_handler_registered = true |
19 end |
151 hooks_d['hook-post-connect'].pubsub = nil |
20 |
152 hooks_d['hook-quit'].pubsub = |
21 function F.message_handler ( conn, mess ) |
153 function ( args ) |
22 local e = mess:child ( 'event' ) |
154 if pubsub_handler_registered then |
23 if e and e:attribute ( 'xmlns' ) == 'http://jabber.org/protocol/pubsub#event' then |
155 lm.connection.bless( main.connection () ):handler ( pubsub_handler, 'message' ) |
24 local is = e:child ( 'items' ) |
156 pubsub_handler_registered = false |
25 if is then |
|
26 local from = mess:attribute ( 'from' ) |
|
27 local node = is:attribute ( 'node' ) |
|
28 local item = is:child () |
|
29 while item do |
|
30 local id = item:attribute ( 'id' ) |
|
31 local n = item:child () |
|
32 while n do |
|
33 local xmlns = n:attribute ( 'xmlns' ) |
|
34 if O.handlers[xmlns] then |
|
35 O.handlers[xmlns] ( from, node, n, id ) |
|
36 end |
|
37 n = n:next () |
|
38 end |
|
39 item = item:next () |
|
40 end |
|
41 return true |
|
42 end |
|
43 end |
|
44 return false |
|
45 end |
|
46 |
|
47 -- SUBSCRIBER USE CASES |
|
48 |
|
49 function F.subscribe ( conn, to, node, success, fail ) |
|
50 local jid = conn:jid():gsub ( '/.*', '' ) |
|
51 iq.send ( conn, to, 'set', |
|
52 { |
|
53 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
54 subscribe = { node = node, jid = jid }, |
|
55 }, |
|
56 }, |
|
57 function ( mess ) |
|
58 local s = mess:path ( 'pubsub', 'subscription' ) |
|
59 if s then |
|
60 success ( s:attribute ( 'subid' ) ) |
|
61 else |
|
62 success () |
|
63 end |
|
64 end, |
|
65 fail ) |
|
66 end |
|
67 |
|
68 function F.unsubscribe ( conn, to, node, success, fail ) |
|
69 local jid = conn:jid():gsub ( '/.*', '' ) |
|
70 iq.send ( conn, to, 'set', |
|
71 { |
|
72 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
73 unsubscribe = { node = node, jid = jid }, |
|
74 }, |
|
75 }, success, fail ) |
|
76 end |
|
77 |
|
78 -- I found no servers with subscription options support thus it is not implemented. |
|
79 |
|
80 -- untested :( |
|
81 function F.retrieve ( conn, to, node, success, fail, max, ids ) |
|
82 local items = { node = node, max_items = max } |
|
83 if ids then |
|
84 items.item = { } |
|
85 for k, id in pairs ( ids ) do |
|
86 table.insert ( items.item, { id = id } ) |
|
87 end |
|
88 end |
|
89 iq.send ( conn, to, 'get', |
|
90 { |
|
91 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
92 items = items, |
|
93 }, |
|
94 }, |
|
95 function ( mess ) |
|
96 local items = mess:path ( 'pubsub', 'items' ) |
|
97 if items then |
|
98 local from = mess:attribute ( 'from' ) |
|
99 local node = items:attribute ( 'node' ) |
|
100 local item = items:child () |
|
101 while item do |
|
102 success ( from, node, item ) -- XXX use registered xmlns handlers for that? |
|
103 item = item:next () |
|
104 end |
|
105 else |
|
106 -- XXX |
|
107 end |
|
108 end, fail ) |
|
109 end |
|
110 |
|
111 -- OWNER USE CASES |
|
112 |
|
113 -- node may be nil |
|
114 function F.create_node ( conn, to, node, success, fail ) |
|
115 iq.send ( conn, to, 'set', |
|
116 { |
|
117 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub', |
|
118 create = { node = node }, |
|
119 }, |
|
120 }, |
|
121 function ( mess ) |
|
122 if node then |
|
123 success () |
|
124 else |
|
125 local create = mess:path ( 'pubsub', 'create' ) |
|
126 if create then |
|
127 success ( create:attribute ( 'node' ) ) |
|
128 else |
|
129 success () |
|
130 end |
157 end |
131 end |
158 end |
132 end, fail ) |
159 end |
133 end |
|
134 |
|
135 function F.delete_node ( conn, to, node, success, fail ) |
|
136 iq.send ( conn, to, 'set', |
|
137 { |
|
138 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
139 delete = { node = node }, |
|
140 }, |
|
141 }, success, fail ) |
|
142 end |
|
143 |
|
144 function F.purge_node ( conn, to, node, success, fail ) |
|
145 iq.send ( conn, to, 'set', |
|
146 { |
|
147 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
148 purge = { node = node }, |
|
149 }, |
|
150 }, success, fail ) |
|
151 end |
|
152 |
|
153 function F.configure_node ( conn, to, node, success, fail ) |
|
154 iq.send ( conn, to, 'get', |
|
155 { |
|
156 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
157 configure = { node = node }, |
|
158 }, |
|
159 }, |
|
160 function ( mess ) |
|
161 local x = mess:path ( 'pubsub', 'configure', 'x' ) |
|
162 if x then |
|
163 success ( x_data.parse ( x ), |
|
164 function ( form, success, fail ) |
|
165 iq.send ( conn, to, 'set', |
|
166 { |
|
167 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
168 configure = form:format ( { node = node }, 'submit' ), |
|
169 }, |
|
170 }, success, fail ) |
|
171 end, |
|
172 function ( form, success, fail ) |
|
173 iq.send ( conn, to, 'set', |
|
174 { |
|
175 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
176 configure = form:format ( { node = node }, 'cancel' ), |
|
177 }, |
|
178 }, success, fail ) |
|
179 end ) |
|
180 else |
|
181 fail ( mess:xml () ) -- XXX |
|
182 end |
|
183 end, fail ) |
|
184 end |
|
185 |
|
186 function F.list_subscriptions ( conn, to, node, success, fail ) |
|
187 iq.send ( conn, to, 'get', |
|
188 { |
|
189 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
190 subscriptions = { node = node }, |
|
191 }, |
|
192 }, |
|
193 function ( mess ) |
|
194 local s = mess:path ( 'pubsub', 'subscriptions' ) |
|
195 if s then |
|
196 local sub = s:child () |
|
197 local ret = { } |
|
198 while sub do |
|
199 table.insert ( ret, { jid = sub:attribute ( 'jid' ), subscription = sub:attribute ( 'subscription' ), subid = sub:attribute ( 'subid' ) } ) |
|
200 sub = sub:next () |
|
201 end |
|
202 success ( ret ) |
|
203 else |
|
204 fail ( mess:xml () ) -- XXX |
|
205 end |
|
206 end, fail ) |
|
207 end |
|
208 |
|
209 function F.modify_subscription ( conn, to, node, jid, state, success, fail, id ) |
|
210 iq.send ( conn, to, 'set', |
|
211 { |
|
212 pubsub = { xmlns = 'http://jabber.org/protocol/pubsub#owner', |
|
213 subscriptions = { node = node, |
|
214 subscription = { jid = jid, subscription = state, id = id }, |
|
215 }, |
|
216 }, |
|
217 }, success, fail ) |
|
218 end |
|
219 |
|
220 return F |
|
221 |
160 |
222 -- vim: se ts=4: -- |
161 -- vim: se ts=4: -- |