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

135 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-03-10 15:47:00 +08:00
/*
Description: Rebroadcasts any `text` to all clients in a `channel`
2018-03-10 15:47:00 +08:00
*/
import * as UAC from '../utility/UAC/_info';
// module support functions
const parseText = (text) => {
2018-06-04 15:07:24 +08:00
// verifies user input is text
if (typeof text !== 'string') {
return false;
}
2019-11-07 15:35:23 +08:00
let sanitizedText = text;
2018-03-10 15:47:00 +08:00
// strip newlines from beginning and end
2019-11-07 15:35:23 +08:00
sanitizedText = sanitizedText.replace(/^\s*\n|^\s+$|\n\s*$/g, '');
2018-03-10 15:47:00 +08:00
// replace 3+ newlines with just 2 newlines
2019-11-07 15:35:23 +08:00
sanitizedText = sanitizedText.replace(/\n{3,}/g, '\n\n');
2019-11-07 15:35:23 +08:00
return sanitizedText;
};
// module main
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?
2019-11-07 15:35:23 +08:00
return server.police.frisk(socket.address, 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;
if (server.police.frisk(socket.address, 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);
}
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,
2018-03-10 15:47:00 +08:00
};
if (UAC.isAdmin(socket.level)) {
2020-09-17 13:44:32 +08:00
outgoingPayload.admin = true;
} else if (UAC.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
}
// 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
// module hook functions
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
}
// checks for miscellaneous '/' based commands
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
}
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;
}
2019-11-07 15:35:23 +08:00
export const requiredData = ['text'];
export const info = {
2018-05-13 18:33:22 +08:00
name: 'chat',
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
};