Skip to content

Commit

Permalink
Add lock/unlock room
Browse files Browse the repository at this point in the history
  • Loading branch information
0x24a committed Jun 22, 2023
1 parent e509e78 commit 8017e2d
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 0 deletions.
167 changes: 167 additions & 0 deletions server/src/commands/mod/lockroom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
Description: Applies setting that only allows privileged trips or elevated users to join target channel
*/

import * as UAC from '../utility/UAC/_info';

// module constructor
export async function init(core) {
if (typeof core.locked === 'undefined') {
core.locked = [];
}

if (typeof core.config.authedtrips === 'undefined') {
core.config.authedtrips = [];
}
}

// module main
export async function run(core, server, socket) {
// increase rate limit chance and ignore if not admin or mod
if (!UAC.isModerator(socket.level)) {
return server.police.frisk(socket.address, 10);
}

if (core.locked[socket.channel]) {
return server.reply({
cmd: 'info',
text: '频道已被锁定,无需二次操作.',
}, socket);
}

// apply lock flag to channel list
core.locked[socket.channel] = true;

// inform mods
server.broadcast({
cmd: 'info',
text: `房间被锁定: ${socket.channel}`,
}, { level: UAC.isModerator });

return true;
}

// module hook functions
export function initHooks(server) {
server.registerHook('in', 'changenick', this.changeNickCheck.bind(this), 1);
server.registerHook('in', 'chat', this.chatCheck.bind(this), 1);
server.registerHook('in', 'invite', this.inviteCheck.bind(this), 1);
server.registerHook('in', 'join', this.joinCheck.bind(this), 1);
server.registerHook('in', 'move', this.moveCheck.bind(this), 1);
// TODO: add whisper hook, need hook priorities todo finished first
}

// prevent all name changes in purgatory
export function changeNickCheck(core, server, socket, payload) {
if (socket.channel === 'purgatory') {
return false;
}

return payload;
}

// hook incoming chat commands, prevent chat if they are user
export function chatCheck(core, server, socket, payload) {
// TODO: Change this uType to use level / uac
if (socket.channel === 'purgatory' && socket.uType === 'user') {
return false;
}

return payload;
}

// prevent all invites in purgatory
export function inviteCheck(core, server, socket, payload) {
if (socket.channel === 'purgatory') {
return false;
}

return payload;
}

// check if a user is attempting to join a locked channel, shunt to purgatory
export function joinCheck(core, server, socket, payload) {
// always verifiy user input
if (typeof payload.nick !== 'string' || typeof payload.channel !== 'string') {
return false;
}

// check if target channel is locked
if (typeof core.locked[payload.channel] === 'undefined' || core.locked[payload.channel] !== true) {
if (payload.channel !== 'purgatory') {
return payload;
}
}

// parse their credentials
const joinModule = core.commands.get('join');
const userInfo = joinModule.parseNickname(core, payload);

// `userInfo` will be string on join failure, continue to allow join to emmit error
if (typeof userInfo === 'string') {
return payload;
}

// TODO: Change this uType to use level / uac
// check if trip is allowed
if (userInfo.uType === 'user') {
if (userInfo.trip == null || core.config.authedtrips.indexOf(userInfo.trip) === -1) {
const origNick = userInfo.nick;
const origChannel = payload.channel;

// not allowed, shunt to purgatory
payload.channel = 'purgatory';

// lost souls have no names
if (origChannel === 'purgatory') {
// someone is pulling a Dante
payload.nick = `Dante_${Math.random().toString(36).substr(2, 8)}`;
} else {
payload.nick = `${Math.random().toString(36).substr(2, 8)}${Math.random().toString(36).substr(2, 8)}`;

server.reply({
cmd: 'info',
text: '你被阻止进入此房间,请稍后再试或等待一个管理员来运行你进入.',
}, socket);
}

server.broadcast({
cmd: 'info',
text: `${payload.nick} 原名称: ${origNick}, 识别码: ${userInfo.trip || '无'},TA想去: ?${origChannel}`,
}, { channel: 'purgatory', level: UAC.isModerator });
}
}

return payload;
}

// prevent all move commands out of purgatory or into locked room
export function moveCheck(core, server, socket, payload) {
// ignore if already in purgatory
if (socket.channel === 'purgatory') {
return false;
}

// always verifiy user input
if (typeof payload.channel !== 'string') {
return false;
}

// check if target channel is locked
if (typeof core.locked[payload.channel] !== 'undefined' && core.locked[payload.channel] === true) {
// TODO: Change this uType to use level / uac
if (socket.uType === 'user') {
return false;
}
}

return payload;
}

// module meta
export const info = {
name: 'lockroom',
description: '锁定你所在的频道',
usage: `
API: { cmd: 'lockroom' }`,
};
48 changes: 48 additions & 0 deletions server/src/commands/mod/unlockroom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Description: Removes the calling sockets channel from the privileged room list
*/

import * as UAC from '../utility/UAC/_info';

// module main
export async function run(core, server, socket, data) {
// increase rate limit chance and ignore if not admin or mod
if (!UAC.isModerator(socket.level)) {
return server.police.frisk(socket.address, 10);
}

if (typeof core.locked === 'undefined') {
core.locked = [];
}

let { channel } = socket;
if (typeof data.channel !== 'undefined') {
channel = data.channel;
}

if (!core.locked[socket.channel]) {
return server.reply({
cmd: 'info',
text: '频道未被锁定,无需二次操作.',
}, socket);
}

core.locked[channel] = false;

server.broadcast({
cmd: 'info',
text: `频道: ?${channel} 被 [${socket.trip}]${socket.nick} 解锁了`,
}, { channel, level: UAC.isModerator });

console.log(`Channel: ?${channel} unlocked by [${socket.trip}]${socket.nick} in ${socket.channel}`);

return true;
}

// module meta
export const info = {
name: 'unlockroom',
description: '解锁一个房间(如果不提供channel参数,即为当前频道)',
usage: `
API: { cmd: 'unlockroom', channel: '<目标频道>' }`,
};

1 comment on commit 8017e2d

@vercel
Copy link

@vercel vercel bot commented on 8017e2d Jun 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.