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

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-03-10 15:47:00 +08:00
/*
Description: Generates a semi-unique channel name then broadcasts it to each client
2018-03-10 15:47:00 +08:00
*/
import {
findUser,
} from '../utility/_Channels';
import {
Errors,
} from '../utility/_Constants';
import {
legacyInviteOut,
legacyInviteReply,
} from '../utility/_LegacyFunctions';
// module support functions
/**
* Returns the channel that should be invited to.
* @param {any} channel
* @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);
}
// module main
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;
}
2020-09-17 13:44:32 +08:00
export const requiredData = []; // ['nick'];
2019-11-07 15:35:23 +08:00
export const info = {
2018-05-13 18:33:22 +08:00
name: 'invite',
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
};