2021-12-08 14:29:34 +00:00
|
|
|
import type { XMPPStanza, XMPPStanzaType } from './types'
|
|
|
|
import type { Node } from '@xmpp/xml'
|
2021-12-07 17:57:08 +00:00
|
|
|
import { logger } from '../logger'
|
2021-12-08 14:29:34 +00:00
|
|
|
import { component, xml, Component, Options } from '@xmpp/component'
|
|
|
|
import { parse, JID } from '@xmpp/jid'
|
|
|
|
import { BotRoom } from './room'
|
2021-12-07 17:57:08 +00:00
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
class BotComponent {
|
2021-12-08 11:29:21 +00:00
|
|
|
protected xmpp?: Component
|
|
|
|
protected address?: JID
|
2021-12-08 14:29:34 +00:00
|
|
|
public readonly xml = xml
|
|
|
|
protected rooms: Map<string, BotRoom> = new Map()
|
2021-12-07 17:57:08 +00:00
|
|
|
|
|
|
|
constructor (
|
|
|
|
public readonly botName: string,
|
2021-12-08 14:29:34 +00:00
|
|
|
protected readonly connectionConfig: Options,
|
|
|
|
protected readonly mucDomain: string
|
2021-12-07 17:57:08 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
public async connect (): Promise<void> {
|
|
|
|
this.xmpp = component({
|
|
|
|
service: this.connectionConfig.service,
|
|
|
|
domain: this.connectionConfig.domain,
|
|
|
|
password: this.connectionConfig.password
|
2021-12-08 11:29:21 +00:00
|
|
|
})
|
2021-12-07 17:57:08 +00:00
|
|
|
|
|
|
|
this.xmpp.on('error', (err: any) => {
|
|
|
|
logger.error(err)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.xmpp.on('offline', () => {
|
|
|
|
logger.info(`${this.botName} is now offline.`)
|
|
|
|
})
|
|
|
|
|
|
|
|
this.xmpp.on('stanza', (stanza: XMPPStanza) => {
|
2021-12-08 14:29:34 +00:00
|
|
|
logger.debug('stanza received' + stanza.toString())
|
|
|
|
if (!stanza.attrs.from) { return }
|
|
|
|
const jid = parse(stanza.attrs.from)
|
|
|
|
const roomJid = jid.bare() // removing the «resource» part of the jid.
|
|
|
|
const room = this.rooms.get(roomJid.toString())
|
|
|
|
if (!room) {
|
|
|
|
return
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
2021-12-08 14:29:34 +00:00
|
|
|
room.emit('stanza', stanza, jid.getResource())
|
2021-12-07 17:57:08 +00:00
|
|
|
})
|
|
|
|
|
2021-12-08 11:29:21 +00:00
|
|
|
this.xmpp.on('online', (address) => {
|
2021-12-07 17:57:08 +00:00
|
|
|
logger.debug('Online with address' + address.toString())
|
|
|
|
|
|
|
|
this.address = address
|
2021-12-08 14:29:34 +00:00
|
|
|
|
|
|
|
// 'online' is emitted at reconnection, so we must reset rooms rosters
|
|
|
|
this.rooms.forEach(room => room.emit('reset'))
|
|
|
|
})
|
|
|
|
|
|
|
|
this.xmpp.on('offline', () => {
|
|
|
|
logger.info(`Stoppping process: ${this.botName} is now offline.`)
|
2021-12-07 17:57:08 +00:00
|
|
|
})
|
|
|
|
|
2021-12-08 11:29:21 +00:00
|
|
|
await this.xmpp.start()
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public async disconnect (): Promise<any> {
|
|
|
|
for (const [roomId, room] of this.rooms) {
|
|
|
|
logger.debug(`Leaving room ${roomId}...`)
|
2021-12-08 15:12:47 +00:00
|
|
|
await room.detachHandlers()
|
2021-12-08 14:29:34 +00:00
|
|
|
await room.part()
|
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
await this.xmpp?.stop()
|
2021-12-08 14:29:34 +00:00
|
|
|
this.xmpp = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendStanza (
|
|
|
|
type: XMPPStanzaType,
|
|
|
|
attrs: object,
|
|
|
|
...children: Node[]
|
|
|
|
): Promise<void> {
|
|
|
|
attrs = Object.assign({
|
|
|
|
from: this.address?.toString()
|
|
|
|
}, attrs)
|
|
|
|
|
|
|
|
const stanza = this.xml(type, attrs, ...children)
|
|
|
|
logger.debug('stanza to emit: ' + stanza.toString())
|
|
|
|
await this.xmpp?.send(stanza)
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public async joinRoom (roomId: string, nick: string): Promise<BotRoom> {
|
|
|
|
const roomJID = new JID(roomId, this.mucDomain)
|
|
|
|
const roomJIDstr = roomJID.toString()
|
|
|
|
let room: BotRoom | undefined = this.rooms.get(roomJIDstr)
|
|
|
|
if (!room) {
|
|
|
|
room = new BotRoom(this, roomJID)
|
|
|
|
this.rooms.set(roomJIDstr, room)
|
|
|
|
}
|
|
|
|
await room.join(nick)
|
|
|
|
return room
|
|
|
|
}
|
|
|
|
|
|
|
|
public async partRoom (roomId: string): Promise<void> {
|
|
|
|
const roomJID = new JID(roomId, this.mucDomain)
|
|
|
|
const room = this.rooms.get(roomJID.toString())
|
|
|
|
if (!room) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
await room.part()
|
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2021-12-08 14:29:34 +00:00
|
|
|
BotComponent
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|