Set config file as parameter and refactoring XMPP

This commit is contained in:
nioc 2019-10-20 01:07:10 +02:00
parent d1da553ec2
commit 5b968af106
2 changed files with 11 additions and 7 deletions

View file

@ -9,13 +9,16 @@
* @license AGPL-3.0+ * @license AGPL-3.0+
*/ */
module.exports = function Configuration (logger) { module.exports = function Configuration (logger, configPath = null) {
let config let config
if (configPath === null) {
configPath = './lib/config/config.json'
}
try { try {
let data = require('fs').readFileSync('./lib/config/config.json') let data = require('fs').readFileSync(configPath)
config = JSON.parse(data) config = JSON.parse(data)
} catch (error) { } catch (error) {
logger.fatal(`Invalid configuration file: ${error.message}`) logger.fatal(`Invalid configuration file: ${error.message}, current directory is: ${process.cwd()}`)
process.exit(99) process.exit(99)
} }
return { return {

View file

@ -12,6 +12,7 @@
module.exports = (logger, config) => { module.exports = (logger, config) => {
const xmpp = require('simple-xmpp') const xmpp = require('simple-xmpp')
const outgoing = require('../outgoing')
this.jid = null this.jid = null
// handle connection // handle connection
@ -30,15 +31,15 @@ module.exports = (logger, config) => {
xmpp.on('chat', function (from, message) { xmpp.on('chat', function (from, message) {
logger.info(`Incoming chat message from ${from}`) logger.info(`Incoming chat message from ${from}`)
logger.debug(`Message: "${message}"`) logger.debug(`Message: "${message}"`)
let xmppHook = config.getXmppHookAction('bot') let xmppHook = config.getXmppHookAction(this.jid)
if (!xmppHook) { if (!xmppHook) {
logger.error('There is no action for incoming chat message from bot') logger.error(`There is no action for incoming chat message to ${this.jid}`)
return return
} }
switch (xmppHook.action) { switch (xmppHook.action) {
case 'outgoing_webhook': case 'outgoing_webhook':
logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`) logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`)
require('../outgoing')(logger, config, xmpp, from, from, message, false, xmppHook.args[0]) outgoing(logger, config, xmpp, from, from, message, false, xmppHook.args[0])
break break
default: default:
break break
@ -62,7 +63,7 @@ module.exports = (logger, config) => {
switch (xmppHook.action) { switch (xmppHook.action) {
case 'outgoing_webhook': case 'outgoing_webhook':
logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`) logger.debug(`Call outgoing webhook: ${xmppHook.args[0]}`)
require('../outgoing')(logger, config, xmpp, from, conference, message, true, xmppHook.args[0]) outgoing(logger, config, xmpp, from, conference, message, true, xmppHook.args[0])
break break
default: default:
break break