view modules.js @ 57:addbf6bbfaa8

Various fixes for the migration to ltx.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 12 Sep 2011 23:45:00 +0200
parents 99bd1d1ac071
children
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var modules = exports;

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 fdsq = require('./fdsq');

var sandbox = {
	process: process,
	console: console,
	config: require('./configuration'),
	Element: require('ltx').Element,
	uuid: require('uuid'),
	errors: require('./errors'),
	forms: require('./forms'),
	makeError: require('./errors').makeError,
	notifs: require('./notifs'),
	NS: require('./namespaces'),
	require: require,
	storage: require('./storage'),
	JID: {
		toBare: fdsq.toBare,
		toResource: fdsq.toResource
	},
	exports: {}
};

for (var i in files) {
	var file = files[i];
	if (!regex.test(file))
		continue;

	fs.readFile(dir + '/' + file, function(err, data) {
		if (err) {
			console.log('Erreur de chargement de module.');
			return;
		}

		Script.runInNewContext(data, sandbox, dir + '/' + file);
		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];
			console.log('Module '+j+' loaded!');
		}
	});
}