aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/core/join.js
blob: 7e98721061a4ff8db0692876428cf4ab827cb800 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
  Description: Initial entry point, applies `channel` and `nick` to the calling socket
*/

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

// module support functions
const crypto = require('crypto');

const hash = (password) => {
  const sha = crypto.createHash('sha256');
  sha.update(password);
  return sha.digest('base64').substr(0, 6);
};

// exposed "login" function to allow hooks to verify user join events
// returns object containing user info or string if error
export function parseNickname(core, data) {
  const userInfo = {
    nick: '',
    uType: 'user',
    trip: null,
    level: UAC.levels.default,
  };

  // seperate nick from password
  const nickArray = data.nick.split('#', 2);
  userInfo.nick = nickArray[0].trim();

  if (!UAC.verifyNickname(userInfo.nick)) {
    // return error as string
    return 'Nickname must consist of up to 24 letters, numbers, and underscores';
  }

  const password = nickArray[1];

  if (hash(password + core.config.tripSalt) === core.config.adminTrip) {
    userInfo.uType = 'admin';
    userInfo.trip = 'Admin';
    userInfo.level = UAC.levels.admin;
  } else if (userInfo.nick.toLowerCase() === core.config.adminName.toLowerCase()) {
    // they've got the main-admin name while not being an admin
    return 'You are not the admin, liar!';
  } else if (password) {
    userInfo.trip = hash(password + core.config.tripSalt);
  }

  // TODO: disallow moderator impersonation
  // for (const mod of core.config.mods) {
  core.config.mods.forEach((mod) => {
    if (userInfo.trip === mod.trip) {
      userInfo.uType = 'mod';
      userInfo.level = UAC.levels.moderator;
    }
  });

  return userInfo;
}

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

  // calling socket already in a channel
  if (typeof socket.channel !== 'undefined') {
    return true;
  }

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

  const channel = data.channel.trim();
  if (!channel) {
    // must join a non-blank channel
    return true;
  }

  const userInfo = this.parseNickname(core, data);
  if (typeof userInfo === 'string') {
    return server.reply({
      cmd: 'warn',
      text: userInfo,
    }, socket);
  }

  // check if the nickname already exists in the channel
  const userExists = server.findSockets({
    channel: data.channel,
    nick: (targetNick) => targetNick.toLowerCase() === userInfo.nick.toLowerCase(),
  });

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

  userInfo.userHash = server.getSocketHash(socket);

  // prepare to notify channel peers
  const newPeerList = server.findSockets({ channel: data.channel });
  const nicks = [];

  const joinAnnouncement = {
    cmd: 'onlineAdd',
    nick: userInfo.nick,
    trip: userInfo.trip || 'null',
    hash: userInfo.userHash,
    level: userInfo.level,
  };

  // send join announcement and prep online set
  for (let i = 0, l = newPeerList.length; i < l; i += 1) {
    server.reply(joinAnnouncement, newPeerList[i]);
    nicks.push(newPeerList[i].nick);
  }

  // store user info
  socket.uType = userInfo.uType;
  socket.nick = userInfo.nick;
  socket.channel = data.channel;
  socket.hash = userInfo.userHash;
  socket.level = userInfo.level;
  if (userInfo.trip !== null) socket.trip = userInfo.trip;

  nicks.push(socket.nick);

  // reply with channel peer list
  server.reply({
    cmd: 'onlineSet',
    nicks,
  }, socket);

  // stats are fun
  core.stats.increment('users-joined');

  return true;
}

export const requiredData = ['channel', 'nick'];
export const info = {
  name: 'join',
  description: 'Place calling socket into target channel with target nick & broadcast event to channel',
  usage: `
    API: { cmd: 'join', nick: '<your nickname>', channel: '<target channel>' }`,
};