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