From b0d5e41bd9f3675ef8442c0d0e99f92639110d74 Mon Sep 17 00:00:00 2001 From: Sheogorath Date: Wed, 17 Jul 2019 22:37:14 +0200 Subject: [PATCH] Add ability to send messages to channel --- src/server.js | 17 ++++++++++++----- test/command_privmsg.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/server.js b/src/server.js index 0d12a46..6ee62f9 100644 --- a/src/server.js +++ b/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] diff --git a/test/command_privmsg.js b/test/command_privmsg.js index 10df41e..d1d433f 100644 --- a/test/command_privmsg.js +++ b/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")) + }) })