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

96 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-04-29 13:29:38 +08:00
/*
Description: Generates a semi-unique channel name then broadcasts it to each client
*/
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
exports.run = async (core, server, socket, data) => {
if (server._police.frisk(socket.remoteAddress, 6)) {
server.reply({
cmd: 'warn',
text: 'You are changing nicknames too fast. Wait a moment before trying again.'
}, socket);
return;
}
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)) {
server.reply({
cmd: 'warn',
text: 'Nickname must consist of up to 24 letters, numbers, and underscores'
}, socket);
return;
}
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()) {
server._police.frisk(socket.remoteAddress, 4);
server.reply({
cmd: 'warn',
text: 'Gtfo'
}, socket);
return;
}
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
server.reply({
cmd: 'warn',
text: 'Nickname taken'
}, socket);
return;
}
2018-06-04 15:07:24 +08:00
// build join and leave notices
2018-04-29 13:29:38 +08:00
let leaveNotice = {
cmd: 'onlineRemove',
nick: socket.nick
};
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;
};
exports.requiredData = ['nick'];
2018-05-13 18:33:22 +08:00
exports.info = {
name: 'changenick',
usage: 'changenick {nick}',
description: 'This will change your current connections nickname'
};