Browse Source

Merge commit '9ea3e9bccfe6edf1a1c9fee3ef628c79f4fd163f' into HEAD

command_pass
Jenkins 6 years ago
parent
commit
4f585b68aa
  1. 13
      src/server.js
  2. 13
      src/user.js
  3. 28
      test/command_privmsg.js
  4. 25
      test/command_quit.js

13
src/server.js

@ -21,7 +21,7 @@ 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++) {
for (let i = 0; i < splitted.length; i++) {
if (lastParam > 0) { if (lastParam > 0) {
tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i]
} else if (splitted[i].charAt(0) == ":" && i > 0) { } else if (splitted[i].charAt(0) == ":" && i > 0) {
@ -68,6 +68,9 @@ server.create = function create() {
target.sendMsg(user, message) target.sendMsg(user, message)
break; break;
case "QUIT":
server.closeConnection(user.nickname)
break;
default: default:
console.error(`Unknown command: ${command}`); console.error(`Unknown command: ${command}`);
} }
@ -79,10 +82,16 @@ server.create = function create() {
throw err; throw err;
}); });
server.getUserlist = function() {
server.getUserlist = function () {
return userlist return userlist
} }
server.closeConnection = function (user_nickname) {
let user = userlist[user_nickname]
user.closeConnection()
delete userlist[user_nickname];
}
return server return server
} }

13
src/user.js

@ -22,8 +22,19 @@ function User(socket) {
this.getAddress = function() { this.getAddress = function() {
return this.connection.address().address return this.connection.address().address
} }
this.setNickname = function (nickname) {
this.nickname = nickname
}
this.setRealName = function (realname) {
this.realname = realname
}
this.closeConnection = function () {
socket.destroy()
}
this.sendMsg = function(from, message) {
this.sendMsg = function (from, message) {
this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`) this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`)
} }
} }

28
test/command_privmsg.js

@ -1,28 +0,0 @@
const assert = require('assert');
const EventEmitter = require('events');
const IRCServer = require("../src/server.js");
describe("PRIVMSG OK", function () {
it("should handle a PRIVMSG command -> PRIVMSG some_nick :I'm a message", 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) {
let answer = data.toString("ascii")
if (answer.indexOf("PRIVMSG") >= 0) {
assert.equal(answer, ":some_nick PRIVMSG some_nick :I'm a message")
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"))
mockedSock.emit('data', Buffer.from("PRIVMSG some_nick :I'm a message\r\n", "ascii"))
})
})

25
test/command_quit.js

@ -0,0 +1,25 @@
const assert = require('assert');
const EventEmitter = require('events');
const IRCServer = require("../src/server.js");
describe("QUIT OK", function () {
it("should handle a QUIT command -> QUIT quitmessage", function (done) {
const server = IRCServer.create()
for (var i = 0; i < 3; i++) {
let mockedSock = new EventEmitter()
mockedSock.write = function (data) {
}
server.emit('connection', mockedSock)
mockedSock.emit('data', Buffer.from("NICK nick" + i + "\r\n", "ascii"))
mockedSock.destroy = function () {
done()
}
}
server.getUserlist()["nick0"].connection.emit('data', Buffer.from("QUIT :Gone to have lunch\r\n", "ascii"))
})
})
Loading…
Cancel
Save