Mercurial > psgxs
diff modules/mod_owner.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 |
line wrap: on
line diff
new file mode 100644 --- /dev/null +++ b/modules/mod_owner.js @@ -0,0 +1,121 @@ +var config = require('../configuration'); +var storage = require('../storage'); +var forms = require('../forms'); +var notifs = require('../notifs'); +var errors = require('../errors'); +var makeError = errors.makeError; +var toBareJID = require('../util').toBareJID; +var NS = require('../namespaces'); + +// SECTION 8.1: Create a Node +exports.getConfigure = { + type: 'set', + child: 'pubsub', + ns: NS.PUBSUB, + pschild: 'create', + func: function(response, stanza, request, to) { + if (!config.enabled('create-nodes')) + return makeError(response, errors.owner.create.node_creation_not_supported.n); + + var instant = false; + + var nodeID = request.getAttribute('node'); + if (!nodeID || nodeID == '') { + if (!config.enabled('instant-nodes')) + return makeError(response, errors.owner.create.instant_nodes_not_supported.n); + nodeID = utils.makeRandomId(); + instant = true; + } + if (storage.existsNode(nodeID)) + return makeError(response, errors.owner.create.nodeid_already_exists.n); + + var bare = toBareJID(to); + var right = false; + + // Check for super-owner + for (var i in config.owner) + if (config.owner[i] == bare) + right = true; + + // Check for authorized user + for (var i in config.allowCreateNode) + if (config.allowCreateNode[i].exec(bare)) + right = true; + + if (!right) + return makeError(response, errors.forbidden.n); + + var r = storage.createNode(nodeID); + if (typeof r == 'number') + return makeError(response, r); + + if (instant) + response.c('pubsub', {xmlns: NS.PUBSUB}) + .c('create', {node: nodeID}); + + return response; + } +} + +// SECTION 8.4: Delete a Node +exports['delete'] = { + type: 'set', + child: 'pubsub', + ns: NS.PUBSUB_OWNER, + pschild: 'delete', + func: function(response, stanza, request, to) { + if (!config.enabled('delete-nodes')) + return makeError(response, errors.feature_not_implemented.n); //XXX + + var nodeID = request.getAttribute('node'); + if (!nodeID) + return makeError(response, errors.nodeid_required.n); + if (!storage.existsNode(nodeID)) + return makeError(response, errors.node_does_not_exist.n); + + var affil = storage.getAffiliation(toBareJID(to), nodeID); + if (affil != 'super-owner' && affil != 'owner') + return makeError(response, errors.forbidden.n); + + var notifs = storage.deleteNode(nodeID); + if (typeof notifs == 'number') + return makeError(response, r); + + notifs.send(notifs, 'delete', nodeID); + + return response; + } +} + +// SECTION 8.5: Purge All Node Items +exports.purge = { + type: 'set', + child: 'pubsub', + ns: NS.PUBSUB_OWNER, + pschild: 'purge', + func: function(response, stanza, request, to) { + if (!config.enabled('purge-nodes')) + return makeError(response, errors.owner.purge.node_purging_not_supported.n); //XXX + + var nodeID = request.getAttribute('node'); + if (!nodeID) + return makeError(response, errors.nodeid_required.n); + if (!storage.existsNode(nodeID)) + return makeError(response, errors.node_does_not_exist.n); + + var affil = storage.getAffiliation(toBareJID(to), nodeID); + if (affil != 'super-owner' && affil != 'owner') + return makeError(response, errors.forbidden.n); + + if (!config.enabled('persistent-items')) //FIXME: autre condition, supporté par le node + return makeError(response, errors.owner.purge.node_does_not_persist_items.n); + + var notifs = storage.purgeNode(nodeID); + if (typeof notifs == 'number') + return makeError(response, r); + + notifs.send(notifs, 'purge', nodeID); + + return response; + } +}