From 26852d825b1ef39cf57cdda79c86c4adb9276335 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Tue, 16 Jul 2019 00:26:39 +0200 Subject: [PATCH 1/2] Refactor: Abstracted uppercase tokenized command --- src/server.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/server.js b/src/server.js index c160085..ed1e62d 100644 --- a/src/server.js +++ b/src/server.js @@ -28,7 +28,8 @@ server.create = function create() { return net.createServer((socket) => { socket.on('data', function (data) { let tokenized = data.toString("ascii").split("\r\n")[0].split(" ") - switch (tokenized[0].toUpperCase()) { + let command = tokenized[0].toUpperCase() + switch (command) { case "PING": if (tokenized[1] && tokenized[1] === "irc.example.com") { socket.write("PONG irc.example.com\r\n") @@ -62,7 +63,7 @@ server.create = function create() { break; default: - console.error(`Unknown command: ${tokenized[0].toUpperCase()}`); + console.error(`Unknown command: ${command}`); } }).on('error', (err) => { console.error(err); From 1662775da7a38268f32426655d03f41bac693e0a Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Tue, 16 Jul 2019 16:32:40 +0200 Subject: [PATCH 2/2] Refactor nick implementation to use a global userlist --- src/server.js | 33 +++++++-------------------------- src/user.js | 12 ++++++++++++ src/userlist.js | 3 +++ 3 files changed, 22 insertions(+), 26 deletions(-) create mode 100644 src/user.js create mode 100644 src/userlist.js diff --git a/src/server.js b/src/server.js index ed1e62d..71174f3 100644 --- a/src/server.js +++ b/src/server.js @@ -1,4 +1,5 @@ const net = require("net") +const User = require("../src/user.js") const RPL_WELCOME = '001' const ERR_NOSUCHNICK = '401' @@ -9,23 +10,12 @@ const ERR_ERRONEUSNICKNAME = '432' const ERR_NICKNAMEINUSE = '433' const ERR_NEEDMOREPARAMS = '461' - let server = {} -let userlist = { - 0: - { - "nick": "chris", - 1: "some value" - }, - 1: - { - "nick": "wayne", - 1: "some_other_value" - } -} +let registeredUserlist = require("../src/userlist.js") server.create = function create() { return net.createServer((socket) => { + const user = new User(socket) socket.on('data', function (data) { let tokenized = data.toString("ascii").split("\r\n")[0].split(" ") let command = tokenized[0].toUpperCase() @@ -38,19 +28,10 @@ server.create = function create() { case "NICK": if (tokenized[1]) { let nickname = tokenized[1] - let nickNameCollision = 0 - for (let clientID in userlist) { - console.log(clientID) - if (userlist[clientID]["nick"] === nickname) { - nickNameCollision = 1 - delete userlist[clientID] - } - } - if (!nickNameCollision) { - let index = Object.keys(userlist).length - userlist[index] = { - "nick": nickname - } + // nick collision test + if (!Object.keys(registeredUserlist).includes(nickname)) { + user.setNickname(nickname) + registeredUserlist[nickname] = user socket.write(RPL_WELCOME, " nick " + nickname + " succesfully added.") } else { socket.write(ERR_NICKNAMEINUSE, " nickname in use") diff --git a/src/user.js b/src/user.js new file mode 100644 index 0000000..7000784 --- /dev/null +++ b/src/user.js @@ -0,0 +1,12 @@ +const userlist = require("../src/userlist.js") + +function User(socket) { + this.nickname = "" + this.connection = socket + + this.setNickname = function(nickname) { + this.nickname = nickname + } +} + +module.exports = User diff --git a/src/userlist.js b/src/userlist.js new file mode 100644 index 0000000..88cea11 --- /dev/null +++ b/src/userlist.js @@ -0,0 +1,3 @@ +let userlist = {} + +module.exports = userlist