Browse Source

Merge commit '6ffedcd74e47cd0c71119200cb5078a41b2e2ef5' into HEAD

feature/privmsg
Jenkins 5 years ago
parent
commit
d189abba50
  1. 16
      src/server.js
  2. 6
      src/user.js
  3. 41
      test/command_pass.js

16
src/server.js

@ -13,10 +13,17 @@ const ERR_NEEDMOREPARAMS = '461'
let server = {}
server.create = function create() {
server.create = function create(config = {}) {
let password
if (config.password) {
password = config.password
}
let userlist = {}
let server = net.createServer((socket) => {
const user = new User(socket)
if (!config.password) {
user.authenticated = true
}
socket.on('data', function (data) {
data.toString("ascii").split("\r\n").forEach(function (commandline) {
if (commandline == "") {
@ -76,8 +83,11 @@ server.create = function create() {
server.closeConnection(user.nickname)
break;
case "PASS":
if (tokenized[1])
user.setPassword(tokenized[1])
if (tokenized[1] === config.password) {
user.authenticated = true
} else {
user.closeConnection()
}
break;
default:

6
src/user.js

@ -1,5 +1,6 @@
function User(socket) {
this.registered = false
this.authenticated = false
this.nickname = ""
this.connection = socket
this.realname = ""
@ -17,6 +18,9 @@ function User(socket) {
}
this.register = function(username, realname) {
if (!this.authenticated) {
this.connection.destroy()
}
this.username = username
this.realname = realname
this.registered = true
@ -33,7 +37,7 @@ function User(socket) {
}
this.closeConnection = function () {
socket.destroy()
this.connection.destroy()
}

41
test/command_pass.js

@ -4,21 +4,54 @@ const IRCServer = require("../src/server.js");
describe("PASS OK", function () {
it("should handle a PASS command -> PASS some_passwd", function (done) {
const server = IRCServer.create()
const server = IRCServer.create({
password: "itsJustATest"
})
let mockedSock = new EventEmitter()
mockedSock.address = function() {
return { port: 12346, family: 'IPv4', address: '127.0.0.1' }
}
mockedSock.write = function (data) {
return
}
mockedSock.destroy = function () {
done("Destroyed socket without answering")
}
server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("PASS some_pass\r\n", "ascii"))
mockedSock.emit('data', Buffer.from("PASS itsJustATest\r\n", "ascii"))
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"))
user = server.getUserlist()["some_nick"]
assert.equal("some_pass", user["password"])
assert.equal(user["registered"], true)
done()
})
it("should end connection on wrong password", function (done) {
const server = IRCServer.create({
password: "itsJustATest"
})
let mockedSock = new EventEmitter()
mockedSock.address = function() {
return { port: 12346, family: 'IPv4', address: '127.0.0.1' }
}
mockedSock.write = function (data) {
return
}
mockedSock.destroy = function () {
// connection has to be destroyed
done("")
}
server.emit("connection", mockedSock)
mockedSock.emit('data', Buffer.from("PASS wrongpassword\r\n", "ascii"))
})
})
})
Loading…
Cancel
Save