From 233a9aef72f969d6c146164329b957d6f6ff2f67 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 12:14:05 +0200 Subject: [PATCH] Add concept of channels and allow users to create them --- src/channel.js | 11 +++++++++++ src/server.js | 8 ++++++++ src/user.js | 3 +++ test/command_join.js | 28 ++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 src/channel.js create mode 100644 test/command_join.js diff --git a/src/channel.js b/src/channel.js new file mode 100644 index 0000000..4eab6ae --- /dev/null +++ b/src/channel.js @@ -0,0 +1,11 @@ +function Channel(name) { + this.name = name + this.userlist = [] + + this.join = function(user) { + this.userlist.push(user) + user.sendRaw(`:${user.nickname} JOIN ${this.name}`) + } +} + +module.exports = Channel diff --git a/src/server.js b/src/server.js index 988abd9..46d25fb 100644 --- a/src/server.js +++ b/src/server.js @@ -1,5 +1,6 @@ const net = require("net") const User = require("../src/user.js") +const Channel = require("../src/channel.js") const RPL_WELCOME = '001' const ERR_NOSUCHNICK = '401' @@ -20,6 +21,7 @@ server.create = function create(config = {}) { password = config.password } let userlist = {} + let channellist = {} let server = net.createServer((socket) => { const user = new User(socket) if (!config.password) { @@ -85,6 +87,12 @@ server.create = function create(config = {}) { } break; + case "JOIN": + let channelname = tokenized[1] + let channel = new Channel(channelname) + channel.join(user) + channellist[channelname] = channel + break; case "QUIT": server.closeConnection(user.nickname) break; diff --git a/src/user.js b/src/user.js index b54678e..a9b454f 100644 --- a/src/user.js +++ b/src/user.js @@ -44,6 +44,9 @@ function User(socket) { this.sendMsg = function (from, message) { this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}\r\n`, "ascii") } + this.sendRaw = function(message) { + this.connection.write(`${message}\r\n`, "ascii") + } } module.exports = User diff --git a/test/command_join.js b/test/command_join.js new file mode 100644 index 0000000..da2a65a --- /dev/null +++ b/test/command_join.js @@ -0,0 +1,28 @@ +const assert = require('assert'); +const EventEmitter = require('events'); +const IRCServer = require("../src/server.js"); + +describe("JOIN OK", function () { + it("should handle a JOIN command -> JOIN #testchan", function (done) { + const server = IRCServer.create() + let mockedSock = new EventEmitter() + mockedSock.address = function () { + return {port: 12346, family: 'IPv4', address: '127.0.0.1'} + } + mockedSock.write = function (data) { + let answer = data.toString("ascii") + if (answer.indexOf("JOIN") >= 0) { + assert.equal(answer, ":some_nick JOIN #testchan\r\n") + done() + } + } + mockedSock.destroy = function () { + done("Destroyed socket without answering") + } + + server.emit("connection", mockedSock) + mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii")) + mockedSock.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii")) + mockedSock.emit('data', Buffer.from("JOIN #testchan\r\n", "ascii")) + }) +})