2020-09-17 13:44:32 +08:00
|
|
|
/* eslint no-param-reassign: 0 */
|
2022-06-23 00:32:51 +08:00
|
|
|
/* eslint import/no-cycle: [0, { ignoreExternal: true }] */
|
2020-09-17 13:44:32 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* @author Marzavec ( https://github.com/marzavec )
|
|
|
|
* @summary Join target channel
|
|
|
|
* @version 1.0.0
|
|
|
|
* @description Join the target channel using the supplied nick and password
|
|
|
|
* @module join
|
|
|
|
*/
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
import {
|
|
|
|
getSession,
|
|
|
|
} from './session.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
canJoinChannel,
|
2022-06-23 00:32:51 +08:00
|
|
|
socketInChannel,
|
|
|
|
} from '../utility/_Channels.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
Errors,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_Constants.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
upgradeLegacyJoin,
|
|
|
|
legacyLevelToLabel,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_LegacyFunctions.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
verifyNickname,
|
|
|
|
getUserPerms,
|
2020-11-07 05:16:43 +08:00
|
|
|
getUserDetails,
|
2023-12-27 16:51:52 +08:00
|
|
|
isModerator,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_UAC.js';
|
2018-09-30 14:44:36 +08:00
|
|
|
|
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({
|
|
|
|
core, server, socket, payload,
|
2022-06-23 00:32:51 +08:00
|
|
|
}) {
|
|
|
|
// check for spam
|
2023-12-27 16:26:49 +08:00
|
|
|
if (server.police.frisk(socket, 3)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-22 13:34:30 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'You are joining channels too fast. Wait a moment and try again.',
|
2020-09-22 13:34:30 +08:00
|
|
|
id: Errors.Join.RATELIMIT,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: false, // @todo Multichannel, false for global event
|
2020-09-22 13:34:30 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
// `join` is the legacy entry point, check if it needs to be upgraded
|
2023-12-22 15:14:03 +08:00
|
|
|
if (typeof socket.hcProtocol === 'undefined' || socket.hcProtocol === 1) {
|
2020-09-22 13:34:30 +08:00
|
|
|
payload = upgradeLegacyJoin(server, socket, payload);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store payload values
|
|
|
|
const { channel, nick, pass } = payload;
|
|
|
|
|
|
|
|
// check if a client is able to join target channel
|
|
|
|
const mayJoin = canJoinChannel(channel, socket);
|
|
|
|
if (mayJoin !== true) {
|
|
|
|
return server.reply({
|
|
|
|
cmd: 'warn',
|
|
|
|
text: 'You may not join that channel.',
|
|
|
|
id: mayJoin,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: false, // @todo Multichannel, false for global event
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// calling socket already in a channel
|
2020-09-22 13:34:30 +08:00
|
|
|
// @todo multichannel update, will remove
|
2018-03-10 15:47:00 +08:00
|
|
|
if (typeof socket.channel !== 'undefined') {
|
2020-09-08 12:51:47 +08:00
|
|
|
return server.reply({
|
|
|
|
cmd: 'warn', // @todo Remove this
|
|
|
|
text: 'Joining more than one channel is not currently supported',
|
2020-09-22 13:34:30 +08:00
|
|
|
id: Errors.Join.ALREADY_JOINED,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: false, // @todo Multichannel, false for global event
|
2020-09-08 12:51:47 +08:00
|
|
|
}, socket);
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
2020-09-22 13:34:30 +08:00
|
|
|
// end todo
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
// validates the user input for `nick`
|
2020-10-17 12:36:28 +08:00
|
|
|
if (verifyNickname(nick, socket) !== true) {
|
2020-09-22 13:34:30 +08:00
|
|
|
return server.reply({
|
|
|
|
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: false, // @todo Multichannel, false for global event
|
2020-09-22 13:34:30 +08:00
|
|
|
}, socket);
|
2018-03-11 14:41:17 +08:00
|
|
|
}
|
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
// get trip and level
|
2022-06-23 00:32:51 +08:00
|
|
|
const { trip, level } = getUserPerms(pass, core.saltKey, core.appConfig.data, channel);
|
2020-11-10 03:55:54 +08:00
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
// store the user values
|
|
|
|
const userInfo = {
|
|
|
|
nick,
|
|
|
|
trip,
|
|
|
|
uType: legacyLevelToLabel(level),
|
|
|
|
hash: socket.hash,
|
|
|
|
level,
|
|
|
|
userid: socket.userid,
|
2020-11-07 05:16:43 +08:00
|
|
|
isBot: socket.isBot,
|
|
|
|
color: socket.color,
|
2020-09-22 13:34:30 +08:00
|
|
|
channel,
|
|
|
|
};
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// check if the nickname already exists in the channel
|
2019-11-07 15:35:23 +08:00
|
|
|
const userExists = server.findSockets({
|
2020-09-22 13:34:30 +08:00
|
|
|
channel,
|
2019-11-07 15:35:23 +08:00
|
|
|
nick: (targetNick) => targetNick.toLowerCase() === userInfo.nick.toLowerCase(),
|
2018-04-29 13:29:38 +08:00
|
|
|
});
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-04-29 13:29:38 +08:00
|
|
|
if (userExists.length > 0) {
|
2018-06-04 15:07:24 +08:00
|
|
|
// that nickname is already in that channel
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-22 13:34:30 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Nickname taken',
|
2020-09-22 13:34:30 +08:00
|
|
|
id: Errors.Join.NAME_TAKEN,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: false, // @todo Multichannel, false for global event
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// prepare to notify channel peers
|
2020-09-22 13:34:30 +08:00
|
|
|
const newPeerList = server.findSockets({ channel });
|
2020-03-13 02:28:20 +08:00
|
|
|
const nicks = []; /* @legacy */
|
|
|
|
const users = [];
|
2020-09-22 13:34:30 +08:00
|
|
|
const joinAnnouncement = { ...{ cmd: 'onlineAdd' }, ...userInfo };
|
2018-06-04 15:07:24 +08:00
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
// send join announcement and prep online set reply
|
2019-11-07 15:35:23 +08:00
|
|
|
for (let i = 0, l = newPeerList.length; i < l; i += 1) {
|
2018-04-29 13:29:38 +08:00
|
|
|
server.reply(joinAnnouncement, newPeerList[i]);
|
2020-11-10 03:55:54 +08:00
|
|
|
|
2020-03-13 02:28:20 +08:00
|
|
|
nicks.push(newPeerList[i].nick); /* @legacy */
|
|
|
|
users.push({
|
2020-11-07 05:16:43 +08:00
|
|
|
...{
|
|
|
|
channel,
|
|
|
|
isme: false,
|
|
|
|
},
|
|
|
|
...getUserDetails(newPeerList[i]),
|
2020-03-13 02:28:20 +08:00
|
|
|
});
|
2018-04-29 13:29:38 +08:00
|
|
|
}
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// store user info
|
2018-09-30 14:44:36 +08:00
|
|
|
socket.nick = userInfo.nick;
|
2020-03-13 02:28:20 +08:00
|
|
|
socket.trip = userInfo.trip;
|
2020-03-06 00:48:58 +08:00
|
|
|
socket.level = userInfo.level;
|
2020-09-22 13:34:30 +08:00
|
|
|
socket.uType = userInfo.uType; /* @legacy */
|
|
|
|
socket.channel = channel; /* @legacy */
|
|
|
|
// @todo multi-channel patch
|
|
|
|
// socket.channels.push(channel);
|
2022-06-23 00:32:51 +08:00
|
|
|
socket.channels = [channel];
|
2020-03-13 02:28:20 +08:00
|
|
|
|
2023-12-27 16:38:18 +08:00
|
|
|
// global mod perks
|
|
|
|
if (isModerator(socket.level)) {
|
|
|
|
socket.ratelimitImmune = true;
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
nicks.push(userInfo.nick); /* @legacy */
|
2020-10-15 12:43:38 +08:00
|
|
|
users.push({ ...{ isme: true, isBot: socket.isBot }, ...userInfo });
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// reply with channel peer list
|
2018-03-10 15:47:00 +08:00
|
|
|
server.reply({
|
|
|
|
cmd: 'onlineSet',
|
2020-03-13 02:28:20 +08:00
|
|
|
nicks, /* @legacy */
|
|
|
|
users,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel, // @todo Multichannel (?)
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
// update client with new session info
|
|
|
|
server.reply({
|
|
|
|
cmd: 'session',
|
|
|
|
restored: false,
|
|
|
|
token: getSession(socket, core),
|
|
|
|
channels: socket.channels,
|
|
|
|
}, socket);
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// stats are fun
|
2019-03-19 14:36:21 +08:00
|
|
|
core.stats.increment('users-joined');
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
export function restoreJoin({
|
|
|
|
server, socket, channel,
|
|
|
|
}) {
|
|
|
|
// check if a client is able to join target channel
|
|
|
|
const mayJoin = canJoinChannel(channel, socket);
|
|
|
|
if (mayJoin !== true) {
|
|
|
|
return server.reply({
|
|
|
|
cmd: 'warn',
|
|
|
|
text: 'You may not join that channel.',
|
|
|
|
id: mayJoin,
|
|
|
|
channel: false, // @todo Multichannel, false for global event
|
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
|
|
|
// store the user values
|
|
|
|
const userInfo = {
|
|
|
|
nick: socket.nick,
|
|
|
|
trip: socket.trip,
|
|
|
|
uType: legacyLevelToLabel(socket.level),
|
|
|
|
hash: socket.hash,
|
|
|
|
level: socket.level,
|
|
|
|
userid: socket.userid,
|
|
|
|
isBot: socket.isBot,
|
|
|
|
color: socket.color,
|
|
|
|
channel,
|
|
|
|
};
|
|
|
|
|
|
|
|
// prepare to notify channel peers
|
|
|
|
const newPeerList = server.findSockets({ channel });
|
|
|
|
const nicks = []; /* @legacy */
|
|
|
|
const users = [];
|
|
|
|
const joinAnnouncement = { ...{ cmd: 'onlineAdd' }, ...userInfo };
|
|
|
|
// build update notice with new privileges
|
|
|
|
const updateAnnouncement = {
|
|
|
|
...getUserDetails(socket),
|
|
|
|
...{
|
|
|
|
cmd: 'updateUser',
|
|
|
|
online: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const isDuplicate = socketInChannel(server, channel, socket);
|
|
|
|
|
|
|
|
// send join announcement and prep online set reply
|
|
|
|
for (let i = 0, l = newPeerList.length; i < l; i += 1) {
|
|
|
|
if (isDuplicate) {
|
|
|
|
server.reply(updateAnnouncement, newPeerList[i]);
|
|
|
|
} else {
|
|
|
|
server.reply(joinAnnouncement, newPeerList[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
nicks.push(newPeerList[i].nick); /* @legacy */
|
|
|
|
users.push({
|
|
|
|
...{
|
|
|
|
channel,
|
|
|
|
isme: false,
|
|
|
|
},
|
|
|
|
...getUserDetails(newPeerList[i]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
nicks.push(userInfo.nick); /* @legacy */
|
|
|
|
users.push({ ...{ isme: true, isBot: socket.isBot }, ...userInfo });
|
|
|
|
|
|
|
|
// reply with channel peer list
|
|
|
|
server.reply({
|
|
|
|
cmd: 'onlineSet',
|
|
|
|
nicks, /* @legacy */
|
|
|
|
users,
|
|
|
|
channel, // @todo Multichannel (?)
|
|
|
|
}, socket);
|
|
|
|
|
|
|
|
socket.channel = channel; /* @legacy */
|
|
|
|
socket.channels.push(channel);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module meta information
|
|
|
|
* @public
|
|
|
|
* @typedef {Object} join/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: 'join',
|
2022-06-23 00:32:51 +08:00
|
|
|
category: 'core',
|
2020-09-22 13:34:30 +08:00
|
|
|
description: 'Join the target channel using the supplied nick and password',
|
2018-09-30 14:44:36 +08:00
|
|
|
usage: `
|
2020-09-08 12:51:47 +08:00
|
|
|
API: { cmd: 'join', nick: '<your nickname>', pass: '<optional password>', channel: '<target channel>' }`,
|
2018-06-01 03:34:39 +08:00
|
|
|
};
|