Browse Source

finished command_quit command tests

feature/command_quit
Wayne 5 years ago
parent
commit
53f75858bb
  1. 48
      src/server.js
  2. 11
      src/user.js
  3. 5
      test/command_quit.js

48
src/server.js

@ -21,15 +21,15 @@ server.create = function create() {
let splitted = data.toString("ascii").split("\r\n")[0].split(" ") let splitted = data.toString("ascii").split("\r\n")[0].split(" ")
let tokenized = [] let tokenized = []
let lastParam = -1 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]
} 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]
} else {
tokenized[i] = splitted[i]
}
} }
let command = tokenized[0].toUpperCase() let command = tokenized[0].toUpperCase()
switch (command) { switch (command) {
@ -57,13 +57,14 @@ server.create = function create() {
break; break;
case "USER": 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 "QUIT": case "QUIT":
server.closeConnection(user)
server.closeConnection(user.nickname)
break;
default: default:
console.error(`Unknown command: ${command}`); console.error(`Unknown command: ${command}`);
} }
@ -75,21 +76,14 @@ server.create = function create() {
throw err; throw err;
}); });
server.getUserlist = function() {
return userlist
}
server.getUserObjectFromNickName = function (nickname) {
user_list = server.getUserlist()
if (nickname in user_list)
return user_list[nickname]
server.getUserlist = function () {
return userlist
} }
server.closeConnection = function (user_nickname) { server.closeConnection = function (user_nickname) {
console.log(user_nickname)
userObject = server.getUserObjectFromNickName(user_nickname)
userObject.closeConnection()
delete server.getUserlist()[user_nickname]
let user = userlist[user_nickname]
user.closeConnection()
delete userlist[user_nickname];
} }
return server return server

11
src/user.js

@ -29,19 +29,10 @@ function User(socket) {
this.realname = realname this.realname = realname
} }
this.quit = function (quit_message) {
if (quit_message === undefined)
this.sendMessage("QUIT")
else this.sendMessage(("QUIT :" + quit_message))
}
this.closeConnection = function () { this.closeConnection = function () {
socket.close()
socket.destroy()
} }
this.sendMessage = function (message) {
connection.write(message)
}
} }

5
test/command_quit.js

@ -15,8 +15,11 @@ describe("QUIT OK", function () {
server.emit('connection', mockedSock) server.emit('connection', mockedSock)
mockedSock.emit('data', Buffer.from("NICK nick" + i + "\r\n", "ascii")) mockedSock.emit('data', Buffer.from("NICK nick" + i + "\r\n", "ascii"))
mockedSock.destroy = function () {
done()
}
} }
console.log(server.getUserlist())
server.getUserlist()["nick0"].connection.emit('data', Buffer.from("QUIT :Gone to have lunch\r\n", "ascii"))
}) })
}) })
Loading…
Cancel
Save