3 Commits

Author SHA1 Message Date
Jenkins 79f32062c1 Merge commit 'b0d5e41bd9f3675ef8442c0d0e99f92639110d74' into HEAD 5 years ago
Sheogorath b0d5e41bd9
Add ability to send messages to channel 5 years ago
Sheogorath 0251dda8ad
Add sendMsg function to channel class 5 years ago
  1. 7
      src/channel.js
  2. 17
      src/server.js
  3. 17
      test/channel.js
  4. 24
      test/command_privmsg.js

7
src/channel.js

@ -20,6 +20,13 @@ function Channel(name) {
this.userlist.splice(index, 1)
}
}
this.sendMsg = function(from, message) {
const channel = this
this.userlist.forEach(function(item) {
item.sendMsg(from, message, channel.name)
})
}
}
module.exports = Channel

17
src/server.js

@ -74,15 +74,22 @@ server.create = function create(config = {}) {
socket.write(`:irc.example.com 001 ${user.nickname} :Welcome to the example IRC Project ${user.nickname}!~${user.username}@${address}\r\n`, "ascii")
break;
case "PRIVMSG":
let target = userlist[tokenized[1]]
let target
let message = tokenized[2]
if (target.registered === false) {
socket.write(`:irc.example.com ${ERR_NOSUCHNICK} ${target} no such nick/channel`)
if (typeof tokenized[1] === 'string' && tokenized[1].charAt(0) === "#") {
target = channellist[tokenized[1]]
} else {
let targetUser = userlist[tokenized[1]]
if (targetUser && targetUser.registered) {
target = targetUser
}
}
if (target) {
target.sendMsg(user, message)
} else {
user.sendRaw(`:irc.example.com ${ERR_NOSUCHNICK} ${target} no such nick/channel`)
}
break;
case "JOIN":
let channelname = tokenized[1]

17
test/channel.js

@ -38,4 +38,21 @@ describe('Channel', function () {
channel.part(mockedUser)
})
})
describe('#sendMsg(from, message)', function () {
it("should remove a user from a channel and send a PART command to the user", function (done) {
const channel = new Channel("#testchan")
let mockedUser = {nickname: "some_nick", sendRaw: function() {}}
mockedUser.sendMsg = function(from, message, to) {
assert.equal(from, mockedUser)
assert.equal(message, "test message")
assert.equal(to, "#testchan")
done()
}
// we can't part a channel without joining it first
channel.join(mockedUser)
channel.sendMsg(mockedUser, "test message")
})
})
})

24
test/command_privmsg.js

@ -101,4 +101,28 @@ describe("PRIVMSG OK", function () {
mockedSock1.emit('data', Buffer.from("PRIVMSG other_nick :I'm a message\r\n", "ascii"))
})
it("should allow to send message to channel -> PRIVMSG #testchan :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 #testchan :I'm a message\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"))
mockedSock.emit('data', Buffer.from("PRIVMSG #testchan :I'm a message\r\n", "ascii"))
})
})
Loading…
Cancel
Save