|
@ -1,4 +1,5 @@ |
|
|
const net = require("net") |
|
|
const net = require("net") |
|
|
|
|
|
const User = require("../src/user.js") |
|
|
|
|
|
|
|
|
const RPL_WELCOME = '001' |
|
|
const RPL_WELCOME = '001' |
|
|
const ERR_NOSUCHNICK = '401' |
|
|
const ERR_NOSUCHNICK = '401' |
|
@ -9,23 +10,12 @@ const ERR_ERRONEUSNICKNAME = '432' |
|
|
const ERR_NICKNAMEINUSE = '433' |
|
|
const ERR_NICKNAMEINUSE = '433' |
|
|
const ERR_NEEDMOREPARAMS = '461' |
|
|
const ERR_NEEDMOREPARAMS = '461' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let server = {} |
|
|
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() { |
|
|
server.create = function create() { |
|
|
return net.createServer((socket) => { |
|
|
return net.createServer((socket) => { |
|
|
|
|
|
const user = new User(socket) |
|
|
socket.on('data', function (data) { |
|
|
socket.on('data', function (data) { |
|
|
let tokenized = data.toString("ascii").split("\r\n")[0].split(" ") |
|
|
let tokenized = data.toString("ascii").split("\r\n")[0].split(" ") |
|
|
let command = tokenized[0].toUpperCase() |
|
|
let command = tokenized[0].toUpperCase() |
|
@ -38,19 +28,10 @@ server.create = function create() { |
|
|
case "NICK": |
|
|
case "NICK": |
|
|
if (tokenized[1]) { |
|
|
if (tokenized[1]) { |
|
|
let nickname = 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.") |
|
|
socket.write(RPL_WELCOME, " nick " + nickname + " succesfully added.") |
|
|
} else { |
|
|
} else { |
|
|
socket.write(ERR_NICKNAMEINUSE, " nickname in use") |
|
|
socket.write(ERR_NICKNAMEINUSE, " nickname in use") |
|
|