kulmapaikka-ircbot/plugins/kernel.js

89 lines
2.4 KiB
JavaScript

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;