# HG changeset patch # User Emmanuel Gil Peyrot # Date 1298213472 -3600 # Node ID bc717575e66aa5bb0a268842c18af76186390f96 # Parent 741110cdcfcfc62ecad7c3a7f74a7d72c71ad50c Much better handling of modules. diff --git a/configuration.js b/configuration.js --- 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', diff --git a/modules.js b/modules.js --- 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]; + } + }); } diff --git a/modules/http/mod_atom.js b/modules/http/mod_atom.js --- 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') diff --git a/modules/mod_adhoc.js b/modules/mod_adhoc.js --- 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', diff --git a/modules/mod_configure.js b/modules/mod_configure.js --- 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); diff --git a/modules/mod_disco.js b/modules/mod_disco.js --- 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', diff --git a/modules/mod_http.js b/modules/mod_http.js --- 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) { diff --git a/modules/mod_manage.js b/modules/mod_manage.js --- 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(); diff --git a/modules/mod_options.js b/modules/mod_options.js --- 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); diff --git a/modules/mod_owner.js b/modules/mod_owner.js --- 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', diff --git a/modules/mod_publish.js b/modules/mod_publish.js --- 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', diff --git a/modules/mod_publish_message.js b/modules/mod_publish_message.js --- 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', diff --git a/modules/mod_retrieve.js b/modules/mod_retrieve.js --- 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}); diff --git a/modules/mod_subscribe.js b/modules/mod_subscribe.js --- 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', diff --git a/modules/mod_version.js b/modules/mod_version.js --- a/modules/mod_version.js +++ b/modules/mod_version.js @@ -1,8 +1,4 @@ // XEP-0092: Software Version - -var config = require('../configuration'); -var NS = require('../namespaces'); - exports.version = { type: 'get', child: 'query',