Sheogorath
5 years ago
No known key found for this signature in database
GPG Key ID: 1F05CC3635CDDFFD
3 changed files with
39 additions and
1 deletions
-
src/server.js
-
src/user.js
-
test/command_privmsg.js
|
|
@ -26,7 +26,7 @@ server.create = function create() { |
|
|
|
tokenized[lastParam] = tokenized[lastParam] + " " + splitted[i] |
|
|
|
} else if (splitted[i].charAt(0) == ":" && i > 0) { |
|
|
|
lastParam = i |
|
|
|
tokenized[lastParam] = splitted[i] |
|
|
|
tokenized[lastParam] = splitted[i].slice(1) |
|
|
|
} else { |
|
|
|
tokenized[i] = splitted[i] |
|
|
|
} |
|
|
@ -62,6 +62,12 @@ server.create = function create() { |
|
|
|
let address = user.getAddress() |
|
|
|
socket.write(`001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}`) |
|
|
|
break; |
|
|
|
case "PRIVMSG": |
|
|
|
let target = userlist[tokenized[1]] |
|
|
|
let message = tokenized[2] |
|
|
|
|
|
|
|
target.sendMsg(user, message) |
|
|
|
break; |
|
|
|
default: |
|
|
|
console.error(`Unknown command: ${command}`); |
|
|
|
} |
|
|
|
|
|
@ -22,6 +22,10 @@ function User(socket) { |
|
|
|
this.getAddress = function() { |
|
|
|
return this.connection.address().address |
|
|
|
} |
|
|
|
|
|
|
|
this.sendMsg = function(from, message) { |
|
|
|
this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = User |
|
|
@ -0,0 +1,28 @@ |
|
|
|
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")) |
|
|
|
}) |
|
|
|
}) |