comparison modules/mod_retrieve.js @ 24:b80ab94da447

Add new modules files.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 01 Nov 2010 00:02:27 +0100
parents
children b2faacfefb90
comparison
equal deleted inserted replaced
23:5fc4ee90c1bc 24:b80ab94da447
1 var config = require('../configuration');
2 var storage = require('../storage');
3 var errors = require('../errors');
4 var makeError = errors.makeError;
5 var toBareJID = require('../util').toBareJID;
6 var NS = require('../namespaces');
7
8 // SECTION 5.6: Retrieve Subscriptions
9 exports.retrieveSubscriptions = {
10 type: 'get',
11 child: 'pubsub',
12 ns: NS.PUBSUB,
13 pschild: 'subscriptions',
14 func: function(response, stanza, request) {
15 if (!config.enabled('retrieve-subscriptions'))
16 return makeError(response, errors.subscriptions_retrieval_not_supported.n);
17
18 var nodeID = request.getAttribute('node');
19 if (nodeID && nodeID != '') {
20 if (!storage.existsNode(nodeID))
21 return makeError(response, errors.node_does_not_exist.n);
22 var subs = storage.getSubscription(toBareJID(to), node);
23 } else
24 var subs = storage.getSubscription(toBareJID(to));
25
26 response.c('pubsub', {xmlns: NS.PUBSUB});
27 response.c('subscriptions');
28
29 for (i in subs)
30 response.c('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}).up();
31
32 return response;
33 }
34 }
35
36 // SECTION 5.7: Retrieve Affiliations
37 exports.retrieveAffiliations = {
38 type: 'get',
39 child: 'pubsub',
40 ns: NS.PUBSUB,
41 pschild: 'affiliations',
42 func: function(response, stanza, request) {
43 if (!config.enabled('retrieve-affiliations'))
44 return makeError(response, errors.affiliations_retrieval_not_supported.n);
45
46 var nodeID = request.getAttribute('node');
47 if (nodeID && nodeID != '') {
48 if (!storage.existsNode(nodeID))
49 return makeError(response, errors.node_does_not_exist.n);
50 var affils = {};
51 affils[nodeID] = storage.getAffiliation(toBareJID(to), nodeID);
52 } else
53 var affils = storage.getAffiliationsFromJID(toBareJID(to));
54
55 response.c('pubsub', {xmlns: NS.PUBSUB});
56 response.c('affiliations');
57
58 for (i in affils)
59 response.c('affiliation', {node: i, affiliation: affils[i]}).up();
60
61 return response;
62 }
63 }
64
65 // SECTION 6.5: Retrieve Items from a Node
66 exports.retrieveItems = {
67 type: 'get',
68 child: 'pubsub',
69 ns: NS.PUBSUB,
70 pschild: 'items',
71 func: function(response, stanza, request) {
72 if (!config.enabled('retrieve-items'))
73 return makeError(response, errors.sub.default_options.node_configuration_not_supported.n);
74
75 var nodeID = request.getAttribute('node');
76 if (!nodeID || nodeID == '')
77 return makeError(response, errors.nodeid_required.n);
78 if (!storage.existsNode(nodeID))
79 return makeError(response, errors.node_does_not_exist.n);
80
81 var configuration = storage.getConfiguration(nodeID);
82 if (configuration['pubsub#access_model'] == 'whitelist') {
83 var affil = storage.getAffiliation(toBareJID(to), nodeID);
84 if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member')
85 return makeError(response, errors.pub.publish.insufficient_privileges.n);
86 }
87
88 var item = [];
89 for (var i=0; i<request.children.length; i++) {
90 var j = request.children[i];
91 if (j.name == 'item' && j.attr['id'] && j.attr['id'] != '')
92 item.push(j.attr['id']);
93 }
94
95 response.c('pubsub', {xmlns: NS.PUBSUB});
96
97 var max_items = request.getAttribute('max_items');
98 if (max_items)
99 max_items = Number (max_items);
100
101 if (item.length) {
102 response.c('items', {node: nodeID});
103
104 for (var i=0; i<item.length; i++) {
105 var j = storage.getItem(nodeID, item[i]);
106 if (typeof j == 'number')
107 return makeError(response, j);
108 if (j == errors.success)
109 continue;
110
111 response.c('item', {id: item[i]})
112 response.cnode(j).up().up();
113 }
114 } else {
115 response.c('items', {node: nodeID});
116
117 var j;
118 if (max_items)
119 j = storage.getLastItem(nodeID, max_items);
120 else
121 j = storage.getItems(nodeID);
122 if (typeof j == 'number')
123 return makeError(response, j);
124
125 var k = 0;
126 for (var i in j)
127 response.c('item', {id: i}).t(j[i].content).up();
128 }
129
130 return response;
131 }
132 }