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

66 lines
1.5 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
*/
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
exports.run = async (core, server, socket, data) => {
2018-06-04 15:07:24 +08:00
// check for spam
2018-04-29 13:29:38 +08:00
if (server._police.frisk(socket.remoteAddress, 2)) {
server.reply({
cmd: 'warn',
text: 'You are sending invites too fast. Wait a moment before trying again.'
}, socket);
return;
}
2018-06-04 15:07:24 +08:00
// verify user input
if (typeof data.nick !== 'string' || !verifyNickname(data.nick)) {
2018-03-10 15:47:00 +08:00
return;
}
2018-06-04 15:07:24 +08:00
// why would you invite yourself?
if (data.nick == socket.nick) {
2018-03-10 15:47:00 +08:00
return;
}
2018-06-04 15:07:24 +08:00
// generate common channel
2018-03-10 15:47:00 +08:00
let channel = Math.random().toString(36).substr(2, 8);
2018-06-04 15:07:24 +08:00
// build and send invite
2018-03-10 15:47:00 +08:00
let payload = {
cmd: 'info',
invite: channel,
2018-03-10 15:47:00 +08:00
text: `${socket.nick} invited you to ?${channel}`
};
let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: data.nick });
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) {
server.reply({
cmd: 'warn',
text: 'Could not find user in channel'
}, socket);
return;
}
2018-06-04 15:07:24 +08:00
// reply with common channel
2018-03-10 15:47:00 +08:00
server.reply({
cmd: 'info',
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
2018-03-10 15:47:00 +08:00
core.managers.stats.increment('invites-sent');
};
exports.requiredData = ['nick'];
2018-05-13 18:33:22 +08:00
exports.info = {
name: 'invite',
usage: 'invite {nick}',
description: 'Generates a unique (more or less) room name and passes it to two clients'
2018-06-04 15:07:24 +08:00
};