2023-10-02 02:11:25 +00:00
|
|
|
|
const config = require('../config/default.json')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
const uconfig = require('../config/usersettings.json')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
const { parentPort, workerData } = require('worker_threads');
|
2023-10-02 02:11:27 +00:00
|
|
|
|
const { d1, d2, d3, d4, d5, d6 } = workerData;
|
2023-10-02 02:11:26 +00:00
|
|
|
|
var user = d1;
|
|
|
|
|
var setting = d2;
|
|
|
|
|
var setting2 = d3;
|
|
|
|
|
var value = d4;
|
|
|
|
|
var value2 = d5;
|
2023-10-02 02:11:27 +00:00
|
|
|
|
var hostmask = d6
|
2023-10-02 02:11:25 +00:00
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
|
let Parser = require('rss-parser');
|
|
|
|
|
let parser = new Parser({
|
|
|
|
|
headers: {'User-Agent': config.feed.useragent},
|
|
|
|
|
});
|
|
|
|
|
const editJsonFile = require("edit-json-file");
|
|
|
|
|
const timer = ms => new Promise(res => setTimeout(res, ms))
|
2023-10-02 02:11:27 +00:00
|
|
|
|
const moment = require('moment-timezone');
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
warningMsg = ''+config.colours.brackets+'['+config.colours.warning+'WARNING'+config.colours.brackets+']'
|
|
|
|
|
errorMsg = ''+config.colours.brackets+'['+config.colours.error+'ERROR'+config.colours.brackets+']'
|
|
|
|
|
|
2023-10-02 02:11:26 +00:00
|
|
|
|
function consoleLog(log) {
|
|
|
|
|
if (config.misc.logging === "true") {
|
|
|
|
|
console.log(log)
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
async function sendUpstream(content) {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
parentPort.postMessage(content);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function errorMessage(error, code, extra) {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.errorMessage] '+error.code)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
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.'
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (code == "ALREADYEXISTS" ) {
|
|
|
|
|
var error = errorMsg+' '+extra+' already exists in your feed list.'
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (error == "NOFEEDS") {
|
|
|
|
|
var error = errorMsg+" No saved feeds for "+provfeed
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else {
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var error = errorMsg+" Unknown error"
|
|
|
|
|
}
|
|
|
|
|
parentPort.postMessage(error);
|
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:27 +00:00
|
|
|
|
function arrayRemove(array, value) {
|
|
|
|
|
return array.filter(function (o) {
|
|
|
|
|
return o != value;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
async function testFeed(feedURL) {
|
|
|
|
|
try {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.testFeed] Testing '+feed)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var feed = await parser.parseURL(feedURL);
|
|
|
|
|
} catch (e) {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.testFeed] '+e)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
errorMessage(e, "INVALID", feedURL);
|
|
|
|
|
}
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog("[options.testFeed] Feed is valid, continuing")
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function feed(nick, setting, value) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (setting == undefined ) {
|
|
|
|
|
content = []
|
|
|
|
|
content.push(config.irc.prefix+'opt feed Help Menu')
|
|
|
|
|
content.push(config.irc.prefix+'opt feed add [VALUE] - Adds a valid RSS feed URL to your feed list')
|
|
|
|
|
content.push(config.irc.prefix+'opt feed del [VALUE] - Removes any existing RSS feed from your feed list')
|
|
|
|
|
content.push(config.irc.prefix+'opt feed list - Lists all of the exiting feeds in your feed list')
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output)
|
|
|
|
|
}
|
|
|
|
|
if (setting != undefined ) {
|
|
|
|
|
var setting = setting.toLowerCase()
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (setting === 'add') {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
consoleLog('[options.feed] '+nick+' is adding feed entry: '+value)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
await testFeed(value);
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-02 02:11:27 +00:00
|
|
|
|
try {
|
|
|
|
|
var feedsArr = uconfig[nick].feeds
|
|
|
|
|
if (feedsArr.includes(value) == true) {
|
|
|
|
|
errorMessage("null", "ALREADYEXISTS", value)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
consoleLog('[options.feed] No user feed list in usersettings.json, it will be made')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
2023-10-02 02:11:27 +00:00
|
|
|
|
file.append(nick+".feeds", value);
|
|
|
|
|
file.save();
|
|
|
|
|
sendUpstream(value + ' added to your feed list')
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (setting === "del") {
|
|
|
|
|
consoleLog('[options.feed] '+nick+' is removing feed entry: '+value)
|
|
|
|
|
var array = uconfig[nick].feeds
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
|
|
|
|
if (array.includes(value) == false ) {
|
|
|
|
|
sendUpstream(errorMsg+' '+value+' is not saved in your feed list')
|
|
|
|
|
}
|
|
|
|
|
var newArray = arrayRemove(array, value)
|
|
|
|
|
file.set(nick+'.feeds', newArray)
|
|
|
|
|
file.save();
|
|
|
|
|
sendUpstream('Feed entry removed ('+value+' ==> BTFO\'d)')
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (setting === 'list') {
|
|
|
|
|
content = [];
|
|
|
|
|
try {
|
|
|
|
|
var feedsArr = uconfig[nick].feeds
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.feed] Listing existing feeds for '+nick+': '+feedsArr)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
content.push("These are your added feeds:")
|
|
|
|
|
} catch (e) {
|
|
|
|
|
errorMessage(e, "NOFEEDS", nick);
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < feedsArr.length; i++) {
|
|
|
|
|
content.push(i+1+'. '+feedsArr[i])
|
|
|
|
|
}
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output)
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
async function alias(setting, value, url, nick) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (setting == undefined ) {
|
|
|
|
|
content = []
|
|
|
|
|
content.push(config.irc.prefix+'opt alias Help Menu')
|
|
|
|
|
content.push(config.irc.prefix+'opt alias add [ALIAS] [URL] - Adds an alias for any valid RSS feed')
|
|
|
|
|
content.push(config.irc.prefix+'opt alias del [ALIAS] [URL] - Removes an alias')
|
|
|
|
|
content.push(config.irc.prefix+'opt alias list - Lists all of your existing alises')
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output)
|
|
|
|
|
}
|
|
|
|
|
if (setting != undefined ) {
|
|
|
|
|
var setting = setting.toLowerCase()
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (setting === 'add') {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
var value = value.toUpperCase()
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.alias] Adding/editing an alias for'+nick+': '+value+' ==> '+url)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
await testFeed(url);
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-02 02:11:26 +00:00
|
|
|
|
file.set(nick+'.alias.'+value.toUpperCase(), url);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
file.save();
|
2023-10-02 02:11:26 +00:00
|
|
|
|
sendUpstream('Alias added ('+value.toUpperCase()+' ==> '+url+')')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
if (setting === 'del') {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.alias] Removing an alias for '+nick+': '+value+' ==> \"\"')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-02 02:11:26 +00:00
|
|
|
|
file.set(nick+'.alias.'+value.toUpperCase(), "");
|
2023-10-02 02:11:25 +00:00
|
|
|
|
file.save();
|
2023-10-02 02:11:26 +00:00
|
|
|
|
sendUpstream('Alias removed ('+value.toUpperCase()+' ==> BTFO\'d)')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
if (setting === 'list') {
|
|
|
|
|
content = [];
|
|
|
|
|
var obj = uconfig[nick].alias
|
2023-10-02 02:11:26 +00:00
|
|
|
|
consoleLog('[options.alias] Listing aliases for '+nick+': '+obj)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
console.log(obj)
|
|
|
|
|
for (const [key, val] of Object.entries(obj)) {
|
|
|
|
|
if (val !== "") {
|
|
|
|
|
content.push(key + ' ==> '+val)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:27 +00:00
|
|
|
|
async function get(setting, hostmask, nick) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (setting == "hostmask") {
|
|
|
|
|
sendUpstream('Your hostmask ==> '+hostmask)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
} else if (setting == "user.tz" || setting == "user.timezone" ) {
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json')
|
|
|
|
|
sendUpstream(setting + ' ==> ' + file.get(nick+".timezone"))
|
2023-10-02 02:11:27 +00:00
|
|
|
|
} else {
|
|
|
|
|
consoleLog('[options.get] Getting value of '+setting)
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/default.json')
|
|
|
|
|
consoleLog(setting + ' ==> ' + file.get(setting));
|
|
|
|
|
if ( file.get(setting) == undefined) {
|
|
|
|
|
sendUpstream(setting + 'is not a valid setting')
|
|
|
|
|
} else {
|
|
|
|
|
sendUpstream(setting + ' ==> ' + file.get(setting))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:27 +00:00
|
|
|
|
async function set(setting, value, value2, nick) {
|
|
|
|
|
if (setting == 'user.tz' || setting == 'user.timezone') {
|
|
|
|
|
content = []
|
|
|
|
|
var tzarray = moment.tz.names().map(a => a.toLowerCase())
|
|
|
|
|
if (tzarray.includes(value.toLowerCase()) == false) {
|
|
|
|
|
consoleLog('[options.set.tz] Invalid timezone entered')
|
|
|
|
|
sendUpstream(errorMsg+' Invalid timezone entered, not changing')
|
|
|
|
|
}
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
|
|
|
|
if (uconfig[nick].timezone == undefined || uconfig[nick].timezone == "" ) {
|
|
|
|
|
var oldvalue = "Unset"
|
|
|
|
|
} else {
|
|
|
|
|
var oldvalue = file.get(nick+".timezone")
|
|
|
|
|
}
|
|
|
|
|
consoleLog('[options.set.tz] Adding/editing the timezone for'+nick+': '+oldvalue+' ==> '+value)
|
|
|
|
|
file.set(nick+".timezone", value)
|
|
|
|
|
file.save()
|
|
|
|
|
content.push(setting+' ==> ' +oldvalue+' (PREVIOUS)')
|
|
|
|
|
content.push(setting+ ' ==> '+file.get(nick+".timezone")+' (UPDATED)')
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function operset(setting, value, value2, hostmask) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
content = []
|
|
|
|
|
if (value2 == "-s" || value2 == '--spaces') {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
consoleLog('[options.operset] ' + value2+' called, replacing all dashes with spaces')
|
2023-10-02 02:11:27 +00:00
|
|
|
|
var value = value.replace(/-/g,' ')
|
|
|
|
|
}
|
2023-10-02 02:11:25 +00:00
|
|
|
|
var file = editJsonFile('/home/node/app/config/default.json')
|
2023-10-02 02:11:27 +00:00
|
|
|
|
var disallowedSettings = [
|
|
|
|
|
"irc",
|
|
|
|
|
"twitter",
|
|
|
|
|
"motd"
|
|
|
|
|
]
|
|
|
|
|
var disallowedCheck = setting.split(".")
|
|
|
|
|
var disallowedCheck = disallowedCheck[0]
|
|
|
|
|
if (config.irc.settings_auth_enable == 'true' ) {
|
|
|
|
|
if (hostmask != config.irc.settings_auth_hostmask) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
consoleLog('[options.operset] Unauthorised user '+hostmask+' attempted to set '+setting+' to '+value)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
sendUpstream(errorMsg+' You are not permitted to perform this action')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (file.get(setting) == undefined) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
consoleLog('[options.operset] '+setting+' is not a valid setting')
|
2023-10-02 02:11:27 +00:00
|
|
|
|
sendUpstream(errorMsg+' '+setting+' is not a valid setting or has not been defined')
|
|
|
|
|
}
|
|
|
|
|
if (disallowedSettings.includes(disallowedCheck)) {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
consoleLog('[options.operset] '+hostmask+' attempted to edit disallowed setting category: '+disallowedCheck)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
sendUpstream(errorMsg+" You may not modify this setting")
|
|
|
|
|
}
|
2023-10-02 02:11:27 +00:00
|
|
|
|
if (setting == "feed.timezone") {
|
|
|
|
|
var tzarray = moment.tz.names().map(a => a.toLowerCase())
|
|
|
|
|
if (tzarray.includes(value.toLowerCase()) == false) {
|
|
|
|
|
consoleLog('[options.operset] Invalid timezone entered')
|
|
|
|
|
content.push(warningMsg+' Invalid timezone entered, changing anyways but output will default to Etc/UTC')
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-02 02:11:27 +00:00
|
|
|
|
var oldvalue = file.get(setting)
|
|
|
|
|
file.set(setting, value);
|
|
|
|
|
file.save();
|
|
|
|
|
content.push(setting+' ==> ' +oldvalue+' (PREVIOUS)')
|
|
|
|
|
content.push(setting+ ' ==> '+file.get(setting)+' (UPDATED)')
|
|
|
|
|
var output = content.join("\n")
|
|
|
|
|
sendUpstream(output)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 02:11:25 +00:00
|
|
|
|
if (setting === 'feed') {
|
|
|
|
|
feed(user, setting2, value);
|
2023-10-02 02:11:27 +00:00
|
|
|
|
//} else if (setting === 'list') {
|
|
|
|
|
// feed(user, setting2)
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if (setting === 'get') {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
get(setting2, hostmask, user);
|
2023-10-02 02:11:25 +00:00
|
|
|
|
} else if(setting === 'alias') {
|
|
|
|
|
alias(setting2, value, value2, user)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
} else if(setting === 'operset') {
|
|
|
|
|
operset(setting2, value, value2, hostmask)
|
2023-10-02 02:11:27 +00:00
|
|
|
|
} else if(setting === 'set') {
|
2023-10-02 02:11:27 +00:00
|
|
|
|
set(setting2, value, value2, user)
|
|
|
|
|
} else {
|
2023-10-02 02:11:26 +00:00
|
|
|
|
sendUpstream(errorMsg+' '+setting+' is not a valid option')
|
2023-10-02 02:11:25 +00:00
|
|
|
|
}
|