2021-12-07 17:57:08 +00:00
|
|
|
import { BotsConfig } from './lib/config'
|
|
|
|
import { logger } from './lib/logger'
|
2021-12-08 14:29:34 +00:00
|
|
|
import { BotComponent } from './lib/bot/component'
|
|
|
|
import { BotHandlerDemo } from './lib/bot/handlers/demo'
|
2021-12-07 12:14:01 +00:00
|
|
|
|
2021-12-07 17:57:08 +00:00
|
|
|
if (!process.argv[2]) {
|
2021-12-07 12:14:01 +00:00
|
|
|
throw new Error('Missing parameter: the demobot configuration file path')
|
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
const botsConfig = new BotsConfig(process.argv[2])
|
2021-12-07 12:14:01 +00:00
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
const runningBots: BotComponent[] = []
|
2021-12-07 17:57:08 +00:00
|
|
|
|
|
|
|
async function start (botsConfig: BotsConfig): Promise<void> {
|
|
|
|
await botsConfig.load()
|
|
|
|
|
|
|
|
let atLeastOne: boolean = false
|
|
|
|
if (botsConfig.useDemoBot()) {
|
|
|
|
atLeastOne = true
|
|
|
|
logger.info('Starting DemoBot...')
|
|
|
|
|
|
|
|
const config = botsConfig.getDemoBotConfig()
|
2021-12-08 14:29:34 +00:00
|
|
|
const instance = new BotComponent(
|
2021-12-07 17:57:08 +00:00
|
|
|
'DemoBot',
|
|
|
|
{
|
|
|
|
service: config.service,
|
|
|
|
domain: config.domain,
|
|
|
|
password: config.password
|
|
|
|
},
|
2021-12-08 14:29:34 +00:00
|
|
|
config.mucDomain
|
2021-12-07 17:57:08 +00:00
|
|
|
)
|
|
|
|
runningBots.push(instance)
|
2021-12-08 14:29:34 +00:00
|
|
|
|
|
|
|
instance.connect().then(async () => {
|
|
|
|
for (const roomId of config.rooms) {
|
|
|
|
const room = await instance.joinRoom(roomId, 'DemoBot')
|
|
|
|
room.attachHandler(new BotHandlerDemo(room))
|
|
|
|
}
|
|
|
|
}).catch(err => { throw err })
|
2021-12-07 12:14:01 +00:00
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
if (!atLeastOne) {
|
|
|
|
logger.info('No bot to launch, exiting.')
|
|
|
|
process.exit(0)
|
2021-12-07 12:14:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:57:08 +00:00
|
|
|
async function shutdown (): Promise<void> {
|
|
|
|
logger.info('Shutdown...')
|
|
|
|
for (const bot of runningBots) {
|
|
|
|
logger.info('Stopping the bot ' + bot.botName + '...')
|
2021-12-08 14:29:34 +00:00
|
|
|
await bot.disconnect()
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
2021-12-07 12:14:01 +00:00
|
|
|
process.exit(0)
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:57:08 +00:00
|
|
|
// catching signals and do something before exit
|
|
|
|
['SIGHUP', 'SIGINT', 'SIGQUIT', 'SIGILL', 'SIGTRAP', 'SIGABRT',
|
|
|
|
'SIGBUS', 'SIGFPE', 'SIGUSR1', 'SIGSEGV', 'SIGUSR2', 'SIGTERM'
|
|
|
|
].forEach((sig) => {
|
|
|
|
process.on(sig, () => {
|
|
|
|
logger.debug('Receiving signal: ' + sig)
|
|
|
|
shutdown().catch((err) => {
|
|
|
|
logger.error(`Error on shutting down: ${err as string}`)
|
2021-12-07 12:14:01 +00:00
|
|
|
})
|
2021-12-07 17:57:08 +00:00
|
|
|
})
|
2021-12-07 12:14:01 +00:00
|
|
|
})
|
|
|
|
|
2021-12-07 17:57:08 +00:00
|
|
|
start(botsConfig).catch((err) => {
|
|
|
|
logger.error(`Function start failed: ${err as string}`)
|
|
|
|
process.exit(1)
|
|
|
|
})
|