comparison modules/mod_subscribe.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 6.1: Subscribe to a Node
9 exports.subscribe = {
10 type: 'set',
11 child: 'pubsub',
12 ns: NS.PUBSUB,
13 pschild: 'subscribe',
14 func: function(response, stanza, request, to) {
15 if (!config.enabled('subscribe'))
16 return makeError(response, errors.sub.subscribe.not_supported.n);
17
18 var nodeID = request.getAttribute('node');
19 if (!nodeID || nodeID == '')
20 return makeError(response, errors.nodeid_required.n);
21 if (!storage.existsNode(nodeID))
22 return makeError(response, errors.node_does_not_exist.n);
23
24 var configuration = storage.getConfiguration(nodeID);
25 if (!configuration['pubsub#subscribe'])
26 return makeError(response, errors.sub.subscribe.not_supported.n);
27
28 var affil = storage.getAffiliation(toBareJID(to), nodeID);
29 if (affil == 'publish-only' || affil == 'outcast')
30 return makeError(response, errors.pub.publish.insufficient_privileges.n);
31
32 var jid = request.getAttribute('jid');
33 if (!jid || toBareJID(jid) != toBareJID(to))
34 return makeError(response, errors.sub.subscribe.jids_do_not_match.n);
35
36 /*
37 // SECTION 6.3.7
38 var options = pubsub.getChild('options');
39 if (options && config.enabled('subscription-options')) {
40 if (options.getAttribute('node') || options.getAttribute('jid'))
41 return makeError(response, errors.bad_request.n);
42
43 var x = options.getChild('x', 'jabber:x:data');
44 if (!x || x.getAttribute('type') != 'submit')
45 return makeError(response, errors.bad_request.n);
46
47 var form = forms.parse(x, true);
48 if (typeof form == 'number')
49 return makeError(response, form);
50
51 var conf = form;
52 }*/
53
54 var subID;
55 if (configuration['pubsub#access_model'] == 'open') {
56 subID = storage.subscribe(nodeID, jid, 'subscribe');
57 if (typeof subID == 'number')
58 return makeError(response, subID);
59 } else if (configuration['pubsub#access_model'] == 'authorize') {
60 subID = storage.subscribe(nodeID, jid, 'pending');
61 if (typeof subID == 'number')
62 return makeError(response, subID);
63
64 var affiliates = storage.getAffiliationsFromNodeID(nodeID);
65 var form = forms.build('form', 'subscribe_authorization', {allow: false, node: nodeID, subscriber_jid: jid}, true); //168
66
67 for (var i in affiliates) {
68 if (affiliates[i] == 'super-owner' || affiliates[i] == 'owner') {
69 var message = xmpp.message({to: i}).cnode(form);
70 conn.send(message); // FIXME: impossible à faire d’ici
71 }
72 }
73 } else if (configuration['pubsub#access_model'] == 'whitelist') {
74 var affil = storage.getAffiliation(jid, nodeID);
75 if (affil != 'super-owner' && affil != 'owner' && affil != 'publisher' && affil != 'member')
76 return makeError(response, errors.sub.subscribe.not_on_whitelist.n);
77
78 subID = storage.subscribe(nodeID, jid);
79 if (typeof subID == 'number')
80 return makeError(response, subID);
81 }
82
83 response.c('pubsub', {xmlns: NS.PUBSUB})
84 .c('subscription', {node: nodeID, jid: jid, subid: subID.subid, subscription: subID.type});
85
86 if (config.enabled('last-published')) {
87 var last = storage.getLastItem(nodeID);
88 if (typeof last != 'number') {
89 var item = storage.getItem(nodeID, last);
90 if (typeof item != 'number') {
91 var attr = {};
92 attr[last] = {content: item};
93 notifs.send(jid, 'items', nodeID, attr);
94 }
95 }
96 }
97
98 return response;
99 }
100 }
101
102 // SECTION 6.2: Unsubscribe from a Node
103 exports.unsubscribe = {
104 type: 'set',
105 child: 'pubsub',
106 ns: NS.PUBSUB,
107 pschild: 'unsubscribe',
108 func: function(response, stanza, request) {
109 if (!config.enabled('subscribe'))
110 return makeError(response, errors.sub.subscribe.not_supported.n);
111
112 var nodeID = request.getAttribute('node');
113 if (!nodeID || nodeID == '')
114 return makeError(response, errors.nodeid_required.n);
115 if (!storage.existsNode(nodeID))
116 return makeError(response, errors.node_does_not_exist.n);
117
118 var jid = request.getAttribute('jid');
119 if (!jid || toBareJID(jid) != toBareJID(to))
120 return makeError(response, errors.sub.unsubscribe.insufficient_privileges.n);
121
122 var subID = storage.subscribe(nodeID, jid, 'none');
123 if (typeof subID == 'number')
124 return makeError(response, subID);
125
126 return response;
127 }
128 }