xmpp-bot/lib/config/index.js

54 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2019-10-13 03:38:25 +02:00
/**
* Configuration
*
* Handle configuration file
*
* @file This files defines the configuration
* @author nioc
* @since 1.0.0
* @license AGPL-3.0+
*/
module.exports = function Configuration (logger, configPath = null) {
2019-10-13 03:38:25 +02:00
let config
if (configPath === null) {
configPath = './lib/config/config.json'
}
2019-10-13 03:38:25 +02:00
try {
let data = require('fs').readFileSync(configPath)
2019-10-13 03:38:25 +02:00
config = JSON.parse(data)
} catch (error) {
logger.fatal(`Invalid configuration file: ${error.message}, current directory is: ${process.cwd()}`)
2019-10-13 03:38:25 +02:00
process.exit(99)
}
return {
listener: {
path: config.webhooksListener.path,
port: config.webhooksListener.port,
ssl: config.webhooksListener.ssl,
log: config.webhooksListener.accessLog,
users: config.webhooksListener.users.reduce((acc, user) => {
acc[user.login] = user.password
return acc
}, {})
},
xmpp: config.xmppServer,
logger: config.logger,
getWebhookAction: (path) => {
return config.incomingWebhooks.find((webhook) => {
return (webhook.path === path)
})
},
getOutgoingWebhook: (code) => {
return config.outgoingWebhooks.find((webhook) => {
return (webhook.code === code)
})
},
getXmppHookAction: (room) => {
return config.xmppHooks.find((xmppHook) => {
return (xmppHook.room === room)
})
}
}
}