comparison psgxs.js @ 8:efe8dbd34780

Rename cx method to cnode and define it if xmpp.js didn’t.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 26 Aug 2010 23:50:04 +0200
parents 781ac4f1e304
children a6429f48e403
comparison
equal deleted inserted replaced
7:781ac4f1e304 8:efe8dbd34780
44 if (c) 44 if (c)
45 sys.print('\033[0m'); 45 sys.print('\033[0m');
46 sys.puts(''); 46 sys.puts('');
47 }; 47 };
48 48
49 if (typeof xmpp.StanzaBuilder.cnode != 'function' || typeof xmpp.StanzaBuilder.prototype.cnode != 'function') {
50 xmpp.StanzaBuilder.prototype.cnode = function (stanza)
51 {
52 var parent = this;
53 parent.tags.push(stanza);
54 parent.children.push(stanza);
55 return this;
56 };
57 }
58
49 function onIq(stanza) { 59 function onIq(stanza) {
50 var type = stanza.getAttribute('type'); 60 var type = stanza.getAttribute('type');
51 var from = stanza.getAttribute('to'); 61 var from = stanza.getAttribute('to');
52 var to = stanza.getAttribute('from'); 62 var to = stanza.getAttribute('from');
53 var id = stanza.getAttribute('id'); 63 var id = stanza.getAttribute('id');
64 if (stanza.getChild('query', 'jabber:iq:version')) { 74 if (stanza.getChild('query', 'jabber:iq:version')) {
65 var query = xmpp.stanza('query', {xmlns: 'jabber:iq:version'}) 75 var query = xmpp.stanza('query', {xmlns: 'jabber:iq:version'})
66 .s('name').t('PSĜS') 76 .s('name').t('PSĜS')
67 .s('version').t(config.version) 77 .s('version').t(config.version)
68 .s('os').t(config.os); 78 .s('os').t(config.os);
69 response.cx(query); 79 response.cnode(query);
70 80
71 // SECTION 5.1 81 // SECTION 5.1
72 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#info')) { 82 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#info')) {
73 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#info'); 83 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#info');
74 var nodeID = query.getAttribute('node'); 84 var nodeID = query.getAttribute('node');
92 102
93 // SECTION 5.4 103 // SECTION 5.4
94 if (config.enabled('meta-data')) { 104 if (config.enabled('meta-data')) {
95 var x = forms.build('result', 'node_metadata', storage.getMetadata(nodeID), true); 105 var x = forms.build('result', 'node_metadata', storage.getMetadata(nodeID), true);
96 if (x) 106 if (x)
97 q.cx(x); 107 q.cnode(x);
98 } 108 }
99 response.cx(q); 109 response.cnode(q);
100 110
101 // SECTION 5.1 111 // SECTION 5.1
102 } else { 112 } else {
103 var q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#info'}) 113 var q = xmpp.stanza('query', {xmlns: 'http://jabber.org/protocol/disco#info'})
104 .s('identity', {category: 'pubsub', type: 'service', name: 'PubSub JavaScript Server'}) 114 .s('identity', {category: 'pubsub', type: 'service', name: 'PubSub JavaScript Server'})
108 .s('feature', {'var': 'jabber:iq:version'}) 118 .s('feature', {'var': 'jabber:iq:version'})
109 // .s('feature', {'var': 'http://jabber.org/protocol/commands'}) 119 // .s('feature', {'var': 'http://jabber.org/protocol/commands'})
110 for (var i in config.activated) 120 for (var i in config.activated)
111 if (typeof i == 'string') 121 if (typeof i == 'string')
112 q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]}); 122 q.s('feature', {'var': 'http://jabber.org/protocol/pubsub#' + config.activated[i]});
113 response.cx(q); 123 response.cnode(q);
114 } 124 }
115 125
116 // SECTION 5.2 126 // SECTION 5.2
117 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#items')) { 127 } else if (stanza.getChild('query', 'http://jabber.org/protocol/disco#items')) {
118 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#items'); 128 var query = stanza.getChild('query', 'http://jabber.org/protocol/disco#items');
150 } else 160 } else
151 attr.name = i; 161 attr.name = i;
152 162
153 q.s('item', attr); 163 q.s('item', attr);
154 } 164 }
155 response.cx(q); 165 response.cnode(q);
156 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) { 166 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub')) {
157 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub'); 167 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub');
158 168
159 // SECTION 5.6 169 // SECTION 5.6
160 if (pubsub.getChild('subscriptions')) { 170 if (pubsub.getChild('subscriptions')) {
175 var s = xmpp.stanza('subscriptions'); 185 var s = xmpp.stanza('subscriptions');
176 for (i in subs) 186 for (i in subs)
177 s.s('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}); 187 s.s('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid});
178 188
179 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); 189 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
180 p.cx(s); 190 p.cnode(s);
181 response.cx(p); 191 response.cnode(p);
182 192
183 // SECTION 5.7 193 // SECTION 5.7
184 } else if (pubsub.getChild('affiliations')) { 194 } else if (pubsub.getChild('affiliations')) {
185 if (!config.enabled('retrieve-affiliations')) 195 if (!config.enabled('retrieve-affiliations'))
186 return makeError(response, errors.affiliations_retrieval_not_supported.n); 196 return makeError(response, errors.affiliations_retrieval_not_supported.n);
199 var s = xmpp.stanza('affiliations'); 209 var s = xmpp.stanza('affiliations');
200 for (i in affils) 210 for (i in affils)
201 s.s('affiliation', {node: i, affiliation: affils[i]}); 211 s.s('affiliation', {node: i, affiliation: affils[i]});
202 212
203 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); 213 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
204 p.cx(s); 214 p.cnode(s);
205 response.cx(p); 215 response.cnode(p);
206 216
207 // SECTION 6.3.2 217 // SECTION 6.3.2
208 } else if (pubsub.getChild('options')) { 218 } else if (pubsub.getChild('options')) {
209 if (!config.enabled('subscription-options')) 219 if (!config.enabled('subscription-options'))
210 return makeError(response, errors.sub.configure.subscription_options_not_supported.n); 220 return makeError(response, errors.sub.configure.subscription_options_not_supported.n);
228 return makeError(response, errors.sub.configure.no_such_subscriber.n); 238 return makeError(response, errors.sub.configure.no_such_subscriber.n);
229 239
230 var s = xmpp.stanza('options', {node: nodeID, jid: jid}); 240 var s = xmpp.stanza('options', {node: nodeID, jid: jid});
231 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); 241 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
232 var form = forms.build('form', 'subscribe_options', subs.options, true); 242 var form = forms.build('form', 'subscribe_options', subs.options, true);
233 s.cx(form); 243 s.cnode(form);
234 p.cx(s); 244 p.cnode(s);
235 response.cx(p); 245 response.cnode(p);
236 246
237 // SECTION 6.4 247 // SECTION 6.4
238 } else if (pubsub.getChild('default')) { 248 } else if (pubsub.getChild('default')) {
239 if (!config.enabled('retrieve-default-sub')) 249 if (!config.enabled('retrieve-default-sub'))
240 return makeError(response, errors.sub.default_options.default_subscription_configuration_retrieval_not_supported.n); 250 return makeError(response, errors.sub.default_options.default_subscription_configuration_retrieval_not_supported.n);
251 s = xmpp.stanza('default', {node: nodeID}); 261 s = xmpp.stanza('default', {node: nodeID});
252 else 262 else
253 s = xmpp.stanza('default'); 263 s = xmpp.stanza('default');
254 264
255 var form = forms.build('form', 'subscribe_options', 'default', false); 265 var form = forms.build('form', 'subscribe_options', 'default', false);
256 s.cx(form); 266 s.cnode(form);
257 p.cx(s); 267 p.cnode(s);
258 response.cx(p); 268 response.cnode(p);
259 269
260 // SECTION 6.5 270 // SECTION 6.5
261 } else if (pubsub.getChild('items')) { 271 } else if (pubsub.getChild('items')) {
262 if (!config.enabled('retrieve-items')) 272 if (!config.enabled('retrieve-items'))
263 return makeError(response, errors.sub.default_options.node_configuration_not_supported.n); 273 return makeError(response, errors.sub.default_options.node_configuration_not_supported.n);
295 var j = storage.getItem(nodeID, item[i]); 305 var j = storage.getItem(nodeID, item[i]);
296 if (typeof j == 'number') 306 if (typeof j == 'number')
297 return makeError(response, j); 307 return makeError(response, j);
298 308
299 var k = xmpp.stanza('item', {id: item[i]}) 309 var k = xmpp.stanza('item', {id: item[i]})
300 k.cx(j); 310 k.cnode(j);
301 s.cx(k); 311 s.cnode(k);
302 } 312 }
303 } else { 313 } else {
304 var s = xmpp.stanza('items', {node: nodeID}); 314 var s = xmpp.stanza('items', {node: nodeID});
305 315
306 var j; 316 var j;
312 return makeError(response, j); 322 return makeError(response, j);
313 323
314 var k = 0; 324 var k = 0;
315 for (var i in j) { 325 for (var i in j) {
316 var contentItem = xmpp.stanza('item', {id: i}).t(j[i].content); 326 var contentItem = xmpp.stanza('item', {id: i}).t(j[i].content);
317 s.cx(contentItem); 327 s.cnode(contentItem);
318 } 328 }
319 } 329 }
320 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}); 330 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'});
321 p.cx(s); 331 p.cnode(s);
322 response.cx(p); 332 response.cnode(p);
323 } else 333 } else
324 return makeError(response, errors.feature_not_implemented.n); 334 return makeError(response, errors.feature_not_implemented.n);
325 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) { 335 } else if (stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner')) {
326 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner'); 336 var pubsub = stanza.getChild('pubsub', 'http://jabber.org/protocol/pubsub#owner');
327 337
341 return makeError(response, errors.pub.publish.insufficient_privileges.n); 351 return makeError(response, errors.pub.publish.insufficient_privileges.n);
342 352
343 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); 353 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'});
344 var s = xmpp.stanza('configure', {node: nodeID}); 354 var s = xmpp.stanza('configure', {node: nodeID});
345 var form = forms.build('form', 'node_config', 'default', true); 355 var form = forms.build('form', 'node_config', 'default', true);
346 s.cx(form); 356 s.cnode(form);
347 p.cx(s); 357 p.cnode(s);
348 response.cx(p); 358 response.cnode(p);
349 359
350 // SECTION 8.3 360 // SECTION 8.3
351 } else if (pubsub.getChild('default')) { 361 } else if (pubsub.getChild('default')) {
352 if (!config.enabled('config-node')) 362 if (!config.enabled('config-node'))
353 return makeError(response, errors.owner.default_options.node_configuration_not_supported.n); 363 return makeError(response, errors.owner.default_options.node_configuration_not_supported.n);
354 364
355 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'}); 365 var p = xmpp.stanza('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub#owner'});
356 var s = xmpp.stanza('default'); 366 var s = xmpp.stanza('default');
357 var form = forms.build('node_config', service_configuration.node_config, null, true); 367 var form = forms.build('node_config', service_configuration.node_config, null, true);
358 s.cx(form); 368 s.cnode(form);
359 p.cx(s); 369 p.cnode(s);
360 response.cx(p); 370 response.cnode(p);
361 371
362 // SECTION 8.8 372 // SECTION 8.8
363 } else if (pubsub.getChild('subscriptions')) { 373 } else if (pubsub.getChild('subscriptions')) {
364 if (!config.enabled('manage-subscriptions')) 374 if (!config.enabled('manage-subscriptions'))
365 return makeError(response, errors.owner.manage_subscriptions.not_supported.n); 375 return makeError(response, errors.owner.manage_subscriptions.not_supported.n);
381 391
382 var subs = storage.getSubscriptionsFromNodeID(nodeID) 392 var subs = storage.getSubscriptionsFromNodeID(nodeID)
383 for (var jid in subs) 393 for (var jid in subs)
384 s.s('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid}) 394 s.s('subscription', {jid: jid, subscription: subs[jid].type, subid: subs[jid].subid})
385 395
386 p.cx(s); 396 p.cnode(s);
387 response.cx(p); 397 response.cnode(p);
388 398
389 // SECTION 8.9 399 // SECTION 8.9
390 } else if (pubsub.getChild('affiliations')) { 400 } else if (pubsub.getChild('affiliations')) {
391 if (!config.enabled('modify-affiliations')) 401 if (!config.enabled('modify-affiliations'))
392 return makeError(response, errors.owner.manage_affiliations.not_supported.n); 402 return makeError(response, errors.owner.manage_affiliations.not_supported.n);
408 var s = xmpp.stanza('affiliations', {node: nodeID}); 418 var s = xmpp.stanza('affiliations', {node: nodeID});
409 419
410 for (var jid in affils) 420 for (var jid in affils)
411 s.s('affiliation', {jid: jid, affiliation: affils[jid]}) 421 s.s('affiliation', {jid: jid, affiliation: affils[jid]})
412 422
413 p.cx(s); 423 p.cnode(s);
414 response.cx(p); 424 response.cnode(p);
415 } else 425 } else
416 return makeError(response, errors.feature_not_implemented.n); 426 return makeError(response, errors.feature_not_implemented.n);
417 } else 427 } else
418 return makeError(response, errors.feature_not_implemented.n); 428 return makeError(response, errors.feature_not_implemented.n);
419 } else if (type == 'set') { 429 } else if (type == 'set') {
483 493
484 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'}) 494 response.c('pubsub', {xmlns: 'http://jabber.org/protocol/pubsub'})
485 .c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type}); 495 .c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type});
486 496
487 if (conf) 497 if (conf)
488 response.cx(options); 498 response.cnode(options);
489 499
490 if (config.enabled('get-pending')) { 500 if (config.enabled('get-pending')) {
491 var affiliates = storage.getAffiliationsFromNodeID(nodeID); 501 var affiliates = storage.getAffiliationsFromNodeID(nodeID);
492 var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168 502 var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168
493 for (var i in affiliates) { 503 for (var i in affiliates) {
494 if (affiliates[i] == 'super-owner' || affiliates[i] == 'owner') { 504 if (affiliates[i] == 'super-owner' || affiliates[i] == 'owner') {
495 var message = xmpp.message({to: i}).cx(form); 505 var message = xmpp.message({to: i}).cnode(form);
496 conn.send(message); 506 conn.send(message);
497 } 507 }
498 } 508 }
499 } 509 }
500 510
943 } 953 }
944 954
945 function makeError(response, errorNumber, payload) { 955 function makeError(response, errorNumber, payload) {
946 response.attr.type = 'error'; 956 response.attr.type = 'error';
947 if (payload) 957 if (payload)
948 response.cx(payload); 958 response.cnode(payload);
949 959
950 var e = errors.reverse[errorNumber]; 960 var e = errors.reverse[errorNumber];
951 var error = xmpp.stanza('error', {type: e.type}); 961 var error = xmpp.stanza('error', {type: e.type});
952 962
953 error.s(e.error, {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'}) 963 error.s(e.error, {xmlns: 'urn:ietf:params:xml:ns:xmpp-stanzas'})
957 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors', feature: e.feature}); 967 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors', feature: e.feature});
958 else 968 else
959 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'}); 969 error.s(e.reason, {xmlns: 'http://jabber.org/protocol/pubsub#errors'});
960 } 970 }
961 971
962 response.cx(error); 972 response.cnode(error);
963 conn.send(response); 973 conn.send(response);
964 } 974 }
965 975
966 function sendNotifs(notifs, type, nodeID, a1, a2) { 976 function sendNotifs(notifs, type, nodeID, a1, a2) {
967 var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'}); 977 var ev = xmpp.stanza('event', {xmlns: 'http://jabber.org/protocol/pubsub#event'});
977 else if (i == 'jid') 987 else if (i == 'jid')
978 args.jid = attr; 988 args.jid = attr;
979 } 989 }
980 var affiliations = xmpp.stanza('affiliations', {node: nodeID}) 990 var affiliations = xmpp.stanza('affiliations', {node: nodeID})
981 .c('affiliation', args); 991 .c('affiliation', args);
982 ev.cx(affiliations); 992 ev.cnode(affiliations);
983 } else if (type == 'collection') { 993 } else if (type == 'collection') {
984 var collection = xmpp.stanza('collection', {node: nodeID}); 994 var collection = xmpp.stanza('collection', {node: nodeID});
985 if (a1 == 'associate') 995 if (a1 == 'associate')
986 collection.cx('associate', {node: nodeID}); 996 collection.cnode('associate', {node: nodeID});
987 else 997 else
988 collection.cx('disassociate', {node: nodeID}); 998 collection.cnode('disassociate', {node: nodeID});
989 ev.cx(collection); 999 ev.cnode(collection);
990 } else if (type == 'configuration') { 1000 } else if (type == 'configuration') {
991 if (!config.enabled('config-node')) { 1001 if (!config.enabled('config-node')) {
992 _('Error #4', 41) 1002 _('Error #4', 41)
993 return; 1003 return;
994 } 1004 }
995 1005
996 var configuration = xmpp.stanza('configuration', {node: nodeID}); 1006 var configuration = xmpp.stanza('configuration', {node: nodeID});
997 if (a1) { 1007 if (a1) {
998 var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID)); 1008 var x = forms.build('node_config', service_configuration.node_config, storage.getConfiguration(nodeID));
999 if (x) 1009 if (x)
1000 configuration.cx(x); //TODO: voir exemple 150 1010 configuration.cnode(x); //TODO: voir exemple 150
1001 } 1011 }
1002 ev.cx(configuration); 1012 ev.cnode(configuration);
1003 } else if (type == 'delete') { 1013 } else if (type == 'delete') {
1004 var del = xmpp.stanza('delete', {node: nodeID}); 1014 var del = xmpp.stanza('delete', {node: nodeID});
1005 if (a1) 1015 if (a1)
1006 del.c('redirect', {uri: a1}); 1016 del.c('redirect', {uri: a1});
1007 ev.cx(del); 1017 ev.cnode(del);
1008 } else if (type == 'items') { 1018 } else if (type == 'items') {
1009 var items = xmpp.stanza(type, {node: nodeID}); 1019 var items = xmpp.stanza(type, {node: nodeID});
1010 if (a2 == 'retract') 1020 if (a2 == 'retract')
1011 for (var i in a1) 1021 for (var i in a1)
1012 items.s('retract', {id: i}); 1022 items.s('retract', {id: i});
1020 args.node = item.node; 1030 args.node = item.node;
1021 if (item.publisher) 1031 if (item.publisher)
1022 args.publisher = item.publisher; 1032 args.publisher = item.publisher;
1023 var it = xmpp.stanza('item', args); 1033 var it = xmpp.stanza('item', args);
1024 if (item.content) 1034 if (item.content)
1025 it.cx(item.content); 1035 it.cnode(item.content);
1026 items.cx(it); 1036 items.cnode(it);
1027 } 1037 }
1028 } 1038 }
1029 ev.cx(items); 1039 ev.cnode(items);
1030 } else if (type == 'purge') { 1040 } else if (type == 'purge') {
1031 ev.c('purge', {node: nodeID}); 1041 ev.c('purge', {node: nodeID});
1032 } else if (type == 'subscription') { 1042 } else if (type == 'subscription') {
1033 if (!config.enabled('subscription-notifications')) 1043 if (!config.enabled('subscription-notifications'))
1034 return; 1044 return;
1051 if (!args.jid || args.jid == '') { 1061 if (!args.jid || args.jid == '') {
1052 _('Error #2', 41) 1062 _('Error #2', 41)
1053 return; 1063 return;
1054 } 1064 }
1055 var sub = xmpp.stanza('subscription', args); 1065 var sub = xmpp.stanza('subscription', args);
1056 ev.cx(sub); 1066 ev.cnode(sub);
1057 } else { 1067 } else {
1058 _('Error #1', 41) 1068 _('Error #1', 41)
1059 return; 1069 return;
1060 } 1070 }
1061 1071
1095 continue; 1105 continue;
1096 } 1106 }
1097 } 1107 }
1098 1108
1099 var message = xmpp.message({to: i, from: componentJID, id: conn.getUniqueId()}); 1109 var message = xmpp.message({to: i, from: componentJID, id: conn.getUniqueId()});
1100 message.cx(ev); 1110 message.cnode(ev);
1101 conn.send(message); 1111 conn.send(message);
1102 } 1112 }
1103 } 1113 }
1104 1114
1105 function sendDigest(jid, nodeID) { 1115 function sendDigest(jid, nodeID) {
1107 if (sub.digestTimeout) 1117 if (sub.digestTimeout)
1108 sub.digestTimeout = false; 1118 sub.digestTimeout = false;
1109 1119
1110 var message = xmpp.message({to: jid, from: componentJID, id: conn.getUniqueId()}); 1120 var message = xmpp.message({to: jid, from: componentJID, id: conn.getUniqueId()});
1111 for (var i in sub.digest) 1121 for (var i in sub.digest)
1112 message.cx(sub.digest[i]); 1122 message.cnode(sub.digest[i]);
1113 conn.send(message); 1123 conn.send(message);
1114 } 1124 }
1115 1125
1116 conn.connect(componentJID, componentPassword, function (status, condition) { 1126 conn.connect(componentJID, componentPassword, function (status, condition) {
1117 if (status == xmpp.Status.CONNECTED) { 1127 if (status == xmpp.Status.CONNECTED) {