1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/core/invite.js

124 lines
3.3 KiB
JavaScript
Raw Normal View History

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
import {
findUser,
2022-06-23 00:32:51 +08:00
} from '../utility/_Channels.js';
import {
Errors,
2022-06-23 00:32:51 +08:00
} from '../utility/_Constants.js';
import {
legacyInviteOut,
legacyInviteReply,
2022-06-23 00:32:51 +08:00
} from '../utility/_LegacyFunctions.js';
/**
* Returns the channel that should be invited to.
* @param {any} channel
2022-06-23 00:32:51 +08:00
* @private
* @return {string}
*/
2020-09-17 13:44:32 +08:00
export function getChannel(channel = undefined) {
if (typeof channel === 'string') {
return channel;
}
2020-09-17 13:44:32 +08:00
return Math.random().toString(36).substr(2, 8);
}
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
2019-11-07 15:35:23 +08:00
if (server.police.frisk(socket.address, 2)) {
return server.reply({
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: 'You are sending invites too fast. Wait a moment before trying again.',
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
// 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
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
const targetUser = findUser(server, payload);
if (!targetUser) {
return server.reply({
cmd: 'warn',
2020-09-08 12:51:47 +08:00
text: 'Could not find user in that channel',
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,
to: targetUser.userid,
2020-09-08 12:51:47 +08:00
inviteChannel: channel,
};
// send invite notice to target client
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
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',
description: 'Sends an invite to the target client with the provided channel, or a random channel.',
usage: `
API: { cmd: 'invite', nick: '<target nickname>', to: '<optional destination channel>' }`,
2018-06-04 15:07:24 +08:00
};