2023-10-01 22:11:25 -04:00
|
|
|
|
const config = require('../config/default.json')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
const uconfig = require('../config/usersettings.json')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
const { parentPort, workerData } = require('worker_threads');
|
2023-10-01 22:11:26 -04:00
|
|
|
|
const { d1, d2, d3, d4, d5 } = workerData;
|
|
|
|
|
var user = d1;
|
|
|
|
|
var setting = d2;
|
|
|
|
|
var setting2 = d3;
|
|
|
|
|
var value = d4;
|
|
|
|
|
var value2 = d5;
|
2023-10-01 22:11:25 -04: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-01 22:11:25 -04: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-01 22:11:26 -04:00
|
|
|
|
function consoleLog(log) {
|
|
|
|
|
if (config.misc.logging === "true") {
|
|
|
|
|
console.log(log)
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 22:11:25 -04:00
|
|
|
|
async function sendUpstream(content) {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
parentPort.postMessage(content);
|
2023-10-01 22:11:25 -04:00
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function errorMessage(error, code, extra) {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.errorMessage] '+error.code)
|
2023-10-01 22:11:25 -04: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-01 22:11:25 -04:00
|
|
|
|
} else if (code == "ALREADYEXISTS" ) {
|
|
|
|
|
var error = errorMsg+' '+extra+' already exists in your feed list.'
|
2023-10-01 22:11:25 -04:00
|
|
|
|
} else if (error == "NOFEEDS") {
|
|
|
|
|
var error = errorMsg+" No saved feeds for "+provfeed
|
2023-10-01 22:11:25 -04:00
|
|
|
|
} else {
|
2023-10-01 22:11:25 -04:00
|
|
|
|
var error = errorMsg+" Unknown error"
|
|
|
|
|
}
|
|
|
|
|
parentPort.postMessage(error);
|
|
|
|
|
process.exit()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function testFeed(feedURL) {
|
|
|
|
|
try {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.testFeed] Testing '+feed)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
var feed = await parser.parseURL(feedURL);
|
|
|
|
|
} catch (e) {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.testFeed] '+e)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
errorMessage(e, "INVALID", feedURL);
|
|
|
|
|
}
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog("[options.testFeed] Feed is valid, continuing")
|
2023-10-01 22:11:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function feed(nick, setting, value) {
|
|
|
|
|
if (setting === 'add') {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.feed] '+nick+' is adding '+value)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
await testFeed(value);
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-01 22:11:27 -04: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-01 22:11:25 -04:00
|
|
|
|
}
|
2023-10-01 22:11:27 -04:00
|
|
|
|
file.append(nick+".feeds", value);
|
|
|
|
|
file.save();
|
|
|
|
|
sendUpstream(value + ' added to your feed list')
|
|
|
|
|
|
2023-10-01 22:11:25 -04:00
|
|
|
|
}
|
2023-10-01 22:11:25 -04:00
|
|
|
|
if (setting === 'list') {
|
|
|
|
|
content = [];
|
|
|
|
|
try {
|
|
|
|
|
var feedsArr = uconfig[nick].feeds
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.feed] Listing existing feeds for '+nick+': '+feedsArr)
|
2023-10-01 22:11:25 -04: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-01 22:11:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 22:11:25 -04:00
|
|
|
|
async function alias(setting, value, url, nick) {
|
|
|
|
|
if (setting === 'add') {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
var value = value.toUpperCase()
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.alias] Adding/editing an alias for'+nick+': '+value+' ==> '+url)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
await testFeed(url);
|
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-01 22:11:26 -04:00
|
|
|
|
file.set(nick+'.alias.'+value.toUpperCase(), url);
|
2023-10-01 22:11:25 -04:00
|
|
|
|
file.save();
|
2023-10-01 22:11:26 -04:00
|
|
|
|
sendUpstream('Alias added ('+value.toUpperCase()+' ==> '+url+')')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
}
|
|
|
|
|
if (setting === 'del') {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.alias] Removing an alias for '+nick+': '+value+' ==> \"\"')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
var file = editJsonFile('/home/node/app/config/usersettings.json');
|
2023-10-01 22:11:26 -04:00
|
|
|
|
file.set(nick+'.alias.'+value.toUpperCase(), "");
|
2023-10-01 22:11:25 -04:00
|
|
|
|
file.save();
|
2023-10-01 22:11:26 -04:00
|
|
|
|
sendUpstream('Alias removed ('+value.toUpperCase()+' ==> BTFO\'d)')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
}
|
|
|
|
|
if (setting === 'list') {
|
|
|
|
|
content = [];
|
|
|
|
|
var obj = uconfig[nick].alias
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.alias] Listing aliases for '+nick+': '+obj)
|
2023-10-01 22:11:25 -04: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-01 22:11:25 -04:00
|
|
|
|
async function get(setting) {
|
2023-10-01 22:11:26 -04:00
|
|
|
|
consoleLog('[options.get] Getting value of '+setting)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
var file = editJsonFile('/home/node/app/config/default.json')
|
|
|
|
|
console.log(file.get(setting));
|
|
|
|
|
sendUpstream(file.get(setting))
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-01 22:11:25 -04:00
|
|
|
|
if (setting === 'feed') {
|
|
|
|
|
feed(user, setting2, value);
|
2023-10-01 22:11:25 -04:00
|
|
|
|
} else if (setting === 'list') {
|
|
|
|
|
feed(user, setting2)
|
2023-10-01 22:11:25 -04:00
|
|
|
|
} else if (setting === 'get') {
|
|
|
|
|
get(setting2);
|
2023-10-01 22:11:25 -04:00
|
|
|
|
} else if(setting === 'alias') {
|
|
|
|
|
alias(setting2, value, value2, user)
|
2023-10-01 22:11:26 -04:00
|
|
|
|
} else {
|
|
|
|
|
sendUpstream(errorMsg+' '+setting+' is not a valid option')
|
2023-10-01 22:11:25 -04:00
|
|
|
|
}
|