Mercurial > psgxs
annotate psgxs.js @ 10:44889cfb2f8c
Fix getItem method.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 27 Aug 2010 01:02:56 +0200 |
parents | a6429f48e403 |
children | 0ed3c06c5191 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env node |
2 | |
3 /* | |
4 * Copyright (C) 2010 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | |
5 * | |
6 * This file is part of PSĜS, a PubSub server written in JavaScript. | |
7 * | |
8 * PSĜS is free software: you can redistribute it and/or modify | |
9 * it under the terms of the GNU Affero General Public License as | |
10 * published by the Free Software Foundation, either version 3 of the | |
11 * License. | |
12 * | |
13 * PSĜS is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with PSĜS. If not, see <http://www.gnu.org/licenses/>. | |
20 */ | |
21 | |
22 var sys = require('sys'); | |
23 var xmpp = require('xmpp'); | |
24 var sha1 = require('sha1'); | |
25 require('./iso8601'); | |
26 var storage = require('./storage'); | |
27 var errors = require('./errors'); | |
28 var utils = require('./util'); | |
29 var toBareJID = utils.toBareJID; | |
30 var config = require('./configuration'); | |
31 var forms = require('./forms'); | |
32 var conn = new xmpp.Connection(); | |
33 | |
34 var service_configuration = config.service_configuration; | |
35 var componentJID = config.jid; | |
36 var componentPassword = config.password; | |
37 | |
38 conn.log = function (_, m) { sys.puts(m); }; | |
39 | |
40 function _(m, c) { | |
41 if (c) | |
42 sys.print('\033[1;'+c+'m'); | |
43 sys.print(sys.inspect(m, false, null)); | |
44 if (c) | |
45 sys.print('\033[0m'); | |
46 sys.puts(''); | |
47 }; | |
48 | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
49 if (typeof xmpp.StanzaBuilder.cnode != 'function' || typeof xmpp.StanzaBuilder.prototype.cnode != 'function') { |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
50 xmpp.StanzaBuilder.prototype.cnode = function (stanza) |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
51 { |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
52 var parent = this; |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
53 parent.tags.push(stanza); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
54 parent.children.push(stanza); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
55 return this; |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
56 }; |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
57 } |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
58 |
0 | 59 function onIq(stanza) { |
60 var type = stanza.getAttribute('type'); | |
61 var from = stanza.getAttribute('to'); | |
62 var to = stanza.getAttribute('from'); | |
63 var id = stanza.getAttribute('id'); | |
64 | |
65 var response; | |
66 if (id) | |
67 response = xmpp.iq({to: to, from: from, type: 'result', id: id}); | |
68 else | |
69 response = xmpp.iq({to: to, from: from, type: 'result'}); | |
70 | |
71 if (type == 'get') { | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
72 |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
73 // XEP-0092: Software Version |
0 | 74 if (stanza.getChild('query', 'jabber:iq:version')) { |
75 var query = xmpp.stanza('query', {xmlns: 'jabber:iq:version'}) | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
76 .s('name').t('PSĜS') |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
77 .s('version').t(config.version) |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
78 .s('os').t(config.os); |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
79 response.cnode(query); |
0 | 80 |
81 // SECTION 5.1 | |
82 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#info')) { | |
83 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#info'); | |
84 var nodeID = query.getAttribute('node'); | |
85 | |
86 // SECTION 5.3 | |
87 if (nodeID && nodeID != '') { | |
88 if (!storage.existsNode(nodeID)) | |
89 return makeError(response, errors.node_does_not_exist.n); | |
90 | |
91 var conf = storage.getConfiguration(nodeID); | |
92 if (typeof conf == 'number') | |
93 return makeError(response, conf); | |
94 | |
95 var type = 'leaf' | |
96 if (conf['pubsub#node_type']) | |
97 type = conf['pubsub#node_type']; | |
98 | |
6 | 99 var q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#info', node: nodeID}) |
0 | 100 q.s('identity', {category: 'pubsub', type: type}); |
101 q.s('feature', {'var': 'http://jabber.org/protocol/pubsub'}); | |
102 | |
103 // SECTION 5.4 | |
104 if (config.enabled('meta-data')) { | |
4
4c93e76fa371
Ensure that the creator of a node is the default owner; don’t crash
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
105 var x = forms.build('result', 'node_metadata', storage.getMetadata(nodeID), true); |
0 | 106 if (x) |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
107 q.cnode(x); |
0 | 108 } |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
109 response.cnode(q); |
0 | 110 |
111 // SECTION 5.1 | |
112 } else { | |
113 var q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#info'}) | |
114 .s('identity', {category: 'pubsub', type: 'service', name: 'PubSub JavaScript Server'}) | |
115 .s('feature', {'var': 'http://jabber.org/protocol/disco#info'}) | |
116 .s('feature', {'var': 'http://jabber.org/protocol/disco#items'}) | |
117 .s('feature', {'var': 'http://jabber.org/protocol/pubsub'}) | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
118 .s('feature', {'var': 'jabber:iq:version'}) |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
119 .s('feature', {'var': 'http://jabber.org/protocol/commands'}); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
120 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
121 for (var i in config.activated) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
122 if (typeof i == 'string') |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
123 q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]}); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
124 |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
125 response.cnode(q); |
0 | 126 } |
127 | |
128 // SECTION 5.2 | |
129 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#items')) { | |
130 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#items'); | |
131 var q; | |
132 var children; | |
133 var nodeID = query.getAttribute('node'); | |
134 if (nodeID && nodeID != '') { | |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
135 if (nodeID == 'http://jabber.org/protocol/commands') { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
136 // XEP-0050: Ad-Hoc Commands |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
137 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
138 q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: nodeID}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
139 .s('item', {jid: componentJID, node: 'ping', name: 'Ping'}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
140 .s('item', {jid: componentJID, node: 'reload', name: 'Reload'}); |
0 | 141 |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
142 response.cnode(q); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
143 return conn.send(response); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
144 } else { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
145 if (!storage.existsNode(nodeID)) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
146 return makeError(response, errors.node_does_not_exist.n); |
0 | 147 |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
148 q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items', node: nodeID}); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
149 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
150 children = storage.getChildren(nodeID); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
151 if (typeof children == 'number') |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
152 return makeError(response, children); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
153 } |
0 | 154 } else { |
155 q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#items'}); | |
156 | |
157 children = storage.getChildren(); | |
158 if (typeof children == 'number') | |
159 return makeError(response, children); | |
160 } | |
161 | |
162 for (var i in children) { | |
163 var attr = {jid: componentJID}; | |
164 if (children[i] == 'node') { | |
165 if (config.enabled('meta-data')) { | |
166 var metadata = storage.getMetadata(i); | |
167 if (metadata['pubsub#title']) | |
168 attr.name = metadata['pubsub#title']; | |
169 } | |
170 attr.node = i; | |
171 | |
172 // SECTION 5.5 | |
173 } else | |
174 attr.name = i; | |
175 | |
176 q.s('item', attr); | |
177 } | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
178 response.cnode(q); |
0 | 179 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) { |
180 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub'); | |
181 | |
182 // SECTION 5.6 | |
183 if (pubsub.getChild('subscriptions')) { | |
184 if (!config.enabled('retrieve-subscriptions')) | |
185 return makeError(response, errors.subscriptions_retrieval_not_supported.n); | |
186 | |
187 var subscriptions = pubsub.getChild('subscriptions'); | |
188 var subs; | |
189 | |
190 var nodeID = subscriptions.getAttribute('node'); | |
191 if (nodeID && nodeID != '') { | |
192 if (!storage.existsNode(nodeID)) | |
193 return makeError(response, errors.node_does_not_exist.n); | |
194 subs = storage.getSubscription(toBareJID(to), node); | |
195 } else | |
196 subs = storage.getSubscription(toBareJID(to)); | |
197 | |
198 var s = xmpp.stanza('subscriptions'); | |
199 for (i in subs) | |
200 s.s('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}); | |
201 | |
202 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
203 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
204 response.cnode(p); |
0 | 205 |
206 // SECTION 5.7 | |
207 } else if (pubsub.getChild('affiliations')) { | |
208 if (!config.enabled('retrieve-affiliations')) | |
209 return makeError(response, errors.affiliations_retrieval_not_supported.n); | |
210 | |
211 var affiliations = pubsub.getChild('affiliations'); | |
212 var nodeID = affiliations.getAttribute('node'); | |
213 var affils; | |
214 if (nodeID && nodeID != '') { | |
215 if (!storage.existsNode(nodeID)) | |
216 return makeError(response, errors.node_does_not_exist.n); | |
6 | 217 affils = {}; |
218 affils[nodeID] = storage.getAffiliation(toBareJID(to), nodeID); | |
0 | 219 } else |
220 affils = storage.getAffiliationsFromJID(toBareJID(to)); | |
6 | 221 |
0 | 222 var s = xmpp.stanza('affiliations'); |
223 for (i in affils) | |
224 s.s('affiliation', {node: i, affiliation: affils[i]}); | |
6 | 225 |
0 | 226 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
227 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
228 response.cnode(p); |
0 | 229 |
230 // SECTION 6.3.2 | |
231 } else if (pubsub.getChild('options')) { | |
232 if (!config.enabled('subscription-options')) | |
233 return makeError(response, errors.sub.configure.subscription_options_not_supported.n); | |
234 | |
235 var options = pubsub.getChild('options'); | |
236 | |
237 var nodeID = options.getAttribute('node'); | |
238 if (!nodeID || nodeID == '') | |
239 return makeError(response, errors.nodeid_required.n); | |
240 if (!storage.existsNode(nodeID)) | |
241 return makeError(response, errors.node_does_not_exist.n); | |
242 | |
243 var jid = options.getAttribute('jid'); | |
244 if (!jid) | |
245 return makeError(response, errors.sub.configure.subscriber_jid_required.n); | |
246 if (toBareJID(jid) != toBareJID(to)) | |
247 return makeError(response, errors.sub.configure.insufficient_privileges.n); | |
248 | |
249 var subs = storage.getSubscription(jid, nodeID); | |
250 if (subs == {}) | |
251 return makeError(response, errors.sub.configure.no_such_subscriber.n); | |
252 | |
253 var s = xmpp.stanza('options', {node: nodeID, jid: jid}); | |
254 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); | |
255 var form = forms.build('form', 'subscribe_options', subs.options, true); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
256 s.cnode(form); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
257 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
258 response.cnode(p); |
0 | 259 |
260 // SECTION 6.4 | |
261 } else if (pubsub.getChild('default')) { | |
262 if (!config.enabled('retrieve-default-sub')) | |
263 return makeError(response, errors.sub.default_options.default_subscription_configuration_retrieval_not_supported.n); | |
264 | |
265 var def = pubsub.getChild('default'); | |
266 | |
267 var nodeID = def.getAttribute('node'); | |
268 if (nodeID && !storage.existsNode(nodeID)) | |
269 return makeError(response, errors.node_does_not_exist.n); | |
270 | |
271 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); | |
272 var s; | |
273 if (nodeID) | |
274 s = xmpp.stanza('default', {node: nodeID}); | |
275 else | |
276 s = xmpp.stanza('default'); | |
277 | |
278 var form = forms.build('form', 'subscribe_options', 'default', false); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
279 s.cnode(form); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
280 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
281 response.cnode(p); |
0 | 282 |
283 // SECTION 6.5 | |
284 } else if (pubsub.getChild('items')) { | |
285 if (!config.enabled('retrieve-items')) | |
286 return makeError(response, errors.sub.default_options.node_configuration_not_supported.n); | |
287 | |
288 var items = pubsub.getChild('items'); | |
289 | |
290 var nodeID = items.getAttribute('node'); | |
291 if (!nodeID || nodeID == '') | |
292 return makeError(response, errors.nodeid_required.n); | |
293 if (!storage.existsNode(nodeID)) | |
294 return makeError(response, errors.node_does_not_exist.n); | |
295 | |
6 | 296 var configuration = storage.getConfiguration(nodeID); |
297 if (configuration['pubsub#access_model'] == 'whitelist') { | |
298 var affil = storage.getAffiliation(toBareJID(to), nodeID); | |
299 if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member') | |
300 return makeError(response, errors.pub.publish.insufficient_privileges.n); | |
301 } | |
0 | 302 |
303 var item = []; | |
304 for (var i=0; i<items.children.length; i++) { | |
305 var j = items.children[i]; | |
306 if (j.name == 'item' && j.attr['id'] && j.attr['id'] != '') | |
307 item.push(j.attr['id']); | |
308 } | |
309 | |
310 var max_items = items.getAttribute('max_items'); | |
311 if (max_items) | |
312 max_items = Number (max_items); | |
313 | |
314 if (item.length) { | |
315 var s = xmpp.stanza('items', {node: nodeID}); | |
316 | |
317 for (var i=0; i<item.length; i++) { | |
318 var j = storage.getItem(nodeID, item[i]); | |
319 if (typeof j == 'number') | |
320 return makeError(response, j); | |
10
44889cfb2f8c
Fix getItem method.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
321 if (j == errors.success) |
44889cfb2f8c
Fix getItem method.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
322 continue; |
0 | 323 |
324 var k = xmpp.stanza('item', {id: item[i]}) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
325 k.cnode(j); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
326 s.cnode(k); |
0 | 327 } |
328 } else { | |
329 var s = xmpp.stanza('items', {node: nodeID}); | |
330 | |
331 var j; | |
332 if (max_items) | |
333 j = storage.getLastItem(nodeID, max_items); | |
334 else | |
335 j = storage.getItems(nodeID); | |
336 if (typeof j == 'number') | |
337 return makeError(response, j); | |
338 | |
339 var k = 0; | |
340 for (var i in j) { | |
341 var contentItem = xmpp.stanza('item', {id: i}).t(j[i].content); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
342 s.cnode(contentItem); |
0 | 343 } |
344 } | |
345 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
346 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
347 response.cnode(p); |
0 | 348 } else |
349 return makeError(response, errors.feature_not_implemented.n); | |
350 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) { | |
351 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner'); | |
352 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
353 // SECTION 8.2 |
0 | 354 if (pubsub.getChild('configure')) { |
355 if (!config.enabled('config-node')) | |
356 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
357 | |
358 var nodeID = pubsub.getChild('configure').getAttribute('node'); | |
359 if (!nodeID || nodeID == '') | |
360 return makeError(response, errors.nodeid_required.n); | |
361 if (!storage.existsNode(nodeID)) | |
362 return makeError(response, errors.node_does_not_exist.n); | |
363 | |
364 var affil = storage.getAffiliation(toBareJID(to), nodeID); | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
365 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') |
0 | 366 return makeError(response, errors.pub.publish.insufficient_privileges.n); |
367 | |
368 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
369 var s = xmpp.stanza('configure', {node: nodeID}); | |
370 var form = forms.build('form', 'node_config', 'default', true); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
371 s.cnode(form); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
372 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
373 response.cnode(p); |
0 | 374 |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
375 // SECTION 8.3 |
0 | 376 } else if (pubsub.getChild('default')) { |
377 if (!config.enabled('config-node')) | |
378 return makeError(response, errors.owner.default_options.node_configuration_not_supported.n); | |
379 | |
380 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
381 var s = xmpp.stanza('default'); | |
382 var form = forms.build('node_config', service_configuration.node_config, null, true); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
383 s.cnode(form); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
384 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
385 response.cnode(p); |
0 | 386 |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
387 // SECTION 8.8 |
0 | 388 } else if (pubsub.getChild('subscriptions')) { |
389 if (!config.enabled('manage-subscriptions')) | |
390 return makeError(response, errors.owner.manage_subscriptions.not_supported.n); | |
391 | |
392 var subscriptions = pubsub.getChild('subscriptions'); | |
393 | |
394 var nodeID = subscriptions.getAttribute('node'); | |
395 if (!nodeID || nodeID == '') | |
396 return makeError(response, errors.nodeid_required.n); | |
397 if (!storage.existsNode(nodeID)) | |
398 return makeError(response, errors.node_does_not_exist.n); | |
399 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
400 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
401 if (affil != 'super-owner' && affil != 'owner') |
0 | 402 return makeError(response, errors.forbidden.n); |
403 | |
404 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
405 var s = xmpp.stanza('subscriptions', {node: nodeID}); | |
406 | |
407 var subs = storage.getSubscriptionsFromNodeID(nodeID) | |
408 for (var jid in subs) | |
409 s.s('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid}) | |
410 | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
411 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
412 response.cnode(p); |
0 | 413 |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
414 // SECTION 8.9 |
0 | 415 } else if (pubsub.getChild('affiliations')) { |
416 if (!config.enabled('modify-affiliations')) | |
417 return makeError(response, errors.owner.manage_affiliations.not_supported.n); | |
418 | |
419 var affiliations = pubsub.getChild('affiliations'); | |
420 | |
421 var nodeID = affiliations.getAttribute('node'); | |
422 if (!nodeID || nodeID == '') | |
423 return makeError(response, errors.nodeid_required.n); | |
424 if (!storage.existsNode(nodeID)) | |
425 return makeError(response, errors.node_does_not_exist.n); | |
426 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
427 var affils = storage.getAffiliationsFromNodeID(nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
428 var affil = affils[toBareJID(to)]; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
429 if (affil != 'super-owner' && affil != 'owner') |
0 | 430 return makeError(response, errors.owner.manage_affiliations.retrieve_list.entity_is_not_an_owner.n); |
431 | |
432 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
433 var s = xmpp.stanza('affiliations', {node: nodeID}); | |
434 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
435 for (var jid in affils) |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
436 s.s('affiliation', {jid: jid, affiliation: affils[jid]}) |
0 | 437 |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
438 p.cnode(s); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
439 response.cnode(p); |
0 | 440 } else |
441 return makeError(response, errors.feature_not_implemented.n); | |
442 } else | |
443 return makeError(response, errors.feature_not_implemented.n); | |
444 } else if (type == 'set') { | |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
445 if (stanza.getChild('command', 'http://jabber.org/protocol/commands')) { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
446 // XEP-0050: Ad-Hoc Commands |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
447 var command = stanza.getChild('command', 'http://jabber.org/protocol/commands'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
448 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
449 var action = command.getAttribute('action'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
450 if (action != 'execute') |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
451 return makeError(response, errors.bad_request.n); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
452 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
453 var node = command.getAttribute('node'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
454 if (node == 'ping') { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
455 var cmd = xmpp.stanza('command', {xmlns: 'http://jabber.org/protocol/commands', |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
456 // sessionid: 'list:20020923T213616Z-700', |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
457 node: node, |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
458 'status': 'completed'}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
459 .c('note', {type: 'info'}).t('pong'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
460 response.cnode(cmd); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
461 } else if (node == 'reload') { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
462 storage.load(); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
463 response.c('command', {xmlns: 'http://jabber.org/protocol/commands', |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
464 node: node, |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
465 'status': 'completed'}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
466 .c('note', {type: 'info'}).t('The server has correctly reloaded.'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
467 } else |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
468 return makeError(response, errors.bad_request.n); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
469 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) { |
0 | 470 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub'); |
471 | |
472 // SECTION 6.1 | |
473 if (pubsub.getChild('subscribe')) { | |
474 if (!config.enabled('subscribe')) | |
475 return makeError(response, errors.sub.subscribe.not_supported.n); | |
476 | |
477 var subscribe = pubsub.getChild('subscribe'); | |
478 | |
479 var nodeID = subscribe.getAttribute('node'); | |
480 if (!nodeID || nodeID == '') | |
481 return makeError(response, errors.nodeid_required.n); | |
482 if (!storage.existsNode(nodeID)) | |
483 return makeError(response, errors.node_does_not_exist.n); | |
484 | |
485 var configuration = storage.getConfiguration(nodeID); | |
486 if (!configuration['pubsub#subscribe']) | |
487 return makeError(response, errors.sub.subscribe.not_supported.n); | |
488 | |
489 var affil = storage.getAffiliation(toBareJID(to), nodeID); | |
490 if (affil == 'publish-only' || affil == 'outcast') | |
491 return makeError(response, errors.pub.publish.insufficient_privileges.n); | |
492 | |
493 var jid = subscribe.getAttribute('jid'); | |
494 if (!jid || toBareJID(jid) != toBareJID(to)) | |
495 return makeError(response, errors.sub.subscribe.jids_do_not_match.n); | |
496 | |
497 // SECTION 6.3.7 | |
498 var options = pubsub.getChild('options'); | |
499 if (options && config.enabled('subscription-options')) { | |
500 if (options.getAttribute('node') || options.getAttribute('jid')) | |
501 return makeError(response, errors.bad_request.n); | |
502 | |
503 var x = options.getChild('x', 'jabber:x:data'); | |
504 if (!x || x.getAttribute('type') != 'submit') | |
505 return makeError(response, errors.bad_request.n); | |
506 | |
507 var form = forms.parse(x, true); | |
508 if (typeof form == 'number') | |
509 return makeError(response, form); | |
510 | |
511 var conf = form; | |
512 } | |
513 | |
514 var subID; | |
515 if (configuration['pubsub#access_model'] == 'open') { | |
516 subID = storage.subscribe(nodeID, jid, 'subscribe', conf); | |
517 if (typeof subID == 'number') | |
518 return makeError(response, subID); | |
519 } else if (configuration['pubsub#access_model'] == 'authorize') { | |
520 subID = storage.subscribe(nodeID, jid, 'pending', conf); | |
521 if (typeof subID == 'number') | |
522 return makeError(response, subID); | |
523 } else if (configuration['pubsub#access_model'] == 'whitelist') { | |
524 var affil = storage.getAffiliation(jid, nodeID); | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
525 if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member') |
0 | 526 return makeError(response, errors.sub.subscribe.not_on_whitelist.n); |
527 | |
528 subID = storage.subscribe(nodeID, jid, conf); | |
529 if (typeof subID == 'number') | |
530 return makeError(response, subID); | |
531 } | |
532 | |
533 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
534 .c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type}); | |
535 | |
536 if (conf) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
537 response.cnode(options); |
0 | 538 |
539 if (config.enabled('get-pending')) { | |
540 var affiliates = storage.getAffiliationsFromNodeID(nodeID); | |
541 var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168 | |
542 for (var i in affiliates) { | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
543 if (affiliates[i] == 'super-owner' || affiliates[i] == 'owner') { |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
544 var message = xmpp.message({to: i}).cnode(form); |
0 | 545 conn.send(message); |
546 } | |
547 } | |
548 } | |
549 | |
550 if (config.enabled('last-published')) { | |
551 var last = storage.getLastItem(nodeID); | |
552 if (typeof last != 'number') { | |
553 var item = storage.getItem(nodeID, last); | |
554 if (typeof item != 'number') { | |
555 var attr = {}; | |
556 attr[last] = {content: item}; | |
557 sendNotifs(jid, 'items', nodeID, attr); | |
558 } | |
559 } | |
560 } | |
561 | |
562 // SECTION 6.2 | |
563 } else if (pubsub.getChild('unsubscribe')) { | |
564 if (!config.enabled('subscribe')) | |
565 return makeError(response, errors.sub.subscribe.not_supported.n); | |
566 | |
567 var unsubscribe = pubsub.getChild('unsubscribe'); | |
568 var nodeID = unsubscribe.getAttribute('node'); | |
569 if (!nodeID || nodeID == '') | |
570 return makeError(response, errors.nodeid_required.n); | |
571 if (!storage.existsNode(nodeID)) | |
572 return makeError(response, errors.node_does_not_exist.n); | |
573 | |
574 var jid = unsubscribe.getAttribute('jid'); | |
575 if (!jid || toBareJID(jid) != toBareJID(to)) | |
576 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n); | |
577 | |
578 var subID = storage.subscribe(nodeID, jid, 'none'); | |
579 if (typeof subID == 'number') | |
580 return makeError(response, subID); | |
581 | |
582 // SECTIONS 6.3.5 | |
583 } else if (pubsub.getChild('options')) { | |
584 if (!config.enabled('subscription-options')) | |
585 return makeError(response, errors.sub.subscribe.not_supported.n); | |
586 | |
587 var options = pubsub.getChild('options'); | |
588 | |
589 var nodeID = unsubscribe.getAttribute('node'); | |
590 if (!nodeID || nodeID == '') | |
591 return makeError(response, errors.nodeid_required.n); | |
592 if (!storage.existsNode(nodeID)) | |
593 return makeError(response, errors.node_does_not_exist.n); | |
594 | |
595 var jid = unsubscribe.getAttribute('jid'); | |
596 if (!jid || toBareJID(jid) != toBareJID(to)) | |
597 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n); | |
598 | |
599 var x = options.getChild('x', 'jabber:x:data'); | |
600 if (!x || x.getAttribute(type) != 'submit') | |
601 return makeError(response, errors.bad_request); | |
602 | |
603 var form = forms.parse(x, true); | |
604 if (typeof form == 'number') | |
605 return makeError(response, form); | |
606 | |
607 var set = storage.configureSubscription(nodeID, jid, form); | |
608 if (typeof form == 'number') | |
609 return makeError(response, form); | |
610 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
611 // SECTION 7.1 |
0 | 612 } else if (pubsub.getChild('publish')) { |
613 if (!config.enabled('publish')) | |
614 return makeError(response, errors.pub.publish.item_publication_not_supported.n); | |
615 | |
616 var publish = pubsub.getChild('publish'); | |
617 var nodeID = publish.getAttribute('node'); | |
618 if (!nodeID || nodeID == '') | |
619 return makeError(response, errors.nodeid_required.n); | |
620 | |
6 | 621 var autocreate = false; |
622 if (!storage.existsNode(nodeID)) { | |
623 if (config.enabled('auto-create')) | |
624 autocreate = true; | |
625 else | |
626 return makeError(response, errors.node_does_not_exist.n); | |
627 } | |
628 | |
0 | 629 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
6 | 630 if (typeof affil == 'number' && affil != errors.node_does_not_exist.n) |
0 | 631 return makeError(response, affil); |
6 | 632 if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only') |
0 | 633 return makeError(response, errors.forbidden.n); |
634 | |
635 var item = publish.getChild('item'); | |
636 var itemID = item.getAttribute('id'); | |
637 if (!config.enabled('item-ids') && itemID) | |
638 return makeError(response, errors.itemid_required.n); | |
639 itemID = itemID? itemID: utils.makeRandomId(); | |
640 | |
641 if (item.tags.length != 1) | |
642 return makeError(response, errors.pub.publish.bad_payload.n); | |
643 | |
644 var conf = storage.getConfiguration(nodeID); | |
645 var publishOptions = pubsub.getChild('publish-options'); | |
646 if (publishOptions && config.enabled('publish-options')) { | |
647 var x = publishOptions.getChild('x', 'jabber:x:data'); | |
648 if (!x || x.getAttribute('type') != 'submit') | |
649 return makeError(response, errors.bad_request.n); | |
650 | |
651 var form = forms.parse(x, true); | |
652 if (form.access_model != conf['pubsub#access_model'] && !autocreate) | |
653 return makeError(response, errors.pub.configuration.precondition.n); | |
654 } | |
655 | |
656 if (!config.enabled('persistent-items')) { | |
657 var notifs = storage.purgeNode(nodeID); | |
658 if (typeof notifs == 'number') | |
659 return makeError(response, r); | |
660 } | |
661 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
662 if (autocreate) { |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
663 if (!form) |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
664 form = {}; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
665 form['pubsub#creator'] = toBareJID(to); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
666 |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
667 var r = storage.createNode(nodeID, form); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
668 if (typeof r == 'number') |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
669 return makeError(response, r); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
670 } |
0 | 671 |
672 var content = item.getChild(); | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
673 |
0 | 674 subscribers = storage.setItem(nodeID, itemID, content); |
675 if (typeof subscribers == 'number') | |
676 return makeError(response, subscribers); | |
677 | |
678 var attrs = {}; | |
679 if (content) | |
680 attrs[itemID] = {content: content}; | |
681 else | |
682 attrs[itemID] = {}; | |
683 sendNotifs(subscribers, 'items', nodeID, attrs); | |
684 | |
685 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
686 .c('publish', {node: nodeID}) | |
687 .c('item', {id: itemID}); | |
688 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
689 // SECTION 7.2 |
0 | 690 } else if (pubsub.getChild('retract')) { |
691 if (!config.enabled('retract-items')) | |
692 return makeError(response, errors.pub.retract.item_deletion_not_supported.n); | |
693 | |
694 var retract = pubsub.getChild('retract'); | |
695 | |
696 var nodeID = retract.getAttribute('node'); | |
697 if (!nodeID || nodeID == '') | |
698 return makeError(response, errors.nodeid_required.n); | |
699 if (!storage.existsNode(nodeID)) | |
700 return makeError(response, errors.node_does_not_exist.n); | |
701 | |
702 var item = retract.getChild('item'); | |
703 if (!item) | |
704 return makeError(response, errors.pub.retract.item_or_itemid_required.n); | |
705 | |
706 var itemID = item.getAttribute('id') | |
707 if (!itemID || itemID == '') | |
708 return makeError(response, errors.pub.retract.item_or_itemid_required.n); | |
709 | |
6 | 710 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
711 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') | |
712 return makeError(response, errors.forbidden.n); | |
713 | |
0 | 714 var subscribers = storage.deleteItem(nodeID, itemID); |
715 if (typeof subscribers == 'number') | |
716 return makeError(response, subscribers); | |
717 | |
718 var attrs = {}; | |
719 attrs[itemID] = {}; | |
720 sendNotifs(subscribers, 'items', nodeID, attrs, 'retract') | |
721 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
722 // SECTION 8.1 |
0 | 723 } else if (pubsub.getChild('create')) { |
724 if (!config.enabled('create-nodes')) | |
725 return makeError(response, errors.owner.create.node_creation_not_supported.n); | |
726 | |
727 var instant = false; | |
728 | |
729 var nodeID = pubsub.getChild('create').getAttribute('node'); | |
730 if (!nodeID || nodeID == '') { | |
4
4c93e76fa371
Ensure that the creator of a node is the default owner; don’t crash
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
731 if (!config.enabled('instant-nodes')) |
0 | 732 return makeError(response, errors.owner.create.instant_nodes_not_supported.n); |
733 nodeID = utils.makeRandomId(); | |
734 instant = true; | |
735 } | |
736 if (storage.existsNode(nodeID)) | |
737 return makeError(response, errors.nodeid_already_exists.n); | |
738 | |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
739 var bare = toBareJID(to); |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
740 var right = false; |
7
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
741 |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
742 // Check for super-owner |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
743 for (var i in config.owner) |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
744 if (config.owner[i] == bare) |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
745 right = true; |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
746 |
7
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
747 // Check for authorized user |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
748 for (var i in config.allowCreateNode) |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
749 if (config.allowCreateNode[i].exec(bare)) |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
750 right = true; |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
751 |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
752 if (!right) |
0 | 753 return makeError(response, errors.forbidden.n); |
754 | |
755 var configure = pubsub.getChild('configure'); | |
756 if (configure && config.enabled('create-and-configure')) { | |
757 if (!config.enabled('config-node')) | |
758 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
759 | |
760 if (configure.getAttribute('node')) | |
761 return makeError(response, errors.bad_request.n); | |
762 | |
763 var x = configure.getChild('x', 'jabber:x:data'); | |
764 if (!x || x.getAttribute('type') != 'submit') | |
765 return makeError(response, errors.bad_request.n); | |
766 | |
767 var form = forms.parse(x, true); | |
768 if (typeof form == 'number') | |
769 return makeError(response, form); | |
770 | |
771 var conf = form; | |
772 } | |
773 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
774 if (!conf) |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
775 conf = {}; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
776 conf['pubsub#creator'] = toBareJID(to); |
0 | 777 var r = storage.createNode(nodeID, conf); |
778 if (typeof r == 'number') | |
779 return makeError(response, r); | |
780 | |
781 if (instant) | |
782 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
783 .c('create', {node: nodeID}); | |
784 } else | |
785 return makeError(response, errors.feature_not_implemented.n); | |
786 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) { | |
787 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner'); | |
788 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
789 // SECTION 8.2.4 |
0 | 790 if (pubsub.getChild('configure')) { |
791 if (!config.enabled('config-node')) | |
792 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
793 | |
794 var nodeID = configure.getAttribute('node'); | |
795 if (!nodeID) | |
796 return makeError(response, errors.nodeid_required.n); | |
797 if (!storage.existsNode(nodeID)) | |
798 return makeError(response, errors.node_does_not_exist.n); | |
799 | |
800 var affil = storage.getAffiliation(toBareJID(to), nodeID); | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
801 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') |
0 | 802 return makeError(response, errors.forbidden.n); |
803 | |
804 var x = configure.getChild('x', 'jabber:x:data'); | |
805 if (!x || x.getAttribute(type) != 'submit') | |
806 return makeError(response, errors.bad_request.n); | |
807 | |
808 var form = forms.parse(x, true); | |
809 if (typeof form == 'number') | |
810 return makeError(response, form); | |
811 | |
812 var conf = form; | |
813 | |
814 var set = storage.configure(nodeID, conf); | |
815 if (typeof set == 'number') | |
816 return makeError(response, set); | |
817 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
818 // SECTION 8.4 |
0 | 819 } else if (pubsub.getChild('delete')) { |
820 if (!config.enabled('delete-nodes')) | |
821 return makeError(response, errors.feature_not_implemented.n); //XXX | |
822 | |
823 var del = pubsub.getChild('delete'); | |
824 | |
825 var nodeID = del.getAttribute('node'); | |
826 if (!nodeID) | |
827 return makeError(response, errors.nodeid_required.n); | |
828 if (!storage.existsNode(nodeID)) | |
829 return makeError(response, errors.node_does_not_exist.n); | |
830 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
831 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
832 if (affil != 'super-owner' && affil != 'owner') |
0 | 833 return makeError(response, errors.forbidden.n); |
834 | |
835 var notifs = storage.deleteNode(nodeID); | |
836 if (typeof notifs == 'number') | |
837 return makeError(response, r); | |
838 | |
839 sendNotifs(notifs, 'delete', nodeID); | |
840 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
841 // SECTION 8.5 |
0 | 842 } else if (pubsub.getChild('purge')) { |
843 if (!config.enabled('purge-nodes')) | |
844 return makeError(response, errors.owner.purge.node_purging_not_supported.n); //XXX | |
845 | |
846 var purge = pubsub.getChild('purge'); | |
847 | |
848 var nodeID = purge.getAttribute('node'); | |
849 if (!nodeID) | |
850 return makeError(response, errors.nodeid_required.n); | |
851 if (!storage.existsNode(nodeID)) | |
852 return makeError(response, errors.node_does_not_exist.n); | |
853 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
854 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
855 if (affil != 'super-owner' && affil != 'owner') |
0 | 856 return makeError(response, errors.forbidden.n); |
857 | |
858 if (!config.enabled('persistent-items')) //FIXME: autre condition, supporté par le node | |
859 return makeError(response, errors.owner.purge.node_does_not_persist_items.n); | |
860 | |
861 var notifs = storage.purgeNode(nodeID); | |
862 if (typeof notifs == 'number') | |
863 return makeError(response, r); | |
864 | |
865 sendNotifs(notifs, 'purge', nodeID); | |
866 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
867 // SECTION 8.8.2 |
0 | 868 } else if (pubsub.getChild('subscriptions')) { |
869 if (!config.enabled('manage-subscriptions')) | |
870 return makeError(response, errors.owner.manage_subscriptions.not_supported.n); //XXX | |
871 | |
872 var subscriptions = pubsub.getChild('subscriptions'); | |
873 | |
874 var nodeID = subscriptions.getAttribute('node'); | |
875 if (!nodeID) | |
876 return makeError(response, errors.nodeid_required.n); | |
877 if (!storage.existsNode(nodeID)) | |
878 return makeError(response, errors.node_does_not_exist.n); | |
879 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
880 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
881 if (affil != 'super-owner' && affil != 'owner') |
0 | 882 return makeError(response, errors.forbidden.n); |
883 | |
884 var e = false; | |
885 for (i in subscriptions.tags) { | |
886 var jid = subscriptions.tags[i].getAttribute('jid'); | |
887 var subscription = subscriptions.tags[i].getAttribute('subscription'); | |
888 | |
889 var set = storage.subscribe(nodeID, jid, subscription); | |
890 if (typeof set == 'number') | |
891 e = true; | |
892 else { | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
893 // SECTION 8.8.4 |
0 | 894 sendNotifs(jid, 'subscription', nodeID, {jid: jid, subscription: subscription}); |
895 subscriptions.tags.splice(i, 1); | |
896 } | |
897 } | |
898 | |
899 if (e) | |
900 return makeError(response, errors.owner.manage_subscriptions.modify.multiple_simultaneous_modifications.n, pubsub); | |
901 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
902 // SECTION 8.9.2 |
0 | 903 } else if (pubsub.getChild('affiliations')) { |
904 if (!config.enabled('modify-affiliations')) | |
905 return makeError(response, errors.owner.manage_affiliations.not_supported.n); //XXX | |
906 | |
907 var affiliations = pubsub.getChild('affiliations'); | |
908 | |
909 var nodeID = affiliations.getAttribute('node'); | |
910 if (!nodeID) | |
911 return makeError(response, errors.nodeid_required.n); | |
912 if (!storage.existsNode(nodeID)) | |
913 return makeError(response, errors.node_does_not_exist.n); | |
914 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
915 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
916 if (affil != 'super-owner' && affil != 'owner') |
0 | 917 return makeError(response, errors.forbidden.n); |
918 | |
919 var e = false; | |
920 for (i in affiliations.children) { | |
921 var jid = affiliations.children[i].getAttribute('jid'); | |
922 var affiliation = affiliations.children[i].getAttribute('affiliation'); | |
923 | |
924 var set = storage.setAffiliation(nodeID, jid, affiliation); | |
925 if (typeof set == 'number') | |
926 e = true; | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
927 else { |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
928 // SECTION 8.9.4 |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
929 sendNotifs(jid, 'affiliations', nodeID, {jid: jid, affiliation: affiliation}); |
0 | 930 affiliations.children.splice(i, 1); |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
931 } |
0 | 932 } |
933 | |
934 if (e) | |
935 return makeError(response, errors.owner.manage_affiliations.modify.multiple_simultaneous_modifications.n, pubsub); | |
936 } else | |
937 return makeError(response, errors.feature_not_implemented.n); | |
938 } else | |
939 return makeError(response, errors.feature_not_implemented.n); | |
940 } else | |
941 return makeError(response, errors.feature_not_implemented.n); | |
942 conn.send(response); | |
943 } | |
944 | |
945 function onMessage(stanza) { | |
946 var from = stanza.getAttribute('to'); | |
947 var to = stanza.getAttribute('from'); | |
948 var id = stanza.getAttribute('id'); | |
949 | |
950 var response; | |
951 if (id) | |
952 response = xmpp.message({to: to, from: from, id: id}); | |
953 else | |
954 response = xmpp.message({to: to, from: from}); | |
955 | |
956 var x = stanza.getChild('x', 'jabber:x:data'); | |
957 if (x) { | |
958 var form = forms.parse(x); | |
959 if (form.type == 'submit' && form.fields.FORM_TYPE.value == 'subscribe_authorization') { | |
960 if (form.fields.subid && form.fields.subid.value) | |
961 var subID = form.fields.subid.value; | |
962 if (form.fields.node && form.fields.node.value) | |
963 var nodeID = form.fields.node.value; | |
964 if (form.fields.subscriber_jid && form.fields.subscriber_jid.value) | |
965 var jid = form.fields.subscriber_jid.value; | |
966 if (form.fields.allow && form.fields.allow.value) | |
967 var allow = form.fields.allow.value; | |
968 | |
969 var type = allow? 'subscribed': 'none'; | |
970 var set = storage.subscribe(nodeID, jid, type) | |
971 //if (set.subid != subID) //TODO: support the multi-subscribe feature | |
972 sendNotifs(jid, 'subscription', nodeID, {jid: jid, subscription: type}); | |
973 } else | |
974 return makeError(response, errors.feature_not_implemented.n); | |
975 } else | |
976 return makeError(response, errors.feature_not_implemented.n); | |
977 conn.send(response) | |
978 } | |
979 | |
980 function onPresence(stanza) { | |
981 var from = stanza.getAttribute('to'); | |
982 var to = stanza.getAttribute('from'); | |
983 var id = stanza.getAttribute('id'); | |
984 | |
985 var response; | |
986 if (id) | |
987 response = xmpp.presence({to: to, from: from, id: id}); | |
988 else | |
989 response = xmpp.presence({to: to, from: from}); | |
990 | |
991 makeError(response, errors.feature_not_implemented.n); | |
992 } | |
993 | |
994 function makeError(response, errorNumber, payload) { | |
995 response.attr.type = 'error'; | |
996 if (payload) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
997 response.cnode(payload); |
0 | 998 |
999 var e = errors.reverse[errorNumber]; | |
1000 var error = xmpp.stanza('error', {type: e.type}); | |
1001 | |
1002 error.s(e.error, {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}) | |
1003 | |
1004 if (e.reason) { | |
1005 if (e.feature) | |
1006 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors', feature: e.feature}); | |
1007 else | |
1008 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'}); | |
1009 } | |
1010 | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1011 response.cnode(error); |
0 | 1012 conn.send(response); |
1013 } | |
1014 | |
1015 function sendNotifs(notifs, type, nodeID, a1, a2) { | |
1016 var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'}); | |
1017 | |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1018 if (type == 'affiliations') { |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1019 ev.attr.xmlns = 'http://jabber.org/protocol/pubsub'; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1020 |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1021 var args = {}; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1022 for (i in a1) { |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1023 var attr = a1[i]; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1024 if (i == 'affiliation') |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1025 args.affiliation = attr; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1026 else if (i == 'jid') |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1027 args.jid = attr; |
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1028 } |
3
80e607c0b39e
Make affiliation notifications working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
1029 var affiliations = xmpp.stanza('affiliations', {node: nodeID}) |
4
4c93e76fa371
Ensure that the creator of a node is the default owner; don’t crash
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3
diff
changeset
|
1030 .c('affiliation', args); |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1031 ev.cnode(affiliations); |
1
c2954a9e5665
Add a super-owner that has power over all nodes; add support for
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
1032 } else if (type == 'collection') { |
0 | 1033 var collection = xmpp.stanza('collection', {node: nodeID}); |
1034 if (a1 == 'associate') | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1035 collection.cnode('associate', {node: nodeID}); |
0 | 1036 else |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1037 collection.cnode('disassociate', {node: nodeID}); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1038 ev.cnode(collection); |
0 | 1039 } else if (type == 'configuration') { |
1040 if (!config.enabled('config-node')) { | |
1041 _('Error #4', 41) | |
1042 return; | |
1043 } | |
1044 | |
1045 var configuration = xmpp.stanza('configuration', {node: nodeID}); | |
1046 if (a1) { | |
1047 var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID)); | |
1048 if (x) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1049 configuration.cnode(x); //TODO: voir exemple 150 |
0 | 1050 } |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1051 ev.cnode(configuration); |
0 | 1052 } else if (type == 'delete') { |
1053 var del = xmpp.stanza('delete', {node: nodeID}); | |
1054 if (a1) | |
1055 del.c('redirect', {uri: a1}); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1056 ev.cnode(del); |
0 | 1057 } else if (type == 'items') { |
1058 var items = xmpp.stanza(type, {node: nodeID}); | |
1059 if (a2 == 'retract') | |
1060 for (var i in a1) | |
1061 items.s('retract', {id: i}); | |
1062 else { | |
1063 for (var i in a1) { | |
1064 var item = a1[i]; | |
1065 var args = {}; | |
1066 if (i != '') | |
1067 args.id = i; | |
1068 if (item.node) | |
1069 args.node = item.node; | |
1070 if (item.publisher) | |
1071 args.publisher = item.publisher; | |
1072 var it = xmpp.stanza('item', args); | |
1073 if (item.content) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1074 it.cnode(item.content); |
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1075 items.cnode(it); |
0 | 1076 } |
1077 } | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1078 ev.cnode(items); |
0 | 1079 } else if (type == 'purge') { |
1080 ev.c('purge', {node: nodeID}); | |
1081 } else if (type == 'subscription') { | |
1082 if (!config.enabled('subscription-notifications')) | |
1083 return; | |
1084 | |
1085 var args = {node: nodeID}; | |
1086 for (i in a1) { | |
1087 var attr = a1[i]; | |
1088 if (i == 'subscription') { | |
1089 if (attr == 'none' || attr == 'pending' || attr == 'subscribed' || attr == 'unconfigured') | |
1090 args[i] = attr; | |
1091 else { | |
1092 _('Error #3', 41) | |
1093 return; | |
1094 } | |
1095 } else if (i == 'jid' || i == 'subid') | |
1096 args[i] = attr; | |
1097 else if (i == 'expiry') | |
1098 args[i] = attr.toString(); | |
1099 } | |
1100 if (!args.jid || args.jid == '') { | |
1101 _('Error #2', 41) | |
1102 return; | |
1103 } | |
1104 var sub = xmpp.stanza('subscription', args); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1105 ev.cnode(sub); |
0 | 1106 } else { |
1107 _('Error #1', 41) | |
1108 return; | |
1109 } | |
1110 | |
1111 var subs; | |
1112 if (typeof notifs == 'string') { | |
1113 subs = {}; | |
1114 subs[notifs] = storage.getSubscription(notifs, nodeID); | |
1115 } else | |
1116 subs = notifs; | |
1117 | |
1118 for (var i in subs) { | |
1119 var sub = subs[i]; | |
1120 | |
1121 if (sub.options) { | |
1122 if (typeof sub.options['pubsub#deliver'] != 'undefined' && !sub.options['pubsub#deliver']) | |
1123 continue; | |
1124 | |
1125 if (typeof sub.options['pubsub#digest'] != 'undefined' && sub.options['pubsub#digest']) { | |
1126 if (!sub.digest) | |
1127 sub.digest = []; | |
1128 sub.digest.push(ev) | |
1129 | |
1130 if (sub.digestTimeout) | |
1131 continue; | |
1132 | |
1133 var freq; | |
1134 if (typeof sub.options['pubsub#digest_frequency'] == 'undefined') | |
1135 freq = 0; | |
1136 else | |
1137 freq = parseInt(sub.options['pubsub#digest_frequency']); | |
1138 | |
1139 if (freq == 0) | |
1140 freq = 24*60*60*1000; | |
1141 | |
1142 setTimeout(sendDigest, freq, notifs[i], nodeID); | |
1143 sub.digestTimeout = true; | |
1144 continue; | |
1145 } | |
1146 } | |
1147 | |
1148 var message = xmpp.message({to: i, from: componentJID, id: conn.getUniqueId()}); | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1149 message.cnode(ev); |
0 | 1150 conn.send(message); |
1151 } | |
1152 } | |
1153 | |
1154 function sendDigest(jid, nodeID) { | |
1155 var sub = storage.getSubscription(jid, nodeID); | |
1156 if (sub.digestTimeout) | |
1157 sub.digestTimeout = false; | |
1158 | |
1159 var message = xmpp.message({to: jid, from: componentJID, id: conn.getUniqueId()}); | |
1160 for (var i in sub.digest) | |
8
efe8dbd34780
Rename cx method to cnode and define it if xmpp.js didn’t.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
1161 message.cnode(sub.digest[i]); |
0 | 1162 conn.send(message); |
1163 } | |
1164 | |
1165 conn.connect(componentJID, componentPassword, function (status, condition) { | |
1166 if (status == xmpp.Status.CONNECTED) { | |
1167 conn.addHandler(onMessage, null, 'message', null, null, null); | |
1168 conn.addHandler(onIq, null, 'iq', null, null, null); | |
1169 conn.addHandler(onPresence, null, 'presence', null, null, null); | |
1170 | |
1171 if (process.argv.length >= 3) | |
1172 storage.load(process.argv[2]); | |
1173 else | |
1174 storage.load(); | |
1175 | |
1176 var stdin = process.openStdin(); | |
1177 stdin.setEncoding('utf8'); | |
1178 stdin.addListener('data', storage.debug); | |
1179 } else | |
1180 conn.log(xmpp.LogLevel.DEBUG, 'New connection status: ' + status + (condition? (' ('+condition+')'): '')); | |
1181 }); |