aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/core/invite.js
blob: b98115be96136eab3ef44ca9b5b76bd2d924724f (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
  Description: Generates a semi-unique channel name then broadcasts it to each client
*/

import * as UAC from '../utility/UAC/_info';

// module main
export async function run(core, server, socket, data) {
  // check for spam
  if (server.police.frisk(socket.address, 2)) {
    return server.reply({
      cmd: 'warn',
      text: 'You are sending invites too fast. Wait a moment before trying again.',
    }, socket);
  }

  // verify user input
  if (typeof data.nick !== 'string' || !UAC.verifyNickname(data.nick)) {
    return true;
  }

  // why would you invite yourself?
  if (data.nick === socket.nick) {
    return true;
  }

  let channel;
  if (typeof data.to === 'string') {
    channel = data.to;
  } else {
    channel = Math.random().toString(36).substr(2, 8);
  }

  // build and send invite
  const payload = {
    cmd: 'info',
    type: 'invite',
    from: socket.nick,
    invite: channel,
    text: `${socket.nick} invited you to ?${channel}`,
  };

  const inviteSent = server.broadcast(payload, {
    channel: socket.channel,
    nick: data.nick,
  });

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

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

  // stats are fun
  core.stats.increment('invites-sent');

  return true;
}

export const requiredData = ['nick'];
export const info = {
  name: 'invite',
  description: 'Sends an invite to the target client with the provided channel, or a random channel.',
  usage: `
    API: { cmd: 'invite', nick: '<target nickname>', to: '<optional destination channel>' }`,
};