Browse Source

Merge commit '7d02c4d4a56eead8a2b6cc028c3311280b534ae2' into HEAD

feature/command_quit
Jenkins 6 years ago
parent
commit
1f4703c4cd
  1. 30
      src/server.js
  2. 32
      src/user.js
  3. 3
      src/userlist.js
  4. 24
      test/command_nick.js
  5. 24
      test/command_user.js

30
src/server.js

@ -12,13 +12,25 @@ const ERR_NICKNAMEINUSE = '433'
const ERR_NEEDMOREPARAMS = '461'
let server = {}
let registeredUserlist = require("../src/userlist.js")
server.create = function create() {
let userlist = {}
let server = net.createServer((socket) => {
const user = new User(socket)
socket.on('data', function (data) {
let tokenized = data.toString("ascii").split("\r\n")[0].split(" ")
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]
} else {
tokenized[i] = splitted[i]
}
}
let command = tokenized[0].toUpperCase()
switch (command) {
case "PING":
@ -30,10 +42,9 @@ server.create = function create() {
if (tokenized[1]) {
let nickname = tokenized[1]
// nick collision test
if (!Object.keys(registeredUserlist).includes(nickname)) {
if (!Object.keys(userlist).includes(nickname)) {
user.setNickname(nickname)
registeredUserlist[nickname] = user
socket.write(RPL_WELCOME, " nick " + nickname + " succesfully added.")
userlist[user.nickname] = user
} else {
socket.write(ERR_NICKNAMEINUSE, " nickname in use")
}
@ -44,6 +55,13 @@ 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;
default:
console.error(`Unknown command: ${command}`);
}
@ -56,7 +74,7 @@ server.create = function create() {
});
server.getUserlist = function() {
return registeredUserlist
return userlist
}
return server

32
src/user.js

@ -1,19 +1,27 @@
const userlist = require("../src/userlist.js")
function User(socket) {
this.registered = false
this.nickname = ""
this.connection = socket
this.realname = ""
this.username = ""
this.setNickname = function(nickname) {
this.nickname = nickname
}
this.nickname = ""
this.user = ""
this.realname = ""
this.getNickname = function(nickname) {
return this.nickname
}
this.connection = socket
this.register = function(username, realname) {
this.username = username
this.realname = realname
this.registered = true
}
this.setNickname = function (nickname) {
this.nickname = nickname
}
this.setRealName = function (realname) {
this.realname = realname
}
this.getAddress = function() {
return this.connection.address().address
}
}
module.exports = User

3
src/userlist.js

@ -1,3 +0,0 @@
let userlist = {}
module.exports = userlist

24
test/command_nick.js

@ -3,7 +3,7 @@ const EventEmitter = require('events');
const IRCServer = require("../src/server.js");
describe("NICK OK", function () {
it("should handle a NICK command -> NICK some_nickname", function (done) {
it("should handle a NICK command -> NICK some_nickname", function () {
const server = IRCServer.create()
let mockedSock = new EventEmitter()
mockedSock.write = function (data) {
@ -16,22 +16,32 @@ describe("NICK OK", function () {
server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
assert.deepEqual(Object.keys(server.getUserlist()), ["some_nick"]);
})
})
describe("NICK already registered", function () {
it("should handle a NICK command -> NICK some_nickname", function (done) {
const server = IRCServer.create()
let mockedSock = new EventEmitter()
mockedSock.write = function (data) {
let mockedSock1 = new EventEmitter()
mockedSock1.write = function (data) {
done("send data to wrong socket")
}
mockedSock1.destroy = function () {
done("Destroyed socket without answering")
}
let mockedSock2 = new EventEmitter()
mockedSock2.write = function (data) {
assert.equal(data.toString("ascii"), "433")
done()
}
mockedSock.destroy = function () {
mockedSock2.destroy = function () {
done("Destroyed socket without answering")
}
server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
server.emit("connection", mockedSock1)
server.emit("connection", mockedSock2)
mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
mockedSock2.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
})
})
describe("NICK no NICK given", function () {
@ -49,4 +59,4 @@ describe("NICK no NICK given", function () {
server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("NICK \r\n", "ascii"))
})
})
})

24
test/command_user.js

@ -0,0 +1,24 @@
const assert = require('assert');
const EventEmitter = require('events');
const IRCServer = require("../src/server.js");
describe("USER OK", function () {
it("should handle a USER command -> USER guest tolmoon tolsun :Ronnie Reagan", 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) {
assert.equal(data.toString("ascii"), "001 some_nick :Welcome to the example IRC Project some_nick!~guest@127.0.0.1")
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"))
})
})
Loading…
Cancel
Save