Browse Source

Implement non-direct messages to users

In order to send channel messages, we need to set a different target
than the user himself. This patch adds a new parameter `to` to the
`sendMsg()` function of the user object and allows to provide a string
as target.

This uses by default the user's nickname as target, which makes the
parameter optional and doesn't break the existing interface.
feature/addNonDirectMsg
Sheogorath 5 years ago
parent
commit
ba6a424bfe
No known key found for this signature in database GPG Key ID: 1F05CC3635CDDFFD
  1. 4
      src/user.js
  2. 15
      test/user.js

4
src/user.js

@ -38,8 +38,8 @@ function User(socket, authenticatedDefault) {
} }
this.sendMsg = function (from, message) {
this.sendRaw(`:${from.nickname} PRIVMSG ${this.nickname} :${message}`, "ascii")
this.sendMsg = function (from, message, to = this.nickname) {
this.sendRaw(`:${from.nickname} PRIVMSG ${to} :${message}`, "ascii")
} }
this.sendRaw = function(message) { this.sendRaw = function(message) {
this.connection.write(`${message}\r\n`, "ascii") this.connection.write(`${message}\r\n`, "ascii")

15
test/user.js

@ -16,8 +16,8 @@ describe('User', function () {
}) })
}) })
describe('#sendMsg(from, message)', function() {
it("should send a message to the user's socket", function (done) {
describe('#sendMsg(from, message, to)', function() {
it("should send a message to the user's socket with the user himself as target", function (done) {
let mockedSock = {write: function (data) { let mockedSock = {write: function (data) {
assert.equal(data, ":some_nick PRIVMSG some_nick :test message\r\n") assert.equal(data, ":some_nick PRIVMSG some_nick :test message\r\n")
done() done()
@ -27,6 +27,17 @@ describe('User', function () {
user.register("some_nick") user.register("some_nick")
user.sendMsg(user, "test message") user.sendMsg(user, "test message")
}) })
it("should send a message to the user's socket with a different target", function (done) {
let mockedSock = {write: function (data) {
assert.equal(data, ":some_nick PRIVMSG #testchan :test message\r\n")
done()
}}
const user = new User(mockedSock, true)
user.setNickname("some_nick")
user.register("some_nick")
user.sendMsg(user, "test message", "#testchan")
})
}) })
describe('#sendRaw(message)', function() { describe('#sendRaw(message)', function() {

Loading…
Cancel
Save