Mercurial > psgxs
annotate psgxs.js @ 9:a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Fri, 27 Aug 2010 00:39:03 +0200 |
parents | efe8dbd34780 |
children | 44889cfb2f8c |
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); | |
321 | |
322 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
|
323 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
|
324 s.cnode(k); |
0 | 325 } |
326 } else { | |
327 var s = xmpp.stanza('items', {node: nodeID}); | |
328 | |
329 var j; | |
330 if (max_items) | |
331 j = storage.getLastItem(nodeID, max_items); | |
332 else | |
333 j = storage.getItems(nodeID); | |
334 if (typeof j == 'number') | |
335 return makeError(response, j); | |
336 | |
337 var k = 0; | |
338 for (var i in j) { | |
339 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
|
340 s.cnode(contentItem); |
0 | 341 } |
342 } | |
343 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
|
344 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
|
345 response.cnode(p); |
0 | 346 } else |
347 return makeError(response, errors.feature_not_implemented.n); | |
348 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) { | |
349 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner'); | |
350 | |
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
|
351 // SECTION 8.2 |
0 | 352 if (pubsub.getChild('configure')) { |
353 if (!config.enabled('config-node')) | |
354 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
355 | |
356 var nodeID = pubsub.getChild('configure').getAttribute('node'); | |
357 if (!nodeID || nodeID == '') | |
358 return makeError(response, errors.nodeid_required.n); | |
359 if (!storage.existsNode(nodeID)) | |
360 return makeError(response, errors.node_does_not_exist.n); | |
361 | |
362 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
|
363 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') |
0 | 364 return makeError(response, errors.pub.publish.insufficient_privileges.n); |
365 | |
366 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
367 var s = xmpp.stanza('configure', {node: nodeID}); | |
368 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
|
369 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
|
370 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
|
371 response.cnode(p); |
0 | 372 |
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
|
373 // SECTION 8.3 |
0 | 374 } else if (pubsub.getChild('default')) { |
375 if (!config.enabled('config-node')) | |
376 return makeError(response, errors.owner.default_options.node_configuration_not_supported.n); | |
377 | |
378 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
379 var s = xmpp.stanza('default'); | |
380 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
|
381 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
|
382 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
|
383 response.cnode(p); |
0 | 384 |
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
|
385 // SECTION 8.8 |
0 | 386 } else if (pubsub.getChild('subscriptions')) { |
387 if (!config.enabled('manage-subscriptions')) | |
388 return makeError(response, errors.owner.manage_subscriptions.not_supported.n); | |
389 | |
390 var subscriptions = pubsub.getChild('subscriptions'); | |
391 | |
392 var nodeID = subscriptions.getAttribute('node'); | |
393 if (!nodeID || nodeID == '') | |
394 return makeError(response, errors.nodeid_required.n); | |
395 if (!storage.existsNode(nodeID)) | |
396 return makeError(response, errors.node_does_not_exist.n); | |
397 | |
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
|
398 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
|
399 if (affil != 'super-owner' && affil != 'owner') |
0 | 400 return makeError(response, errors.forbidden.n); |
401 | |
402 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
403 var s = xmpp.stanza('subscriptions', {node: nodeID}); | |
404 | |
405 var subs = storage.getSubscriptionsFromNodeID(nodeID) | |
406 for (var jid in subs) | |
407 s.s('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid}) | |
408 | |
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
|
409 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
|
410 response.cnode(p); |
0 | 411 |
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
|
412 // SECTION 8.9 |
0 | 413 } else if (pubsub.getChild('affiliations')) { |
414 if (!config.enabled('modify-affiliations')) | |
415 return makeError(response, errors.owner.manage_affiliations.not_supported.n); | |
416 | |
417 var affiliations = pubsub.getChild('affiliations'); | |
418 | |
419 var nodeID = affiliations.getAttribute('node'); | |
420 if (!nodeID || nodeID == '') | |
421 return makeError(response, errors.nodeid_required.n); | |
422 if (!storage.existsNode(nodeID)) | |
423 return makeError(response, errors.node_does_not_exist.n); | |
424 | |
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
|
425 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
|
426 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
|
427 if (affil != 'super-owner' && affil != 'owner') |
0 | 428 return makeError(response, errors.owner.manage_affiliations.retrieve_list.entity_is_not_an_owner.n); |
429 | |
430 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); | |
431 var s = xmpp.stanza('affiliations', {node: nodeID}); | |
432 | |
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
|
433 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
|
434 s.s('affiliation', {jid: jid, affiliation: affils[jid]}) |
0 | 435 |
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
|
436 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
|
437 response.cnode(p); |
0 | 438 } else |
439 return makeError(response, errors.feature_not_implemented.n); | |
440 } else | |
441 return makeError(response, errors.feature_not_implemented.n); | |
442 } else if (type == 'set') { | |
9
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
443 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
|
444 // XEP-0050: Ad-Hoc Commands |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
445 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
|
446 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
447 var action = command.getAttribute('action'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
448 if (action != 'execute') |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
449 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
|
450 |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
451 var node = command.getAttribute('node'); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
452 if (node == 'ping') { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
453 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
|
454 // sessionid: 'list:20020923T213616Z-700', |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
455 node: node, |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
456 'status': 'completed'}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
457 .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
|
458 response.cnode(cmd); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
459 } else if (node == 'reload') { |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
460 storage.load(); |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
461 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
|
462 node: node, |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
463 'status': 'completed'}) |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
464 .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
|
465 } else |
a6429f48e403
First implementation of XEP-0050 (ad-hoc commands).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
8
diff
changeset
|
466 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
|
467 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) { |
0 | 468 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub'); |
469 | |
470 // SECTION 6.1 | |
471 if (pubsub.getChild('subscribe')) { | |
472 if (!config.enabled('subscribe')) | |
473 return makeError(response, errors.sub.subscribe.not_supported.n); | |
474 | |
475 var subscribe = pubsub.getChild('subscribe'); | |
476 | |
477 var nodeID = subscribe.getAttribute('node'); | |
478 if (!nodeID || nodeID == '') | |
479 return makeError(response, errors.nodeid_required.n); | |
480 if (!storage.existsNode(nodeID)) | |
481 return makeError(response, errors.node_does_not_exist.n); | |
482 | |
483 var configuration = storage.getConfiguration(nodeID); | |
484 if (!configuration['pubsub#subscribe']) | |
485 return makeError(response, errors.sub.subscribe.not_supported.n); | |
486 | |
487 var affil = storage.getAffiliation(toBareJID(to), nodeID); | |
488 if (affil == 'publish-only' || affil == 'outcast') | |
489 return makeError(response, errors.pub.publish.insufficient_privileges.n); | |
490 | |
491 var jid = subscribe.getAttribute('jid'); | |
492 if (!jid || toBareJID(jid) != toBareJID(to)) | |
493 return makeError(response, errors.sub.subscribe.jids_do_not_match.n); | |
494 | |
495 // SECTION 6.3.7 | |
496 var options = pubsub.getChild('options'); | |
497 if (options && config.enabled('subscription-options')) { | |
498 if (options.getAttribute('node') || options.getAttribute('jid')) | |
499 return makeError(response, errors.bad_request.n); | |
500 | |
501 var x = options.getChild('x', 'jabber:x:data'); | |
502 if (!x || x.getAttribute('type') != 'submit') | |
503 return makeError(response, errors.bad_request.n); | |
504 | |
505 var form = forms.parse(x, true); | |
506 if (typeof form == 'number') | |
507 return makeError(response, form); | |
508 | |
509 var conf = form; | |
510 } | |
511 | |
512 var subID; | |
513 if (configuration['pubsub#access_model'] == 'open') { | |
514 subID = storage.subscribe(nodeID, jid, 'subscribe', conf); | |
515 if (typeof subID == 'number') | |
516 return makeError(response, subID); | |
517 } else if (configuration['pubsub#access_model'] == 'authorize') { | |
518 subID = storage.subscribe(nodeID, jid, 'pending', conf); | |
519 if (typeof subID == 'number') | |
520 return makeError(response, subID); | |
521 } else if (configuration['pubsub#access_model'] == 'whitelist') { | |
522 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
|
523 if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member') |
0 | 524 return makeError(response, errors.sub.subscribe.not_on_whitelist.n); |
525 | |
526 subID = storage.subscribe(nodeID, jid, conf); | |
527 if (typeof subID == 'number') | |
528 return makeError(response, subID); | |
529 } | |
530 | |
531 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
532 .c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type}); | |
533 | |
534 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
|
535 response.cnode(options); |
0 | 536 |
537 if (config.enabled('get-pending')) { | |
538 var affiliates = storage.getAffiliationsFromNodeID(nodeID); | |
539 var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168 | |
540 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
|
541 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
|
542 var message = xmpp.message({to: i}).cnode(form); |
0 | 543 conn.send(message); |
544 } | |
545 } | |
546 } | |
547 | |
548 if (config.enabled('last-published')) { | |
549 var last = storage.getLastItem(nodeID); | |
550 if (typeof last != 'number') { | |
551 var item = storage.getItem(nodeID, last); | |
552 if (typeof item != 'number') { | |
553 var attr = {}; | |
554 attr[last] = {content: item}; | |
555 sendNotifs(jid, 'items', nodeID, attr); | |
556 } | |
557 } | |
558 } | |
559 | |
560 // SECTION 6.2 | |
561 } else if (pubsub.getChild('unsubscribe')) { | |
562 if (!config.enabled('subscribe')) | |
563 return makeError(response, errors.sub.subscribe.not_supported.n); | |
564 | |
565 var unsubscribe = pubsub.getChild('unsubscribe'); | |
566 var nodeID = unsubscribe.getAttribute('node'); | |
567 if (!nodeID || nodeID == '') | |
568 return makeError(response, errors.nodeid_required.n); | |
569 if (!storage.existsNode(nodeID)) | |
570 return makeError(response, errors.node_does_not_exist.n); | |
571 | |
572 var jid = unsubscribe.getAttribute('jid'); | |
573 if (!jid || toBareJID(jid) != toBareJID(to)) | |
574 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n); | |
575 | |
576 var subID = storage.subscribe(nodeID, jid, 'none'); | |
577 if (typeof subID == 'number') | |
578 return makeError(response, subID); | |
579 | |
580 // SECTIONS 6.3.5 | |
581 } else if (pubsub.getChild('options')) { | |
582 if (!config.enabled('subscription-options')) | |
583 return makeError(response, errors.sub.subscribe.not_supported.n); | |
584 | |
585 var options = pubsub.getChild('options'); | |
586 | |
587 var nodeID = unsubscribe.getAttribute('node'); | |
588 if (!nodeID || nodeID == '') | |
589 return makeError(response, errors.nodeid_required.n); | |
590 if (!storage.existsNode(nodeID)) | |
591 return makeError(response, errors.node_does_not_exist.n); | |
592 | |
593 var jid = unsubscribe.getAttribute('jid'); | |
594 if (!jid || toBareJID(jid) != toBareJID(to)) | |
595 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n); | |
596 | |
597 var x = options.getChild('x', 'jabber:x:data'); | |
598 if (!x || x.getAttribute(type) != 'submit') | |
599 return makeError(response, errors.bad_request); | |
600 | |
601 var form = forms.parse(x, true); | |
602 if (typeof form == 'number') | |
603 return makeError(response, form); | |
604 | |
605 var set = storage.configureSubscription(nodeID, jid, form); | |
606 if (typeof form == 'number') | |
607 return makeError(response, form); | |
608 | |
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
|
609 // SECTION 7.1 |
0 | 610 } else if (pubsub.getChild('publish')) { |
611 if (!config.enabled('publish')) | |
612 return makeError(response, errors.pub.publish.item_publication_not_supported.n); | |
613 | |
614 var publish = pubsub.getChild('publish'); | |
615 var nodeID = publish.getAttribute('node'); | |
616 if (!nodeID || nodeID == '') | |
617 return makeError(response, errors.nodeid_required.n); | |
618 | |
6 | 619 var autocreate = false; |
620 if (!storage.existsNode(nodeID)) { | |
621 if (config.enabled('auto-create')) | |
622 autocreate = true; | |
623 else | |
624 return makeError(response, errors.node_does_not_exist.n); | |
625 } | |
626 | |
0 | 627 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
6 | 628 if (typeof affil == 'number' && affil != errors.node_does_not_exist.n) |
0 | 629 return makeError(response, affil); |
6 | 630 if (!autocreate && affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'publish-only') |
0 | 631 return makeError(response, errors.forbidden.n); |
632 | |
633 var item = publish.getChild('item'); | |
634 var itemID = item.getAttribute('id'); | |
635 if (!config.enabled('item-ids') && itemID) | |
636 return makeError(response, errors.itemid_required.n); | |
637 itemID = itemID? itemID: utils.makeRandomId(); | |
638 | |
639 if (item.tags.length != 1) | |
640 return makeError(response, errors.pub.publish.bad_payload.n); | |
641 | |
642 var conf = storage.getConfiguration(nodeID); | |
643 var publishOptions = pubsub.getChild('publish-options'); | |
644 if (publishOptions && config.enabled('publish-options')) { | |
645 var x = publishOptions.getChild('x', 'jabber:x:data'); | |
646 if (!x || x.getAttribute('type') != 'submit') | |
647 return makeError(response, errors.bad_request.n); | |
648 | |
649 var form = forms.parse(x, true); | |
650 if (form.access_model != conf['pubsub#access_model'] && !autocreate) | |
651 return makeError(response, errors.pub.configuration.precondition.n); | |
652 } | |
653 | |
654 if (!config.enabled('persistent-items')) { | |
655 var notifs = storage.purgeNode(nodeID); | |
656 if (typeof notifs == 'number') | |
657 return makeError(response, r); | |
658 } | |
659 | |
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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 |
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 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
|
666 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
|
667 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
|
668 } |
0 | 669 |
670 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
|
671 |
0 | 672 subscribers = storage.setItem(nodeID, itemID, content); |
673 if (typeof subscribers == 'number') | |
674 return makeError(response, subscribers); | |
675 | |
676 var attrs = {}; | |
677 if (content) | |
678 attrs[itemID] = {content: content}; | |
679 else | |
680 attrs[itemID] = {}; | |
681 sendNotifs(subscribers, 'items', nodeID, attrs); | |
682 | |
683 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
684 .c('publish', {node: nodeID}) | |
685 .c('item', {id: itemID}); | |
686 | |
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
|
687 // SECTION 7.2 |
0 | 688 } else if (pubsub.getChild('retract')) { |
689 if (!config.enabled('retract-items')) | |
690 return makeError(response, errors.pub.retract.item_deletion_not_supported.n); | |
691 | |
692 var retract = pubsub.getChild('retract'); | |
693 | |
694 var nodeID = retract.getAttribute('node'); | |
695 if (!nodeID || nodeID == '') | |
696 return makeError(response, errors.nodeid_required.n); | |
697 if (!storage.existsNode(nodeID)) | |
698 return makeError(response, errors.node_does_not_exist.n); | |
699 | |
700 var item = retract.getChild('item'); | |
701 if (!item) | |
702 return makeError(response, errors.pub.retract.item_or_itemid_required.n); | |
703 | |
704 var itemID = item.getAttribute('id') | |
705 if (!itemID || itemID == '') | |
706 return makeError(response, errors.pub.retract.item_or_itemid_required.n); | |
707 | |
6 | 708 var affil = storage.getAffiliation(toBareJID(to), nodeID); |
709 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') | |
710 return makeError(response, errors.forbidden.n); | |
711 | |
0 | 712 var subscribers = storage.deleteItem(nodeID, itemID); |
713 if (typeof subscribers == 'number') | |
714 return makeError(response, subscribers); | |
715 | |
716 var attrs = {}; | |
717 attrs[itemID] = {}; | |
718 sendNotifs(subscribers, 'items', nodeID, attrs, 'retract') | |
719 | |
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
|
720 // SECTION 8.1 |
0 | 721 } else if (pubsub.getChild('create')) { |
722 if (!config.enabled('create-nodes')) | |
723 return makeError(response, errors.owner.create.node_creation_not_supported.n); | |
724 | |
725 var instant = false; | |
726 | |
727 var nodeID = pubsub.getChild('create').getAttribute('node'); | |
728 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
|
729 if (!config.enabled('instant-nodes')) |
0 | 730 return makeError(response, errors.owner.create.instant_nodes_not_supported.n); |
731 nodeID = utils.makeRandomId(); | |
732 instant = true; | |
733 } | |
734 if (storage.existsNode(nodeID)) | |
735 return makeError(response, errors.nodeid_already_exists.n); | |
736 | |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
737 var bare = toBareJID(to); |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
738 var right = false; |
7
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
739 |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
740 // Check for super-owner |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
741 for (var i in config.owner) |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
742 if (config.owner[i] == bare) |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
743 right = true; |
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
744 |
7
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
745 // Check for authorized user |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
746 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
|
747 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
|
748 right = true; |
781ac4f1e304
Allow others JIDs than super-owners to create nodes.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
6
diff
changeset
|
749 |
2
d3ae2f8b685d
Make create node working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
750 if (!right) |
0 | 751 return makeError(response, errors.forbidden.n); |
752 | |
753 var configure = pubsub.getChild('configure'); | |
754 if (configure && config.enabled('create-and-configure')) { | |
755 if (!config.enabled('config-node')) | |
756 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
757 | |
758 if (configure.getAttribute('node')) | |
759 return makeError(response, errors.bad_request.n); | |
760 | |
761 var x = configure.getChild('x', 'jabber:x:data'); | |
762 if (!x || x.getAttribute('type') != 'submit') | |
763 return makeError(response, errors.bad_request.n); | |
764 | |
765 var form = forms.parse(x, true); | |
766 if (typeof form == 'number') | |
767 return makeError(response, form); | |
768 | |
769 var conf = form; | |
770 } | |
771 | |
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
|
772 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
|
773 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
|
774 conf['pubsub#creator'] = toBareJID(to); |
0 | 775 var r = storage.createNode(nodeID, conf); |
776 if (typeof r == 'number') | |
777 return makeError(response, r); | |
778 | |
779 if (instant) | |
780 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) | |
781 .c('create', {node: nodeID}); | |
782 } else | |
783 return makeError(response, errors.feature_not_implemented.n); | |
784 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) { | |
785 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner'); | |
786 | |
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
|
787 // SECTION 8.2.4 |
0 | 788 if (pubsub.getChild('configure')) { |
789 if (!config.enabled('config-node')) | |
790 return makeError(response, errors.owner.configure.node_configuration_not_supported.n); | |
791 | |
792 var nodeID = configure.getAttribute('node'); | |
793 if (!nodeID) | |
794 return makeError(response, errors.nodeid_required.n); | |
795 if (!storage.existsNode(nodeID)) | |
796 return makeError(response, errors.node_does_not_exist.n); | |
797 | |
798 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
|
799 if (affil != 'super-owner' && affil != 'owner' && affil != 'publish-only') |
0 | 800 return makeError(response, errors.forbidden.n); |
801 | |
802 var x = configure.getChild('x', 'jabber:x:data'); | |
803 if (!x || x.getAttribute(type) != 'submit') | |
804 return makeError(response, errors.bad_request.n); | |
805 | |
806 var form = forms.parse(x, true); | |
807 if (typeof form == 'number') | |
808 return makeError(response, form); | |
809 | |
810 var conf = form; | |
811 | |
812 var set = storage.configure(nodeID, conf); | |
813 if (typeof set == 'number') | |
814 return makeError(response, set); | |
815 | |
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
|
816 // SECTION 8.4 |
0 | 817 } else if (pubsub.getChild('delete')) { |
818 if (!config.enabled('delete-nodes')) | |
819 return makeError(response, errors.feature_not_implemented.n); //XXX | |
820 | |
821 var del = pubsub.getChild('delete'); | |
822 | |
823 var nodeID = del.getAttribute('node'); | |
824 if (!nodeID) | |
825 return makeError(response, errors.nodeid_required.n); | |
826 if (!storage.existsNode(nodeID)) | |
827 return makeError(response, errors.node_does_not_exist.n); | |
828 | |
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
|
829 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
|
830 if (affil != 'super-owner' && affil != 'owner') |
0 | 831 return makeError(response, errors.forbidden.n); |
832 | |
833 var notifs = storage.deleteNode(nodeID); | |
834 if (typeof notifs == 'number') | |
835 return makeError(response, r); | |
836 | |
837 sendNotifs(notifs, 'delete', nodeID); | |
838 | |
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
|
839 // SECTION 8.5 |
0 | 840 } else if (pubsub.getChild('purge')) { |
841 if (!config.enabled('purge-nodes')) | |
842 return makeError(response, errors.owner.purge.node_purging_not_supported.n); //XXX | |
843 | |
844 var purge = pubsub.getChild('purge'); | |
845 | |
846 var nodeID = purge.getAttribute('node'); | |
847 if (!nodeID) | |
848 return makeError(response, errors.nodeid_required.n); | |
849 if (!storage.existsNode(nodeID)) | |
850 return makeError(response, errors.node_does_not_exist.n); | |
851 | |
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
|
852 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
|
853 if (affil != 'super-owner' && affil != 'owner') |
0 | 854 return makeError(response, errors.forbidden.n); |
855 | |
856 if (!config.enabled('persistent-items')) //FIXME: autre condition, supporté par le node | |
857 return makeError(response, errors.owner.purge.node_does_not_persist_items.n); | |
858 | |
859 var notifs = storage.purgeNode(nodeID); | |
860 if (typeof notifs == 'number') | |
861 return makeError(response, r); | |
862 | |
863 sendNotifs(notifs, 'purge', nodeID); | |
864 | |
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
|
865 // SECTION 8.8.2 |
0 | 866 } else if (pubsub.getChild('subscriptions')) { |
867 if (!config.enabled('manage-subscriptions')) | |
868 return makeError(response, errors.owner.manage_subscriptions.not_supported.n); //XXX | |
869 | |
870 var subscriptions = pubsub.getChild('subscriptions'); | |
871 | |
872 var nodeID = subscriptions.getAttribute('node'); | |
873 if (!nodeID) | |
874 return makeError(response, errors.nodeid_required.n); | |
875 if (!storage.existsNode(nodeID)) | |
876 return makeError(response, errors.node_does_not_exist.n); | |
877 | |
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
|
878 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
|
879 if (affil != 'super-owner' && affil != 'owner') |
0 | 880 return makeError(response, errors.forbidden.n); |
881 | |
882 var e = false; | |
883 for (i in subscriptions.tags) { | |
884 var jid = subscriptions.tags[i].getAttribute('jid'); | |
885 var subscription = subscriptions.tags[i].getAttribute('subscription'); | |
886 | |
887 var set = storage.subscribe(nodeID, jid, subscription); | |
888 if (typeof set == 'number') | |
889 e = true; | |
890 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
|
891 // SECTION 8.8.4 |
0 | 892 sendNotifs(jid, 'subscription', nodeID, {jid: jid, subscription: subscription}); |
893 subscriptions.tags.splice(i, 1); | |
894 } | |
895 } | |
896 | |
897 if (e) | |
898 return makeError(response, errors.owner.manage_subscriptions.modify.multiple_simultaneous_modifications.n, pubsub); | |
899 | |
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
|
900 // SECTION 8.9.2 |
0 | 901 } else if (pubsub.getChild('affiliations')) { |
902 if (!config.enabled('modify-affiliations')) | |
903 return makeError(response, errors.owner.manage_affiliations.not_supported.n); //XXX | |
904 | |
905 var affiliations = pubsub.getChild('affiliations'); | |
906 | |
907 var nodeID = affiliations.getAttribute('node'); | |
908 if (!nodeID) | |
909 return makeError(response, errors.nodeid_required.n); | |
910 if (!storage.existsNode(nodeID)) | |
911 return makeError(response, errors.node_does_not_exist.n); | |
912 | |
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
|
913 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
|
914 if (affil != 'super-owner' && affil != 'owner') |
0 | 915 return makeError(response, errors.forbidden.n); |
916 | |
917 var e = false; | |
918 for (i in affiliations.children) { | |
919 var jid = affiliations.children[i].getAttribute('jid'); | |
920 var affiliation = affiliations.children[i].getAttribute('affiliation'); | |
921 | |
922 var set = storage.setAffiliation(nodeID, jid, affiliation); | |
923 if (typeof set == 'number') | |
924 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
|
925 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
|
926 // 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
|
927 sendNotifs(jid, 'affiliations', nodeID, {jid: jid, affiliation: affiliation}); |
0 | 928 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
|
929 } |
0 | 930 } |
931 | |
932 if (e) | |
933 return makeError(response, errors.owner.manage_affiliations.modify.multiple_simultaneous_modifications.n, pubsub); | |
934 } else | |
935 return makeError(response, errors.feature_not_implemented.n); | |
936 } else | |
937 return makeError(response, errors.feature_not_implemented.n); | |
938 } else | |
939 return makeError(response, errors.feature_not_implemented.n); | |
940 conn.send(response); | |
941 } | |
942 | |
943 function onMessage(stanza) { | |
944 var from = stanza.getAttribute('to'); | |
945 var to = stanza.getAttribute('from'); | |
946 var id = stanza.getAttribute('id'); | |
947 | |
948 var response; | |
949 if (id) | |
950 response = xmpp.message({to: to, from: from, id: id}); | |
951 else | |
952 response = xmpp.message({to: to, from: from}); | |
953 | |
954 var x = stanza.getChild('x', 'jabber:x:data'); | |
955 if (x) { | |
956 var form = forms.parse(x); | |
957 if (form.type == 'submit' && form.fields.FORM_TYPE.value == 'subscribe_authorization') { | |
958 if (form.fields.subid && form.fields.subid.value) | |
959 var subID = form.fields.subid.value; | |
960 if (form.fields.node && form.fields.node.value) | |
961 var nodeID = form.fields.node.value; | |
962 if (form.fields.subscriber_jid && form.fields.subscriber_jid.value) | |
963 var jid = form.fields.subscriber_jid.value; | |
964 if (form.fields.allow && form.fields.allow.value) | |
965 var allow = form.fields.allow.value; | |
966 | |
967 var type = allow? 'subscribed': 'none'; | |
968 var set = storage.subscribe(nodeID, jid, type) | |
969 //if (set.subid != subID) //TODO: support the multi-subscribe feature | |
970 sendNotifs(jid, 'subscription', nodeID, {jid: jid, subscription: type}); | |
971 } else | |
972 return makeError(response, errors.feature_not_implemented.n); | |
973 } else | |
974 return makeError(response, errors.feature_not_implemented.n); | |
975 conn.send(response) | |
976 } | |
977 | |
978 function onPresence(stanza) { | |
979 var from = stanza.getAttribute('to'); | |
980 var to = stanza.getAttribute('from'); | |
981 var id = stanza.getAttribute('id'); | |
982 | |
983 var response; | |
984 if (id) | |
985 response = xmpp.presence({to: to, from: from, id: id}); | |
986 else | |
987 response = xmpp.presence({to: to, from: from}); | |
988 | |
989 makeError(response, errors.feature_not_implemented.n); | |
990 } | |
991 | |
992 function makeError(response, errorNumber, payload) { | |
993 response.attr.type = 'error'; | |
994 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
|
995 response.cnode(payload); |
0 | 996 |
997 var e = errors.reverse[errorNumber]; | |
998 var error = xmpp.stanza('error', {type: e.type}); | |
999 | |
1000 error.s(e.error, {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}) | |
1001 | |
1002 if (e.reason) { | |
1003 if (e.feature) | |
1004 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors', feature: e.feature}); | |
1005 else | |
1006 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'}); | |
1007 } | |
1008 | |
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
|
1009 response.cnode(error); |
0 | 1010 conn.send(response); |
1011 } | |
1012 | |
1013 function sendNotifs(notifs, type, nodeID, a1, a2) { | |
1014 var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'}); | |
1015 | |
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
|
1016 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
|
1017 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
|
1018 |
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 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
|
1020 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
|
1021 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
|
1022 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
|
1023 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
|
1024 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
|
1025 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
|
1026 } |
3
80e607c0b39e
Make affiliation notifications working.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
1027 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
|
1028 .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
|
1029 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
|
1030 } else if (type == 'collection') { |
0 | 1031 var collection = xmpp.stanza('collection', {node: nodeID}); |
1032 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
|
1033 collection.cnode('associate', {node: nodeID}); |
0 | 1034 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
|
1035 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
|
1036 ev.cnode(collection); |
0 | 1037 } else if (type == 'configuration') { |
1038 if (!config.enabled('config-node')) { | |
1039 _('Error #4', 41) | |
1040 return; | |
1041 } | |
1042 | |
1043 var configuration = xmpp.stanza('configuration', {node: nodeID}); | |
1044 if (a1) { | |
1045 var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID)); | |
1046 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
|
1047 configuration.cnode(x); //TODO: voir exemple 150 |
0 | 1048 } |
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 ev.cnode(configuration); |
0 | 1050 } else if (type == 'delete') { |
1051 var del = xmpp.stanza('delete', {node: nodeID}); | |
1052 if (a1) | |
1053 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
|
1054 ev.cnode(del); |
0 | 1055 } else if (type == 'items') { |
1056 var items = xmpp.stanza(type, {node: nodeID}); | |
1057 if (a2 == 'retract') | |
1058 for (var i in a1) | |
1059 items.s('retract', {id: i}); | |
1060 else { | |
1061 for (var i in a1) { | |
1062 var item = a1[i]; | |
1063 var args = {}; | |
1064 if (i != '') | |
1065 args.id = i; | |
1066 if (item.node) | |
1067 args.node = item.node; | |
1068 if (item.publisher) | |
1069 args.publisher = item.publisher; | |
1070 var it = xmpp.stanza('item', args); | |
1071 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
|
1072 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
|
1073 items.cnode(it); |
0 | 1074 } |
1075 } | |
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
|
1076 ev.cnode(items); |
0 | 1077 } else if (type == 'purge') { |
1078 ev.c('purge', {node: nodeID}); | |
1079 } else if (type == 'subscription') { | |
1080 if (!config.enabled('subscription-notifications')) | |
1081 return; | |
1082 | |
1083 var args = {node: nodeID}; | |
1084 for (i in a1) { | |
1085 var attr = a1[i]; | |
1086 if (i == 'subscription') { | |
1087 if (attr == 'none' || attr == 'pending' || attr == 'subscribed' || attr == 'unconfigured') | |
1088 args[i] = attr; | |
1089 else { | |
1090 _('Error #3', 41) | |
1091 return; | |
1092 } | |
1093 } else if (i == 'jid' || i == 'subid') | |
1094 args[i] = attr; | |
1095 else if (i == 'expiry') | |
1096 args[i] = attr.toString(); | |
1097 } | |
1098 if (!args.jid || args.jid == '') { | |
1099 _('Error #2', 41) | |
1100 return; | |
1101 } | |
1102 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
|
1103 ev.cnode(sub); |
0 | 1104 } else { |
1105 _('Error #1', 41) | |
1106 return; | |
1107 } | |
1108 | |
1109 var subs; | |
1110 if (typeof notifs == 'string') { | |
1111 subs = {}; | |
1112 subs[notifs] = storage.getSubscription(notifs, nodeID); | |
1113 } else | |
1114 subs = notifs; | |
1115 | |
1116 for (var i in subs) { | |
1117 var sub = subs[i]; | |
1118 | |
1119 if (sub.options) { | |
1120 if (typeof sub.options['pubsub#deliver'] != 'undefined' && !sub.options['pubsub#deliver']) | |
1121 continue; | |
1122 | |
1123 if (typeof sub.options['pubsub#digest'] != 'undefined' && sub.options['pubsub#digest']) { | |
1124 if (!sub.digest) | |
1125 sub.digest = []; | |
1126 sub.digest.push(ev) | |
1127 | |
1128 if (sub.digestTimeout) | |
1129 continue; | |
1130 | |
1131 var freq; | |
1132 if (typeof sub.options['pubsub#digest_frequency'] == 'undefined') | |
1133 freq = 0; | |
1134 else | |
1135 freq = parseInt(sub.options['pubsub#digest_frequency']); | |
1136 | |
1137 if (freq == 0) | |
1138 freq = 24*60*60*1000; | |
1139 | |
1140 setTimeout(sendDigest, freq, notifs[i], nodeID); | |
1141 sub.digestTimeout = true; | |
1142 continue; | |
1143 } | |
1144 } | |
1145 | |
1146 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
|
1147 message.cnode(ev); |
0 | 1148 conn.send(message); |
1149 } | |
1150 } | |
1151 | |
1152 function sendDigest(jid, nodeID) { | |
1153 var sub = storage.getSubscription(jid, nodeID); | |
1154 if (sub.digestTimeout) | |
1155 sub.digestTimeout = false; | |
1156 | |
1157 var message = xmpp.message({to: jid, from: componentJID, id: conn.getUniqueId()}); | |
1158 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
|
1159 message.cnode(sub.digest[i]); |
0 | 1160 conn.send(message); |
1161 } | |
1162 | |
1163 conn.connect(componentJID, componentPassword, function (status, condition) { | |
1164 if (status == xmpp.Status.CONNECTED) { | |
1165 conn.addHandler(onMessage, null, 'message', null, null, null); | |
1166 conn.addHandler(onIq, null, 'iq', null, null, null); | |
1167 conn.addHandler(onPresence, null, 'presence', null, null, null); | |
1168 | |
1169 if (process.argv.length >= 3) | |
1170 storage.load(process.argv[2]); | |
1171 else | |
1172 storage.load(); | |
1173 | |
1174 var stdin = process.openStdin(); | |
1175 stdin.setEncoding('utf8'); | |
1176 stdin.addListener('data', storage.debug); | |
1177 } else | |
1178 conn.log(xmpp.LogLevel.DEBUG, 'New connection status: ' + status + (condition? (' ('+condition+')'): '')); | |
1179 }); |