diff --git a/src/server.js b/src/server.js index afdf396..4bbb541 100644 --- a/src/server.js +++ b/src/server.js @@ -21,15 +21,15 @@ server.create = function create() { let splitted = data.toString("ascii").split("\r\n")[0].split(" ") let tokenized = [] let lastParam = -1 - for(let i = 0; i < splitted.length; i++) { - if (lastParam > 0) { - tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] - } else if (splitted[i].charAt(0) == ":" && i > 0) { - lastParam = i - tokenized[lastParam] = splitted[i].slice(1) - } else { - tokenized[i] = splitted[i] - } + for (let i = 0; i < splitted.length; i++) { + if (lastParam > 0) { + tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] + } else if (splitted[i].charAt(0) == ":" && i > 0) { + lastParam = i + tokenized[lastParam] = splitted[i].slice(1) + } else { + tokenized[i] = splitted[i] + } } let command = tokenized[0].toUpperCase() switch (command) { @@ -57,17 +57,20 @@ server.create = function create() { break; case "USER": - // ignoring servername and hostname, not useful these days - user.register(tokenized[1], tokenized[4]) - let address = user.getAddress() - socket.write(`001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}`) - break; + // ignoring servername and hostname, not useful these days + user.register(tokenized[1], tokenized[4]) + let address = user.getAddress() + socket.write(`001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}`) + break; case "PRIVMSG": - let target = userlist[tokenized[1]] - let message = tokenized[2] + let target = userlist[tokenized[1]] + let message = tokenized[2] - target.sendMsg(user, message) - break; + target.sendMsg(user, message) + break; + case "QUIT": + server.closeConnection(user.nickname) + break; default: console.error(`Unknown command: ${command}`); } @@ -79,8 +82,14 @@ server.create = function create() { throw err; }); - server.getUserlist = function() { - return userlist + server.getUserlist = function () { + return userlist + } + + server.closeConnection = function (user_nickname) { + let user = userlist[user_nickname] + user.closeConnection() + delete userlist[user_nickname]; } return server diff --git a/src/user.js b/src/user.js index 7fe1562..2eb23e0 100644 --- a/src/user.js +++ b/src/user.js @@ -22,10 +22,21 @@ function User(socket) { this.getAddress = function() { return this.connection.address().address } + this.setNickname = function (nickname) { + this.nickname = nickname + } + this.setRealName = function (realname) { + this.realname = realname + } - this.sendMsg = function(from, message) { - this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`) - } + this.closeConnection = function () { + socket.destroy() + } + + + this.sendMsg = function (from, message) { + this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`) + } } module.exports = User diff --git a/test/command_privmsg.js b/test/command_privmsg.js deleted file mode 100644 index 093ab56..0000000 --- a/test/command_privmsg.js +++ /dev/null @@ -1,28 +0,0 @@ -const assert = require('assert'); -const EventEmitter = require('events'); -const IRCServer = require("../src/server.js"); - -describe("PRIVMSG OK", function () { - it("should handle a PRIVMSG command -> PRIVMSG some_nick :I'm a message", 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("PRIVMSG") >= 0) { - assert.equal(answer, ":some_nick PRIVMSG some_nick :I'm a message") - 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("PRIVMSG some_nick :I'm a message\r\n", "ascii")) - }) -}) diff --git a/test/command_quit.js b/test/command_quit.js new file mode 100644 index 0000000..f4828a8 --- /dev/null +++ b/test/command_quit.js @@ -0,0 +1,25 @@ +const assert = require('assert'); +const EventEmitter = require('events'); +const IRCServer = require("../src/server.js"); + +describe("QUIT OK", function () { + it("should handle a QUIT command -> QUIT quitmessage", function (done) { + const server = IRCServer.create() + + for (var i = 0; i < 3; i++) { + + let mockedSock = new EventEmitter() + + mockedSock.write = function (data) { + } + + server.emit('connection', mockedSock) + mockedSock.emit('data', Buffer.from("NICK nick" + i + "\r\n", "ascii")) + mockedSock.destroy = function () { + done() + } + } + server.getUserlist()["nick0"].connection.emit('data', Buffer.from("QUIT :Gone to have lunch\r\n", "ascii")) + + }) +}) \ No newline at end of file