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

149 lines
3.8 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
*/
// module support functions
2018-03-10 15:47:00 +08:00
const crypto = require('crypto');
const hash = (password) => {
let 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
exports.parseNickname = (core, data) => {
let userInfo = {
nick: '',
uType: 'user',
trip: null,
};
// seperate nick from password
let 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';
}
let password = nickArray[1];
if (hash(password + core.config.tripSalt) === core.config.adminTrip) {
userInfo.uType = 'admin';
userInfo.trip = '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 (let mod of core.config.mods) {
if (userInfo.trip === mod.trip) {
userInfo.uType = 'mod';
}
}
return userInfo;
};
// module main
2018-03-10 15:47:00 +08:00
exports.run = async (core, server, socket, data) => {
2018-06-04 15:07:24 +08:00
// check for spam
2019-04-08 08:04:10 +08:00
if (server.police.frisk(socket.remoteAddress, 3)) {
return server.reply({
2018-03-10 15:47:00 +08:00
cmd: 'warn',
text: 'You are joining channels too fast. Wait a moment and try again.'
}, 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') {
return;
}
2018-06-04 15:07:24 +08:00
// check user input
if (typeof data.channel !== 'string' || typeof data.nick !== 'string') {
return;
}
let 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
2018-03-10 15:47:00 +08:00
return;
}
let userInfo = this.parseNickname(core, data);
if (typeof userInfo === 'string') {
return server.reply({
2018-03-10 15:47:00 +08:00
cmd: 'warn',
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
2018-04-29 13:29:38 +08:00
let userExists = server.findSockets({
channel: data.channel,
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',
text: 'Nickname taken'
}, 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
2018-04-29 13:29:38 +08:00
let newPeerList = server.findSockets({ channel: data.channel });
2018-06-04 15:07:24 +08:00
let nicks = [];
2018-04-29 13:29:38 +08:00
let joinAnnouncement = {
2018-03-10 15:47:00 +08:00
cmd: 'onlineAdd',
nick: userInfo.nick,
trip: userInfo.trip || 'null',
hash: userInfo.userHash
2018-04-29 13:29:38 +08:00
};
2018-06-04 15:07:24 +08:00
// send join announcement and prep online set
2018-04-29 13:29:38 +08:00
for (let i = 0, l = newPeerList.length; i < l; i++) {
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;
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',
nicks: nicks
}, 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
};
// module meta
2018-03-10 15:47:00 +08:00
exports.requiredData = ['channel', 'nick'];
2018-05-13 18:33:22 +08:00
exports.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>' }`
};