aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/core/changenick.js
blob: 460f81100446b54cbae98b86a68aabe17a9ebe74 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
  Description: Generates a semi-unique channel name then broadcasts it to each client
*/

'use strict';

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, 6)) {
    server.reply({
      cmd: 'warn',
      text: 'You are changing nicknames too fast. Wait a moment before trying again.'
    }, socket);

    return;
  }

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

  let newNick = data.nick.trim();

  if (!verifyNickname(newNick)) {
    server.reply({
      cmd: 'warn',
      text: 'Nickname must consist of up to 24 letters, numbers, and underscores'
    }, socket);

    return;
  }

  if (newNick.toLowerCase() == core.config.adminName.toLowerCase()) {
    server._police.frisk(socket.remoteAddress, 4);

    server.reply({
      cmd: 'warn',
      text: 'Gtfo'
    }, socket);

    return;
  }

  let userExists = server.findSockets({
    channel: data.channel,
    nick: (targetNick) => targetNick.toLowerCase() === newNick.toLowerCase()
  });

  if (userExists.length > 0) {
    // That nickname is already in that channel
    server.reply({
      cmd: 'warn',
      text: 'Nickname taken'
    }, socket);

    return;
  }

  let peerList = server.findSockets({ channel: socket.channel });
  let leaveNotice = {
    cmd: 'onlineRemove',
    nick: socket.nick
  };
  let joinNotice = {
    cmd: 'onlineAdd',
    nick: newNick,
    trip: socket.trip || 'null',
    hash: server.getSocketHash(socket)
  };

  server.broadcast( leaveNotice, { channel: socket.channel });
  server.broadcast( joinNotice, { channel: socket.channel });
  server.broadcast( {
    cmd: 'info',
    text: `${socket.nick} is now ${newNick}`
  }, { channel: socket.channel });

  socket.nick = newNick;
};

exports.requiredData = ['nick'];

exports.info = {
  name: 'changenick',
  usage: 'changenick {nick}',
  description: 'This will change your current connections nickname'
};