132 lines
3.6 KiB
TypeScript
132 lines
3.6 KiB
TypeScript
import { WhoResponse } from '../utility/who-parser';
|
|
import { WhoisResponse } from '../utility/whois-parser';
|
|
import { IIRCOptions, IIRCServerData, IQueue } from './irc.interfaces';
|
|
|
|
export interface IWritableEventEmitter {
|
|
emit(event: string, ...args: any[]): void;
|
|
on(event: string, handler: (...args: any[]) => void): void;
|
|
write(format: string, ...args: any[]): void;
|
|
}
|
|
|
|
export interface IIRCConnector extends IWritableEventEmitter {
|
|
/**
|
|
* Current connection status.
|
|
*/
|
|
connected: boolean;
|
|
/**
|
|
* Connect the socket to the server.
|
|
*/
|
|
connect(): Promise<void>;
|
|
/**
|
|
* Forcefully disconnect the socket from the server.
|
|
*/
|
|
destroy(): Promise<void>;
|
|
}
|
|
|
|
export interface IIRCWrapper extends IWritableEventEmitter {
|
|
/**
|
|
* Connection options for the IRC server.
|
|
*/
|
|
options: IIRCOptions;
|
|
/**
|
|
* The connector to use for connecting to the IRC server.
|
|
*/
|
|
connector: IIRCConnectorConstructor;
|
|
/**
|
|
* Get connection status. `authenticated` is a more reliable indicator
|
|
* of a successful connection.
|
|
*/
|
|
connected: boolean;
|
|
/**
|
|
* Channels the bot is currently in.
|
|
*/
|
|
channels: string[];
|
|
/**
|
|
* Current collectors waiting for their reply.
|
|
*/
|
|
queue: IQueue[];
|
|
/**
|
|
* Login success status.
|
|
*/
|
|
authenticated: boolean;
|
|
/**
|
|
* Information about the IRC server gathered during runtime
|
|
*/
|
|
serverData: IIRCServerData;
|
|
/**
|
|
* Send a raw command to the server.
|
|
*
|
|
* **WARNING:** Line break characters could have an unintended side-effect!
|
|
* Filter user-generated content!
|
|
* @param format Command
|
|
* @param args Command arguments
|
|
*/
|
|
write(format: string, ...args: any[]): void;
|
|
/**
|
|
* Create a new connection to the configured IRC server.
|
|
*/
|
|
connect(): Promise<void>;
|
|
/**
|
|
* Disconnect from the IRC server gracefully, sends `QUIT`.
|
|
* @param reason Reason for disconnection
|
|
*/
|
|
disconnect(reason?: string): Promise<void>;
|
|
/**
|
|
* Set a new nickname. A `nick` event will be fired for self.
|
|
* When setting the new nick fails, the connection will reset it
|
|
* and emit your previous nickname back in the `nick` event.
|
|
* @param newNick New nickname
|
|
*/
|
|
setNick(newNick: string): void;
|
|
/**
|
|
* Asynchronously ping the server.
|
|
* @returns Ping in milliseconds
|
|
*/
|
|
getPing(): Promise<number>;
|
|
/**
|
|
* Asynchronous WHOIS query on `nick`
|
|
* @param nick Nick to query
|
|
* @returns Parsed WHOIS response object
|
|
*/
|
|
whois(nick: string): Promise<WhoisResponse>;
|
|
/**
|
|
* Asynchronous WHO query on `target`
|
|
* @param target Channel or nick to query
|
|
* @returns Parsed WHO response object array
|
|
*/
|
|
who(target: string): Promise<WhoResponse[]>;
|
|
/**
|
|
* Get a list of names in a channel asynchronously
|
|
* @param channel Channel to query
|
|
* @returns String list of nicks (with mode prefixes preserved)
|
|
*/
|
|
names(channel: string): Promise<string[]>;
|
|
/**
|
|
* Get a list of channels asynchronously
|
|
* @returns Channel list in `[<channel>, <# visible>, <topic>][]` format
|
|
*/
|
|
list(): Promise<string[][]>;
|
|
/**
|
|
* Add a collector to the queue.
|
|
* This is used to grab lines from the server, wait for them
|
|
* and return them in a callback.
|
|
* @param collector IRC line collector
|
|
* @param line Command to send to the server
|
|
* @param args Arguments to the command
|
|
*/
|
|
useCollector(collector: IQueue, line: string, ...args: any[]): void;
|
|
}
|
|
|
|
export interface IIRCConnectorConstructor {
|
|
new (
|
|
secure: boolean,
|
|
host: string,
|
|
port?: number,
|
|
opts?: Record<string, unknown>,
|
|
): IIRCConnector;
|
|
}
|
|
|
|
export interface IIRCConnectionConstructor {
|
|
new (options: IIRCOptions, connector: IIRCConnectorConstructor): IIRCWrapper;
|
|
}
|