irclib/src/utility/collector.ts

100 lines
2.1 KiB
TypeScript

import { IIRCLine, IQueue } from '../types/irc.interfaces';
export class Collector implements IQueue<IIRCLine> {
constructor(
public await: string,
private resolve: (lines: IIRCLine) => void,
public from?: string,
) {}
do(line: IIRCLine): void {
this.resolve(line);
}
}
export class MultiLineCollector implements IQueue<IIRCLine[]> {
public buffer: IIRCLine[] = [];
constructor(
public await: string,
public additional: string[],
private resolve: (lines: IIRCLine[]) => void,
) {}
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) {
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,
);
}
}
/**
* Get a full WHO response from the server
*
* `352` - WHO line: `<channel> <user> <host> <server> <nick> <H|G>[*][@|+] :<hopcount> <real_name>`
*
* `315` - end of WHO
*/
export class WhoCollector extends MultiLineCollector {
constructor(resolve: (lines: IIRCLine[]) => void) {
super(
'315', // End of WHO
[
'352', // WHO line: <channel> <user> <host> <server> <nick> <H|G>[*][@|+] :<hopcount> <real_name>
],
resolve,
);
}
}