Add config validity checking

This commit is contained in:
hgw 2023-10-02 02:11:27 +00:00
parent dba5b2ecf1
commit 1662716c15
2 changed files with 132 additions and 17 deletions

33
bot.js
View File

@ -45,6 +45,18 @@ function consoleLog(log) {
var hostmask = null var hostmask = null
function checkConfigValidity() {
consoleLog(`[bot.checkConfigValidity] Opening cvc worker`)
const worker = new Worker(`./commands/cvc.js`, {});
worker.once('message', (string) => {
if (string == 'kill') {
process.exit()
} else if (string == 'allg') {
consoleLog('[bot.checkConfigValidity] Config validity checked, seems all good, continuing with initialisation')
}
});
}
function checkUserHostmask(user) { function checkUserHostmask(user) {
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
setTimeout(() => { setTimeout(() => {
@ -163,9 +175,11 @@ bot.addListener('message', function(nick, to, text, from) {
if (command === config.irc.prefix+'help') { if (command === config.irc.prefix+'help') {
help(to, args[1]); help(to, args[1]);
} else if (command === config.irc.prefix+'feed') { } else if (command === config.irc.prefix+'feed') {
if (args[1] == undefined ) {
help(to, "feed")
} else {
feed(to, nick, args[1], args[2]); feed(to, nick, args[1], args[2]);
} else if (command === config.irc.prefix+'twitter') { }
twitter(to, args[1], args[2])
} else if (command === config.irc.prefix+'opt') { } else if (command === config.irc.prefix+'opt') {
if (args[1] == undefined ) { if (args[1] == undefined ) {
help(to, "opt") help(to, "opt")
@ -185,21 +199,30 @@ bot.addListener('error', function(message) {
consoleLog('[ERROR]' +message) consoleLog('[ERROR]' +message)
}); });
async function init() {
consoleLog('[bot.init] Welcome to Mercury'); consoleLog('[bot.init] Welcome to Mercury');
consoleLog('[bot.init] Checking config validity')
checkConfigValidity()
await timer(1500)
consoleLog('[bot.init] Checking if user settings file exists')
fs.open('./config/usersettings.json','r',function(err, fd){ fs.open('./config/usersettings.json','r',function(err, fd){
if (err) { if (err) {
fs.writeFile('./config/usersettings.json', '', function(err) { fs.writeFile('./config/usersettings.json', '', function(err) {
if(err) { if(err) {
consoleLog(err); consoleLog(err);
} }
consoleLog("[bot.init] usersettings.json did not exist, it has been created"); consoleLog("[bot.init] User settings did not exist, it has been created for you");
}); });
fs.writeFileSync('./config/usersettings.json', "\{\n\}") fs.writeFileSync('./config/usersettings.json', "\{\n\}")
} else { } else {
consoleLog("[bot.init] usersettings.json exists, continuing"); consoleLog("[bot.init] User settings file exists");
} }
}); });
consoleLog('[bot.init] Connecting to '+config.irc.server+'/'+config.irc.port+' as '+config.irc.nickname); await timer(100)
consoleLog('[bot.init] Initialisation completed, connecting to '+config.irc.server+'/'+config.irc.port+' as '+config.irc.nickname);
}
init()
process.on('uncaughtException', function (err) { process.on('uncaughtException', function (err) {
console.error(err); console.error(err);

92
commands/cvc.js Normal file
View File

@ -0,0 +1,92 @@
const config = require('../config/default.json')
//const uconfig = require('../config/usersettings.json')
const { parentPort, workerData } = require('worker_threads');
//const { d1, d2, d3, d4, d5, d6 } = workerData;
//var user = d1;
//var setting = d2;
//var setting2 = d3;
//var value = d4;
//var value2 = d5;
//var hostmask = d6
const fs = require('fs-extra')
const timer = ms => new Promise(res => setTimeout(res, ms))
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
function consoleLog(log) {
if (config.misc.logging === "true") {
console.log(log)
} else {
return;
}
}
async function sendUpstream(content) {
parentPort.postMessage(content);
process.exit()
}
function errorMessage(error, code, extra) {
consoleLog('[cvc.errorMessage] '+error.code)
if (code == "404") {
var error = errorMsg+" 404: " + extra + " not found"
} else if (error.code == "ECONNREFUSED") {
var error = errorMsg+" Connection Refused"
} else if (error.code == "ERR_UNESCAPED_CHARACTERS"){
var error = errorMsg+" Unescaped Characters"
} else if (code == "INVALID") {
var error = errorMsg+' '+extra+' either does not exist or is not a valid feed.'
} else if (code == "ALREADYEXISTS" ) {
var error = errorMsg+' '+extra+' already exists in your feed list.'
} else if (error == "NOFEEDS") {
var error = errorMsg+" No saved feeds for "+provfeed
} else {
var error = errorMsg+" Unknown error"
}
parentPort.postMessage(error);
process.exit()
}
function checkValuesExist() {
const fatalConfigError = new Set();
var reqConfigs = [
config.irc.server, config.irc.port, config.irc.ssl, config.irc.channels, config.irc.nickname, config.irc.username, config.irc.realname, config.irc.autorejoin, config.irc.prefix, config.irc.settings_auth_enable, config.irc.settings_auth_hostmask,
config.floodprotect.flood_protection, config.floodprotect.flood_protection_delay,
config.errorhandling.error_logging, config.errorhandling.error_channel, config.errorhandling.error_channel_pass,
config.colours.date, config.colours.brackets, config.colours.title, config.colours.author, config.colours.body, config.colours.link, config.colours.help_logo, config.colours.help_motd, config.colours.warning, config.colours.error,
config.feed.useragent, config.feed.body_max_chars, config.feed.time_format, config.feed.timezone, config.feed.default_amount,
config.twitter.nitter_instances, config.twitter.default_amount,
config.misc.display_help_logo, config.misc.logging,
config.motd.enable, config.motd.list, config.motd.version
]
var reqConfigs2 = [
"config.irc.server", "config.irc.port", "config.irc.ssl", "config.irc.channels", "config.irc.nickname", "config.irc.username", "config.irc.realname", "config.irc.autorejoin", "config.irc.prefix", "config.irc.settings_auth_enable", "config.irc.settings_auth_hostmask",
"config.floodprotect.flood_protection", "config.floodprotect.flood_protection_delay",
"config.errorhandling.error_logging","config.errorhandling.error_channel","config.errorhandling.error_channel_pass",
"config.colours.date","config.colours.brackets","config.colours.title","config.colours.author","config.colours.body","config.colours.link","config.colours.help_logo","config.colours.help_motd","config.colours.warning","config.colours.error",
"config.feed.useragent","config.feed.body_max_chars","config.feed.time_format","config.feed.timezone","config.feed.default_amount",
"config.twitter.nitter_instances","config.twitter.default_amount",
"config.misc.display_help_logo","config.misc.logging",
"config.motd.enable","config.motd.list","config.motd.version"
]
reqConfigs.forEach(function(item, index) {
try {
if (item == undefined ) {
consoleLog('[cvc.checkValuesExist] [FATAL] '+reqConfigs2[index]+' is incorrectly defined or does not exist')
fatalConfigError.add('kill')
}
} catch (e) {
consoleLog('[cvc.checkValuesExist] [FATAL] Unknown Error')
}
})
if (fatalConfigError.has('kill') == true ){
consoleLog('[cvc.checkValuesExist] [FATAL] Config is improperly configured, Mercury can not start')
sendUpstream('kill')
} else {
sendUpstream('allg')
}
}
checkValuesExist()