From 308130e2b0509dd7105ce84eac5e824c9958bdda Mon Sep 17 00:00:00 2001 From: marzavec Date: Wed, 14 Oct 2020 23:17:11 -0500 Subject: [PATCH] Updated Whisper --- server/src/commands/core/invite.js | 2 +- server/src/commands/core/whisper.js | 69 +++++++++++-------- .../src/commands/utility/_LegacyFunctions.js | 38 +++++++++- 3 files changed, 78 insertions(+), 31 deletions(-) diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js index 9fab2ed..092a30f 100644 --- a/server/src/commands/core/invite.js +++ b/server/src/commands/core/invite.js @@ -70,7 +70,7 @@ export async function run({ // build invite const outgoingPayload = { cmd: 'invite', - channel: socket.channel, + channel: socket.channel, // @todo Multichannel from: socket.userid, to: targetUser.userid, inviteChannel: channel, diff --git a/server/src/commands/core/whisper.js b/server/src/commands/core/whisper.js index 6105ede..a2d85dd 100644 --- a/server/src/commands/core/whisper.js +++ b/server/src/commands/core/whisper.js @@ -4,7 +4,16 @@ and accept a `userid` rather than `nick` */ -import * as UAC from '../utility/UAC/_info'; +import { + findUser, +} from '../utility/_Channels'; +import { + Errors, +} from '../utility/_Constants'; +import { + legacyWhisperOut, + legacyWhisperReply, +} from '../utility/_LegacyFunctions'; // module support functions @@ -26,7 +35,12 @@ const parseText = (text) => { // module main export async function run({ server, socket, payload }) { - // check user input + // if this is a legacy client add missing params to payload + if (socket.hcProtocol === 1) { + payload.channel = socket.channel; // eslint-disable-line no-param-reassign + } + + // verify user input const text = parseText(payload.text); if (!text) { @@ -44,41 +58,37 @@ export async function run({ server, socket, payload }) { }, socket); } - const targetNick = payload.nick; - if (!UAC.verifyNickname(targetNick)) { - return true; - } - - // find target user - let targetClient = server.findSockets({ channel: socket.channel, nick: targetNick }); - - if (targetClient.length === 0) { + const targetUser = findUser(server, payload); + if (!targetUser) { return server.reply({ - cmd: 'warn', // @todo Add numeric error code as `id` - text: 'Could not find user in channel', + cmd: 'warn', + text: 'Could not find user in that channel', + id: Errors.Global.UNKNOWN_USER, channel: socket.channel, // @todo Multichannel }, socket); } - [targetClient] = targetClient; - - server.reply({ - cmd: 'info', - type: 'whisper', - from: socket.nick, - trip: socket.trip || 'null', - text: `${socket.nick} whispered: ${text}`, + const outgoingPayload = { + cmd: 'whisper', channel: socket.channel, // @todo Multichannel - }, targetClient); + from: socket.userid, + to: targetUser.userid, + text, + }; - targetClient.whisperReply = socket.nick; + // send invite notice to target client + if (targetUser.hcProtocol === 1) { + server.reply(legacyWhisperOut(outgoingPayload, socket), targetUser); + } else { + server.reply(outgoingPayload, targetUser); + } - server.reply({ - cmd: 'info', - type: 'whisper', - text: `You whispered to @${targetNick}: ${text}`, - channel: socket.channel, // @todo Multichannel - }, socket); + // send invite notice to this client + if (socket.hcProtocol === 1) { + server.reply(legacyWhisperReply(outgoingPayload, targetUser.nick), socket); + } else { + server.reply(outgoingPayload, socket); + } return true; } @@ -120,6 +130,7 @@ export function whisperCheck({ socket, payload: { cmd: 'whisper', + channel: socket.channel, // @todo Multichannel nick: target, text: whisperText, }, diff --git a/server/src/commands/utility/_LegacyFunctions.js b/server/src/commands/utility/_LegacyFunctions.js index 4553a48..5568da8 100644 --- a/server/src/commands/utility/_LegacyFunctions.js +++ b/server/src/commands/utility/_LegacyFunctions.js @@ -59,7 +59,7 @@ export function legacyLevelToLabel(level) { * @param {string} nick Sender nick * @return {object} */ -export function legacyInviteOut(payload, nick) { + export function legacyInviteOut(payload, nick) { return { ...payload, ...{ @@ -90,3 +90,39 @@ export function legacyInviteReply(payload, nick) { }, }; } + +/** + * Alter the outgoing payload to a `whisper` cmd and add/change missing props + * @param {object} payload Original payload + * @param {string} nick Sender nick + * @return {object} + */ + export function legacyWhisperOut(payload, from) { + return { + ...payload, + ...{ + cmd: 'info', + type: 'whisper', + from: from.nick, + trip: from.trip || 'null', + text: `${from.nick} whispered: ${payload.text}`, + }, + }; +} + +/** + * Alter the outgoing payload to a `whisper` cmd and add/change missing props + * @param {object} payload Original payload + * @param {string} nick Receiver nick + * @return {object} + */ +export function legacyWhisperReply(payload, nick) { + return { + ...payload, + ...{ + cmd: 'info', + type: 'whisper', + text: `You whispered to @${nick}: ${payload.text}`, + }, + }; +}