Browse Source

Add ability to send messages to channel

feature/sendChannelMsg
Sheogorath 5 years ago
parent
commit
b0d5e41bd9
No known key found for this signature in database GPG Key ID: 1F05CC3635CDDFFD
  1. 17
      src/server.js
  2. 24
      test/command_privmsg.js

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]

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