46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
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
|
|
log("Sending messages to:", 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()}`)
|
|
})
|
|
|
|
await xmpp.start().catch(console.error)
|
|
|
|
return sendMessage
|
|
}
|