2018-03-10 15:47:00 +08:00
|
|
|
/*
|
2018-03-14 13:26:53 +08:00
|
|
|
Description: Rebroadcasts any `text` to all clients in a `channel`
|
2018-03-10 15:47:00 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-07 01:00:30 +08:00
|
|
|
import * as UAC from '../utility/UAC/_info';
|
2020-03-06 00:49:25 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module support functions
|
2018-03-14 13:26:53 +08:00
|
|
|
const parseText = (text) => {
|
2018-06-04 15:07:24 +08:00
|
|
|
// verifies user input is text
|
2018-03-11 14:41:17 +08:00
|
|
|
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');
|
2018-03-11 14:41:17 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
return sanitizedText;
|
2018-03-14 13:26:53 +08:00
|
|
|
};
|
2018-03-11 14:41:17 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// 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-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?
|
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)) {
|
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);
|
|
|
|
}
|
|
|
|
|
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,
|
2018-03-10 15:47:00 +08:00
|
|
|
};
|
|
|
|
|
2020-03-06 00:49:25 +08:00
|
|
|
if (UAC.isAdmin(socket.level)) {
|
2020-09-17 13:44:32 +08:00
|
|
|
outgoingPayload.admin = true;
|
2020-03-06 00:49:25 +08:00
|
|
|
} 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-30 14:44:36 +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
|
|
|
}
|
2018-09-30 14:44:36 +08:00
|
|
|
|
|
|
|
// checks for miscellaneous '/' based commands
|
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
|
|
|
|
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
|
|
|
|
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',
|
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
|
|
|
};
|