import { IIRCLine, IQueue } from '../types/irc.interfaces'; /** * Collect a single line from the server. */ export class Collector implements IQueue { constructor( public await: string, private resolve: (lines: IIRCLine) => void, public from?: string, public match?: (line: IIRCLine) => boolean, ) {} do(line: IIRCLine): void { this.resolve(line); } } /** * Collect lines from the server. */ export class MultiLineCollector implements IQueue { public buffer: IIRCLine[] = []; constructor( public await: string, public additional: string[], private resolve: (lines: IIRCLine[]) => void, public match?: (line: IIRCLine) => boolean, ) {} do(line: IIRCLine, data: IIRCLine[]): void { this.resolve([...data, line]); } digest(line: IIRCLine): void { this.buffer.push(line); } } /** * Get a full WHOIS response from the server * * `311` - Start of WHOIS, Nickname, hostmask, realname * * `319` - Channels * * `378` - Connecting from * * `379` - User modes * * `312` - Server and server name * * `313` - User title * * `330` - Login time * * `335` - Is a bot * * `307` - Registered * * `671` - Secure connection * * `317` - Sign on time and idle time * * `318` - End of WHOIS */ export class WhoisCollector extends MultiLineCollector { constructor(resolve: (lines: IIRCLine[]) => void, target: string) { super( '318', // End of WHOIS [ '311', // Start of WHOIS, Nickname, hostmask, realname '319', // Channels '378', // Connecting from '379', // User modes '312', // Server and server name '313', // User title '330', // Login time '335', // Is a bot '307', // Registered '671', // Secure connection '317', // Sign on time and idle time ], resolve, (line) => target === line.arguments[1], ); } } /** * Get a full WHO response from the server * * `352` - WHO line: ` [*][@|+] : ` * * `315` - end of WHO */ export class WhoCollector extends MultiLineCollector { constructor(resolve: (lines: IIRCLine[]) => void, target: string) { super( '315', // End of WHO [ '352', // WHO line: [*][@|+] : ], resolve, (line) => line.arguments[1] === target || line.arguments[2] === target, ); } }