Mercurial > psgxs
changeset 41:bc717575e66a
Much better handling of modules.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 20 Feb 2011 15:51:12 +0100 |
parents | 741110cdcfcf |
children | 07ca0263a53f |
files | configuration.js modules.js modules/http/mod_atom.js modules/mod_adhoc.js modules/mod_configure.js modules/mod_disco.js modules/mod_http.js modules/mod_manage.js modules/mod_options.js modules/mod_owner.js modules/mod_publish.js modules/mod_publish_message.js modules/mod_retrieve.js modules/mod_subscribe.js modules/mod_version.js |
diffstat | 15 files changed, 87 insertions(+), 129 deletions(-) [+] |
line wrap: on
line diff
--- a/configuration.js +++ b/configuration.js @@ -27,6 +27,7 @@ config.os = 'GNU/Linux'; config.allowCreateNode = [/@example\.com$/]; // value is an array of RegExp JIDs. If only super-owner should be able to create nodes, use []. config.backend = 'directory'; // Put backends in “backends” directory. config.access = 'open'; // values include open, whitelist (by members) and authorize (owners of the node receive notification). presence and roster aren’t implemented yet. +config.pluginsDir = 'modules'; config.activated = [ 'auto-create',
--- a/modules.js +++ b/modules.js @@ -1,18 +1,46 @@ var modules = exports; -var files = require('fs').readdirSync('modules'); +var fs = require('fs'); +var dir = require('./configuration').pluginsDir; +var files = fs.readdirSync(dir); var regex = /^mod_.*\.js/; +var Script = process.binding('evals').Script; +var sandbox = { + process: process, + config: require('./configuration'), + xmpp: require('xmpp'), + errors: require('./errors'), + forms: require('./forms'), + makeError: require('./errors').makeError, + notifs: require('./notifs'), + NS: require('./namespaces'), + require: require, + storage: require('./storage'), + toBareJID: require('./fdsq').toBareJID, + exports: {} +}; + for (var i in files) { var file = files[i]; if (!regex.test(file)) continue; - var module = require('./modules/' + files[i]); - for (var j in module) { - if (typeof modules[j] != 'undefined') - console.log('WARNING: module '+j+' already loaded.'); + fs.readFile(dir + '/' + file, function(err, data) { + if (err) { + console.log('ERROR: loading module “'+file+'”.'); // FIXME: file is always the last file of the list… + return; + } - modules[j] = module[j]; - } + Script.runInNewContext(data, sandbox, dir + '/' + file); // FIXME: the same. + var module = sandbox.exports; + sandbox.exports = {}; + + for (var j in module) { + if (typeof modules[j] != 'undefined') + console.log('WARNING: module '+j+' already loaded.'); + + modules[j] = module[j]; + } + }); }
--- a/modules/http/mod_atom.js +++ b/modules/http/mod_atom.js @@ -1,11 +1,3 @@ -var config = require('../../configuration'); -var storage = require('../../storage'); -var forms = require('../../forms'); -var errors = require('../../errors'); -var makeError = errors.makeError; -var NS = require('../../namespaces'); -var xmpp = require('xmpp'); - exports.atom = { url: /^\/atom/, func: function (req, res) { @@ -28,7 +20,7 @@ exports.atom = { if (md['pubsub#description']) response.c('subtitle').t(md['pubsub#description']).up(); if (md['pubsub#creation_date']) - response.c('published').t(md['pubsub#creation_date']).up(); + response.c('published').t(md['pubsub#creation_date'].toString()).up(); children = storage.getItems(nodeID); if (typeof children == 'number')
--- a/modules/mod_adhoc.js +++ b/modules/mod_adhoc.js @@ -1,12 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var forms = require('../forms'); -var errors = require('../errors'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); -var xmpp = require('xmpp'); - // XEP-0050: Ad-Hoc Commands exports.adHoc = { type: 'set',
--- a/modules/mod_configure.js +++ b/modules/mod_configure.js @@ -1,11 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var forms = require('../forms'); -var errors = require('../errors'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); - // SECTION 8.2: Configure a Node exports.getConfigure = { type: 'get', @@ -30,8 +22,8 @@ exports.getConfigure = { if (!conf) return makeError(response, 42); // FIXME - response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}); - response.c('configure', {node: nodeID}); + response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}) + .c('configure', {node: nodeID}); var form = forms.build('form', 'node_config', conf, true); response.cnode(form); @@ -50,8 +42,8 @@ exports.default = { if (!config.enabled('config-node')) return makeError(response, errors.owner.default_options.node_configuration_not_supported.n); - response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}); - response.c('default'); + response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}) + .c('default'); var form = forms.build('node_config', service_configuration.node_config, null, true); response.cnode(form);
--- a/modules/mod_disco.js +++ b/modules/mod_disco.js @@ -1,10 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var forms = require('../forms'); -var errors = require('../errors'); -var makeError = errors.makeError; -var NS = require('../namespaces'); - // SECTION 5.1: Discover Features exports.disco_info = { type: 'get',
--- a/modules/mod_http.js +++ b/modules/mod_http.js @@ -1,23 +1,49 @@ -var config = require('../configuration'); +var sites = {}; + var http = require('http'); -var files = require('fs').readdirSync('modules/http'); +var fs = require('fs'); +var dir = require('./configuration').pluginsDir; +var files = fs.readdirSync(dir + '/http'); var regex = /^mod_.*\.js/; -var sites = {}; +var Script = process.binding('evals').Script; +var sandbox = { + config: require('./configuration'), + xmpp: require('xmpp'), + errors: require('./errors'), + forms: require('./forms'), + makeError: require('./errors').makeError, + notifs: require('./notifs'), + NS: require('./namespaces'), + require: require, + storage: require('./storage'), + toBareJID: require('./fdsq').toBareJID, + exports: {} +}; for (var i in files) { var file = files[i]; if (!regex.test(file)) continue; - var module = require('./http/' + files[i]); - for (var j in module) { - if (typeof sites[j] != 'undefined') - console.log('WARNING: module '+j+' already loaded.'); + fs.readFile(dir + '/http/' + file, function(err, data) { + if (err) { + console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list… + return; + } - sites[j] = module[j]; - } + Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same. + var module = sandbox.exports; + sandbox.exports = {}; + + for (var j in module) { + if (typeof sites[j] != 'undefined') + console.log('WARNING: module http/'+j+' already loaded.'); + + sites[j] = module[j]; + } + }); } http.createServer(function (req, res) {
--- a/modules/mod_manage.js +++ b/modules/mod_manage.js @@ -1,10 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var errors = require('../errors'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); - // SECTION 8.8.1: Retrieve Subscriptions List exports.manageRetrieveSub = { type: 'get', @@ -25,8 +18,8 @@ exports.manageRetrieveSub = { if (affil != 'super-owner' && affil != 'owner') return makeError(response, errors.forbidden.n); - response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}); - response.c('subscriptions', {node: nodeID}); + response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}) + .c('subscriptions', {node: nodeID}); var subs = storage.getSubscriptionsFromNodeID(nodeID) for (var jid in subs) @@ -57,8 +50,8 @@ exports.manageRetrieveAff = { if (affil != 'super-owner' && affil != 'owner') return makeError(response, errors.owner.manage_affiliations.retrieve_list.entity_is_not_an_owner.n); - response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}); - response.c('affiliations', {node: nodeID}); + response.c('pubsub', {xmlns: NS.PUBSUB_OWNER}) + .c('affiliations', {node: nodeID}); for (var jid in affils) response.c('affiliation', {jid: jid, affiliation: affils[jid]}).up();
--- a/modules/mod_options.js +++ b/modules/mod_options.js @@ -1,11 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var forms = require('../forms'); -var errors = require('../errors'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); - // SECTION 6.3.2: Configure Subscription Options (Request) exports.getConfigureSub = { type: 'get', @@ -32,8 +24,8 @@ exports.getConfigureSub = { if (!subs.subid) // FIXME: better test for empty object. return makeError(response, errors.sub.configure.no_such_subscriber.n); - response.c('pubsub', {xmlns: NS.PUBSUB}); - response.c('options', {node: nodeID, jid: jid}); + response.c('pubsub', {xmlns: NS.PUBSUB}) + .c('options', {node: nodeID, jid: jid}); var form = forms.build('form', 'subscribe_options', subs.options, true); response.cnode(form);
--- a/modules/mod_owner.js +++ b/modules/mod_owner.js @@ -1,13 +1,3 @@ -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 fdsq = require('../fdsq'); -var toBareJID = fdsq.toBareJID; -var NS = require('../namespaces'); - // SECTION 8.1: Create a Node exports.create = { type: 'set',
--- a/modules/mod_publish.js +++ b/modules/mod_publish.js @@ -1,12 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var notifs = require('../notifs'); -var errors = require('../errors'); -var makeError = errors.makeError; -var fdsq = require('../fdsq'); -var toBareJID = fdsq.toBareJID; -var NS = require('../namespaces'); - // SECTION 7.1: Publish an Item to a Node exports.publish = { type: 'set',
--- a/modules/mod_publish_message.js +++ b/modules/mod_publish_message.js @@ -1,13 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var notifs = require('../notifs'); -var errors = require('../errors'); -var makeError = errors.makeError; -var fdsq = require('../fdsq'); -var toBareJID = fdsq.toBareJID; -var NS = require('../namespaces'); -var xmpp = require('xmpp'); - exports.publishMessage = { stanza: 'message', child: 'body',
--- a/modules/mod_retrieve.js +++ b/modules/mod_retrieve.js @@ -1,12 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var errors = require('../errors'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); - -console.log(require('sys').inspect(toBareJID, false, null)); - // SECTION 5.6: Retrieve Subscriptions exports.retrieveSubscriptions = { type: 'get', @@ -25,8 +16,8 @@ exports.retrieveSubscriptions = { } else var subs = storage.getSubscription(toBareJID(to)); - response.c('pubsub', {xmlns: NS.PUBSUB}); - response.c('subscriptions'); + response.c('pubsub', {xmlns: NS.PUBSUB}) + .c('subscriptions'); for (i in subs) response.c('subscription', {node: i, jid: to, subscription: subs[i].type, subid: subs[i].subid}).up(); @@ -54,8 +45,8 @@ exports.retrieveAffiliations = { } else var affils = storage.getAffiliationsFromJID(toBareJID(to)); - response.c('pubsub', {xmlns: NS.PUBSUB}); - response.c('affiliations'); + response.c('pubsub', {xmlns: NS.PUBSUB}) + .c('affiliations'); for (i in affils) response.c('affiliation', {node: i, affiliation: affils[i]}).up(); @@ -111,7 +102,7 @@ exports.retrieveItems = { continue; response.c('item', {id: item[i]}) - response.cnode(j).up().up(); + .cnode(j).up().up(); } } else { response.c('items', {node: nodeID});
--- a/modules/mod_subscribe.js +++ b/modules/mod_subscribe.js @@ -1,11 +1,3 @@ -var config = require('../configuration'); -var storage = require('../storage'); -var errors = require('../errors'); -var notifs = require('../notifs'); -var makeError = errors.makeError; -var toBareJID = require('../fdsq').toBareJID; -var NS = require('../namespaces'); - // SECTION 6.1: Subscribe to a Node exports.subscribe = { type: 'set',