|
|
@ -3,325 +3,343 @@ var fs = require('fs'),
|
|
|
|
async = require('async'),
|
|
|
|
async = require('async'),
|
|
|
|
winston = require('winston'),
|
|
|
|
winston = require('winston'),
|
|
|
|
eventEmitter = require('events').EventEmitter,
|
|
|
|
eventEmitter = require('events').EventEmitter,
|
|
|
|
db = require('./database'),
|
|
|
|
db = require('./database');
|
|
|
|
|
|
|
|
|
|
|
|
plugins = {
|
|
|
|
(function(Plugins) {
|
|
|
|
libraries: {},
|
|
|
|
|
|
|
|
loadedHooks: {},
|
|
|
|
|
|
|
|
staticDirs: {},
|
|
|
|
|
|
|
|
cssFiles: [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Events
|
|
|
|
Plugins.libraries = {};
|
|
|
|
readyEvent: new eventEmitter,
|
|
|
|
Plugins.loadedHooks = {};
|
|
|
|
|
|
|
|
Plugins.staticDirs = {};
|
|
|
|
|
|
|
|
Plugins.cssFiles = [];
|
|
|
|
|
|
|
|
|
|
|
|
init: function() {
|
|
|
|
Plugins.initialized = false;
|
|
|
|
if (this.initialized) return;
|
|
|
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Initializing plugins system');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.reload(function(err) {
|
|
|
|
// Events
|
|
|
|
if (err) {
|
|
|
|
Plugins.readyEvent = new eventEmitter;
|
|
|
|
if (global.env === 'development') winston.info('[plugins] NodeBB encountered a problem while loading plugins', err.message);
|
|
|
|
|
|
|
|
return;
|
|
|
|
Plugins.init = function() {
|
|
|
|
|
|
|
|
console.log('plugins init called');
|
|
|
|
|
|
|
|
if (Plugins.initialized) {
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
|
|
|
winston.info('[plugins] Initializing plugins system');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Plugins.reload(function(err) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
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');
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
|
|
|
winston.info('[plugins] Plugins OK');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Plugins.initialized = true;
|
|
|
|
|
|
|
|
plugins.readyEvent.emit('ready');
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Plugins.ready = function(callback) {
|
|
|
|
|
|
|
|
if (!Plugins.initialized) {
|
|
|
|
|
|
|
|
Plugins.readyEvent.once('ready', callback);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Plugins.reload = function(callback) {
|
|
|
|
|
|
|
|
// Resetting all local plugin data
|
|
|
|
|
|
|
|
Plugins.loadedHooks = {};
|
|
|
|
|
|
|
|
Plugins.staticDirs = {};
|
|
|
|
|
|
|
|
Plugins.cssFiles.length = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the list of activated plugins and require their libraries
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
db.getSetMembers('plugins:active', next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(plugins, next) {
|
|
|
|
|
|
|
|
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)) {
|
|
|
|
|
|
|
|
Plugins.loadPlugin(modulePath, next);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
|
|
|
winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
next(); // Ignore this plugin silently
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}, next);
|
|
|
|
|
|
|
|
} else next();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence');
|
|
|
|
|
|
|
|
Object.keys(Plugins.loadedHooks).forEach(function(hook) {
|
|
|
|
|
|
|
|
var hooks = Plugins.loadedHooks[hook];
|
|
|
|
|
|
|
|
hooks = hooks.sort(function(a, b) {
|
|
|
|
|
|
|
|
return a.priority - b.priority;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
plugins.initialized = true;
|
|
|
|
next();
|
|
|
|
plugins.readyEvent.emit('ready');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
], callback);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ready: function(callback) {
|
|
|
|
|
|
|
|
if (!this.initialized) this.readyEvent.once('ready', callback);
|
|
|
|
Plugins.loadPlugin = function(pluginPath, callback) {
|
|
|
|
else callback();
|
|
|
|
fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) {
|
|
|
|
},
|
|
|
|
if (err) {
|
|
|
|
initialized: false,
|
|
|
|
return callback(err);
|
|
|
|
reload: function(callback) {
|
|
|
|
}
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
|
|
|
|
var pluginData = JSON.parse(data),
|
|
|
|
// Resetting all local plugin data
|
|
|
|
libraryPath, staticDir;
|
|
|
|
this.loadedHooks = {};
|
|
|
|
|
|
|
|
this.staticDirs = {};
|
|
|
|
async.parallel([
|
|
|
|
this.cssFiles.length = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Read the list of activated plugins and require their libraries
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
function(next) {
|
|
|
|
db.getSetMembers('plugins:active', next);
|
|
|
|
if (pluginData.library) {
|
|
|
|
},
|
|
|
|
libraryPath = path.join(pluginPath, pluginData.library);
|
|
|
|
function(plugins, next) {
|
|
|
|
|
|
|
|
if (plugins && Array.isArray(plugins) && plugins.length > 0) {
|
|
|
|
fs.exists(libraryPath, function(exists) {
|
|
|
|
async.each(plugins, function(plugin, next) {
|
|
|
|
if (exists) {
|
|
|
|
var modulePath = path.join(__dirname, '../node_modules/', plugin);
|
|
|
|
if (!Plugins.libraries[pluginData.id]) {
|
|
|
|
if (fs.existsSync(modulePath)) _self.loadPlugin(modulePath, next);
|
|
|
|
Plugins.libraries[pluginData.id] = require(libraryPath);
|
|
|
|
else {
|
|
|
|
}
|
|
|
|
if (global.env === 'development') winston.warn('[plugins] Plugin \'' + plugin + '\' not found');
|
|
|
|
|
|
|
|
next(); // Ignore this plugin silently
|
|
|
|
// Register hooks for this plugin
|
|
|
|
|
|
|
|
if (pluginData.hooks && Array.isArray(pluginData.hooks) && pluginData.hooks.length > 0) {
|
|
|
|
|
|
|
|
async.each(pluginData.hooks, function(hook, next) {
|
|
|
|
|
|
|
|
Plugins.registerHook(pluginData.id, hook, next);
|
|
|
|
|
|
|
|
}, next);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
next(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, next);
|
|
|
|
});
|
|
|
|
} else next();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
function(next) {
|
|
|
|
function(next) {
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Sorting hooks to fire in priority sequence');
|
|
|
|
// Static Directories for Plugins
|
|
|
|
Object.keys(_self.loadedHooks).forEach(function(hook) {
|
|
|
|
if (pluginData.staticDir) {
|
|
|
|
var hooks = _self.loadedHooks[hook];
|
|
|
|
staticDir = path.join(pluginPath, pluginData.staticDir);
|
|
|
|
hooks = hooks.sort(function(a, b) {
|
|
|
|
|
|
|
|
return a.priority - b.priority;
|
|
|
|
fs.exists(staticDir, function(exists) {
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
|
|
|
|
Plugins.staticDirs[pluginData.id] = staticDir;
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
} else next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else next();
|
|
|
|
|
|
|
|
},
|
|
|
|
next();
|
|
|
|
function(next) {
|
|
|
|
}
|
|
|
|
// CSS Files for plugins
|
|
|
|
], callback);
|
|
|
|
if (pluginData.css && pluginData.css instanceof Array) {
|
|
|
|
},
|
|
|
|
if (global.env === 'development') {
|
|
|
|
loadPlugin: function(pluginPath, callback) {
|
|
|
|
winston.info('[plugins] Found ' + pluginData.css.length + ' CSS file(s) for plugin ' + pluginData.id);
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.readFile(path.join(pluginPath, 'plugin.json'), function(err, data) {
|
|
|
|
|
|
|
|
if (err) return callback(err);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var pluginData = JSON.parse(data),
|
|
|
|
|
|
|
|
libraryPath, staticDir;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
if (pluginData.library) {
|
|
|
|
|
|
|
|
libraryPath = path.join(pluginPath, pluginData.library);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.exists(libraryPath, function(exists) {
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
|
|
|
|
if (!_self.libraries[pluginData.id]) {
|
|
|
|
|
|
|
|
_self.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);
|
|
|
|
|
|
|
|
}, next);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
next(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
winston.warn('[plugins.reload] Library not found for plugin: ' + pluginData.id);
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
// Static Directories for Plugins
|
|
|
|
|
|
|
|
if (pluginData.staticDir) {
|
|
|
|
|
|
|
|
staticDir = path.join(pluginPath, pluginData.staticDir);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fs.exists(staticDir, function(exists) {
|
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
|
|
|
|
_self.staticDirs[pluginData.id] = staticDir;
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
} else next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else next();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
// CSS Files for plugins
|
|
|
|
|
|
|
|
if (pluginData.css && pluginData.css instanceof Array) {
|
|
|
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
|
|
|
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);
|
|
|
|
return path.join('/plugins', pluginData.id, file);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
next();
|
|
|
|
next();
|
|
|
|
} else next();
|
|
|
|
} else next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], function(err) {
|
|
|
|
], function(err) {
|
|
|
|
if (!err) {
|
|
|
|
if (!err) {
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Loaded plugin: ' + pluginData.id);
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Loaded plugin: ' + pluginData.id);
|
|
|
|
callback();
|
|
|
|
callback();
|
|
|
|
} else callback(new Error('Could not load plugin system'))
|
|
|
|
} else callback(new Error('Could not load plugin system'))
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
registerHook: function(id, data, callback) {
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
|
|
|
|
`data` is an object consisting of (* is required):
|
|
|
|
Plugins.registerHook = function(id, data, callback) {
|
|
|
|
`data.hook`*, the name of the NodeBB hook
|
|
|
|
/*
|
|
|
|
`data.method`*, the method called in that plugin
|
|
|
|
`data` is an object consisting of (* is required):
|
|
|
|
`data.callbacked`, whether or not the hook expects a callback (true), or a return (false). Only used for filters. (Default: false)
|
|
|
|
`data.hook`*, the name of the NodeBB hook
|
|
|
|
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
|
|
|
|
`data.method`*, the method called in that plugin
|
|
|
|
*/
|
|
|
|
`data.callbacked`, whether or not the hook expects a callback (true), or a return (false). Only used for filters. (Default: false)
|
|
|
|
var _self = this;
|
|
|
|
`data.priority`, the relative priority of the method when it is eventually called (default: 10)
|
|
|
|
|
|
|
|
*/
|
|
|
|
if (data.hook && data.method) {
|
|
|
|
|
|
|
|
data.id = id;
|
|
|
|
if (data.hook && data.method) {
|
|
|
|
if (!data.priority) data.priority = 10;
|
|
|
|
data.id = id;
|
|
|
|
data.method = data.method.split('.').reduce(function(memo, prop) {
|
|
|
|
if (!data.priority) data.priority = 10;
|
|
|
|
if (memo[prop]) {
|
|
|
|
data.method = data.method.split('.').reduce(function(memo, prop) {
|
|
|
|
return memo[prop];
|
|
|
|
if (memo[prop]) {
|
|
|
|
} else {
|
|
|
|
return memo[prop];
|
|
|
|
// Couldn't find method by path, assuming property with periods in it (evil!)
|
|
|
|
} else {
|
|
|
|
_self.libraries[data.id][data.method];
|
|
|
|
// Couldn't find method by path, assuming property with periods in it (evil!)
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
callback();
|
|
|
|
if (global.env === 'development') {
|
|
|
|
} else return;
|
|
|
|
winston.info('[plugins] Hook registered: ' + data.hook + ' will call ' + id);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
fireHook: function(hook, args, callback) {
|
|
|
|
callback();
|
|
|
|
var _self = this
|
|
|
|
} else return;
|
|
|
|
hookList = this.loadedHooks[hook];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (hookList && Array.isArray(hookList)) {
|
|
|
|
Plugins.fireHook = function(hook, args, callback) {
|
|
|
|
//if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
|
|
|
|
hookList = Plugins.loadedHooks[hook];
|
|
|
|
var hookType = hook.split(':')[0];
|
|
|
|
|
|
|
|
switch (hookType) {
|
|
|
|
if (hookList && Array.isArray(hookList)) {
|
|
|
|
case 'filter':
|
|
|
|
//if (global.env === 'development') winston.info('[plugins] Firing hook: \'' + hook + '\'');
|
|
|
|
async.reduce(hookList, args, function(value, hookObj, next) {
|
|
|
|
var hookType = hook.split(':')[0];
|
|
|
|
if (hookObj.method) {
|
|
|
|
switch (hookType) {
|
|
|
|
if (hookObj.callbacked) { // If a callback is present (asynchronous method)
|
|
|
|
case 'filter':
|
|
|
|
hookObj.method.call(_self.libraries[hookObj.id], value, next);
|
|
|
|
async.reduce(hookList, args, function(value, hookObj, next) {
|
|
|
|
} else { // Synchronous method
|
|
|
|
if (hookObj.method) {
|
|
|
|
value = hookObj.method.call(_self.libraries[hookObj.id], value);
|
|
|
|
if (hookObj.callbacked) { // If a callback is present (asynchronous method)
|
|
|
|
next(null, value);
|
|
|
|
hookObj.method.call(Plugins.libraries[hookObj.id], value, next);
|
|
|
|
}
|
|
|
|
} else { // Synchronous method
|
|
|
|
} else {
|
|
|
|
value = hookObj.method.call(Plugins.libraries[hookObj.id], value);
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
|
|
|
|
next(null, value);
|
|
|
|
next(null, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(err, value) {
|
|
|
|
} else {
|
|
|
|
if (err) {
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
if (global.env === 'development') {
|
|
|
|
next(null, value);
|
|
|
|
winston.info('[plugins] Problem executing hook: ' + hook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, function(err, value) {
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
|
|
|
if (global.env === 'development') {
|
|
|
|
|
|
|
|
winston.info('[plugins] Problem executing hook: ' + hook);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
callback.apply(plugins, arguments);
|
|
|
|
callback.apply(plugins, arguments);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case 'action':
|
|
|
|
case 'action':
|
|
|
|
async.each(hookList, function(hookObj) {
|
|
|
|
async.each(hookList, function(hookObj) {
|
|
|
|
if (hookObj.method) {
|
|
|
|
if (hookObj.method) {
|
|
|
|
hookObj.method.call(_self.libraries[hookObj.id], args);
|
|
|
|
hookObj.method.call(Plugins.libraries[hookObj.id], args);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (global.env === 'development') {
|
|
|
|
if (global.env === 'development') {
|
|
|
|
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
winston.info('[plugins] Expected method \'' + hookObj.method + '\' in plugin \'' + hookObj.id + '\' not found, skipping.');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
// Do nothing...
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
// Do nothing...
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
// Otherwise, this hook contains no methods
|
|
|
|
} else {
|
|
|
|
var returnVal = args;
|
|
|
|
// Otherwise, this hook contains no methods
|
|
|
|
if (callback) {
|
|
|
|
var returnVal = args;
|
|
|
|
callback(null, returnVal);
|
|
|
|
if (callback) {
|
|
|
|
}
|
|
|
|
callback(null, returnVal);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
isActive: function(id, callback) {
|
|
|
|
};
|
|
|
|
db.isSetMember('plugins:active', id, callback);
|
|
|
|
|
|
|
|
},
|
|
|
|
Plugins.isActive = function(id, callback) {
|
|
|
|
toggleActive: function(id, callback) {
|
|
|
|
db.isSetMember('plugins:active', 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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db[(active ? 'setRemove' : 'setAdd')]('plugins:active', id, function(err, success) {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\'');
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\'');
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
db[(active ? 'setRemove' : 'setAdd')]('plugins:active', id, function(err, success) {
|
|
|
|
// Reload meta data
|
|
|
|
if (err) {
|
|
|
|
plugins.reload(function() {
|
|
|
|
if (global.env === 'development') winston.info('[plugins] Could not toggle active state on plugin \'' + id + '\'');
|
|
|
|
// (De)activation Hooks
|
|
|
|
return;
|
|
|
|
plugins.fireHook('action:plugin.' + (active ? 'de' : '') + 'activate', id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Reload meta data
|
|
|
|
if (callback) {
|
|
|
|
plugins.reload(function() {
|
|
|
|
callback({
|
|
|
|
// (De)activation Hooks
|
|
|
|
id: id,
|
|
|
|
plugins.fireHook('action:plugin.' + (active ? 'de' : '') + 'activate', id);
|
|
|
|
active: !active
|
|
|
|
|
|
|
|
});
|
|
|
|
if (callback) {
|
|
|
|
}
|
|
|
|
callback({
|
|
|
|
|
|
|
|
id: id,
|
|
|
|
|
|
|
|
active: !active
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
showInstalled: function(callback) {
|
|
|
|
}
|
|
|
|
var _self = this;
|
|
|
|
|
|
|
|
npmPluginPath = path.join(__dirname, '../node_modules');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
fs.readdir(npmPluginPath, function(err, dirs) {
|
|
|
|
|
|
|
|
dirs = dirs.map(function(file) {
|
|
|
|
|
|
|
|
return path.join(npmPluginPath, file);
|
|
|
|
|
|
|
|
}).filter(function(file) {
|
|
|
|
|
|
|
|
var stats = fs.statSync(file);
|
|
|
|
|
|
|
|
if (stats.isDirectory() && file.substr(npmPluginPath.length + 1, 14) === 'nodebb-plugin-') return true;
|
|
|
|
|
|
|
|
else return false;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
next(err, dirs);
|
|
|
|
Plugins.showInstalled = function(callback) {
|
|
|
|
|
|
|
|
npmPluginPath = path.join(__dirname, '../node_modules');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
fs.readdir(npmPluginPath, function(err, dirs) {
|
|
|
|
|
|
|
|
dirs = dirs.map(function(file) {
|
|
|
|
|
|
|
|
return path.join(npmPluginPath, file);
|
|
|
|
|
|
|
|
}).filter(function(file) {
|
|
|
|
|
|
|
|
var stats = fs.statSync(file);
|
|
|
|
|
|
|
|
if (stats.isDirectory() && file.substr(npmPluginPath.length + 1, 14) === 'nodebb-plugin-') return true;
|
|
|
|
|
|
|
|
else return false;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function(files, next) {
|
|
|
|
|
|
|
|
var plugins = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.each(files, function(file, next) {
|
|
|
|
|
|
|
|
var configPath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
fs.readFile(path.join(file, 'plugin.json'), next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(configJSON, next) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
var config = JSON.parse(configJSON);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
winston.warn("Plugin: " + file + " is corrupted or invalid. Please check plugin.json for errors.")
|
|
|
|
|
|
|
|
return next(err, null);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_self.isActive(config.id, function(err, active) {
|
|
|
|
|
|
|
|
if (err) next(new Error('no-active-state'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delete config.library;
|
|
|
|
next(err, dirs);
|
|
|
|
delete config.hooks;
|
|
|
|
});
|
|
|
|
config.active = active;
|
|
|
|
},
|
|
|
|
config.activeText = '<i class="fa fa-power-off"></i> ' + (active ? 'Dea' : 'A') + 'ctivate';
|
|
|
|
function(files, next) {
|
|
|
|
next(null, config);
|
|
|
|
var plugins = [];
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
async.each(files, function(file, next) {
|
|
|
|
|
|
|
|
var configPath;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
|
|
|
|
|
|
|
function(next) {
|
|
|
|
|
|
|
|
fs.readFile(path.join(file, 'plugin.json'), next);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
function(configJSON, next) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
var config = JSON.parse(configJSON);
|
|
|
|
|
|
|
|
} catch (err) {
|
|
|
|
|
|
|
|
winston.warn("Plugin: " + file + " is corrupted or invalid. Please check plugin.json for errors.")
|
|
|
|
|
|
|
|
return next(err, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
], function(err, config) {
|
|
|
|
|
|
|
|
if (err) return next(); // Silently fail
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plugins.push(config);
|
|
|
|
Plugins.isActive(config.id, function(err, active) {
|
|
|
|
next();
|
|
|
|
if (err) next(new Error('no-active-state'));
|
|
|
|
});
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
|
|
|
next(null, plugins);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
], function(err, plugins) {
|
|
|
|
|
|
|
|
callback(err, plugins);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plugins.init();
|
|
|
|
delete config.library;
|
|
|
|
|
|
|
|
delete config.hooks;
|
|
|
|
|
|
|
|
config.active = active;
|
|
|
|
|
|
|
|
config.activeText = '<i class="fa fa-power-off"></i> ' + (active ? 'Dea' : 'A') + 'ctivate';
|
|
|
|
|
|
|
|
next(null, config);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
], function(err, config) {
|
|
|
|
|
|
|
|
if (err) return next(); // Silently fail
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = plugins;
|
|
|
|
plugins.push(config);
|
|
|
|
|
|
|
|
next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, function(err) {
|
|
|
|
|
|
|
|
next(null, plugins);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
], function(err, plugins) {
|
|
|
|
|
|
|
|
callback(err, plugins);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}(exports));
|
|
|
|