53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { IRCConnection, IRCNickList, TypedEventEmitter } from '@icynet/irclib';
|
|
import { IRCWebSocketConnector } from '@icynet/irclib/lib/connector/websocket.connector';
|
|
|
|
export interface IRCConnectionWrapper {
|
|
name: string;
|
|
connection: IRCConnection;
|
|
nicklist: IRCNickList;
|
|
}
|
|
|
|
export type ManagerEvents = {
|
|
connectionAdded: (connection: IRCConnectionWrapper) => void;
|
|
connectionRemoved: (connection: IRCConnectionWrapper) => void;
|
|
};
|
|
|
|
let connectionIndex = 0;
|
|
export class IRCManager extends TypedEventEmitter<ManagerEvents> {
|
|
public connections: IRCConnectionWrapper[] = [];
|
|
|
|
createNewConnection(
|
|
nick: string,
|
|
channels: string[],
|
|
host: string,
|
|
ssl = false,
|
|
port = 6667,
|
|
password?: string,
|
|
): IRCConnectionWrapper {
|
|
// TODO: validate inputs
|
|
|
|
const connection = new IRCConnection(
|
|
{
|
|
nick,
|
|
channels,
|
|
host,
|
|
ssl,
|
|
port,
|
|
password,
|
|
},
|
|
IRCWebSocketConnector,
|
|
);
|
|
const nicklist = new IRCNickList(connection);
|
|
const instance = {
|
|
name: `${nick}-${host}-${connectionIndex++}`,
|
|
connection,
|
|
nicklist,
|
|
};
|
|
|
|
this.connections.push(instance);
|
|
this.emit('connectionAdded', instance);
|
|
connection.connect();
|
|
return instance;
|
|
}
|
|
}
|