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
|
|
|
*/
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module support functions
|
2018-06-04 15:07:24 +08:00
|
|
|
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
|
2018-03-10 15:47:00 +08:00
|
|
|
|
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
|
|
|
|
if (typeof data.nick !== 'string' || !verifyNickname(data.nick)) {
|
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
|
|
|
// why would you invite yourself?
|
2019-11-07 15:35:23 +08:00
|
|
|
if (data.nick === socket.nick) {
|
|
|
|
return true;
|
2018-03-10 15:47:00 +08:00
|
|
|
}
|
2018-06-04 15:07:24 +08:00
|
|
|
|
|
|
|
// generate common channel
|
2019-11-07 15:35:23 +08:00
|
|
|
const channel = Math.random().toString(36).substr(2, 8);
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-06-04 15:07:24 +08:00
|
|
|
// build and send invite
|
2019-11-07 15:35:23 +08:00
|
|
|
const payload = {
|
2018-03-10 15:47:00 +08:00
|
|
|
cmd: 'info',
|
2019-02-03 05:34:06 +08:00
|
|
|
type: 'invite',
|
|
|
|
from: socket.nick,
|
2018-03-26 05:16:02 +08:00
|
|
|
invite: channel,
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `${socket.nick} invited you to ?${channel}`,
|
2018-03-10 15:47:00 +08:00
|
|
|
};
|
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
|
2018-03-10 15:47:00 +08:00
|
|
|
server.reply({
|
|
|
|
cmd: 'info',
|
2019-02-03 05:34:06 +08:00
|
|
|
type: 'invite',
|
|
|
|
invite: channel,
|
2019-11-07 15:35:23 +08:00
|
|
|
text: `You invited ${data.nick} to ?${channel}`,
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
|
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',
|
2018-09-30 14:44:36 +08:00
|
|
|
description: 'Generates a unique (more or less) room name and passes it to two clients',
|
|
|
|
usage: `
|
2019-11-07 15:35:23 +08:00
|
|
|
API: { cmd: 'invite', nick: '<target nickname>' }`,
|
2018-06-04 15:07:24 +08:00
|
|
|
};
|