2020-09-17 13:44:32 +08:00
|
|
|
/* eslint eqeqeq: 0 */
|
|
|
|
|
2018-04-29 13:29:38 +08:00
|
|
|
/*
|
2018-09-30 14:44:36 +08:00
|
|
|
Description: Allows calling client to change their current nickname
|
2018-04-29 13:29:38 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-07 05:12:48 +08:00
|
|
|
import * as UAC from '../utility/UAC/_info';
|
2018-04-29 13:29:38 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module main
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({
|
|
|
|
core, server, socket, payload,
|
|
|
|
}) {
|
2019-11-07 15:35:23 +08:00
|
|
|
if (server.police.frisk(socket.address, 6)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'You are changing nicknames too fast. Wait a moment before trying again.',
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// verify user data is string
|
2020-09-17 13:44:32 +08:00
|
|
|
if (typeof payload.nick !== 'string') {
|
2019-11-07 15:35:23 +08:00
|
|
|
return true;
|
2018-04-29 13:29:38 +08:00
|
|
|
}
|
|
|
|
|
2020-07-02 20:30:55 +08:00
|
|
|
const previousNick = socket.nick;
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// make sure requested nickname meets standards
|
2020-09-17 13:44:32 +08:00
|
|
|
const newNick = payload.nick.trim();
|
2020-03-07 04:41:09 +08:00
|
|
|
if (!UAC.verifyNickname(newNick)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Nickname must consist of up to 24 letters, numbers, and underscores',
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// prevent admin impersonation
|
2020-09-17 13:44:32 +08:00
|
|
|
// @todo prevent mod impersonation
|
2019-11-07 15:35:23 +08:00
|
|
|
if (newNick.toLowerCase() === core.config.adminName.toLowerCase()) {
|
|
|
|
server.police.frisk(socket.address, 4);
|
2018-04-29 13:29:38 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'You are not the admin, liar!',
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:30:55 +08:00
|
|
|
if (newNick == previousNick) {
|
|
|
|
return server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2020-07-02 20:30:55 +08:00
|
|
|
text: 'You already have that name',
|
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// find any sockets that have the same nickname
|
2019-11-07 15:35:23 +08:00
|
|
|
const userExists = server.findSockets({
|
2018-04-30 00:07:16 +08:00
|
|
|
channel: socket.channel,
|
2020-09-17 13:44:32 +08:00
|
|
|
nick: (targetNick) => targetNick.toLowerCase() === newNick.toLowerCase()
|
2020-07-02 20:30:55 +08:00
|
|
|
// Allow them to rename themselves to a different case
|
2020-09-17 13:44:32 +08:00
|
|
|
&& targetNick != previousNick,
|
2018-04-29 13:29:38 +08:00
|
|
|
});
|
|
|
|
|
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
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Nickname taken',
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// build join and leave notices
|
2020-09-17 13:44:32 +08:00
|
|
|
// @todo this is a legacy client holdover, name changes in the future will
|
2019-02-03 05:34:06 +08:00
|
|
|
// have thieir own event
|
2019-11-07 15:35:23 +08:00
|
|
|
const leaveNotice = {
|
2018-04-29 13:29:38 +08:00
|
|
|
cmd: 'onlineRemove',
|
2019-11-07 15:35:23 +08:00
|
|
|
nick: socket.nick,
|
2018-04-29 13:29:38 +08:00
|
|
|
};
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
const joinNotice = {
|
2018-04-29 13:29:38 +08:00
|
|
|
cmd: 'onlineAdd',
|
|
|
|
nick: newNick,
|
|
|
|
trip: socket.trip || 'null',
|
2019-11-07 15:35:23 +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
|
2019-11-07 15:35:23 +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
|
2019-11-07 15:35:23 +08:00
|
|
|
server.broadcast({
|
2018-04-29 13:29:38 +08:00
|
|
|
cmd: 'info',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `${socket.nick} is now ${newNick}`,
|
2018-04-29 13:29:38 +08:00
|
|
|
}, { channel: socket.channel });
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// commit change to nickname
|
2020-09-17 13:44:32 +08:00
|
|
|
socket.nick = newNick; // eslint-disable-line no-param-reassign
|
2019-11-07 15:35:23 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-29 13:29:38 +08:00
|
|
|
|
2019-02-03 05:34:06 +08:00
|
|
|
// module hook functions
|
2019-11-07 15:35:23 +08:00
|
|
|
export function initHooks(server) {
|
2019-11-08 01:46:55 +08:00
|
|
|
server.registerHook('in', 'chat', this.nickCheck.bind(this), 29);
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2019-02-03 05:34:06 +08:00
|
|
|
|
2019-02-21 16:43:25 +08:00
|
|
|
// hooks chat commands checking for /nick
|
2020-09-17 13:44:32 +08:00
|
|
|
export function nickCheck({
|
|
|
|
core, server, socket, payload,
|
|
|
|
}) {
|
2019-02-03 05:34:06 +08:00
|
|
|
if (typeof payload.text !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (payload.text.startsWith('/nick')) {
|
2019-11-07 15:35:23 +08:00
|
|
|
const input = payload.text.split(' ');
|
2019-02-03 05:34:06 +08:00
|
|
|
|
|
|
|
// If there is no nickname target parameter
|
|
|
|
if (input[1] === undefined) {
|
|
|
|
server.reply({
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'warn', // @todo Remove english and change to numeric id
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Refer to `/help nick` for instructions on how to use this command.',
|
2019-02-03 05:34:06 +08:00
|
|
|
}, socket);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
const newNick = input[1].replace(/@/g, '');
|
2019-02-03 05:34:06 +08:00
|
|
|
|
2020-09-17 13:44:32 +08:00
|
|
|
this.run({
|
|
|
|
core,
|
|
|
|
server,
|
|
|
|
socket,
|
|
|
|
payload: {
|
|
|
|
cmd: 'changenick',
|
|
|
|
nick: newNick,
|
|
|
|
},
|
2019-02-03 05:34:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return payload;
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2019-02-03 05:34:06 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
export const requiredData = ['nick'];
|
|
|
|
export const info = {
|
2018-05-13 18:33:22 +08:00
|
|
|
name: 'changenick',
|
2018-09-30 14:44:36 +08:00
|
|
|
description: 'This will change your current connections nickname',
|
|
|
|
usage: `
|
2019-02-03 05:34:06 +08:00
|
|
|
API: { cmd: 'changenick', nick: '<new nickname>' }
|
2019-11-07 15:35:23 +08:00
|
|
|
Text: /nick <new nickname>`,
|
2018-05-13 18:33:22 +08:00
|
|
|
};
|