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/mod/dumb.js

148 lines
3.6 KiB
JavaScript
Raw Normal View History

2018-06-04 02:08:35 +08:00
/*
* Description: Make a user (spammer) dumb (mute)
* Author: simple
*/
import * as UAC from '../utility/UAC/_info';
// module constructor
2019-11-07 15:35:23 +08:00
export function init(core) {
if (typeof core.muzzledHashes === 'undefined') {
core.muzzledHashes = {};
}
2019-11-07 15:35:23 +08:00
}
// module main
2019-11-07 15:35:23 +08:00
export async function run(core, server, socket, data) {
2018-06-04 15:07:24 +08:00
// increase rate limit chance and ignore if not admin or mod
if (!UAC.isModerator(socket.level)) {
2019-11-07 15:35:23 +08:00
return server.police.frisk(socket.address, 10);
}
2018-06-04 15:07:24 +08:00
// check user input
if (typeof data.nick !== 'string') {
2019-11-07 15:35:23 +08:00
return true;
}
2018-06-04 02:08:35 +08:00
2018-06-04 15:07:24 +08:00
// find target user
2018-06-01 21:35:15 +08:00
let badClient = server.findSockets({ channel: socket.channel, nick: data.nick });
if (badClient.length === 0) {
return server.reply({
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: 'Could not find user in channel',
}, socket);
}
2019-11-07 15:35:23 +08:00
[badClient] = badClient;
2018-06-04 15:07:24 +08:00
// likely dont need this, muting mods and admins is fine
if (badClient.level >= socket.level) {
return server.reply({
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: 'This trick wont work on mods and admin',
}, socket);
}
2018-06-04 02:08:35 +08:00
2018-06-04 15:07:24 +08:00
// store hash in mute list
2019-11-07 15:35:23 +08:00
const record = core.muzzledHashes[badClient.hash] = {
dumb: true,
};
2018-06-04 02:08:35 +08:00
2018-06-04 15:07:24 +08:00
// store allies if needed
2019-11-07 15:35:23 +08:00
if (data.allies && Array.isArray(data.allies)) {
record.allies = data.allies;
}
2018-06-04 02:08:35 +08:00
2018-06-04 15:07:24 +08:00
// notify mods
server.broadcast({
cmd: 'info',
2019-11-07 15:35:23 +08:00
text: `${socket.nick} muzzled ${data.nick} in ${socket.channel}, userhash: ${badClient.hash}`,
}, { level: UAC.isModerator });
2019-11-07 15:35:23 +08:00
return true;
}
// 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.chatCheck.bind(this), 25);
server.registerHook('in', 'invite', this.inviteCheck.bind(this), 25);
// TODO: add whisper hook, need hook priorities todo finished first
2019-11-07 15:35:23 +08:00
}
// hook incoming chat commands, shadow-prevent chat if they are muzzled
2019-11-07 15:35:23 +08:00
export function chatCheck(core, server, socket, payload) {
if (typeof payload.text !== 'string') {
return false;
}
2019-11-07 15:35:23 +08:00
if (core.muzzledHashes[socket.hash]) {
// build fake chat payload
2019-11-07 15:35:23 +08:00
const mutedPayload = {
cmd: 'chat',
nick: socket.nick,
2019-11-07 15:35:23 +08:00
text: payload.text,
};
if (socket.trip) {
mutedPayload.trip = socket.trip;
}
// broadcast to any duplicate connections in channel
2019-11-07 15:35:23 +08:00
server.broadcast(mutedPayload, { channel: socket.channel, hash: socket.hash });
// broadcast to allies, if any
2019-11-07 15:35:23 +08:00
if (core.muzzledHashes[socket.hash].allies) {
server.broadcast(
mutedPayload,
{
channel: socket.channel,
nick: core.muzzledHashes[socket.hash].allies,
},
);
}
2019-11-07 15:35:23 +08:00
/**
* Blanket "spam" protection.
* May expose the ratelimiting lines from `chat` and use that
* @todo one day #lazydev
*/
server.police.frisk(socket.address, 9);
return false;
}
return payload;
2019-11-07 15:35:23 +08:00
}
// shadow-prevent all invites from muzzled users
2019-11-07 15:35:23 +08:00
export function inviteCheck(core, server, socket, payload) {
if (typeof payload.nick !== 'string') {
return false;
}
2019-11-07 15:35:23 +08:00
if (core.muzzledHashes[socket.hash]) {
// generate common channel
2019-11-07 15:35:23 +08:00
const channel = Math.random().toString(36).substr(2, 8);
// send fake reply
server.reply({
cmd: 'info',
2019-11-07 15:35:23 +08:00
text: `You invited ${payload.nick} to ?${channel}`,
}, socket);
return false;
}
return payload;
2019-11-07 15:35:23 +08:00
}
2019-11-07 15:35:23 +08:00
export const requiredData = ['nick'];
export const info = {
name: 'dumb',
description: 'Globally shadow mute a connection. Optional allies array will see muted messages.',
usage: `
2019-11-07 15:35:23 +08:00
API: { cmd: 'dumb', nick: '<target nick>', allies: ['<optional nick array>', ...] }`,
};
2019-11-07 15:35:23 +08:00
info.aliases = ['muzzle', 'mute'];