2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* @author Marzavec ( https://github.com/marzavec )
|
|
|
|
* @summary Send an invite
|
|
|
|
* @version 1.0.0
|
|
|
|
* @description Sends an invite to the target client with the provided channel, or a random channel
|
|
|
|
* @module invite
|
|
|
|
*/
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
findUser,
|
2022-06-23 00:32:51 +08:00
|
|
|
} 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 {
|
|
|
|
legacyInviteOut,
|
|
|
|
legacyInviteReply,
|
2022-06-23 00:32:51 +08:00
|
|
|
} from '../utility/_LegacyFunctions.js';
|
2020-09-22 13:34:30 +08:00
|
|
|
|
2020-03-07 07:12:19 +08:00
|
|
|
/**
|
|
|
|
* Returns the channel that should be invited to.
|
|
|
|
* @param {any} channel
|
2022-06-23 00:32:51 +08:00
|
|
|
* @private
|
2020-03-07 07:12:19 +08:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export function getChannel(channel = undefined) {
|
2020-03-07 07:12:19 +08:00
|
|
|
if (typeof channel === 'string') {
|
|
|
|
return channel;
|
|
|
|
}
|
2020-09-17 13:44:32 +08:00
|
|
|
return Math.random().toString(36).substr(2, 8);
|
2020-03-07 07:12:19 +08:00
|
|
|
}
|
|
|
|
|
2022-06-23 00:32:51 +08:00
|
|
|
/**
|
|
|
|
* Executes when invoked by a remote client
|
|
|
|
* @param {Object} env - Enviroment object with references to core, server, socket & payload
|
|
|
|
* @public
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2020-09-17 13:44:32 +08:00
|
|
|
export async function run({
|
|
|
|
core, server, socket, payload,
|
|
|
|
}) {
|
2018-06-04 15:07:24 +08:00
|
|
|
// check for spam
|
2023-12-27 16:26:49 +08:00
|
|
|
if (server.police.frisk(socket, 2)) {
|
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 sending invites too fast. Wait a moment before trying again.',
|
2020-09-22 13:34:30 +08:00
|
|
|
id: Errors.Invite.RATELIMIT,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// verify user input
|
2020-09-22 13:34:30 +08:00
|
|
|
// if this is a legacy client add missing params to payload
|
|
|
|
if (socket.hcProtocol === 1) {
|
|
|
|
if (typeof socket.channel === 'undefined' || typeof payload.nick !== 'string') {
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-04 15:07:24 +08:00
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
payload.channel = socket.channel; // eslint-disable-line no-param-reassign
|
|
|
|
} else if (typeof payload.userid !== 'number' || typeof payload.channel !== 'string') {
|
2020-09-08 12:51:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2020-09-17 13:44:32 +08:00
|
|
|
// @todo Verify this socket is part of payload.channel - multichannel patch
|
2020-09-08 12:51:47 +08:00
|
|
|
// find target user
|
2020-09-22 13:34:30 +08:00
|
|
|
const targetUser = findUser(server, payload);
|
|
|
|
if (!targetUser) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2020-09-22 13:34:30 +08:00
|
|
|
cmd: 'warn',
|
2020-09-08 12:51:47 +08:00
|
|
|
text: 'Could not find user in that channel',
|
2020-09-22 13:34:30 +08:00
|
|
|
id: Errors.Global.UNKNOWN_USER,
|
2020-10-10 13:34:59 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2020-09-08 12:51:47 +08:00
|
|
|
// generate common channel
|
2020-09-17 13:44:32 +08:00
|
|
|
const channel = getChannel(payload.to);
|
2020-09-08 12:51:47 +08:00
|
|
|
|
|
|
|
// build invite
|
2020-09-17 13:44:32 +08:00
|
|
|
const outgoingPayload = {
|
2020-09-08 12:51:47 +08:00
|
|
|
cmd: 'invite',
|
2020-10-15 12:17:11 +08:00
|
|
|
channel: socket.channel, // @todo Multichannel
|
2020-09-08 12:51:47 +08:00
|
|
|
from: socket.userid,
|
2020-09-22 13:34:30 +08:00
|
|
|
to: targetUser.userid,
|
2020-09-08 12:51:47 +08:00
|
|
|
inviteChannel: channel,
|
|
|
|
};
|
|
|
|
|
|
|
|
// send invite notice to target client
|
2020-09-22 13:34:30 +08:00
|
|
|
if (targetUser.hcProtocol === 1) {
|
|
|
|
server.reply(legacyInviteOut(outgoingPayload, socket.nick), targetUser);
|
|
|
|
} else {
|
|
|
|
server.reply(outgoingPayload, targetUser);
|
|
|
|
}
|
2020-09-08 12:51:47 +08:00
|
|
|
|
|
|
|
// send invite notice to this client
|
2020-09-22 13:34:30 +08:00
|
|
|
if (socket.hcProtocol === 1) {
|
|
|
|
server.reply(legacyInviteReply(outgoingPayload, targetUser.nick), socket);
|
|
|
|
} else {
|
|
|
|
server.reply(outgoingPayload, socket);
|
|
|
|
}
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// stats are fun
|
2019-03-19 14:36:21 +08:00
|
|
|
core.stats.increment('invites-sent');
|
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
|
|
|
/**
|
|
|
|
* Module meta information
|
|
|
|
* @public
|
|
|
|
* @typedef {Object} invite/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: 'invite',
|
2022-06-23 00:32:51 +08:00
|
|
|
category: 'core',
|
2020-02-17 11:18:13 +08:00
|
|
|
description: 'Sends an invite to the target client with the provided channel, or a random channel.',
|
2018-09-30 14:44:36 +08:00
|
|
|
usage: `
|
2020-02-17 11:18:13 +08:00
|
|
|
API: { cmd: 'invite', nick: '<target nickname>', to: '<optional destination channel>' }`,
|
2018-06-04 15:07:24 +08:00
|
|
|
};
|