[feature] new commands

This commit is contained in:
方而静 2021-03-25 14:31:54 +08:00
parent 3a870cb70d
commit f77d4db6dc
No known key found for this signature in database
GPG Key ID: C4F2853BC3103681
3 changed files with 83 additions and 28 deletions

View File

@ -100,12 +100,7 @@ function init() {
ws.emit('set-name', username); ws.emit('set-name', username);
}); });
ws.on('system-message', evt => { ws.on('system-message', msg => {
let msg;
if (evt.type === 'join-room') {
msg = `${evt.data.user} joined room ${evt.data.room}.`;
}
write_message({ write_message({
type: 'system-message', type: 'system-message',
msg: msg, msg: msg,

View File

@ -6,10 +6,6 @@ export function is_command(cmd: string) {
return cmd.startsWith('/'); return cmd.startsWith('/');
}; };
function command_reply(msg: string, socket: Socket) {
socket.emit('command-block-reply', msg);
}
function mask_room_name(name: string) { function mask_room_name(name: string) {
if (name === 'global') { if (name === 'global') {
return name; return name;
@ -29,7 +25,9 @@ function unmask_room_name(name: string) {
return null; return null;
} }
export function run_command(cmd: string, uid: string, users: Map<string, User>, io: SocketServer, silent: boolean = false) { export function run_command(cmd_raw: string, uid: string, users: Map<string, User>, io: SocketServer, silent: boolean = false) {
const cmd = cmd_raw.replace(/(^\s*)|(\s*$)/, '');
if (!is_command(cmd)) { return; } if (!is_command(cmd)) { return; }
const user = users.get(uid); const user = users.get(uid);
@ -39,6 +37,28 @@ export function run_command(cmd: string, uid: string, users: Map<string, User>,
socket.emit('command-block-reply', msg); socket.emit('command-block-reply', msg);
}; };
const system_reply = (msg: string, socket: Socket) => {
socket.emit('system-message', msg);
};
const request_administrator_access = () => {
if (user.is_administrator) { return true; }
command_reply('You must be an administrator to run this command.');
return false;
};
const safe_find_user = (filter_string: string) => {
let res: string[];
try {
res = find_user(filter_string, users, uid);
} catch (err) {
res = [];
}
return res;
};
const cmd_set = cmd.split(/\s+/); const cmd_set = cmd.split(/\s+/);
if (cmd.startsWith('/disconnect')) { if (cmd.startsWith('/disconnect')) {
@ -57,17 +77,25 @@ export function run_command(cmd: string, uid: string, users: Map<string, User>,
} }
if (cmd.startsWith('/ls')) { if (cmd.startsWith('/ls')) {
if (cmd_set[1] === 'own') { if (cmd === '/ls') {
command_reply(JSON.stringify( command_reply(JSON.stringify(all_rooms()));
Array
.from(socket.rooms)
.map(x => unmask_room_name(x))
.filter(x => x !== null)
));
return; return;
} }
command_reply(JSON.stringify(all_rooms())); const filter_string = cmd.substring('/ls'.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; return;
} }
@ -94,19 +122,28 @@ export function run_command(cmd: string, uid: string, users: Map<string, User>,
} }
if (cmd.startsWith('/ps')) { if (cmd.startsWith('/ps')) {
let result = '['; if (cmd === '/ps') {
users.forEach(user => { command_reply(JSON.stringify(Array
result += `"${user.name}",`; .from(users)
}); .map(x => x[1].name)
result = result.substring(0, result.length - 1) + ']'; ));
command_reply(result); return;
}
const filter_string = cmd.substring('/ps'.length);
const checked_user = safe_find_user(filter_string);
command_reply(JSON.stringify(Array
.from(users)
.filter(([id,]) => checked_user.includes(id))
.map(x => x[1].name)
));
return; return;
} }
if (cmd.startsWith('/su')) { if (cmd.startsWith('/su')) {
const code = cmd_set[1]; const code = cmd_set[1];
grant_access(user, code); if (grant_access(user, code)) { command_reply('You are administartor now.'); }
command_reply('You are administartor now.'); else { command_reply('Code incorrect.'); }
return; return;
} }
@ -118,7 +155,27 @@ export function run_command(cmd: string, uid: string, users: Map<string, User>,
if (cmd.startsWith('/filter')) { if (cmd.startsWith('/filter')) {
const filter_string = cmd.substring('/filter'.length); const filter_string = cmd.substring('/filter'.length);
command_reply(JSON.stringify(find_user(filter_string, users, uid))); command_reply(JSON.stringify(safe_find_user(filter_string)));
return; return;
} }
if (cmd.startsWith('/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);
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));
}
}; };

View File

@ -114,6 +114,7 @@ export function find_user(filter_string_raw: string, users: Map<string, User>, o
if (/^\@R\([^\&]*\)\&\(.*\)$/.test(filter_string)) { if (/^\@R\([^\&]*\)\&\(.*\)$/.test(filter_string)) {
const [, regex_filter, sub_filter] = filter_string.match(/^\@R\(([^\&]*)\)\&\((.*)\)$/); const [, regex_filter, sub_filter] = filter_string.match(/^\@R\(([^\&]*)\)\&\((.*)\)$/);
const target_regex = new RegExp(regex_filter); const target_regex = new RegExp(regex_filter);
const sub_res = parse_lower(sub_filter); const sub_res = parse_lower(sub_filter);
return sub_res return sub_res
.map(id => [users.get(id).name, id]) .map(id => [users.get(id).name, id])
@ -157,6 +158,8 @@ refreash_admin_passcode();
export function grant_access(user: User, code: string) { export function grant_access(user: User, code: string) {
if (admin_passcode === code) { if (admin_passcode === code) {
user.is_administrator = true; user.is_administrator = true;
return true;
} }
return false;
}; };