2018-03-10 15:47:00 +08:00
|
|
|
/*
|
2018-03-14 13:26:53 +08:00
|
|
|
Description: Initial entry point, applies `channel` and `nick` to the calling socket
|
2018-03-10 15:47:00 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-07 01:00:30 +08:00
|
|
|
import * as UAC from '../utility/UAC/_info';
|
2020-03-06 00:48:58 +08:00
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// module support functions
|
2018-03-10 15:47:00 +08:00
|
|
|
const crypto = require('crypto');
|
|
|
|
|
2018-03-14 13:26:53 +08:00
|
|
|
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-14 13:26:53 +08:00
|
|
|
};
|
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
|
|
|
|
2018-09-30 14:44:36 +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 = {
|
2018-09-30 14:44:36 +08:00
|
|
|
nick: '',
|
|
|
|
uType: 'user',
|
|
|
|
trip: null,
|
2020-03-07 01:00:30 +08:00
|
|
|
level: UAC.levels.default,
|
2018-09-30 14:44:36 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// seperate nick from password
|
2019-11-07 15:35:23 +08:00
|
|
|
const nickArray = data.nick.split('#', 2);
|
2018-09-30 14:44:36 +08:00
|
|
|
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];
|
2019-02-03 05:34:06 +08:00
|
|
|
|
2018-10-01 03:41:28 +08:00
|
|
|
if (hash(password + core.config.tripSalt) === core.config.adminTrip) {
|
|
|
|
userInfo.uType = 'admin';
|
|
|
|
userInfo.trip = 'Admin';
|
2020-03-06 00:48:58 +08:00
|
|
|
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
|
2018-10-01 03:41:28 +08:00
|
|
|
return 'You are not the admin, liar!';
|
2018-09-30 14:44:36 +08:00
|
|
|
} 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) => {
|
2018-09-30 14:44:36 +08:00
|
|
|
if (userInfo.trip === mod.trip) {
|
|
|
|
userInfo.uType = 'mod';
|
2020-03-06 00:48:58 +08:00
|
|
|
userInfo.level = UAC.levels.moderator;
|
2018-09-30 14:44:36 +08:00
|
|
|
}
|
2019-11-07 15:35:23 +08:00
|
|
|
});
|
2018-09-30 14:44:36 +08:00
|
|
|
|
|
|
|
return userInfo;
|
2019-11-07 15:35:23 +08:00
|
|
|
}
|
2018-09-30 14:44:36 +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)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
2018-03-11 14:41:17 +08:00
|
|
|
if (typeof data.channel !== 'string' || typeof data.nick !== 'string') {
|
2019-11-07 15:35:23 +08:00
|
|
|
return true;
|
2018-03-11 14:41:17 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +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',
|
2018-09-30 14:44:36 +08:00
|
|
|
nick: userInfo.nick,
|
|
|
|
trip: userInfo.trip || 'null',
|
2019-11-07 15:35:23 +08:00
|
|
|
hash: userInfo.userHash,
|
2020-03-06 00:48:58 +08:00
|
|
|
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
|
2018-09-30 14:44:36 +08:00
|
|
|
socket.uType = userInfo.uType;
|
|
|
|
socket.nick = userInfo.nick;
|
|
|
|
socket.channel = data.channel;
|
|
|
|
socket.hash = userInfo.userHash;
|
2020-03-06 00:48:58 +08:00
|
|
|
socket.level = userInfo.level;
|
2018-09-30 14:44:36 +08:00
|
|
|
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',
|
2018-09-30 14:44:36 +08:00
|
|
|
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>' }`,
|
2018-06-01 03:34:39 +08:00
|
|
|
};
|