diff --git a/reflection.js b/reflection.js index 8e5bf49..765d22c 100644 --- a/reflection.js +++ b/reflection.js @@ -1,6 +1,6 @@ import { readFileSync } from 'fs'; import { createServer } from 'tls'; -import { Client } from './lib/Client.js'; +import { IRCClient } from './lib/IRCClient.js'; import { Server } from './lib/Server.js'; const config = JSON.parse(readFileSync(process.argv[2])); @@ -12,7 +12,7 @@ const listener = createServer({ }); listener.on('secureConnection', (c) => { - new Client(c, ircd); + new IRCClient(c, ircd); }) listener.listen(config["port"], () => { diff --git a/src/Client.ts b/src/Client.ts index ced975f..5af1944 100644 --- a/src/Client.ts +++ b/src/Client.ts @@ -7,7 +7,7 @@ import { MatrixUser } from './MatrixUser.js'; import { IRCMessage, parseIRCMessage } from './Message.js'; import { Server } from './Server.js'; -export class Client { +export abstract class Client { capVersion: string enabledCaps: Map allCaps: Map @@ -15,7 +15,7 @@ export class Client { isRegistered: boolean apiCall: Axios batchesInProgress: Map - constructor(private socket: Socket, public server: Server) { + constructor(public server: Server) { this.capVersion = '301'; this.enabledCaps = new Map(); this.allCaps = new Map([ @@ -36,9 +36,6 @@ export class Client { this.isRegistered = false; this.apiCall = this.server.apiCall; this.batchesInProgress = new Map(); - this.server.doLog("New client connected"); - this.socket.on('data', (data) => this.receiveData(data)); - //this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)}); } checkIfRegistered() { @@ -652,12 +649,15 @@ export class Client { }) const newMsg = new IRCMessage(ourTags, prefix, command, params); const msgToSend = newMsg.toString(); - //console.log(`SENT: ${msgToSend}`); - this.socket.write(`${msgToSend}\r\n`); + this.writeMessage(`${msgToSend}\r\n`); } + abstract writeMessage(message: string): void; + closeConnectionWithError(message: string) { this.sendMessage(this.server.name, 'ERROR', [message], new Map()); - this.socket.destroy(); + this.closeConnection(); } + + abstract closeConnection(): void; } \ No newline at end of file diff --git a/src/IRCClient.ts b/src/IRCClient.ts new file mode 100644 index 0000000..001a4eb --- /dev/null +++ b/src/IRCClient.ts @@ -0,0 +1,21 @@ +import { Socket } from "net"; +import { Client } from "./Client.js"; +import { Server } from './Server.js'; + +export class IRCClient extends Client { + constructor(private socket: Socket , public server: Server) { + super(server); + this.socket.on('data', (data) => this.receiveData(data)); + //this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)}); + this.server.doLog("New IRCClient connected"); + } + + writeMessage(message: string) { + //console.log(`SENT: ${msgToSend}`); + this.socket.write(message); + } + + closeConnection() { + this.socket.destroy(); + } +} \ No newline at end of file