Skip to content

Commit

Permalink
Version 2.2.0
Browse files Browse the repository at this point in the history
Update dependencies (security fix)
Fix lint errors
  • Loading branch information
nioc committed Sep 27, 2022
1 parent e7f8967 commit 0617255
Show file tree
Hide file tree
Showing 11 changed files with 10,335 additions and 5,075 deletions.
2 changes: 1 addition & 1 deletion lib/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function Configuration (logger, configPath = null) {
configPath = './lib/config/config.json'
}
try {
let data = require('fs').readFileSync(configPath)
const data = require('fs').readFileSync(configPath)
config = JSON.parse(data)
} catch (error) {
logger.fatal(`Invalid configuration file: ${error.message}, current directory is: ${process.cwd()}`)
Expand Down
2 changes: 1 addition & 1 deletion lib/error/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

module.exports = (logger, xmpp) => {
let nodeCleanup = require('node-cleanup')
const nodeCleanup = require('node-cleanup')
nodeCleanup(function (exitCode, signal) {
logger.warn(`Received ${exitCode}/${signal} (application is closing), disconnect from XMPP server`)
try {
Expand Down
4 changes: 2 additions & 2 deletions lib/logger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = () => {
}

// add appenders
let appenders = []
const appenders = []
if (config.file.active) {
const fs = require('fs')
if (!fs.existsSync(config.file.path)) {
Expand Down Expand Up @@ -73,7 +73,7 @@ module.exports = () => {
}
},
categories: {
default: { appenders: appenders, level: 'info' }
default: { appenders, level: 'info' }
}
})
logger = log4js.getLogger()
Expand Down
8 changes: 4 additions & 4 deletions lib/outgoing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
*/

module.exports = async (logger, config, xmpp, user, destination, message, type, code) => {
let webhook = config.getOutgoingWebhook(code)
const webhook = config.getOutgoingWebhook(code)
if (!webhook) {
logger.warn(`There is no webhook with code "${code}"`)
throw new Error(`There is no webhook with code "${code}"`)
}
const { promisify } = require('util')
const request = promisify(require('request'))
// request.debug = true
let options = {
const options = {
method: 'POST',
url: webhook.url,
strictSSL: webhook.strictSSL
Expand Down Expand Up @@ -50,7 +50,7 @@ module.exports = async (logger, config, xmpp, user, destination, message, type,
logger.trace('Content-type: application/x-www-form-urlencoded')
options.form = {
from: user,
message: message,
message,
channel: destination
}
logger.trace('Outgoing webhook request:', options.form)
Expand All @@ -59,7 +59,7 @@ module.exports = async (logger, config, xmpp, user, destination, message, type,
logger.trace('Content-type: application/json')
options.json = {
from: user,
message: message,
message,
channel: destination
}
logger.trace('Outgoing webhook request:', options.json)
Expand Down
4 changes: 2 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

// create default logger
let logger = require('./logger')()
const logger = require('./logger')()

// get configuration
let config = require('./config')(logger)
const config = require('./config')(logger)

// update logger with configuration
logger.updateConfig(config.logger)
Expand Down
28 changes: 14 additions & 14 deletions lib/webhook/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = (logger, config, xmpp) => {
const port = config.listener.port || 8000
const portSsl = config.listener.ssl.port || 8001

var webhook = express()
const webhook = express()

// handle connection from proxy (get IP in 'X-Forwarded-For' header)
webhook.set('trust proxy', true)
Expand Down Expand Up @@ -57,29 +57,28 @@ module.exports = (logger, config, xmpp) => {
// handle post request
webhook.post(config.listener.path + '/*', (req, res) => {
logger.info(`Incoming webhook from ${req.auth.user}`)
let webhook = config.getWebhookAction(req.path)
const webhook = config.getWebhookAction(req.path)
if (!webhook) {
logger.error(`Webhook received: ${req.path}, not found`)
return res.status(404).send('Webhook not found')
}
logger.debug(`Webhook received: ${webhook.path}, start action: ${webhook.action}`)
logger.trace(req.body)
switch (webhook.action) {
case 'send_xmpp_message':

case 'send_xmpp_message': {
// get destination
if ('destination' in req.body === false) {
logger.error('Destination not found')
return res.status(400).send('Destination not found')
}
let destination = req.body.destination
const destination = req.body.destination

// get message
if ('message' in req.body === false) {
logger.error('Message not found')
return res.status(400).send('Message not found')
}
let message = req.body.message
const message = req.body.message

// check if destination is a group chat
const type = config.xmpp.rooms.some((room) => room.id === destination) ? 'groupchat' : 'chat'
Expand All @@ -88,17 +87,17 @@ module.exports = (logger, config, xmpp) => {
logger.trace(`Send to ${destination} (group:${type}) following message :\r\n${message}`)
xmpp.send(destination, message, type)
.then(() => {
return res.status(200).send({ 'status': 'ok', destination })
return res.status(200).send({ status: 'ok', destination })
})
.catch(() => {
return res.status(500).send('Could not send message')
})
break
}

case 'send_xmpp_template':

case 'send_xmpp_template': {
// bind data in template
let msg = webhook.template.replace(/\$\{(.+?)\}/g, (match, $1) => {
const msg = webhook.template.replace(/\$\{(.+?)\}/g, (match, $1) => {
return jmespath.search(req.body, $1) || ''
})
logger.trace(`Message:\r\n${msg}`)
Expand All @@ -114,6 +113,7 @@ module.exports = (logger, config, xmpp) => {
return res.status(500).send('Could not send message')
})
break
}

default:
return res.status(204).send()
Expand All @@ -133,10 +133,10 @@ module.exports = (logger, config, xmpp) => {
// get IP v4 addresses and prepare endpoints for output
let addresses = []
const networkInterfaces = require('os').networkInterfaces()
for (let ifaceName in networkInterfaces) {
for (const ifaceName in networkInterfaces) {
addresses = addresses.concat(networkInterfaces[ifaceName].reduce((add, iface) => {
if (iface['family'] === 'IPv4') {
add.push(iface['address'])
if (iface.family === 'IPv4') {
add.push(iface.address)
}
return add
}, []))
Expand Down Expand Up @@ -166,7 +166,7 @@ module.exports = (logger, config, xmpp) => {
try {
fs.accessSync(config.listener.ssl.certPath, fs.constants.R_OK)
logger.debug('Can read certificate')
let credentials = {
const credentials = {
key: fs.readFileSync(config.listener.ssl.keyPath),
cert: fs.readFileSync(config.listener.ssl.certPath)
}
Expand Down
19 changes: 10 additions & 9 deletions lib/xmpp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = (logger, config) => {
})
// join rooms
config.xmpp.rooms.forEach(function (room) {
let occupantJid = room.id + '/' + address.local
const occupantJid = room.id + '/' + address.local
logger.debug(`Join room: ${room.id} ('${occupantJid}')`)
const stanza = xml(
'presence', {
Expand All @@ -78,16 +78,16 @@ module.exports = (logger, config) => {
// not a message, do nothing
return
}
let type = stanza.attrs.type
const type = stanza.attrs.type
switch (type) {
case 'chat':
case 'groupchat':
let body = stanza.getChild('body')
case 'groupchat': {
const body = stanza.getChild('body')
if (!body) {
// empty body, do nothing
return
}
let fromJid = jid(stanza.attrs.from)
const fromJid = jid(stanza.attrs.from)
// for chat, "to" and "replyTo" must be something like "user@domain.ltd", "from" is local part "user"
let to = this.jid.bare()
let from = fromJid.local
Expand All @@ -102,15 +102,15 @@ module.exports = (logger, config) => {
return
}
}
let message = body.text()
const message = body.text()
// handle message delivery receipts for chat
if (type === 'chat') {
let request = stanza.getChild('request')
const request = stanza.getChild('request')
if (request &&
request.attrs.xmlns &&
request.attrs.xmlns === 'urn:xmpp:receipts' &&
stanza.attrs.id) {
logger.debug(`Message delivery receipt is requested and will be processed`)
logger.debug('Message delivery receipt is requested and will be processed')
const receiptStanza = xml(
'message', {
to: fromJid
Expand All @@ -127,7 +127,7 @@ module.exports = (logger, config) => {
}
logger.info(`Incoming ${type} message from ${from} (${fromJid.toString()}) to ${to}`)
logger.debug(`Message: "${message.replace(/\n|\r/g, ' ')}"`)
let xmppHook = config.getXmppHookAction(to.toString())
const xmppHook = config.getXmppHookAction(to.toString())
if (!xmppHook) {
logger.error(`There is no action for incoming ${type} message to: "${to}"`)
return
Expand All @@ -144,6 +144,7 @@ module.exports = (logger, config) => {
break
}
break
}
}
})

Expand Down
Loading

0 comments on commit 0617255

Please sign in to comment.