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-03-07 05:12:48 +08:00
|
|
|
import * as UAC from '../utility/UAC/_info';
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2020-03-07 07:12:19 +08:00
|
|
|
// module support functions
|
|
|
|
/**
|
|
|
|
* Returns a message for if it's a valid nickname to invite. Returns null if there was no error.
|
|
|
|
* @param {any} nick
|
|
|
|
* @return {(string|null)}
|
|
|
|
*/
|
|
|
|
export function checkNickname (nick, inviterNick) {
|
|
|
|
if (typeof nick !== 'string' || !UAC.verifyNickname(nick)) {
|
|
|
|
return "Nickname was invalid.";
|
|
|
|
} else if (nick === inviterNick) {
|
|
|
|
return "Why would you invite yourself?";
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the channel that should be invited to.
|
|
|
|
* @param {any} channel
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
export function getChannel (channel=undefined) {
|
|
|
|
if (typeof channel === 'string') {
|
|
|
|
return channel;
|
|
|
|
} else {
|
|
|
|
return Math.random().toString(36).substr(2, 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the payload that a user who is being invited would receive.
|
|
|
|
* @param {string} inviter The user who is inviting them.
|
|
|
|
* @param {string} channel The channel they are being invited to.
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
export function createRecipientPayload (inviter, channel) {
|
|
|
|
return {
|
|
|
|
cmd: 'info',
|
|
|
|
type: 'invite',
|
|
|
|
from: inviter,
|
|
|
|
text: `${inviter} invited you to ?${channel}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the payload that a user who invited users (and succeeded) would receive.
|
|
|
|
* @param {string} nick The user who was invited.
|
|
|
|
* @param {string} channel The channel they were invited to.
|
|
|
|
*/
|
|
|
|
export function createSuccessPayload (nick, channel) {
|
|
|
|
return {
|
|
|
|
cmd: 'info',
|
|
|
|
type: 'invite',
|
|
|
|
invite: channel,
|
|
|
|
text: `You invited ${nick} to ?${channel}`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the invites to the recipients.
|
|
|
|
* @param {MainServer} server The server. Required to broadcast the messages.
|
|
|
|
* @param {string} recipientNick The user who is being invited.
|
|
|
|
* @param {string} inviterNick The user who is doing the inviting.
|
|
|
|
* @param {string} originalChannel The channel they have in common, and where the invite is sent in.
|
|
|
|
* @param {string} inviteChannel The channel they are being invited to.
|
|
|
|
*/
|
|
|
|
export function sendInvite (server, recipientNick, inviterNick, originalChannel, inviteChannel) {
|
|
|
|
return server.broadcast(createRecipientPayload(inviterNick, inviteChannel), {
|
|
|
|
channel: originalChannel,
|
|
|
|
nick: recipientNick,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module main
|
2019-11-07 15:35:23 +08:00
|
|
|
export async function run(core, server, socket, data) {
|
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({
|
2018-04-29 13:29:38 +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.',
|
2018-04-29 13:29:38 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// verify user input
|
2020-03-07 07:12:19 +08:00
|
|
|
const nickValid = checkNickname(data.nick, socket.nick);
|
|
|
|
if (nickValid !== null) {
|
|
|
|
server.reply({
|
|
|
|
cmd: 'warn',
|
|
|
|
text: nickValid,
|
|
|
|
}, socket);
|
2019-11-07 15:35:23 +08:00
|
|
|
return true;
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
2018-06-04 15:07:24 +08:00
|
|
|
|
2020-03-07 07:12:19 +08:00
|
|
|
const channel = getChannel(data.to);
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// build and send invite
|
2020-03-07 07:12:19 +08:00
|
|
|
const payload = createRecipientPayload(socket.nick, channel);
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2019-11-07 15:35:23 +08:00
|
|
|
const inviteSent = server.broadcast(payload, {
|
2019-02-03 05:34:06 +08:00
|
|
|
channel: socket.channel,
|
2019-11-07 15:35:23 +08:00
|
|
|
nick: data.nick,
|
2019-02-03 05:34:06 +08:00
|
|
|
});
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// server indicates the user was not found
|
2018-03-10 15:47:00 +08:00
|
|
|
if (!inviteSent) {
|
2018-09-30 14:44:36 +08:00
|
|
|
return server.reply({
|
2018-03-10 15:47:00 +08:00
|
|
|
cmd: 'warn',
|
2019-11-07 15:35:23 +08:00
|
|
|
text: 'Could not find user in channel',
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// reply with common channel
|
2020-03-07 07:12:19 +08:00
|
|
|
server.reply(createSuccessPayload(data.nick, channel), 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const requiredData = ['nick'];
|
|
|
|
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
|
|
|
};
|