Added XMPP notifier.
This commit is contained in:
parent
da33f6c9ac
commit
d23154d596
36
config.default.yml
Normal file
36
config.default.yml
Normal file
|
@ -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"
|
13
index.js
Normal file
13
index.js
Normal file
|
@ -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)
|
5
libs/getNotifier.js
Normal file
5
libs/getNotifier.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export default function getNotifier(notifier) {
|
||||
return new Promise(
|
||||
resolve => import(`../modules/notifiers/${notifier}.js`).then(
|
||||
module => resolve(module.start())))
|
||||
}
|
24
libs/logger.js
Normal file
24
libs/logger.js
Normal file
|
@ -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")
|
||||
}
|
||||
}
|
7
libs/settings.js
Normal file
7
libs/settings.js
Normal file
|
@ -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
|
45
modules/notifiers/xmpp.js
Normal file
45
modules/notifiers/xmpp.js
Normal file
|
@ -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
|
||||
}
|
22
package.json
Normal file
22
package.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user