Browse Source

Merge commit 'f816cd1adad6c29df25c97b754bed59c30862206' into HEAD

fix/command_quit
Jenkins 5 years ago
parent
commit
5c9fabc9ae
  1. 6
      src/server.js
  2. 47
      test/command_privmsg.js

6
src/server.js

@ -11,6 +11,7 @@ const ERR_NONICKNAMEGIVEN = '431'
const ERR_NICKNAMEINUSE = '433' const ERR_NICKNAMEINUSE = '433'
const ERR_NEEDMOREPARAMS = '461' const ERR_NEEDMOREPARAMS = '461'
let server = {} let server = {}
server.create = function create(config = {}) { server.create = function create(config = {}) {
@ -76,8 +77,13 @@ server.create = function create(config = {}) {
case "PRIVMSG": case "PRIVMSG":
let target = userlist[tokenized[1]] let target = userlist[tokenized[1]]
let message = tokenized[2] let message = tokenized[2]
if (target.registered === false) {
socket.write(`:irc.example.com ${ERR_NOSUCHNICK} ${target} no such nick/channel`)
} else {
target.sendMsg(user, message) target.sendMsg(user, message)
}
break; break;
case "QUIT": case "QUIT":
server.closeConnection(user.nickname) server.closeConnection(user.nickname)

47
test/command_privmsg.js

@ -29,8 +29,8 @@ describe("PRIVMSG OK", function () {
it("should handle a PRIVMSG command for multiple users -> PRIVMSG other_nick :I'm a message", function (done) { it("should handle a PRIVMSG command for multiple users -> PRIVMSG other_nick :I'm a message", function (done) {
const server = IRCServer.create() const server = IRCServer.create()
let mockedSock1 = new EventEmitter() let mockedSock1 = new EventEmitter()
mockedSock1.address = function() {
return { port: 12346, family: 'IPv4', address: '127.0.0.1' }
mockedSock1.address = function () {
return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
} }
mockedSock1.write = function (data) { mockedSock1.write = function (data) {
return return
@ -39,8 +39,8 @@ describe("PRIVMSG OK", function () {
done("Destroyed socket without answering") done("Destroyed socket without answering")
} }
let mockedSock2 = new EventEmitter() let mockedSock2 = new EventEmitter()
mockedSock2.address = function() {
return { port: 12346, family: 'IPv4', address: '127.0.0.1' }
mockedSock2.address = function () {
return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
} }
mockedSock2.write = function (data) { mockedSock2.write = function (data) {
let answer = data.toString("ascii") let answer = data.toString("ascii")
@ -60,6 +60,45 @@ describe("PRIVMSG OK", function () {
mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii")) mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
mockedSock2.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii")) mockedSock2.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii"))
})
it("dont send message to unregistered user", function (done) {
const server = IRCServer.create()
let mockedSock1 = new EventEmitter()
mockedSock1.address = function () {
return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
}
mockedSock1.write = function (data) {
answer = data.toString('ascii')
if (answer.indexOf("401")>=0) {
assert.equal(answer, ":irc.example.com 401 some_nick no such nick/channel")
}
done()
}
mockedSock1.destroy = function () {
done("Destroyed socket without answering")
}
let mockedSock2 = new EventEmitter()
mockedSock2.address = function () {
return {port: 12346, family: 'IPv4', address: '127.0.0.1'}
}
mockedSock2.write = function (data) {
let answer = data.toString("ascii")
if (answer.indexOf("PRIVMSG") >= 0) {
assert.equal(answer, ":some_nick PRIVMSG other_nick :I'm a message\r\n")
done("I got a message that I shouldn't have seen")
}
}
mockedSock2.destroy = function () {
done("Destroyed socket without answering")
}
server.emit("connection", mockedSock1)
server.emit("connection", mockedSock2)
mockedSock1.emit('data', Buffer.from("NICK some_nick\r\n", "ascii"))
mockedSock1.emit('data', Buffer.from("USER guest tolmoon tolsun :Ronnie Reagan\r\n", "ascii"))
mockedSock2.emit('data', Buffer.from("NICK other_nick\r\n", "ascii"))
mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii")) mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii"))
}) })
}) })
Loading…
Cancel
Save