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' /** * using a xmpp-server to send messages in a group * * * @author Björn Hase * @license hhttps://www.gnu.org/licenses/gpl-3.0.en.html GPL-3 * @link https://git.node001.net/HerrHase/signpost.git * */ class Xmpp extends Action { /** * run * */ async run() { // cleaning data this._sanitize() const xmpp = client({ service: process.env.XMPP_SERVICE, domain: process.env.XMPP_DOMAIN, username: process.env.XMPP_USERNAME, password: process.env.XMPP_PASSWORD }) if (process.env.APP_DEBUG) { debug(xmpp, true) } // handle if client has errors xmpp.on('error', (error) => { logger(this.flow.uuid).error('xmpp / error ' + error) }) // handle if client goes online xmpp.once('online', async (address) => { // join group await xmpp.send(xml('presence', { to: process.env.XMPP_ROOM + '/' + process.env.XMPP_USERNAME }, xml('x', { xmlns: 'http://jabber.org/protocol/muc' }))) // sends a message to the room const message = xml( 'message', { type: 'groupchat', to: process.env.XMPP_ROOM }, xml('body', {}, this.data.message) ) await xmpp.send(message) logger(this.flow.uuid).info('xmpp / message send') await xmpp.disconnect() }) await xmpp.start() } /** * clearing data from any malicous code * */ _sanitize() { this.data.message = DOMPurify.sanitize(this.data.message) } } export default Xmpp