New PluginManager introduced. getKernel converted to the new plugin system

This commit is contained in:
lanxu 2017-03-12 18:36:32 +02:00
parent a6ad04074c
commit 37e182667b
6 changed files with 180 additions and 64 deletions

View File

@ -11,3 +11,4 @@ deploy
backup/
.log
config.js
.swp

69
app.js
View File

@ -27,6 +27,13 @@ var iconCodeToText = {
// Load config
var config = require('./config');
// Load new plugins
var PluginManager = require('./plugins/pluginManager.js');
var pluginManager = new PluginManager(config);
pluginManager.loadAllPlugins();
// Load plugins
var googletin = require('./googleapi.js')(config);
var owmapi = require('./owmapi')(config);
@ -37,7 +44,6 @@ var getDiceString = require('./plugins/getDice')();
var getUrlString = require('./plugins/getUrl')();
var getTweet = require('./plugins/getTweet')(config);
var getInstantAnswer = require('./plugins/getInstantAnswer')(config);
var getKernelData = require('./plugins/getKernel')();
// Configure logger
// create a write stream (in append mode)
@ -58,6 +64,12 @@ var logger = new (winston.Logger)({
]
});
if(!config.irc.connect) {
logger.info('Not connecting to IRC. Exit');
return;
}
// Start client
var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: true,
@ -67,6 +79,13 @@ var client = new irc.Client(config.irc.server, config.irc.nick, {
var handleMessage = function(from, to, message) {
logger.info(from + ' => ' + to + ': ' + message);
pluginManager.testPlugins(message).then(function(results) {
for(var i = 0; i < results.length; i++) {
client.say(to, results[i]);
}
});
//if (to.match(/^[#&]/)) {
// channel message
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
@ -127,53 +146,6 @@ var handleMessage = function(from, to, message) {
})
}
// Kernel version
if (res = message.match(/^\.kernel( (.+))?/i)) {
console.log('Search kernel version ' + res[2]);
var version = res[2];
getKernelData().then(function(data) {
var text = 'kernel: ';
// Store current versions!
var releaseToVersion = {};
data.releases.forEach(function(val) {
var regex = /([0-9]+?)(\.([0-9]+))?(\.([0-9]+))?/;
var result = val.version.match(regex);
var major = result[1];
var minor = result[3];
if(typeof major !== 'undefined' && typeof minor !== 'undefined') {
var releaseWithVersion = val.moniker + '-' + major + '-' + minor;
var release = val.moniker + '';
if(typeof releaseToVersion[releaseWithVersion] !== 'undefined') {
if(releaseToVersion[releaseWithVersion] !== val.version) {
// new release
}
}
if(typeof releaseToVersion[release] === 'undefined') {
releaseToVersion[release] = val.version;
}
releaseToVersion[releaseWithVersion] = val.version;
}
});
if(typeof releaseToVersion[version] !== 'undefined') {
text += 'Viimeisin versio on ' + releaseToVersion[version] + ' (' + version + ')';
} else {
text += 'Viimeisin vakaa versio on ' + data.latest_stable.version;
}
console.log(text);
client.say(to, text);
});
}
// Throw dice
if (res = message.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
var th = getDiceString(res[2], res[3]);
@ -200,7 +172,6 @@ var handleMessage = function(from, to, message) {
}
// Url specific handling
//if (res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/)) {
if(res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?([a-zA-Z0-9_=#-]+&)*v=([a-zA-Z0-9_-]+)))/)) {
console.log(res);
var id = res[7];

17
plugins/base.js Normal file
View File

@ -0,0 +1,17 @@
class BasePlugin {
constructor(config) {
this.name = 'BasePlugin';
}
test(input) {
}
result(input) {
}
}
// export default BasePlugin;
module.exports = BasePlugin;

View File

@ -1,6 +1,7 @@
var request = require('request');
var Promise = require('promise');
var requestPromise = function(url, urlArgs) {
function RequestPromise(url, urlArgs) {
var promise = new Promise(function(resolve, reject) {
request({
url: url,
@ -17,17 +18,4 @@ var requestPromise = function(url, urlArgs) {
return promise;
};
module.exports = function(config) {
'use strict';
var getKernelData = function(queryText) {
var urlArgs = {};
var url = 'https://www.kernel.org/releases.json';
var promise = requestPromise(url, urlArgs);
return promise;
};
return getKernelData;
};
module.exports = RequestPromise;

88
plugins/kernel.js Normal file
View File

@ -0,0 +1,88 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Kernel extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Kernel';
}
getKernelData(queryText) {
var urlArgs = {};
var url = 'https://www.kernel.org/releases.json';
var promise = requestPromise(url, urlArgs);
return promise;
}
test(input) {
// Kernel version
var res = null;
if (res = input.match(/^\.kernel( (.+))?/i)) {
return true;
}
return false;
}
result(input) {
var that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
var res = null;
if (res = input.match(/^\.kernel( (.+))?/i)) {
var version = res[2];
logger.info('Search kernel version ' + res[2]);
that.getKernelData().then(function(data) {
var text = 'kernel: ';
// Store current versions!
var releaseToVersion = {};
data.releases.forEach(function(val) {
var regex = /([0-9]+?)(\.([0-9]+))?(\.([0-9]+))?/;
var result = val.version.match(regex);
var major = result[1];
var minor = result[3];
if(typeof major !== 'undefined' && typeof minor !== 'undefined') {
var releaseWithVersion = val.moniker + '-' + major + '-' + minor;
var release = val.moniker + '';
if(typeof releaseToVersion[releaseWithVersion] !== 'undefined') {
if(releaseToVersion[releaseWithVersion] !== val.version) {
// new release
}
}
if(typeof releaseToVersion[release] === 'undefined') {
releaseToVersion[release] = val.version;
}
releaseToVersion[releaseWithVersion] = val.version;
}
}, function(error) {
resultReject(error);
});
if(typeof releaseToVersion[version] !== 'undefined') {
text += 'Viimeisin versio on ' + releaseToVersion[version] + ' (' + version + ')';
} else {
text += 'Viimeisin vakaa versio on ' + data.latest_stable.version;
}
resultResolve(text);
}, function(error) {
resultReject(error);
});
}
});
return resultPromise;
}
}
module.exports = Kernel;

51
plugins/pluginManager.js Normal file
View File

@ -0,0 +1,51 @@
var logger = require('winston');
var fs = require('fs');
var Promise = require('promise');
class PluginManager {
constructor(config) {
this.enabledPlugins = [];
this.plugins = [];
this.config = config;
var that = this;
config.plugins.forEach(function(pluginName) {
logger.info('checking ' + pluginName + '...');
if(fs.existsSync('./plugins/'+pluginName.toLowerCase()+'.js')) {
logger.info('* Exists. Enabling...');
that.enabledPlugins.push(pluginName.toLowerCase());
}
});
}
loadAllPlugins() {
var that = this;
this.enabledPlugins.forEach(function(pluginName) {
var Plugin = require('./' + pluginName + '.js');
var plugin = new Plugin(that.config);
that.plugins.push(plugin);
});
}
testPlugins(input) {
var promises = [];
for(let i = 0; i < this.plugins.length; i++) {
var plugin = this.plugins[i];
if(plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit');
promises.push(plugin.result(input));
}
}
return Promise.all(promises);
}
}
// export default BasePlugin;
module.exports = PluginManager;