1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/server/src/commands/core/join.js

159 lines
4.1 KiB
JavaScript
Raw Normal View History

2018-03-10 15:47:00 +08:00
/*
Description: Initial entry point, applies `channel` and `nick` to the calling socket
2018-03-10 15:47:00 +08:00
*/
import * as UAC from "../utility/UAC/info";
// module support functions
2018-03-10 15:47:00 +08:00
const crypto = require('crypto');
const hash = (password) => {
2019-11-07 15:35:23 +08:00
const sha = crypto.createHash('sha256');
2018-03-10 15:47:00 +08:00
sha.update(password);
return sha.digest('base64').substr(0, 6);
};
2018-03-10 15:47:00 +08:00
2018-06-04 15:07:24 +08:00
const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
2018-03-10 15:47:00 +08:00
// exposed "login" function to allow hooks to verify user join events
// returns object containing user info or string if error
2019-11-07 15:35:23 +08:00
export function parseNickname(core, data) {
const userInfo = {
nick: '',
uType: 'user',
trip: null,
level: UAC.levels.user,
};
// seperate nick from password
2019-11-07 15:35:23 +08:00
const nickArray = data.nick.split('#', 2);
userInfo.nick = nickArray[0].trim();
if (!verifyNickname(userInfo.nick)) {
// return error as string
return 'Nickname must consist of up to 24 letters, numbers, and underscores';
}
2019-11-07 15:35:23 +08:00
const password = nickArray[1];
if (hash(password + core.config.tripSalt) === core.config.adminTrip) {
userInfo.uType = 'admin';
userInfo.trip = 'Admin';
userInfo.level = UAC.levels.admin;
2019-11-07 15:35:23 +08:00
} 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
2019-11-07 15:35:23 +08:00
// 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;
}
2019-11-07 15:35:23 +08:00
});
return userInfo;
2019-11-07 15:35:23 +08:00
}
// module main
2019-11-07 15:35:23 +08:00
export async function run(core, server, socket, data) {
2018-06-04 15:07:24 +08:00
// check for spam
2019-11-07 15:35:23 +08:00
if (server.police.frisk(socket.address, 3)) {
return server.reply({
2018-03-10 15:47:00 +08:00
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: 'You are joining channels too fast. Wait a moment and try again.',
2018-03-10 15:47:00 +08:00
}, socket);
}
2018-06-04 15:07:24 +08:00
// calling socket already in a channel
2018-03-10 15:47:00 +08:00
if (typeof socket.channel !== 'undefined') {
2019-11-07 15:35:23 +08:00
return true;
2018-03-10 15:47:00 +08:00
}
2018-06-04 15:07:24 +08:00
// check user input
if (typeof data.channel !== 'string' || typeof data.nick !== 'string') {
2019-11-07 15:35:23 +08:00
return true;
}
2019-11-07 15:35:23 +08:00
const channel = data.channel.trim();
2018-03-10 15:47:00 +08:00
if (!channel) {
2018-06-04 15:07:24 +08:00
// must join a non-blank channel
2019-11-07 15:35:23 +08:00
return true;
2018-03-10 15:47:00 +08:00
}
2019-11-07 15:35:23 +08:00
const userInfo = this.parseNickname(core, data);
if (typeof userInfo === 'string') {
return server.reply({
2018-03-10 15:47:00 +08:00
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: userInfo,
2018-03-10 15:47:00 +08:00
}, socket);
}
2018-06-04 15:07:24 +08:00
// check if the nickname already exists in the channel
2019-11-07 15:35:23 +08:00
const userExists = server.findSockets({
2018-04-29 13:29:38 +08:00
channel: data.channel,
2019-11-07 15:35:23 +08:00
nick: (targetNick) => targetNick.toLowerCase() === userInfo.nick.toLowerCase(),
2018-04-29 13:29:38 +08:00
});
2018-03-10 15:47:00 +08:00
2018-04-29 13:29:38 +08:00
if (userExists.length > 0) {
2018-06-04 15:07:24 +08:00
// that nickname is already in that channel
return server.reply({
2018-04-29 13:29:38 +08:00
cmd: 'warn',
2019-11-07 15:35:23 +08:00
text: 'Nickname taken',
2018-04-29 13:29:38 +08:00
}, socket);
2018-03-10 15:47:00 +08:00
}
userInfo.userHash = server.getSocketHash(socket);
2018-03-10 15:47:00 +08:00
2018-06-04 15:07:24 +08:00
// prepare to notify channel peers
2019-11-07 15:35:23 +08:00
const newPeerList = server.findSockets({ channel: data.channel });
const nicks = [];
2018-06-04 15:07:24 +08:00
2019-11-07 15:35:23 +08:00
const joinAnnouncement = {
2018-03-10 15:47:00 +08:00
cmd: 'onlineAdd',
nick: userInfo.nick,
trip: userInfo.trip || 'null',
2019-11-07 15:35:23 +08:00
hash: userInfo.userHash,
level: userInfo.level,
2018-04-29 13:29:38 +08:00
};
2018-06-04 15:07:24 +08:00
// send join announcement and prep online set
2019-11-07 15:35:23 +08:00
for (let i = 0, l = newPeerList.length; i < l; i += 1) {
2018-04-29 13:29:38 +08:00
server.reply(joinAnnouncement, newPeerList[i]);
nicks.push(newPeerList[i].nick);
}
2018-03-10 15:47:00 +08:00
2018-06-04 15:07:24 +08:00
// 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;
2018-06-04 15:07:24 +08:00
2018-04-29 13:29:38 +08:00
nicks.push(socket.nick);
2018-03-10 15:47:00 +08:00
2018-06-04 15:07:24 +08:00
// reply with channel peer list
2018-03-10 15:47:00 +08:00
server.reply({
cmd: 'onlineSet',
2019-11-07 15:35:23 +08:00
nicks,
2018-03-10 15:47:00 +08:00
}, socket);
2018-06-04 15:07:24 +08:00
// stats are fun
2019-03-19 14:36:21 +08:00
core.stats.increment('users-joined');
2018-03-10 15:47:00 +08:00
2019-11-07 15:35:23 +08:00
return true;
}
export const requiredData = ['channel', 'nick'];
export const info = {
2018-05-13 18:33:22 +08:00
name: 'join',
description: 'Place calling socket into target channel with target nick & broadcast event to channel',
usage: `
2019-11-07 15:35:23 +08:00
API: { cmd: 'join', nick: '<your nickname>', channel: '<target channel>' }`,
};