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/core/changenick.js

132 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-04-29 13:29:38 +08:00
/*
Description: Allows calling client to change their current nickname
2018-04-29 13:29:38 +08:00
*/
// module support functions
2018-06-04 15:07:24 +08:00
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
2018-04-29 13:29:38 +08:00
// module main
2018-04-29 13:29:38 +08:00
exports.run = async (core, server, socket, data) => {
2019-04-08 08:04:10 +08:00
if (server.police.frisk(socket.remoteAddress, 6)) {
return server.reply({
2018-04-29 13:29:38 +08:00
cmd: 'warn',
text: 'You are changing nicknames too fast. Wait a moment before trying again.'
}, socket);
}
2018-06-04 15:07:24 +08:00
// verify user data is string
2018-04-29 13:29:38 +08:00
if (typeof data.nick !== 'string') {
return;
}
2018-06-04 15:07:24 +08:00
// make sure requested nickname meets standards
2018-04-29 13:29:38 +08:00
let newNick = data.nick.trim();
if (!verifyNickname(newNick)) {
return server.reply({
2018-04-29 13:29:38 +08:00
cmd: 'warn',
text: 'Nickname must consist of up to 24 letters, numbers, and underscores'
}, socket);
}
2018-06-04 15:07:24 +08:00
// prevent admin impersonation
// TODO: prevent mod impersonation
2018-04-29 13:29:38 +08:00
if (newNick.toLowerCase() == core.config.adminName.toLowerCase()) {
2019-04-08 08:04:10 +08:00
server.police.frisk(socket.remoteAddress, 4);
2018-04-29 13:29:38 +08:00
return server.reply({
2018-04-29 13:29:38 +08:00
cmd: 'warn',
text: 'You are not the admin, liar!'
2018-04-29 13:29:38 +08:00
}, socket);
}
2018-06-04 15:07:24 +08:00
// find any sockets that have the same nickname
2018-04-29 13:29:38 +08:00
let userExists = server.findSockets({
channel: socket.channel,
2018-04-29 13:29:38 +08:00
nick: (targetNick) => targetNick.toLowerCase() === newNick.toLowerCase()
});
2018-06-04 15:07:24 +08:00
// return error if found
2018-04-29 13:29:38 +08:00
if (userExists.length > 0) {
// That nickname is already in that channel
return server.reply({
2018-04-29 13:29:38 +08:00
cmd: 'warn',
text: 'Nickname taken'
}, socket);
}
2018-06-04 15:07:24 +08:00
// build join and leave notices
// TODO: this is a legacy client holdover, name changes in the future will
// have thieir own event
2018-04-29 13:29:38 +08:00
let leaveNotice = {
cmd: 'onlineRemove',
nick: socket.nick
};
2018-04-29 13:29:38 +08:00
let joinNotice = {
cmd: 'onlineAdd',
nick: newNick,
trip: socket.trip || 'null',
2018-06-04 15:07:24 +08:00
hash: socket.hash
2018-04-29 13:29:38 +08:00
};
2018-06-04 15:07:24 +08:00
// broadcast remove event and join event with new name, this is to support legacy clients and bots
2018-04-29 13:29:38 +08:00
server.broadcast( leaveNotice, { channel: socket.channel });
server.broadcast( joinNotice, { channel: socket.channel });
2018-06-04 15:07:24 +08:00
// notify channel that the user has changed their name
2018-04-29 13:29:38 +08:00
server.broadcast( {
cmd: 'info',
text: `${socket.nick} is now ${newNick}`
}, { channel: socket.channel });
2018-06-04 15:07:24 +08:00
// commit change to nickname
2018-04-29 13:29:38 +08:00
socket.nick = newNick;
};
// module hook functions
exports.initHooks = (server) => {
server.registerHook('in', 'chat', this.nickCheck, 29);
};
// hooks chat commands checking for /nick
exports.nickCheck = (core, server, socket, payload) => {
if (typeof payload.text !== 'string') {
return false;
}
if (payload.text.startsWith('/nick')) {
let input = payload.text.split(' ');
// If there is no nickname target parameter
if (input[1] === undefined) {
server.reply({
cmd: 'warn',
text: 'Refer to `/help nick` for instructions on how to use this command.'
}, socket);
return false;
}
let newNick = input[1].replace(/@/g, '');
this.run(core, server, socket, {
cmd: 'changenick',
nick: newNick
});
return false;
}
return payload;
};
// module meta
2018-04-29 13:29:38 +08:00
exports.requiredData = ['nick'];
2018-05-13 18:33:22 +08:00
exports.info = {
name: 'changenick',
description: 'This will change your current connections nickname',
usage: `
API: { cmd: 'changenick', nick: '<new nickname>' }
Text: /nick <new nickname>`
2018-05-13 18:33:22 +08:00
};