From 334bc0c89fcdc55254f7c38bf8ee1abdcb7a7ccd Mon Sep 17 00:00:00 2001 From: szdytom Date: Fri, 26 Mar 2021 14:04:53 +0800 Subject: [PATCH] [refactory] use map refactory command run --- package.json | 2 +- src/command.ts | 101 ++++++++++++++++++++++++++++--------------------- tsconfig.json | 7 ++++ 3 files changed, 65 insertions(+), 45 deletions(-) create mode 100644 tsconfig.json diff --git a/package.json b/package.json index b8e9567..55d829a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wschat-ng", - "version": "1.0.2", + "version": "1.0.3", "description": "", "main": "index.js", "scripts": { diff --git a/src/command.ts b/src/command.ts index 7a01657..b10db3c 100644 --- a/src/command.ts +++ b/src/command.ts @@ -62,45 +62,45 @@ export function run_command(cmd_raw: string, uid: string, users: Map void> = new Map(); + const admin_command_map: Map void> = new Map(); + + command_map.set('disconnect', () => { command_reply('Diconnecting. Bye!',); socket.disconnect(); - return; - } + }); - if (cmd.startsWith('/join')) { + command_map.set('join', () => { const room_name = cmd_set[1]; set_room(room_name); const masked_name = mask_room_name(room_name); socket.join(masked_name); command_reply('OK.'); - return; - } + }); - if (cmd.startsWith('/ls')) { - if (cmd === '/ls') { + command_map.set('ls', (prefix: string) => { + if (cmd === prefix) { command_reply(JSON.stringify(all_rooms())); return; } - const filter_string = cmd.substring('/ls'.length); + const filter_string = cmd.substring(prefix.length); const target = safe_find_user(filter_string)[0]; - + if (target === undefined) { command_reply('Target user not found.'); return; } - + command_reply(JSON.stringify( Array .from(users.get(target).socket.rooms) .map(x => unmask_room_name(x)) .filter(x => x !== null) )); - return; - } + }); - if (cmd.startsWith('/rename')) { + command_map.set('rename', () => { const new_name = cmd_set[1].toString(); if (validate_username(new_name)) { kill_username(user.name); @@ -110,20 +110,18 @@ export function run_command(cmd_raw: string, uid: string, users: Map { command_reply(JSON.stringify({ name: user.name, id: user.id, is_administrator: user.is_administrator, })); - return; - } + }); - if (cmd.startsWith('/ps')) { - if (cmd === '/ps') { + command_map.set('ps', (prefix: string) => { + if (cmd === prefix) { command_reply(JSON.stringify(Array .from(users) .map(x => x[1].name) @@ -131,60 +129,75 @@ export function run_command(cmd_raw: string, uid: string, users: Map checked_user.includes(id)) .map(x => x[1].name) )); - return; - } + }); - if (cmd.startsWith('/su')) { + command_map.set('su', () => { const code = cmd_set[1]; if (grant_access(user, code)) { command_reply('You are administartor now.'); } else { command_reply('Code incorrect.'); } - return; - } + }); - if (cmd.startsWith('/resign')) { + command_map.set('resign', () => { user.is_administrator = false; command_reply('OK.'); - return; - } + }); - if (cmd.startsWith('/filter')) { - const filter_string = cmd.substring('/filter'.length); + command_map.set('filter', (prefix: string) => { + const filter_string = cmd.substring(prefix.length); command_reply(JSON.stringify(safe_find_user(filter_string))); - return; - } + }); - if (cmd.startsWith('/whois')) { + command_map.set('whois', () => { const id = cmd_set[1]; command_reply(users.get(id).name); - return; - } + }); - if (cmd.startsWith('/kill')) { - if (!request_administrator_access()) { return; } - - const filter_string = cmd.substring('/kill'.length); + admin_command_map.set('kill', (prefix: string) => { + const filter_string = cmd.substring(prefix.length); const checked_user = safe_find_user(filter_string); for (let id of checked_user) { const target = users.get(id).socket; system_reply('Your are killed.', target); target.disconnect(); } - + command_reply(JSON.stringify(checked_user)); - } + }); - if (cmd.startsWith('/anon')) { + command_map.set('anon', () => { io.in(Array.from(socket.rooms)).emit('new message', { type: 'text-message', data: msg, sender: 'Anonymous User', }); + }); + + if (user.is_administrator) { + for (let val of admin_command_map) { + const [prefix, executor] = val; + const command_prefix = '/' + prefix; + if (cmd.startsWith(command_prefix)) { + executor(command_prefix); + return; + } + }; } + + for (let val of command_map) { + const [prefix, executor] = val; + const command_prefix = '/' + prefix; + if (cmd.startsWith(command_prefix)) { + executor(command_prefix); + return; + } + } + + command_reply(`Bad command: "${cmd.match(/^\/[^\s]+/)[0]}".`); }; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..68c19b5 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "downlevelIteration": true, + "removeComments": true, + "target": "ES5" + }, +} \ No newline at end of file