|
|
|
@ -3,45 +3,59 @@ var fs = require('fs'),
|
|
|
|
|
async = require('async'),
|
|
|
|
|
winston = require('winston'),
|
|
|
|
|
eventEmitter = require('events').EventEmitter,
|
|
|
|
|
db = require('./database'),
|
|
|
|
|
db = require('./database');
|
|
|
|
|
|
|
|
|
|
plugins = {
|
|
|
|
|
libraries: {},
|
|
|
|
|
loadedHooks: {},
|
|
|
|
|
staticDirs: {},
|
|
|
|
|
cssFiles: [],
|
|
|
|
|
(function(Plugins) {
|
|
|
|
|
|
|
|
|
|
Plugins.libraries = {};
|
|
|
|
|
Plugins.loadedHooks = {};
|
|
|
|
|
Plugins.staticDirs = {};
|
|
|
|
|
Plugins.cssFiles = [];
|
|
|
|
|
|
|
|
|
|
Plugins.initialized = false;
|
|
|
|
|
|
|
|
|
|
// Events
|
|
|
|
|
readyEvent: new eventEmitter,
|
|
|
|
|
Plugins.readyEvent = new eventEmitter;
|
|
|
|
|
|
|
|
|
|
Plugins.init = function() {
|
|
|
|
|
console.log('plugins init called');
|
|
|
|
|
if (Plugins.initialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
|
if (this.initialized) return;
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Initializing plugins system');
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.info('[plugins] Initializing plugins system');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.reload(function(err) {
|
|
|
|
|
Plugins.reload(function(err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Plugins OK');
|
|
|
|
|
|
|
|
|
|
plugins.initialized = true;
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.info('[plugins] Plugins OK');
|
|
|
|
|
}
|
|
|
|
|
Plugins.initialized = true;
|
|
|
|
|
plugins.readyEvent.emit('ready');
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
ready: function(callback) {
|
|
|
|
|
if (!this.initialized) this.readyEvent.once('ready', callback);
|
|
|
|
|
else callback();
|
|
|
|
|
},
|
|
|
|
|
initialized: false,
|
|
|
|
|
reload: function(callback) {
|
|
|
|
|
var _self = this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.ready = function(callback) {
|
|
|
|
|
if (!Plugins.initialized) {
|
|
|
|
|
Plugins.readyEvent.once('ready', callback);
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.reload = function(callback) {
|
|
|
|
|
// Resetting all local plugin data
|
|
|
|
|
this.loadedHooks = {};
|
|
|
|
|
this.staticDirs = {};
|
|
|
|
|
this.cssFiles.length = 0;
|
|
|
|
|
Plugins.loadedHooks = {};
|
|
|
|
|
Plugins.staticDirs = {};
|
|
|
|
|
Plugins.cssFiles.length = 0;
|
|
|
|
|
|
|
|
|
|
// Read the list of activated plugins and require their libraries
|
|
|
|
|
async.waterfall([
|
|
|
|
@ -52,9 +66,12 @@ var fs = require('fs'),
|
|
|
|
|
if (plugins && Array.isArray(plugins) && plugins.length > 0) {
|
|
|
|
|
async.each(plugins, function(plugin, next) {
|
|
|
|
|
var modulePath = path.join(__dirname, '../node_modules/', plugin);
|
|
|
|
|
if (fs.existsSync(modulePath)) _self.loadPlugin(modulePath, next);
|
|
|
|
|
else {
|
|
|
|
|
if (global.env === 'development') winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
|
|
|
|
|
if (fs.existsSync(modulePath)) {
|
|
|
|
|
Plugins.loadPlugin(modulePath, next);
|
|
|
|
|
} else {
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
|
|
|
|
|
}
|
|
|
|
|
next(); // Ignore this plugin silently
|
|
|
|
|
}
|
|
|
|
|
}, next);
|
|
|
|
@ -62,8 +79,8 @@ var fs = require('fs'),
|
|
|
|
|
},
|
|
|
|
|
function(next) {
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence');
|
|
|
|
|
Object.keys(_self.loadedHooks).forEach(function(hook) {
|
|
|
|
|
var hooks = _self.loadedHooks[hook];
|
|
|
|
|
Object.keys(Plugins.loadedHooks).forEach(function(hook) {
|
|
|
|
|
var hooks = Plugins.loadedHooks[hook];
|
|
|
|
|
hooks = hooks.sort(function(a, b) {
|
|
|
|
|
return a.priority - b.priority;
|
|
|
|
|
});
|
|
|
|
@ -72,12 +89,13 @@ var fs = require('fs'),
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
},
|
|
|
|
|
loadPlugin: function(pluginPath, callback) {
|
|
|
|
|
var _self = this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.loadPlugin = function(pluginPath, callback) {
|
|
|
|
|
fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) {
|
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pluginData = JSON.parse(data),
|
|
|
|
|
libraryPath, staticDir;
|
|
|
|
@ -89,14 +107,14 @@ var fs = require('fs'),
|
|
|
|
|
|
|
|
|
|
fs.exists(libraryPath, function(exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
if (!_self.libraries[pluginData.id]) {
|
|
|
|
|
_self.libraries[pluginData.id] = require(libraryPath);
|
|
|
|
|
if (!Plugins.libraries[pluginData.id]) {
|
|
|
|
|
Plugins.libraries[pluginData.id] = require(libraryPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register hooks for this plugin
|
|
|
|
|
if (pluginData.hooks && Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) {
|
|
|
|
|
async.each(pluginData.hooks, function(hook, next) {
|
|
|
|
|
_self.registerHook(pluginData.id, hook, next);
|
|
|
|
|
Plugins.registerHook(pluginData.id, hook, next);
|
|
|
|
|
}, next);
|
|
|
|
|
} else {
|
|
|
|
|
next(null);
|
|
|
|
@ -118,7 +136,7 @@ var fs = require('fs'),
|
|
|
|
|
|
|
|
|
|
fs.exists(staticDir, function(exists) {
|
|
|
|
|
if (exists) {
|
|
|
|
|
_self.staticDirs[pluginData.id] = staticDir;
|
|
|
|
|
Plugins.staticDirs[pluginData.id] = staticDir;
|
|
|
|
|
next();
|
|
|
|
|
} else next();
|
|
|
|
|
});
|
|
|
|
@ -131,7 +149,7 @@ var fs = require('fs'),
|
|
|
|
|
winston.info('[plugins] Found ' + pluginData.css.length + ' CSS file(s) for plugin ' + pluginData.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_self.cssFiles = _self.cssFiles.concat(pluginData.css.map(function(file) {
|
|
|
|
|
Plugins.cssFiles = Plugins.cssFiles.concat(pluginData.css.map(function(file) {
|
|
|
|
|
return path.join('/plugins', pluginData.id, file);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
@ -145,8 +163,9 @@ var fs = require('fs'),
|
|
|
|
|
} else callback(new Error('Could not load plugin system'))
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
registerHook: function(id, data, callback) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.registerHook = function(id, data, callback) {
|
|
|
|
|
/*
|
|
|
|
|
`data` is an object consisting of (* is required):
|
|
|
|
|
`data.hook`*, the name of the NodeBB hook
|
|
|
|
@ -154,7 +173,6 @@ var fs = require('fs'),
|
|
|
|
|
`data.callbacked`, whether or not the hook expects a callback (true), or a return (false). Only used for filters. (Default: false)
|
|
|
|
|
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
|
|
|
|
|
*/
|
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
|
|
if (data.hook && data.method) {
|
|
|
|
|
data.id = id;
|
|
|
|
@ -164,20 +182,22 @@ var fs = require('fs'),
|
|
|
|
|
return memo[prop];
|
|
|
|
|
} else {
|
|
|
|
|
// Couldn't find method by path, assuming property with periods in it (evil!)
|
|
|
|
|
_self.libraries[data.id][data.method];
|
|
|
|
|
Plugins.libraries[data.id][data.method];
|
|
|
|
|
}
|
|
|
|
|
}, _self.libraries[data.id]);
|
|
|
|
|
}, Plugins.libraries[data.id]);
|
|
|
|
|
|
|
|
|
|
_self.loadedHooks[data.hook] = _self.loadedHooks[data.hook] || [];
|
|
|
|
|
_self.loadedHooks[data.hook].push(data);
|
|
|
|
|
Plugins.loadedHooks[data.hook] = Plugins.loadedHooks[data.hook] || [];
|
|
|
|
|
Plugins.loadedHooks[data.hook].push(data);
|
|
|
|
|
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id);
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id);
|
|
|
|
|
}
|
|
|
|
|
callback();
|
|
|
|
|
} else return;
|
|
|
|
|
},
|
|
|
|
|
fireHook: function(hook, args, callback) {
|
|
|
|
|
var _self = this
|
|
|
|
|
hookList = this.loadedHooks[hook];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.fireHook = function(hook, args, callback) {
|
|
|
|
|
hookList = Plugins.loadedHooks[hook];
|
|
|
|
|
|
|
|
|
|
if (hookList && Array.isArray(hookList)) {
|
|
|
|
|
//if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
|
|
|
|
@ -187,9 +207,9 @@ var fs = require('fs'),
|
|
|
|
|
async.reduce(hookList, args, function(value, hookObj, next) {
|
|
|
|
|
if (hookObj.method) {
|
|
|
|
|
if (hookObj.callbacked) { // If a callback is present (asynchronous method)
|
|
|
|
|
hookObj.method.call(_self.libraries[hookObj.id], value, next);
|
|
|
|
|
hookObj.method.call(Plugins.libraries[hookObj.id], value, next);
|
|
|
|
|
} else { // Synchronous method
|
|
|
|
|
value = hookObj.method.call(_self.libraries[hookObj.id], value);
|
|
|
|
|
value = hookObj.method.call(Plugins.libraries[hookObj.id], value);
|
|
|
|
|
next(null, value);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
@ -209,7 +229,7 @@ var fs = require('fs'),
|
|
|
|
|
case 'action':
|
|
|
|
|
async.each(hookList, function(hookObj) {
|
|
|
|
|
if (hookObj.method) {
|
|
|
|
|
hookObj.method.call(_self.libraries[hookObj.id], args);
|
|
|
|
|
hookObj.method.call(Plugins.libraries[hookObj.id], args);
|
|
|
|
|
} else {
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
@ -228,12 +248,14 @@ var fs = require('fs'),
|
|
|
|
|
callback(null, returnVal);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
isActive: function(id, callback) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.isActive = function(id, callback) {
|
|
|
|
|
db.isSetMember('plugins:active', id, callback);
|
|
|
|
|
},
|
|
|
|
|
toggleActive: function(id, callback) {
|
|
|
|
|
this.isActive(id, function(err, active) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Plugins.toggleActive = function(id, callback) {
|
|
|
|
|
Plugins.isActive(id, function(err, active) {
|
|
|
|
|
if (err) {
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\'');
|
|
|
|
|
return;
|
|
|
|
@ -259,9 +281,9 @@ var fs = require('fs'),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
showInstalled: function(callback) {
|
|
|
|
|
var _self = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Plugins.showInstalled = function(callback) {
|
|
|
|
|
npmPluginPath = path.join(__dirname, '../node_modules');
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
@ -296,7 +318,7 @@ var fs = require('fs'),
|
|
|
|
|
return next(err, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_self.isActive(config.id, function(err, active) {
|
|
|
|
|
Plugins.isActive(config.id, function(err, active) {
|
|
|
|
|
if (err) next(new Error('no-active-state'));
|
|
|
|
|
|
|
|
|
|
delete config.library;
|
|
|
|
@ -320,8 +342,4 @@ var fs = require('fs'),
|
|
|
|
|
callback(err, plugins);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
plugins.init();
|
|
|
|
|
|
|
|
|
|
module.exports = plugins;
|
|
|
|
|
}(exports));
|
|
|
|
|