2018-03-10 15:47:00 +08:00
|
|
|
/*
|
2018-03-14 13:26:53 +08:00
|
|
|
Description: Generates a semi-unique channel name then broadcasts it to each client
|
2018-03-10 15:47:00 +08:00
|
|
|
*/
|
|
|
|
|
2020-09-22 13:34:30 +08:00
|
|
|
import {
|
|
|
|
findUser,
|
|
|
|
} from '../utility/_Channels';
|
|
|
|
import {
|
|
|
|
Errors,
|
|
|
|
} from '../utility/_Constants';
|
|
|
|
import {
|
|
|
|
legacyInviteOut,
|
|
|
|
legacyInviteReply,
|
|
|
|
} from '../utility/_LegacyFunctions';
|
|
|
|
|
2020-03-07 07:12:19 +08:00
|
|
|
// 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) {
|
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
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// 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)) {
|
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,
|
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,
|
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',
|
|
|
|
channel: socket.channel,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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',
|
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
|
|
|
};
|