From 1662716c15379772360c69bfd75f0c09a0104ea6 Mon Sep 17 00:00:00 2001 From: hgw Date: Mon, 2 Oct 2023 02:11:27 +0000 Subject: [PATCH] Add config validity checking --- bot.js | 57 +++++++++++++++++++++--------- commands/cvc.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 17 deletions(-) create mode 100644 commands/cvc.js diff --git a/bot.js b/bot.js index e27c9c4..135d00a 100644 --- a/bot.js +++ b/bot.js @@ -45,6 +45,18 @@ function consoleLog(log) { 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) { return new Promise(function (resolve, reject) { setTimeout(() => { @@ -163,9 +175,11 @@ bot.addListener('message', function(nick, to, text, from) { if (command === config.irc.prefix+'help') { help(to, args[1]); } else if (command === config.irc.prefix+'feed') { - feed(to, nick, args[1], args[2]); - } else if (command === config.irc.prefix+'twitter') { - twitter(to, args[1], args[2]) + if (args[1] == undefined ) { + help(to, "feed") + } else { + feed(to, nick, args[1], args[2]); + } } else if (command === config.irc.prefix+'opt') { if (args[1] == undefined ) { help(to, "opt") @@ -185,21 +199,30 @@ bot.addListener('error', function(message) { consoleLog('[ERROR]' +message) }); -consoleLog('[bot.init] Welcome to Mercury'); -fs.open('./config/usersettings.json','r',function(err, fd){ - if (err) { - fs.writeFile('./config/usersettings.json', '', function(err) { - if(err) { - consoleLog(err); - } - consoleLog("[bot.init] usersettings.json did not exist, it has been created"); +async function init() { + 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){ + if (err) { + fs.writeFile('./config/usersettings.json', '', function(err) { + if(err) { + consoleLog(err); + } + consoleLog("[bot.init] User settings did not exist, it has been created for you"); + }); + fs.writeFileSync('./config/usersettings.json', "\{\n\}") + } else { + consoleLog("[bot.init] User settings file exists"); + } }); - fs.writeFileSync('./config/usersettings.json', "\{\n\}") - } else { - consoleLog("[bot.init] usersettings.json exists, continuing"); - } - }); -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) { console.error(err); diff --git a/commands/cvc.js b/commands/cvc.js new file mode 100644 index 0000000..d7b7197 --- /dev/null +++ b/commands/cvc.js @@ -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() \ No newline at end of file