comparison modules/mod_options.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 forms = require('../forms');
4 var errors = require('../errors');
5 var makeError = errors.makeError;
6 var toBareJID = require('../util').toBareJID;
7 var NS = require('../namespaces');
8
9 // SECTION 6.3.2: Configure Subscription Options (Request)
10 exports.configureSub = {
11 type: 'get',
12 child: 'pubsub',
13 ns: NS.PUBSUB,
14 pschild: 'options',
15 func: function(response, stanza, request, to) {
16 if (!config.enabled('subscription-options'))
17 return makeError(response, errors.sub.configure.subscription_options_not_supported.n);
18
19 var nodeID = request.getAttribute('node');
20 if (!nodeID || nodeID == '')
21 return makeError(response, errors.nodeid_required.n);
22 if (!storage.existsNode(nodeID))
23 return makeError(response, errors.node_does_not_exist.n);
24
25 var jid = request.getAttribute('jid');
26 if (!jid)
27 return makeError(response, errors.sub.configure.subscriber_jid_required.n);
28 if (toBareJID(jid) != toBareJID(to))
29 return makeError(response, errors.sub.configure.insufficient_privileges.n);
30
31 var subs = storage.getSubscription(jid, nodeID);
32 if (!subs.subid) // FIXME: better test for empty object.
33 return makeError(response, errors.sub.configure.no_such_subscriber.n);
34
35 response.c('pubsub', {xmlns: NS.PUBSUB});
36 response.c('options', {node: nodeID, jid: jid});
37
38 var form = forms.build('form', 'subscribe_options', subs.options, true);
39 response.cnode(form);
40
41 return response;
42 }
43 }
44
45 // SECTIONS 6.3.5: Form Submission
46 exports.configureSub = {
47 type: 'set',
48 child: 'pubsub',
49 ns: NS.PUBSUB,
50 pschild: 'options',
51 func: function(response, stanza, request, to) {
52 if (!config.enabled('subscription-options'))
53 return makeError(response, errors.sub.subscribe.not_supported.n);
54
55 var nodeID = unsubscribe.getAttribute('node');
56 if (!nodeID || nodeID == '')
57 return makeError(response, errors.nodeid_required.n);
58 if (!storage.existsNode(nodeID))
59 return makeError(response, errors.node_does_not_exist.n);
60
61 var jid = unsubscribe.getAttribute('jid');
62 if (!jid || toBareJID(jid) != toBareJID(to))
63 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n);
64
65 var x = request.getChild('x', 'jabber:x:data');
66 if (!x || x.getAttribute(type) != 'submit')
67 return makeError(response, errors.bad_request);
68
69 var form = forms.parse(x, true);
70 if (typeof form == 'number')
71 return makeError(response, form);
72
73 var set = storage.configureSubscription(nodeID, jid, form);
74 if (typeof form == 'number')
75 return makeError(response, form);
76
77 return response;
78 }
79 }
80
81 // SECTION 6.4: Request Default Subscription Configuration Options
82 exports.defaultSub = {
83 type: 'get',
84 child: 'pubsub',
85 ns: NS.PUBSUB,
86 pschild: 'default',
87 func: function(response, stanza, request) {
88 if (!config.enabled('retrieve-default-sub'))
89 return makeError(response, errors.sub.default_options.default_subscription_configuration_retrieval_not_supported.n);
90
91 var nodeID = request.getAttribute('node');
92 if (nodeID && !storage.existsNode(nodeID))
93 return makeError(response, errors.node_does_not_exist.n);
94
95 response.c('pubsub', {xmlns: NS.PUBSUB});
96 var s;
97 if (nodeID)
98 response.c('default', {node: nodeID});
99 else
100 response.c('default');
101
102 var form = forms.build('form', 'subscribe_options', 'default', false);
103 response.cnode(form);
104
105 return response;
106 }
107 }