[refactory] use map refactory command run
This commit is contained in:
parent
ffa722a312
commit
334bc0c89f
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wschat-ng",
|
"name": "wschat-ng",
|
||||||
"version": "1.0.2",
|
"version": "1.0.3",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
101
src/command.ts
101
src/command.ts
@ -62,45 +62,45 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
|
|||||||
const cmd_set = cmd.split(/\s+/);
|
const cmd_set = cmd.split(/\s+/);
|
||||||
const msg = cmd.replace(/^[^\n]+\n/m, '');
|
const msg = cmd.replace(/^[^\n]+\n/m, '');
|
||||||
|
|
||||||
if (cmd.startsWith('/disconnect')) {
|
const command_map: Map<string, (prefix: string) => void> = new Map();
|
||||||
|
const admin_command_map: Map<string, (prefix: string) => void> = new Map();
|
||||||
|
|
||||||
|
command_map.set('disconnect', () => {
|
||||||
command_reply('Diconnecting. Bye!',);
|
command_reply('Diconnecting. Bye!',);
|
||||||
socket.disconnect();
|
socket.disconnect();
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/join')) {
|
command_map.set('join', () => {
|
||||||
const room_name = cmd_set[1];
|
const room_name = cmd_set[1];
|
||||||
set_room(room_name);
|
set_room(room_name);
|
||||||
const masked_name = mask_room_name(room_name);
|
const masked_name = mask_room_name(room_name);
|
||||||
socket.join(masked_name);
|
socket.join(masked_name);
|
||||||
command_reply('OK.');
|
command_reply('OK.');
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/ls')) {
|
command_map.set('ls', (prefix: string) => {
|
||||||
if (cmd === '/ls') {
|
if (cmd === prefix) {
|
||||||
command_reply(JSON.stringify(all_rooms()));
|
command_reply(JSON.stringify(all_rooms()));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filter_string = cmd.substring('/ls'.length);
|
const filter_string = cmd.substring(prefix.length);
|
||||||
const target = safe_find_user(filter_string)[0];
|
const target = safe_find_user(filter_string)[0];
|
||||||
|
|
||||||
if (target === undefined) {
|
if (target === undefined) {
|
||||||
command_reply('Target user not found.');
|
command_reply('Target user not found.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
command_reply(JSON.stringify(
|
command_reply(JSON.stringify(
|
||||||
Array
|
Array
|
||||||
.from(users.get(target).socket.rooms)
|
.from(users.get(target).socket.rooms)
|
||||||
.map(x => unmask_room_name(x))
|
.map(x => unmask_room_name(x))
|
||||||
.filter(x => x !== null)
|
.filter(x => x !== null)
|
||||||
));
|
));
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/rename')) {
|
command_map.set('rename', () => {
|
||||||
const new_name = cmd_set[1].toString();
|
const new_name = cmd_set[1].toString();
|
||||||
if (validate_username(new_name)) {
|
if (validate_username(new_name)) {
|
||||||
kill_username(user.name);
|
kill_username(user.name);
|
||||||
@ -110,20 +110,18 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
|
|||||||
} else {
|
} else {
|
||||||
command_reply('Failed to rename.');
|
command_reply('Failed to rename.');
|
||||||
}
|
}
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/whoami')) {
|
command_map.set('whoami', () => {
|
||||||
command_reply(JSON.stringify({
|
command_reply(JSON.stringify({
|
||||||
name: user.name,
|
name: user.name,
|
||||||
id: user.id,
|
id: user.id,
|
||||||
is_administrator: user.is_administrator,
|
is_administrator: user.is_administrator,
|
||||||
}));
|
}));
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/ps')) {
|
command_map.set('ps', (prefix: string) => {
|
||||||
if (cmd === '/ps') {
|
if (cmd === prefix) {
|
||||||
command_reply(JSON.stringify(Array
|
command_reply(JSON.stringify(Array
|
||||||
.from(users)
|
.from(users)
|
||||||
.map(x => x[1].name)
|
.map(x => x[1].name)
|
||||||
@ -131,60 +129,75 @@ export function run_command(cmd_raw: string, uid: string, users: Map<string, Use
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const filter_string = cmd.substring('/ps'.length);
|
const filter_string = cmd.substring(prefix.length);
|
||||||
const checked_user = safe_find_user(filter_string);
|
const checked_user = safe_find_user(filter_string);
|
||||||
command_reply(JSON.stringify(Array
|
command_reply(JSON.stringify(Array
|
||||||
.from(users)
|
.from(users)
|
||||||
.filter(([id,]) => checked_user.includes(id))
|
.filter(([id,]) => checked_user.includes(id))
|
||||||
.map(x => x[1].name)
|
.map(x => x[1].name)
|
||||||
));
|
));
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/su')) {
|
command_map.set('su', () => {
|
||||||
const code = cmd_set[1];
|
const code = cmd_set[1];
|
||||||
if (grant_access(user, code)) { command_reply('You are administartor now.'); }
|
if (grant_access(user, code)) { command_reply('You are administartor now.'); }
|
||||||
else { command_reply('Code incorrect.'); }
|
else { command_reply('Code incorrect.'); }
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/resign')) {
|
command_map.set('resign', () => {
|
||||||
user.is_administrator = false;
|
user.is_administrator = false;
|
||||||
command_reply('OK.');
|
command_reply('OK.');
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/filter')) {
|
command_map.set('filter', (prefix: string) => {
|
||||||
const filter_string = cmd.substring('/filter'.length);
|
const filter_string = cmd.substring(prefix.length);
|
||||||
command_reply(JSON.stringify(safe_find_user(filter_string)));
|
command_reply(JSON.stringify(safe_find_user(filter_string)));
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/whois')) {
|
command_map.set('whois', () => {
|
||||||
const id = cmd_set[1];
|
const id = cmd_set[1];
|
||||||
command_reply(users.get(id).name);
|
command_reply(users.get(id).name);
|
||||||
return;
|
});
|
||||||
}
|
|
||||||
|
|
||||||
if (cmd.startsWith('/kill')) {
|
admin_command_map.set('kill', (prefix: string) => {
|
||||||
if (!request_administrator_access()) { return; }
|
const filter_string = cmd.substring(prefix.length);
|
||||||
|
|
||||||
const filter_string = cmd.substring('/kill'.length);
|
|
||||||
const checked_user = safe_find_user(filter_string);
|
const checked_user = safe_find_user(filter_string);
|
||||||
for (let id of checked_user) {
|
for (let id of checked_user) {
|
||||||
const target = users.get(id).socket;
|
const target = users.get(id).socket;
|
||||||
system_reply('Your are killed.', target);
|
system_reply('Your are killed.', target);
|
||||||
target.disconnect();
|
target.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
command_reply(JSON.stringify(checked_user));
|
command_reply(JSON.stringify(checked_user));
|
||||||
}
|
});
|
||||||
|
|
||||||
if (cmd.startsWith('/anon')) {
|
command_map.set('anon', () => {
|
||||||
io.in(Array.from(socket.rooms)).emit('new message', {
|
io.in(Array.from(socket.rooms)).emit('new message', {
|
||||||
type: 'text-message',
|
type: 'text-message',
|
||||||
data: msg,
|
data: msg,
|
||||||
sender: 'Anonymous User',
|
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]}".`);
|
||||||
};
|
};
|
||||||
|
7
tsconfig.json
Normal file
7
tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"downlevelIteration": true,
|
||||||
|
"removeComments": true,
|
||||||
|
"target": "ES5"
|
||||||
|
},
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user