import { client, xml } from '@xmpp/client' import debug from '@xmpp/debug' import DOMPurify from 'isomorphic-dompurify' import Action from './../../packages/server/actions/action.js' import logger from './../../packages/server/helper/logger.js' /** * * * @author Björn Hase * @license http://opensource.org/licenses/MIT The MIT License * @link https://git.node001.net/HerrHase/super-hog.git * */ class XmppHelper { /** * * */ constructor(service, domain, username, password) { this.service = service this.domain = domain this.username = username this.password = password } /** * * */ async sendToRoom(flow, room, message) { // cleaning data message = DOMPurify.sanitize(message) const xmpp = client({ service: this.service, domain: this.domain, username: this.username, password: this.password }) if (process.env.APP_DEBUG) { debug(xmpp, true) } // handle if client has errors xmpp.on('error', (error) => { logger(flow.uuid).error('xmpp / error ' + error) }) // handle if client goes online xmpp.once('online', async (address) => { // join group await xmpp.send(xml("presence", { to: room + '/' + this.username }, xml("x", { xmlns: 'http://jabber.org/protocol/muc' }))) // sends a message to the room const message = xml( 'message', { type: 'groupchat', to: room }, xml('body', {}, message) ) await xmpp.send(message) logger(flow.uuid).info('xmpp / message send') await xmpp.disconnect() }) await xmpp.start() } } export default XmppHelper