From bd82c52fcd4d71b618c37e2274ea454ddcd07718 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Sun, 25 Sep 2022 09:39:47 +0300 Subject: [PATCH] more tweaks --- README.md | 2 +- src/connector/net.connector.ts | 4 ++-- src/connector/websocket.connector.ts | 4 ++-- src/irc.ts | 8 +++++--- src/types/events.ts | 6 +++--- src/types/impl.interface.ts | 7 +++++++ 6 files changed, 20 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index aacf967..4b22cca 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ This module provides two connectors: `IRCSocketConnector` for Node.js usage (`ne The main classes use a special custom [TypedEventEmitter](https://lunasqu.ee/irclib/classes/TypedEventEmitter.html) to send events. -- [Connection wrapper events](https://lunasqu.ee/irclib/types/IRCCommunicatorEvents.html) +- [Connection wrapper events](https://lunasqu.ee/irclib/types/IRCConnectionEvents.html) - [Nick list events](https://lunasqu.ee/irclib/types/NickListEvents.html) Basically, you're mostly just going to need the `message` event for IRC. diff --git a/src/connector/net.connector.ts b/src/connector/net.connector.ts index 4417d88..3d0d985 100644 --- a/src/connector/net.connector.ts +++ b/src/connector/net.connector.ts @@ -1,12 +1,12 @@ import net, { Socket } from 'net'; import tls, { TLSSocket } from 'tls'; -import { ConnectorEvents } from '../types/events'; +import { IRCConnectorEvents } from '../types/events'; import { IIRCConnector } from '../types/impl.interface'; import { formatstr } from '../utility/formatstr'; import { TypedEventEmitter } from '../utility/typed-event-emitter'; export class IRCSocketConnector - extends TypedEventEmitter + extends TypedEventEmitter implements IIRCConnector { connected = false; diff --git a/src/connector/websocket.connector.ts b/src/connector/websocket.connector.ts index fa8b965..bf3b671 100644 --- a/src/connector/websocket.connector.ts +++ b/src/connector/websocket.connector.ts @@ -1,10 +1,10 @@ -import { ConnectorEvents } from '../types/events'; +import { IRCConnectorEvents } from '../types/events'; import { IIRCConnector } from '../types/impl.interface'; import { formatstr } from '../utility/formatstr'; import { TypedEventEmitter } from '../utility/typed-event-emitter'; export class IRCWebSocketConnector - extends TypedEventEmitter + extends TypedEventEmitter implements IIRCConnector { connected = false; diff --git a/src/irc.ts b/src/irc.ts index 5955db0..83f98c7 100644 --- a/src/irc.ts +++ b/src/irc.ts @@ -1,4 +1,4 @@ -import { IRCCommunicatorEvents } from './types/events'; +import { IRCConnectionEvents } from './types/events'; import { IWritableEventEmitter, IIRCConnector, @@ -24,7 +24,7 @@ import { parseWho, WhoResponse } from './utility/who-parser'; import { parseWhois, WhoisResponse } from './utility/whois-parser'; export class IRCConnection - extends TypedEventEmitter + extends TypedEventEmitter implements IIRCWrapper { public channels: string[] = []; @@ -138,6 +138,7 @@ export class IRCConnection private handleServerLine(line: IIRCLine): void { if (this.pumpQueue(line)) { + this.emit('line', line, true); return; } @@ -160,6 +161,8 @@ export class IRCConnection } } + this.emit('line', line, false); + switch (line.command.toLowerCase()) { case 'cap': if ( @@ -495,7 +498,6 @@ export class IRCConnection this.connection.on('data', (line: string) => { const parsedLine = parse(line); - this.emit('line', parsedLine); this.handleServerLine(parsedLine); }); diff --git a/src/types/events.ts b/src/types/events.ts index 062d4bc..3ba72b1 100644 --- a/src/types/events.ts +++ b/src/types/events.ts @@ -1,16 +1,16 @@ import { IIRCLine } from './irc.interfaces'; -export type ConnectorEvents = { +export type IRCConnectorEvents = { error: (error: any) => void; data: (data: string) => void; close: (reason: string) => void; }; -export type IRCCommunicatorEvents = { +export type IRCConnectionEvents = { /** * Parsed line from the IRC server. */ - line: (line: IIRCLine) => void; + line: (line: IIRCLine, processed: boolean) => void; /** * Supported channel user modes from the server (e.g. `ohv: @%+`) */ diff --git a/src/types/impl.interface.ts b/src/types/impl.interface.ts index c190c9d..d071c63 100644 --- a/src/types/impl.interface.ts +++ b/src/types/impl.interface.ts @@ -71,6 +71,13 @@ export interface IIRCWrapper extends IWritableEventEmitter { * @param reason Reason for disconnection */ disconnect(reason?: string): Promise; + /** + * 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