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
|
|
|
*/
|
|
|
|
|
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) => {
|
|
|
|
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-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
|
|
|
|
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];
|
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';
|
|
|
|
} 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!';
|
2018-09-30 14:44:36 +08:00
|
|
|
} 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)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
2018-03-11 14:41:17 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
let userInfo = this.parseNickname(core, data);
|
|
|
|
if (typeof userInfo === 'string') {
|
|
|
|
return server.reply({
|
2018-03-10 15:47:00 +08:00
|
|
|
cmd: 'warn',
|
2018-09-30 14:44:36 +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
|
2018-04-29 13:29:38 +08:00
|
|
|
let userExists = server.findSockets({
|
|
|
|
channel: data.channel,
|
2018-09-30 14:44:36 +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',
|
|
|
|
text: 'Nickname taken'
|
|
|
|
}, 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
|
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',
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
2018-09-30 14:44:36 +08:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2018-09-30 14:44:36 +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',
|
2018-09-30 14:44:36 +08:00
|
|
|
description: 'Place calling socket into target channel with target nick & broadcast event to channel',
|
|
|
|
usage: `
|
|
|
|
API: { cmd: 'join', nick: '<your nickname>', channel: '<target channel>' }`
|
2018-06-01 03:34:39 +08:00
|
|
|
};
|