2018-03-10 15:47:00 +08:00
|
|
|
/*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
function verifyNickname(nick) {
|
|
|
|
return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.run = async (core, server, socket, data) => {
|
2018-03-11 14:41:17 +08:00
|
|
|
if (typeof data.nick !== 'string') {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-10 15:47:00 +08:00
|
|
|
|
2018-03-11 14:41:17 +08:00
|
|
|
if (!verifyNickname(data.nick)) {
|
2018-03-10 15:47:00 +08:00
|
|
|
// Not a valid nickname? Chances are we won't find them
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-11 14:41:17 +08:00
|
|
|
if (data.nick == socket.nick) {
|
2018-03-10 15:47:00 +08:00
|
|
|
// TODO: reply with something witty? They invited themself
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
let channel = Math.random().toString(36).substr(2, 8);
|
|
|
|
|
|
|
|
let payload = {
|
|
|
|
cmd: 'info',
|
|
|
|
text: `${socket.nick} invited you to ?${channel}`
|
|
|
|
};
|
2018-03-11 14:41:17 +08:00
|
|
|
let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: data.nick });
|
2018-03-10 15:47:00 +08:00
|
|
|
|
|
|
|
if (!inviteSent) {
|
|
|
|
server.reply({
|
|
|
|
cmd: 'warn',
|
|
|
|
text: 'Could not find user in channel'
|
|
|
|
}, socket);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.reply({
|
|
|
|
cmd: 'info',
|
2018-03-11 14:41:17 +08:00
|
|
|
text: `You invited ${data.nick} to ?${channel}`
|
2018-03-10 15:47:00 +08:00
|
|
|
}, socket);
|
|
|
|
|
|
|
|
core.managers.stats.increment('invites-sent');
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.requiredData = ['nick'];
|
|
|
|
|
|
|
|
exports.info = {
|
|
|
|
name: 'invite',
|
|
|
|
usage: 'invite {nick}',
|
|
|
|
description: 'Generates a unique (more or less) room name and passes it to two clients'
|
|
|
|
};
|