import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { TLSSocket, connect } from 'tls'; import * as irc from 'slate-irc'; import { DomainrproxyService } from 'src/domainrproxy/domainrproxy.service'; @Injectable() export class IrcbotService { private readonly socket: TLSSocket; private readonly client: irc.Client; private readonly prefix: string = process.env.NODE_ENV === 'production' ? '.' : '?'; private readonly logger: Logger = new Logger(IrcbotService.name); constructor( public readonly configService: ConfigService, public readonly domainrProxy: DomainrproxyService, ) { if (!this.configService.get('irc.enabled')) { this.logger.verbose('IRC disabled, not connecting'); return; }; const nick = this.configService.get('irc.nick') || 'us-dev'; const ircPassword = this.configService.get('irc.password'); this.socket = connect({ port: this.configService.get('irc.port'), host: this.configService.get('irc.server'), rejectUnauthorized: false, }); this.client = irc(this.socket); if (ircPassword) this.client.pass(ircPassword); this.client.user(nick, `us.dev bot ${process.env.NODE_ENV}`); this.client.nick(nick); this.client.send('nickserv', `identify ${nick} ${ircPassword}`); console.log(nick); const channel: string = this.configService.get( 'irc.channel', ) as string; this.client.join(channel); this.client.names(channel, (err, names) => { this.logger.verbose(`${channel} contains ${JSON.stringify(names)}`); }); // this.client.on('errors', (event: any) => this.logger.error(event)); // this.client.on('error', (event: any) => this.logger.error(event)); // this.client.on('notice', (event) => this.logger.verbose(event)); // this.client.on('data', (event) => this.logger.verbose('data', event)); // this.client.on('quit', console.log); // this.client.on('mode', console.log); this.client.on('message', async (message) => { if (message.to !== channel) return; if (message.message[0] !== this.prefix) return; const [command, ...args] = message.message.substring(1).split(' '); this.logger.verbose(`${command} in ${message.to}`); try { switch (command) { case 'domain': const domain = args[0]; const queryResult = await this.domainrProxy.queryForDomain(domain); this.client.send( channel, `${queryResult[0].domain} ${queryResult[0].status}`, ); return; default: this.client.send(channel, `Dunno what ${command} means`); return; } } catch { } }); } }