Browse Source

Add concept of channels and allow users to create them

feature/channel
Sheogorath 5 years ago
parent
commit
233a9aef72
No known key found for this signature in database GPG Key ID: 1F05CC3635CDDFFD
  1. 11
      src/channel.js
  2. 8
      src/server.js
  3. 3
      src/user.js
  4. 28
      test/command_join.js

11
src/channel.js

@ -0,0 +1,11 @@
function Channel(name) {
this.name = name
this.userlist = []
this.join = function(user) {
this.userlist.push(user)
user.sendRaw(`:${user.nickname} JOIN ${this.name}`)
}
}
module.exports = Channel

8
src/server.js

@ -1,5 +1,6 @@
const net = require("net") const net = require("net")
const User = require("../src/user.js") const User = require("../src/user.js")
const Channel = require("../src/channel.js")
const RPL_WELCOME = '001' const RPL_WELCOME = '001'
const ERR_NOSUCHNICK = '401' const ERR_NOSUCHNICK = '401'
@ -20,6 +21,7 @@ server.create = function create(config = {}) {
password = config.password password = config.password
} }
let userlist = {} let userlist = {}
let channellist = {}
let server = net.createServer((socket) => { let server = net.createServer((socket) => {
const user = new User(socket) const user = new User(socket)
if (!config.password) { if (!config.password) {
@ -85,6 +87,12 @@ server.create = function create(config = {}) {
} }
break; break;
case "JOIN":
let channelname = tokenized[1]
let channel = new Channel(channelname)
channel.join(user)
channellist[channelname] = channel
break;
case "QUIT": case "QUIT":
server.closeConnection(user.nickname) server.closeConnection(user.nickname)
break; break;

3
src/user.js

@ -44,6 +44,9 @@ function User(socket) {
this.sendMsg = function (from, message) { this.sendMsg = function (from, message) {
this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}\r\n`, "ascii") this.connection.write(`:${from.nickname} PRIVMSG ${this.nickname} :${message}\r\n`, "ascii")
} }
this.sendRaw = function(message) {
this.connection.write(`${message}\r\n`, "ascii")
}
} }
module.exports = User module.exports = User

28
test/command_join.js

@ -0,0 +1,28 @@
const assert = require('assert');
const EventEmitter = require('events');
const IRCServer = require("../src/server.js");
describe("JOIN OK", function () {
it("should handle a JOIN command -> JOIN #testchan", 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("JOIN") >= 0) {
assert.equal(answer, ":some_nick JOIN #testchan\r\n")
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("JOIN #testchan\r\n", "ascii"))
})
})
Loading…
Cancel
Save