1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/utility/_LegacyFunctions.js

139 lines
3.5 KiB
JavaScript
Raw Permalink Normal View History

/* eslint no-param-reassign: 0 */
2022-06-23 00:32:51 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
* @summary Support functions for old clients
* @version 1.0.0
* @description Functions to bridge the older v1 clients with the latest protocol
* @module LegacyFunctions
*/
import {
isAdmin,
isModerator,
2022-06-23 00:32:51 +08:00
} from './_UAC.js';
/**
* Marks the socket as using the legacy protocol and
* applies the missing `pass` property to the payload
* @param {MainServer} server Main server reference
* @param {WebSocket} socket Target client socket
* @param {object} payload The original `join` payload
* @returns {object}
*/
export function upgradeLegacyJoin(server, socket, payload) {
const newPayload = payload;
// `join` is the legacy entry point, so apply protocol version
socket.hcProtocol = 1;
2020-10-15 12:43:38 +08:00
// these would have been applied in the `session` module, apply it now
socket.hash = server.getSocketHash(socket);
2020-10-15 12:43:38 +08:00
socket.isBot = false;
2020-11-07 05:16:43 +08:00
socket.color = false;
// pull the password from the nick
const nickArray = payload.nick.split('#', 2);
newPayload.nick = nickArray[0].trim();
if (nickArray[1] && typeof payload.pass === 'undefined') {
newPayload.pass = nickArray[1]; // eslint-disable-line prefer-destructuring
}
// dunno how this happened on the legacy version
if (typeof payload.password !== 'undefined') {
newPayload.pass = payload.password;
}
// apply the missing `userid` prop
if (typeof socket.userid === 'undefined') {
socket.userid = Math.floor(Math.random() * 9999999999999);
}
return newPayload;
}
/**
* Return the correct `uType` label for the specific level
* @param {number} level Numeric level to find the label for
*/
export function legacyLevelToLabel(level) {
if (isAdmin(level)) return 'admin';
if (isModerator(level)) return 'mod';
return 'user';
}
/**
* Alter the outgoing payload to an `info` cmd and add/change missing props
2020-09-22 13:41:45 +08:00
* @param {object} payload Original payload
* @param {string} nick Sender nick
* @return {object}
*/
2020-11-10 03:55:54 +08:00
export function legacyInviteOut(payload, nick) {
return {
...payload,
...{
cmd: 'info',
type: 'invite',
from: nick,
text: `${nick} invited you to ?${payload.inviteChannel}`,
2020-10-10 13:34:59 +08:00
channel: payload.channel, // @todo Multichannel
},
};
}
/**
* Alter the outgoing payload to an `info` cmd and add/change missing props
2020-09-22 13:41:45 +08:00
* @param {object} payload Original payload
* @param {string} nick Receiver nick
* @return {object}
*/
export function legacyInviteReply(payload, nick) {
return {
...payload,
...{
cmd: 'info',
type: 'invite',
from: '',
text: `You invited ${nick} to ?${payload.inviteChannel}`,
2020-10-10 13:34:59 +08:00
channel: payload.channel, // @todo Multichannel
},
};
}
2020-10-15 12:17:11 +08:00
/**
* 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}
*/
2020-11-10 03:55:54 +08:00
export function legacyWhisperOut(payload, from) {
2020-10-15 12:17:11 +08:00
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}`,
},
};
}