2020-09-17 13:44:32 +08:00
|
|
|
/* eslint eqeqeq: 0 */
|
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* @author Marzavec ( https://github.com/marzavec )
|
|
|
|
* @summary Update nickname
|
|
|
|
* @version 1.0.0
|
|
|
|
* @description Allows calling client to change their current nickname
|
|
|
|
* @module changenick
|
|
|
|
*/
|
2018-04-29 13:29:38 +08:00
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
import {
|
|
|
|
verifyNickname,
|
|
|
|
getUserDetails,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_UAC.js';
|
2024-01-09 03:09:59 +08:00
|
|
|
import {
|
|
|
|
Errors,
|
|
|
|
} from '../utility/_Constants.js';
|
2022-06-23 00:32:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes when invoked by a remote client
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
* @param {Object} env - Environment object with references to core, server, socket & payload
|
2022-06-23 00:32:51 +08:00
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({
|
2022-06-23 00:32:51 +08:00
|
|
|
server, socket, payload,
|
2020-09-17 13:44:32 +08:00
|
|
|
}) {
|
2022-06-23 00:32:51 +08:00
|
|
|
const { channel } = socket;
|
2020-11-07 05:16:43 +08:00
|
|
|
|
2023-12-27 16:26:49 +08:00
|
|
|
if (server.police.frisk(socket, 6)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'You are changing nicknames too fast. Wait a moment before trying again.',
|
2024-01-09 03:09:59 +08:00
|
|
|
id: Errors.Global.RATELIMIT,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
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-11-07 05:16:43 +08:00
|
|
|
if (!verifyNickname(newNick)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Nickname must consist of up to 24 letters, numbers, and underscores',
|
2024-01-09 03:09:59 +08:00
|
|
|
id: Errors.Join.INVALID_NICK,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:30:55 +08:00
|
|
|
if (newNick == previousNick) {
|
|
|
|
return server.reply({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'warn',
|
|
|
|
text: 'Nickname taken',
|
|
|
|
id: Errors.Join.NAME_TAKEN,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
2020-07-02 20:30:55 +08:00
|
|
|
}, 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({
|
2020-11-07 05:16:43 +08:00
|
|
|
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({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Nickname taken',
|
2024-01-09 03:09:59 +08:00
|
|
|
id: Errors.Join.NAME_TAKEN,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
// build update notice with new nickname
|
|
|
|
const updateNotice = {
|
|
|
|
...getUserDetails(socket),
|
|
|
|
...{
|
|
|
|
cmd: 'updateUser',
|
|
|
|
nick: newNick,
|
|
|
|
channel, // @todo Multichannel
|
2020-11-10 03:55:54 +08:00
|
|
|
},
|
2020-11-07 05:16:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// build join and leave notices for legacy clients
|
2019-11-07 15:35:23 +08:00
|
|
|
const leaveNotice = {
|
2018-04-29 13:29:38 +08:00
|
|
|
cmd: 'onlineRemove',
|
2020-11-07 05:16:43 +08:00
|
|
|
userid: socket.userid,
|
2019-11-07 15:35:23 +08:00
|
|
|
nick: socket.nick,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
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 = {
|
2020-11-07 05:16:43 +08:00
|
|
|
...getUserDetails(socket),
|
|
|
|
...{
|
|
|
|
cmd: 'onlineAdd',
|
|
|
|
nick: newNick,
|
|
|
|
channel, // @todo Multichannel
|
|
|
|
},
|
2018-04-29 13:29:38 +08:00
|
|
|
};
|
|
|
|
|
2020-11-07 05:16:43 +08:00
|
|
|
// gather channel peers
|
|
|
|
const peerList = server.findSockets({ channel });
|
|
|
|
for (let i = 0, l = peerList.length; i < l; i += 1) {
|
|
|
|
if (peerList[i].hcProtocol === 1) {
|
|
|
|
// send join/leave to legacy clients
|
|
|
|
server.send(leaveNotice, peerList[i]);
|
|
|
|
server.send(joinNotice, peerList[i]);
|
|
|
|
} else {
|
|
|
|
// send update info
|
2020-11-10 03:55:54 +08:00
|
|
|
// @todo this should be sent to every channel the client is in (multichannel)
|
2020-11-07 05:16:43 +08:00
|
|
|
server.send(updateNotice, peerList[i]);
|
|
|
|
}
|
|
|
|
}
|
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({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'info', // @todo Add numeric info code as `id`
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `${socket.nick} is now ${newNick}`,
|
2020-11-07 05:16:43 +08:00
|
|
|
channel, // @todo Multichannel
|
|
|
|
}, { channel });
|
2018-04-29 13:29:38 +08:00
|
|
|
|
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
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Automatically executes once after server is ready to register this modules hooks
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
* @param {Object} server - Reference to server environment object
|
2022-06-23 00:32:51 +08:00
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
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
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Executes every time an incoming chat command is invoked
|
channel ownership base, part 1
Fleshing out the new channel ownership feature, along with a typo fix and small syntax updates.
A claim can now be staked with /claimchannel, a channel owner may change a trip's level using /setlevel
To do: unclaimchannel, setmotd, makeprivate, makepublic, renewclaim, garbage keeping, update mod commands to accept channelOwner and channelModerator, etc
2024-01-05 16:30:28 +08:00
|
|
|
* @param {Object} env - Environment object with references to core, server, socket & payload
|
2022-06-23 00:32:51 +08:00
|
|
|
* @public
|
2023-12-30 15:24:12 +08:00
|
|
|
* @return {(Object|boolean|string)} Object = same/altered payload,
|
2022-06-23 00:32:51 +08:00
|
|
|
* false = suppress action,
|
|
|
|
* string = error
|
|
|
|
*/
|
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
|
2022-06-23 00:32:51 +08:00
|
|
|
if (!input[1]) {
|
2019-02-03 05:34:06 +08:00
|
|
|
server.reply({
|
2024-01-09 03:09:59 +08:00
|
|
|
cmd: 'warn',
|
|
|
|
text: 'Nickname must consist of up to 24 letters, numbers, and underscores',
|
|
|
|
id: Errors.Join.INVALID_NICK,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
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
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* The following payload properties are required to invoke this module:
|
|
|
|
* "nick"
|
|
|
|
* @public
|
|
|
|
* @typedef {Array} changenick/requiredData
|
|
|
|
*/
|
2019-11-07 15:35:23 +08:00
|
|
|
export const requiredData = ['nick'];
|
2022-06-23 00:32:51 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Module meta information
|
|
|
|
* @public
|
|
|
|
* @typedef {Object} changenick/info
|
|
|
|
* @property {string} name - Module command name
|
|
|
|
* @property {string} category - Module category name
|
|
|
|
* @property {string} description - Information about module
|
|
|
|
* @property {string} usage - Information about module usage
|
|
|
|
*/
|
2019-11-07 15:35:23 +08:00
|
|
|
export const info = {
|
2018-05-13 18:33:22 +08:00
|
|
|
name: 'changenick',
|
2022-06-23 00:32:51 +08:00
|
|
|
category: 'core',
|
|
|
|
description: 'Allows calling client to change their current nickname',
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
|
|
};
|