2019-08-13 19:37:22 +03:00
|
|
|
var fs = require('fs')
|
|
|
|
var Promise = require('promise')
|
|
|
|
const logger = require('../logger')
|
2017-03-12 18:36:32 +02:00
|
|
|
class PluginManager {
|
2019-08-13 19:37:22 +03:00
|
|
|
constructor (config) {
|
|
|
|
this.enabledPlugins = []
|
|
|
|
this.enabledPmPlugins = []
|
|
|
|
this.plugins = []
|
|
|
|
this.pmPlugins = []
|
|
|
|
this.config = config
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var that = this
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
config.plugins.forEach(function (pluginName) {
|
|
|
|
logger.info('checking ' + pluginName + '...')
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (fs.existsSync('./plugins/' + pluginName.toLowerCase() + '.js')) {
|
|
|
|
logger.info('* Exists. Enabling...')
|
|
|
|
that.enabledPlugins.push(pluginName.toLowerCase())
|
2017-03-12 18:36:32 +02:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
if (fs.existsSync('./plugins/pm' + pluginName.toLowerCase() + '.js')) {
|
|
|
|
logger.info('* Exists as PM plugin. Enabling...')
|
|
|
|
that.enabledPmPlugins.push(pluginName.toLowerCase())
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
2017-03-12 18:36:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
loadAllPlugins () {
|
|
|
|
var that = this
|
|
|
|
this.enabledPlugins.forEach(function (pluginName) {
|
|
|
|
var Plugin = require('./' + pluginName + '.js')
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var plugin = new Plugin(that.config)
|
|
|
|
that.plugins.push(plugin)
|
|
|
|
})
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
this.enabledPmPlugins.forEach(function (pluginName) {
|
|
|
|
var Plugin = require('./pm' + pluginName + '.js')
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var plugin = new Plugin(that.config)
|
|
|
|
that.pmPlugins.push(plugin)
|
|
|
|
})
|
2017-03-12 18:36:32 +02:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:28:11 +02:00
|
|
|
validateResult (resultPromise) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
let wrapperPromise = new Promise((resolve) => resolve(resultPromise))
|
|
|
|
wrapperPromise.then((results) => {
|
|
|
|
if (!Array.isArray(results)) {
|
|
|
|
results = [results]
|
|
|
|
}
|
|
|
|
|
|
|
|
let promises = []
|
|
|
|
results.forEach((result) => {
|
|
|
|
let to = 'channel'
|
|
|
|
let message = result
|
|
|
|
if (typeof result === 'object' && typeof result.to !== 'undefined') {
|
|
|
|
to = result.to
|
|
|
|
message = result.message
|
|
|
|
}
|
|
|
|
|
|
|
|
promises.push(new Promise({ to, message }))
|
|
|
|
})
|
|
|
|
|
|
|
|
resolve(promises)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
testPlugins (input, publicChat) {
|
2018-04-29 17:37:07 +03:00
|
|
|
if (typeof publicChat === 'undefined') {
|
|
|
|
publicChat = true
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var promises = []
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2018-04-29 17:37:07 +03:00
|
|
|
if (publicChat) {
|
2019-08-13 19:37:22 +03:00
|
|
|
for (let i = 0; i < this.plugins.length; i++) {
|
|
|
|
let plugin = this.plugins[i]
|
|
|
|
if (plugin.test(input)) {
|
|
|
|
logger.info('* Plugin ' + plugin.name + ' reported hit')
|
2019-11-20 20:28:11 +02:00
|
|
|
promises.push(this.validateResult(plugin.result(input)))
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-13 19:37:22 +03:00
|
|
|
for (let i = 0; i < this.pmPlugins.length; i++) {
|
|
|
|
let plugin = this.pmPlugins[i]
|
2017-03-12 18:36:32 +02:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
if (plugin.test(input)) {
|
|
|
|
logger.info('* Plugin ' + plugin.name + ' reported hit')
|
2019-11-20 20:28:11 +02:00
|
|
|
promises.push(this.validateResult(plugin.result(input)))
|
2018-04-29 17:37:07 +03:00
|
|
|
}
|
2017-03-12 18:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-29 17:37:07 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return Promise.all(promises)
|
2017-03-12 18:36:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// export default BasePlugin;
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = PluginManager
|