2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* @author Marzavec ( https://github.com/marzavec )
|
|
|
|
* @summary Send chat messages
|
|
|
|
* @version 1.0.0
|
|
|
|
* @description Broadcasts passed `text` field to the calling users channel
|
|
|
|
* @module chat
|
|
|
|
*/
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2023-12-22 15:14:03 +08:00
|
|
|
import {
|
|
|
|
parseText,
|
|
|
|
} from '../utility/_Text.js';
|
2020-11-07 05:16:43 +08:00
|
|
|
import {
|
|
|
|
isAdmin,
|
|
|
|
isModerator,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_UAC.js';
|
|
|
|
|
2023-12-22 15:14:03 +08:00
|
|
|
/**
|
|
|
|
* Maximum length of the customId property
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
export const MAX_MESSAGE_ID_LENGTH = 6;
|
2023-12-22 15:14:03 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
2023-12-22 15:14:03 +08:00
|
|
|
* The time in milliseconds before a message is considered stale, and thus no longer allowed
|
|
|
|
* to be edited.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
const ACTIVE_TIMEOUT = 5 * 60 * 1000;
|
2023-12-22 15:14:03 +08:00
|
|
|
|
2023-06-02 02:16:12 +08:00
|
|
|
/**
|
2023-12-22 15:14:03 +08:00
|
|
|
* The time in milliseconds that a check for stale messages should be performed.
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
const TIMEOUT_CHECK_INTERVAL = 30 * 1000;
|
2018-03-11 14:41:17 +08:00
|
|
|
|
2023-06-02 02:16:12 +08:00
|
|
|
/**
|
2023-12-22 15:14:03 +08:00
|
|
|
* Stores active messages that can be edited.
|
|
|
|
* @type {{ customId: string, userid: number, sent: number }[]}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
export const ACTIVE_MESSAGES = [];
|
|
|
|
|
|
|
|
/**
|
2023-12-22 15:14:03 +08:00
|
|
|
* Cleans up stale messages.
|
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
export function cleanActiveMessages() {
|
|
|
|
const now = Date.now();
|
2023-12-22 15:14:03 +08:00
|
|
|
for (let i = 0; i < ACTIVE_MESSAGES.length; i += 1) {
|
2023-06-02 02:16:12 +08:00
|
|
|
const message = ACTIVE_MESSAGES[i];
|
2023-11-23 18:07:24 +08:00
|
|
|
if (now - message.sent > ACTIVE_TIMEOUT || message.toDelete) {
|
2023-06-02 02:16:12 +08:00
|
|
|
ACTIVE_MESSAGES.splice(i, 1);
|
2023-12-22 15:14:03 +08:00
|
|
|
i -= 1;
|
2023-06-02 02:16:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 15:35:23 +08:00
|
|
|
|
2023-06-02 02:16:12 +08:00
|
|
|
// TODO: This won't get cleared on module reload.
|
|
|
|
setInterval(cleanActiveMessages, TIMEOUT_CHECK_INTERVAL);
|
2018-03-11 14:41:17 +08:00
|
|
|
|
2023-06-02 02:16:12 +08:00
|
|
|
/**
|
2023-12-22 15:14:03 +08:00
|
|
|
* Adds a message to the active messages map.
|
|
|
|
* @public
|
|
|
|
* @param {string} id
|
|
|
|
* @param {number} userid
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2023-06-02 02:16:12 +08:00
|
|
|
export function addActiveMessage(customId, userid) {
|
|
|
|
ACTIVE_MESSAGES.push({
|
|
|
|
customId,
|
|
|
|
userid,
|
|
|
|
sent: Date.now(),
|
2023-11-23 18:07:24 +08:00
|
|
|
toDelete: false,
|
2023-06-02 02:16:12 +08:00
|
|
|
});
|
|
|
|
}
|
2018-03-11 14:41:17 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Executes when invoked by a remote client
|
|
|
|
* @param {Object} env - Enviroment object with references to core, server, socket & payload
|
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({
|
|
|
|
core, server, socket, payload,
|
|
|
|
}) {
|
2018-06-04 15:07:24 +08:00
|
|
|
// check user input
|
2020-09-17 13:44:32 +08:00
|
|
|
const text = parseText(payload.text);
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2018-03-10 15:47:00 +08:00
|
|
|
if (!text) {
|
2018-03-11 14:41:17 +08:00
|
|
|
// lets not send objects or empty text, yea?
|
2023-12-27 16:26:49 +08:00
|
|
|
return server.police.frisk(socket, 13);
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// check for spam
|
2019-11-07 15:35:23 +08:00
|
|
|
const score = text.length / 83 / 4;
|
2023-12-27 16:26:49 +08:00
|
|
|
if (server.police.frisk(socket, score)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-22 13:34:30 +08:00
|
|
|
cmd: 'warn', // @todo Add numeric error code as `id`
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'You are sending too much text. Wait a moment and try again.\nPress the up arrow key to restore your last message.',
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2023-12-22 15:14:03 +08:00
|
|
|
const { customId } = payload;
|
2023-06-02 02:16:12 +08:00
|
|
|
|
|
|
|
if (typeof (customId) === 'string' && customId.length > MAX_MESSAGE_ID_LENGTH) {
|
|
|
|
// There's a limit on the custom id length.
|
2023-12-27 16:26:49 +08:00
|
|
|
return server.police.frisk(socket, 13);
|
2023-06-02 02:16:12 +08:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// build chat payload
|
2020-09-17 13:44:32 +08:00
|
|
|
const outgoingPayload = {
|
2018-03-10 15:47:00 +08:00
|
|
|
cmd: 'chat',
|
2020-09-08 12:51:47 +08:00
|
|
|
nick: socket.nick, /* @legacy */
|
2020-09-22 13:34:30 +08:00
|
|
|
uType: socket.uType, /* @legacy */
|
2020-09-08 12:51:47 +08:00
|
|
|
userid: socket.userid,
|
|
|
|
channel: socket.channel,
|
2019-11-07 15:35:23 +08:00
|
|
|
text,
|
2020-03-07 01:00:30 +08:00
|
|
|
level: socket.level,
|
2023-06-02 02:16:12 +08:00
|
|
|
customId,
|
2018-03-10 15:47:00 +08:00
|
|
|
};
|
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
if (isAdmin(socket.level)) {
|
2020-09-17 13:44:32 +08:00
|
|
|
outgoingPayload.admin = true;
|
2020-11-07 05:16:43 +08:00
|
|
|
} else if (isModerator(socket.level)) {
|
2020-09-17 13:44:32 +08:00
|
|
|
outgoingPayload.mod = true;
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (socket.trip) {
|
2020-09-17 13:44:32 +08:00
|
|
|
outgoingPayload.trip = socket.trip; /* @legacy */
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
if (socket.color) {
|
|
|
|
outgoingPayload.color = socket.color;
|
|
|
|
}
|
|
|
|
|
2023-06-02 02:16:12 +08:00
|
|
|
addActiveMessage(outgoingPayload.customId, socket.userid);
|
2023-12-22 15:14:03 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// broadcast to channel peers
|
2020-09-17 13:44:32 +08:00
|
|
|
server.broadcast(outgoingPayload, { channel: socket.channel });
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// stats are fun
|
2019-03-19 14:36:21 +08:00
|
|
|
core.stats.increment('messages-sent');
|
2019-11-07 15:35:23 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Automatically executes once after server is ready to register this modules hooks
|
|
|
|
* @param {Object} server - Reference to server enviroment object
|
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2019-11-07 15:35:23 +08:00
|
|
|
export function initHooks(server) {
|
2019-11-08 01:46:55 +08:00
|
|
|
server.registerHook('in', 'chat', this.commandCheckIn.bind(this), 20);
|
|
|
|
server.registerHook('in', 'chat', this.finalCmdCheck.bind(this), 254);
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Executes every time an incoming chat command is invoked;
|
|
|
|
* checks for miscellaneous '/' based commands
|
|
|
|
* @param {Object} env - Enviroment object with references to core, server, socket & payload
|
|
|
|
* @public
|
|
|
|
* @return {{Object|boolean|string}} Object = same/altered payload,
|
|
|
|
* false = suppress action,
|
|
|
|
* string = error
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export function commandCheckIn({ server, socket, payload }) {
|
2018-09-30 14:44:36 +08:00
|
|
|
if (typeof payload.text !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.text.startsWith('/myhash')) {
|
|
|
|
server.reply({
|
|
|
|
cmd: 'info',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `Your hash: ${socket.hash}`,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2018-09-30 14:44:36 +08:00
|
|
|
}, socket);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Executes every time an incoming chat command is invoked;
|
|
|
|
* assumes a failed chat command invocation and will reject with notice
|
|
|
|
* @param {Object} env - Enviroment object with references to core, server, socket & payload
|
|
|
|
* @public
|
|
|
|
* @return {{Object|boolean|string}} Object = same/altered payload,
|
|
|
|
* false = suppress action,
|
|
|
|
* string = error
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export function finalCmdCheck({ server, socket, payload }) {
|
2019-02-21 16:43:25 +08:00
|
|
|
if (typeof payload.text !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
if (!payload.text.startsWith('/')) {
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:43:25 +08:00
|
|
|
if (payload.text.startsWith('//')) {
|
2020-09-17 13:44:32 +08:00
|
|
|
payload.text = payload.text.substr(1); // eslint-disable-line no-param-reassign
|
2018-09-30 14:44:36 +08:00
|
|
|
|
|
|
|
return payload;
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
server.reply({
|
2020-09-22 13:34:30 +08:00
|
|
|
cmd: 'warn', // @todo Add numeric error code as `id`
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `Unknown command: ${payload.text}`,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2019-11-07 15:35:23 +08:00
|
|
|
}, socket);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* The following payload properties are required to invoke this module:
|
|
|
|
* "text"
|
|
|
|
* @public
|
|
|
|
* @typedef {Array} chat/requiredData
|
|
|
|
*/
|
2019-11-07 15:35:23 +08:00
|
|
|
export const requiredData = ['text'];
|
2022-06-23 00:32:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module meta information
|
|
|
|
* @public
|
|
|
|
* @typedef {Object} chat/info
|
|
|
|
* @property {string} name - Module command name
|
|
|
|
* @property {string} category - Module category name
|
|
|
|
* @property {string} description - Information about module
|
|
|
|
* @property {string} usage - Information about module usage
|
|
|
|
*/
|
2019-11-07 15:35:23 +08:00
|
|
|
export const info = {
|
2018-05-13 18:33:22 +08:00
|
|
|
name: 'chat',
|
2022-06-23 00:32:51 +08:00
|
|
|
category: 'core',
|
2018-09-30 14:44:36 +08:00
|
|
|
description: 'Broadcasts passed `text` field to the calling users channel',
|
|
|
|
usage: `
|
|
|
|
API: { cmd: 'chat', text: '<text to send>' }
|
|
|
|
Text: Uuuuhm. Just kind type in that little box at the bottom and hit enter.\n
|
|
|
|
Bonus super secret hidden commands:
|
2019-11-07 15:35:23 +08:00
|
|
|
/myhash`,
|
2018-06-01 03:48:18 +08:00
|
|
|
};
|