aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/core/invite.js
blob: 3fbbe6ed73620c023abbbeebe86bca5577e96758 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
  Description: Generates a semi-unique channel name then broadcasts it to each client
*/

const verifyNickname = (nick) => {
  return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
};

exports.run = async (core, server, socket, data) => {
  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;
  }

  if (typeof data.nick !== 'string') {
    return;
  }

  if (!verifyNickname(data.nick)) {
    // Not a valid nickname? Chances are we won't find them
    return;
  }

  if (data.nick == socket.nick) {
    // They invited themself
    return;
  }
  
  let channel = Math.random().toString(36).substr(2, 8);

  let payload = {
    cmd: 'info',
    invite: channel,
    text: `${socket.nick} invited you to ?${channel}`
  };
  let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: data.nick });

  if (!inviteSent) {
    server.reply({
      cmd: 'warn',
      text: 'Could not find user in channel'
    }, socket);

    return;
  }

  server.reply({
    cmd: 'info',
    text: `You invited ${data.nick} to ?${channel}`
  }, socket);

  core.managers.stats.increment('invites-sent');
};

exports.requiredData = ['nick'];

exports.info = { name: 'invite' };