Browse Source

Feature: Add USER command

feature/user
Sheogorath 5 years ago
parent
commit
7d02c4d4a5
No known key found for this signature in database GPG Key ID: 1F05CC3635CDDFFD
  1. 21
      src/server.js
  2. 24
      src/user.js
  3. 24
      test/command_user.js

21
src/server.js

@ -18,7 +18,19 @@ server.create = function create() {
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":
@ -43,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}`);
}

24
src/user.js

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

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