2021-12-08 14:29:34 +00:00
|
|
|
import type { BotComponent } from './component'
|
|
|
|
import type { BotHandler } from './handlers/base'
|
|
|
|
import type { XMPPStanza, XMPPUser } from './types'
|
|
|
|
import EventEmitter from 'events'
|
|
|
|
import { JID } from '@xmpp/jid'
|
2021-12-07 17:57:08 +00:00
|
|
|
import { logger } from '../logger'
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
export class BotRoom extends EventEmitter {
|
|
|
|
protected state: 'offline' | 'online' = 'offline'
|
|
|
|
protected userJID: JID | undefined
|
|
|
|
protected readonly roster: Map<string, XMPPUser> = new Map()
|
2021-12-07 17:57:08 +00:00
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
protected readonly handlers: BotHandler[] = []
|
2021-12-07 17:57:08 +00:00
|
|
|
|
|
|
|
constructor (
|
2021-12-08 14:29:34 +00:00
|
|
|
protected readonly component: BotComponent,
|
|
|
|
protected readonly roomJID: JID
|
2021-12-07 17:57:08 +00:00
|
|
|
) {
|
2021-12-08 14:29:34 +00:00
|
|
|
super()
|
|
|
|
|
|
|
|
this.on('reset', () => {
|
|
|
|
this.state = 'offline'
|
|
|
|
this.roster.clear()
|
|
|
|
})
|
|
|
|
this.on('stanza', (stanza: XMPPStanza, resource?: string) => {
|
|
|
|
this.receiveStanza(stanza, resource)
|
|
|
|
})
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public isOnline (): boolean {
|
|
|
|
return this.state === 'online'
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 15:12:47 +00:00
|
|
|
public onlineUserCount (): number {
|
|
|
|
let count = 0
|
|
|
|
this.roster.forEach(user => {
|
|
|
|
if (user.state === 'online') { count++ }
|
|
|
|
})
|
|
|
|
return count
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public async join (nick: string): Promise<void> {
|
|
|
|
this.userJID = new JID(this.roomJID.getLocal(), this.roomJID.getDomain(), nick)
|
|
|
|
logger.debug(`Emitting a presence for room ${this.roomJID.toString()}...`)
|
|
|
|
await this.component.sendStanza('presence',
|
|
|
|
{
|
|
|
|
to: this.userJID.toString()
|
|
|
|
},
|
|
|
|
this.component.xml('x', {
|
|
|
|
xmlns: 'http://jabber.org/protocol/muc'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
// FIXME: should wait for a presence stanza from the server.
|
|
|
|
// FIXME: should handle used nick errors.
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public async part (): Promise<void> {
|
|
|
|
if (!this.userJID) { return }
|
|
|
|
logger.debug(`Emitting a presence=unavailable for room ${this.roomJID.toString()}...`)
|
|
|
|
await this.component.sendStanza('presence', {
|
|
|
|
to: this.userJID.toString(),
|
|
|
|
type: 'unavailable'
|
|
|
|
})
|
|
|
|
// FIXME: should wait for a presence stanza from the server.
|
|
|
|
}
|
|
|
|
|
|
|
|
public async sendGroupchat (msg: string): Promise<void> {
|
|
|
|
if (!this.userJID) { return }
|
|
|
|
logger.debug(`Emitting a groupchat message for room ${this.roomJID.toString()}...`)
|
|
|
|
await this.component.sendStanza(
|
2021-12-07 17:57:08 +00:00
|
|
|
'message',
|
|
|
|
{
|
|
|
|
type: 'groupchat',
|
2021-12-08 14:29:34 +00:00
|
|
|
to: this.roomJID.toString()
|
2021-12-07 17:57:08 +00:00
|
|
|
},
|
2021-12-08 14:29:34 +00:00
|
|
|
this.component.xml('body', {}, msg)
|
2021-12-07 17:57:08 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public receiveStanza (stanza: XMPPStanza, fromResource?: string): void {
|
|
|
|
if (stanza.name === 'presence') {
|
|
|
|
this.receivePresenceStanza(stanza, fromResource)
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public receivePresenceStanza (stanza: XMPPStanza, fromResource?: string): void {
|
|
|
|
if (!fromResource) {
|
2021-12-07 17:57:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-08 14:29:34 +00:00
|
|
|
|
|
|
|
const isPresent = stanza.attrs.type !== 'unavailable'
|
|
|
|
|
|
|
|
const statusElems = stanza.getChild('x')?.getChildren('status')
|
|
|
|
const statusCodes = []
|
|
|
|
if (statusElems) {
|
|
|
|
for (const s of statusElems) {
|
|
|
|
statusCodes.push(parseInt(s.attrs.code))
|
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
2021-12-08 14:29:34 +00:00
|
|
|
const isMe = statusCodes.includes(110) // status 110 means that is concern the current user.
|
|
|
|
|
|
|
|
let user = this.roster.get(fromResource)
|
|
|
|
const previousState = user?.state
|
2021-12-07 17:57:08 +00:00
|
|
|
if (!isPresent) {
|
2021-12-08 14:29:34 +00:00
|
|
|
if (!user) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
user.state = 'offline'
|
|
|
|
if (isMe) {
|
|
|
|
this.state = 'offline'
|
|
|
|
}
|
|
|
|
if (previousState === 'online') {
|
|
|
|
this.emit('room_part', user)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!user) {
|
|
|
|
user = {
|
|
|
|
state: 'online',
|
|
|
|
nick: fromResource,
|
|
|
|
isMe: isMe
|
|
|
|
}
|
|
|
|
this.roster.set(fromResource, user)
|
|
|
|
} else {
|
|
|
|
user.state = 'online'
|
|
|
|
}
|
|
|
|
if (isMe) {
|
|
|
|
this.state = 'online'
|
|
|
|
}
|
|
|
|
if (previousState !== 'online') {
|
|
|
|
this.emit('room_join', user)
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:29:34 +00:00
|
|
|
public attachHandler (handler: BotHandler): void {
|
|
|
|
this.handlers.push(handler)
|
|
|
|
}
|
2021-12-08 15:12:47 +00:00
|
|
|
|
|
|
|
public detachHandlers (): void {
|
|
|
|
for (const handler of this.handlers) {
|
|
|
|
handler.stop()
|
|
|
|
}
|
|
|
|
}
|
2021-12-07 17:57:08 +00:00
|
|
|
}
|