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

246 lines
6.1 KiB
JavaScript
Raw Normal View History

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;
2023-06-02 02:16:12 +08:00
/**
2023-12-22 15:14:03 +08:00
* Stores active messages that can be edited.
2023-12-30 15:24:12 +08:00
* @type {Array}
2023-12-22 15:14:03 +08:00
*/
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];
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);
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(),
toDelete: false,
2023-06-02 02:16:12 +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-03-10 15:47:00 +08:00
if (!text) {
// 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)) {
return server.reply({
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 */
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,
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
// 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
}
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
2023-12-30 15:24:12 +08:00
* @return {(Object|boolean|string)} Object = same/altered payload,
2022-06-23 00:32:51 +08:00
* false = suppress action,
* string = error
*/
2020-09-17 13:44:32 +08:00
export function commandCheckIn({ server, socket, payload }) {
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
}, socket);
return false;
}
return payload;
2019-11-07 15:35:23 +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
2023-12-30 15:24:12 +08:00
* @return {(Object|boolean|string)} Object = same/altered payload,
2022-06-23 00:32:51 +08:00
* false = suppress action,
* string = error
*/
2020-09-17 13:44:32 +08:00
export function finalCmdCheck({ server, socket, payload }) {
if (typeof payload.text !== 'string') {
return false;
}
if (!payload.text.startsWith('/')) {
return payload;
}
if (payload.text.startsWith('//')) {
2020-09-17 13:44:32 +08:00
payload.text = payload.text.substr(1); // eslint-disable-line no-param-reassign
return payload;
}
2019-11-07 15:35:23 +08:00
server.reply({
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;
}
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',
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
};