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
2018-05-12 14:13:12 +05:30

85 lines
1.9 KiB
JavaScript

/*
Description: Generates a semi-unique channel name then broadcasts it to each client
*/
const verifyNickname = (nick) => {
return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
};
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;
}
if (typeof data.nick !== 'string') {
return;
}
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;
}
if (newNick.toLowerCase() == core.config.adminName.toLowerCase()) {
server._police.frisk(socket.remoteAddress, 4);
server.reply({
cmd: 'warn',
text: 'Gtfo'
}, socket);
return;
}
let userExists = server.findSockets({
channel: socket.channel,
nick: (targetNick) => targetNick.toLowerCase() === newNick.toLowerCase()
});
if (userExists.length > 0) {
// That nickname is already in that channel
server.reply({
cmd: 'warn',
text: 'Nickname taken'
}, socket);
return;
}
let peerList = server.findSockets({ channel: socket.channel });
let leaveNotice = {
cmd: 'onlineRemove',
nick: socket.nick
};
let joinNotice = {
cmd: 'onlineAdd',
nick: newNick,
trip: socket.trip || 'null',
hash: server.getSocketHash(socket)
};
server.broadcast( leaveNotice, { channel: socket.channel });
server.broadcast( joinNotice, { channel: socket.channel });
server.broadcast( {
cmd: 'info',
text: `${socket.nick} is now ${newNick}`
}, { channel: socket.channel });
socket.nick = newNick;
};
exports.requiredData = ['nick'];
exports.info = { name: 'changenick' };