|
@ -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,26 +10,16 @@ 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(" ") |
|
|
switch (tokenized[0].toUpperCase()) { |
|
|
|
|
|
|
|
|
let command = tokenized[0].toUpperCase() |
|
|
|
|
|
switch (command) { |
|
|
case "PING": |
|
|
case "PING": |
|
|
if (tokenized[1] && tokenized[1] === "irc.example.com") { |
|
|
if (tokenized[1] && tokenized[1] === "irc.example.com") { |
|
|
socket.write("PONG irc.example.com\r\n") |
|
|
socket.write("PONG irc.example.com\r\n") |
|
@ -37,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") |
|
@ -62,7 +44,7 @@ server.create = function create() { |
|
|
|
|
|
|
|
|
break; |
|
|
break; |
|
|
default: |
|
|
default: |
|
|
console.error(`Unknown command: ${tokenized[0].toUpperCase()}`); |
|
|
|
|
|
|
|
|
console.error(`Unknown command: ${command}`); |
|
|
} |
|
|
} |
|
|
}).on('error', (err) => { |
|
|
}).on('error', (err) => { |
|
|
console.error(err); |
|
|
console.error(err); |
|
|