diff --git a/config.default.yml b/config.default.yml new file mode 100644 index 0000000..d85b140 --- /dev/null +++ b/config.default.yml @@ -0,0 +1,36 @@ +# Color can be: +# black, red, green, yellow, blue, magenta, cyan, white, gray, grey +# brightRed, brightGreen, brightYellow, brightBlue, brightMagenta, brightCyan, brightWhite + +notifiers: + discord: + enabled: false + api-token: "" + receivers: [ "IDs..." ] + color: "magenta" + email: + enabled: false + smtp: + port: 587 + name: "username" + pass: "secret" + host: "example.com" + secure: true # use TLS + receivers: [ "user@example.com" ] + color: "green" + xmpp: + enabled: true + sender: + user: "username" + pass: "secret" + host: "xmpps://hostname:port" + receivers: [ "exampleJID@example.com" ] + color: "yellow" + + + + +checkers: + ip-changed: + enabled: true + color: "red" \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..8440749 --- /dev/null +++ b/index.js @@ -0,0 +1,13 @@ +import settings from "./libs/settings.js" +import getNotifier from "./libs/getNotifier.js" +import getLogger from "./libs/logger.js" + +const log = getLogger("MAIN", "blue") + +log("Starting up maintenance-bot...") + +log("Starting up notifiers...") +const enabledNotifiers = Object.keys(settings.notifiers).filter(notifier => settings.notifiers[notifier].enabled) +const notifiers = await Promise.all(enabledNotifiers.map(notifier => getNotifier(notifier))) + +console.dir(notifiers) \ No newline at end of file diff --git a/libs/getNotifier.js b/libs/getNotifier.js new file mode 100644 index 0000000..68bb225 --- /dev/null +++ b/libs/getNotifier.js @@ -0,0 +1,5 @@ +export default function getNotifier(notifier) { + return new Promise( + resolve => import(`../modules/notifiers/${notifier}.js`).then( + module => resolve(module.start()))) +} diff --git a/libs/logger.js b/libs/logger.js new file mode 100644 index 0000000..1a9dbff --- /dev/null +++ b/libs/logger.js @@ -0,0 +1,24 @@ +import colors from "colors" +import { createWriteStream } from "fs" + +const fileLog = createWriteStream('maintenance-bot.log', { encoding: "utf8", flags: "a" }) + +function makeDate() { + const now = new Date(Date.now()) + const date = now.toLocaleDateString("en-GB", { hour12: false }) + const time = now.toLocaleTimeString("en-GB", { hour12: false }) + + return `(${date} -- ${time})` +} + +export default function getLogger(prefix, color) { + const clr = colors[color] ?? colors[red] + + return function (...args) { + fileLog.write(`[${prefix}] -- ${makeDate()}:\n`) + fileLog.write(`${args.join(", ")}\n\n`) + + console.log(`${clr(prefix)} ${colors.grey(makeDate())}:`) + console.log(...args, "\n") + } +} \ No newline at end of file diff --git a/libs/settings.js b/libs/settings.js new file mode 100644 index 0000000..1a8b415 --- /dev/null +++ b/libs/settings.js @@ -0,0 +1,7 @@ +import YAML from "yaml" +import { readFileSync } from "fs" + +const yamlData = readFileSync("./config.yml").toString() +const settings = YAML.parse(yamlData) + +export default settings \ No newline at end of file diff --git a/modules/notifiers/xmpp.js b/modules/notifiers/xmpp.js new file mode 100644 index 0000000..455952b --- /dev/null +++ b/modules/notifiers/xmpp.js @@ -0,0 +1,45 @@ +import settings from "../../libs/settings.js" +import { client, jid, xml } from "@xmpp/client" +import debug from "@xmpp/debug" +import getLogger from "../../libs/logger.js" + +const log = getLogger("XMPP", settings.notifiers.xmpp.color) + +export async function start() { + log("Starting up XMPP notifier...") + + const xmpp = client({ + service: settings.notifiers.xmpp.sender.host, + username: settings.notifiers.xmpp.sender.user, + password: settings.notifiers.xmpp.sender.pass + }) + + xmpp.on("error", err => console.error(err)) + xmpp.on("offline", () => console.log("OFFLINE!")) + + async function sendMessage(msg) { + const receivers = settings.notifiers.xmpp.receivers + const stanzas = receivers.map( + address => xml( + "message", + { to: address, type: "chat" }, + xml( + "body", + null, + msg + ) + ) + ) + + await xmpp.sendMany(stanzas).catch(console.error) + } + + xmpp.on("online", address => { + log(`Logged in as ${address.toString()}`) + sendMessage("Started up 'maintenance-bot'...") + }) + + await xmpp.start().catch(console.error) + + return sendMessage +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..c3ea6fc --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "maintenance-bot", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "maintenance", + "bot" + ], + "author": "BurnyLlama", + "license": "WTFPL", + "dependencies": { + "@xmpp/client": "^0.13.1", + "@xmpp/debug": "^0.13.0", + "colors": "^1.4.0", + "yaml": "^2.1.0" + } +}