aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/commands/core
diff options
context:
space:
mode:
authormarzavec <admin@marzavec.com>2018-06-04 09:07:24 +0200
committermarzavec <admin@marzavec.com>2018-06-04 09:07:24 +0200
commite35fff59ba30e78046c9212e74fce9aef56c6e93 (patch)
treec7d3e0cd75f5a6063d629d4295952488907e9562 /server/src/commands/core
parentCompleted protocol decoupling (diff)
downloadhackchat-e35fff59ba30e78046c9212e74fce9aef56c6e93.tar.gz
hackchat-e35fff59ba30e78046c9212e74fce9aef56c6e93.zip
cleaned up and commented modules
Diffstat (limited to 'server/src/commands/core')
-rw-r--r--server/src/commands/core/changenick.js19
-rw-r--r--server/src/commands/core/chat.js8
-rw-r--r--server/src/commands/core/help.js6
-rw-r--r--server/src/commands/core/invite.js24
-rw-r--r--server/src/commands/core/join.js34
-rw-r--r--server/src/commands/core/morestats.js11
-rw-r--r--server/src/commands/core/move.js15
-rw-r--r--server/src/commands/core/stats.js5
8 files changed, 83 insertions, 39 deletions
diff --git a/server/src/commands/core/changenick.js b/server/src/commands/core/changenick.js
index 4041bb0..5a8b5c2 100644
--- a/server/src/commands/core/changenick.js
+++ b/server/src/commands/core/changenick.js
@@ -2,9 +2,7 @@
Description: Generates a semi-unique channel name then broadcasts it to each client
*/
-const verifyNickname = (nick) => {
- return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
-};
+const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
exports.run = async (core, server, socket, data) => {
if (server._police.frisk(socket.remoteAddress, 6)) {
@@ -16,12 +14,13 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // verify user data is string
if (typeof data.nick !== 'string') {
return;
}
+ // make sure requested nickname meets standards
let newNick = data.nick.trim();
-
if (!verifyNickname(newNick)) {
server.reply({
cmd: 'warn',
@@ -31,6 +30,8 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // prevent admin impersonation
+ // TODO: prevent mod impersonation
if (newNick.toLowerCase() == core.config.adminName.toLowerCase()) {
server._police.frisk(socket.remoteAddress, 4);
@@ -42,11 +43,13 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // find any sockets that have the same nickname
let userExists = server.findSockets({
channel: socket.channel,
nick: (targetNick) => targetNick.toLowerCase() === newNick.toLowerCase()
});
+ // return error if found
if (userExists.length > 0) {
// That nickname is already in that channel
server.reply({
@@ -57,7 +60,7 @@ exports.run = async (core, server, socket, data) => {
return;
}
- let peerList = server.findSockets({ channel: socket.channel });
+ // build join and leave notices
let leaveNotice = {
cmd: 'onlineRemove',
nick: socket.nick
@@ -66,16 +69,20 @@ exports.run = async (core, server, socket, data) => {
cmd: 'onlineAdd',
nick: newNick,
trip: socket.trip || 'null',
- hash: server.getSocketHash(socket)
+ hash: socket.hash
};
+ // broadcast remove event and join event with new name, this is to support legacy clients and bots
server.broadcast( leaveNotice, { channel: socket.channel });
server.broadcast( joinNotice, { channel: socket.channel });
+
+ // notify channel that the user has changed their name
server.broadcast( {
cmd: 'info',
text: `${socket.nick} is now ${newNick}`
}, { channel: socket.channel });
+ // commit change to nickname
socket.nick = newNick;
};
diff --git a/server/src/commands/core/chat.js b/server/src/commands/core/chat.js
index 168c0bb..89d497e 100644
--- a/server/src/commands/core/chat.js
+++ b/server/src/commands/core/chat.js
@@ -3,6 +3,7 @@
*/
const parseText = (text) => {
+ // verifies user input is text
if (typeof text !== 'string') {
return false;
}
@@ -16,12 +17,16 @@ const parseText = (text) => {
};
exports.run = async (core, server, socket, data) => {
+ // check user input
let text = parseText(data.text);
if (!text) {
// lets not send objects or empty text, yea?
+ server._police.frisk(socket.remoteAddress, 6);
+
return;
}
+ // check for spam
let score = text.length / 83 / 4;
if (server._police.frisk(socket.remoteAddress, score)) {
server.reply({
@@ -32,6 +37,7 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // build chat payload
let payload = {
cmd: 'chat',
nick: socket.nick,
@@ -48,6 +54,7 @@ exports.run = async (core, server, socket, data) => {
payload.trip = socket.trip;
}
+ // check if the users connection is muted
// TODO: Add a more contained way for modules to interact, event hooks or something?
if(core.muzzledHashes && core.muzzledHashes[socket.hash]){
server.broadcast( payload, { channel: socket.channel, hash: socket.hash });
@@ -59,6 +66,7 @@ exports.run = async (core, server, socket, data) => {
server.broadcast( payload, { channel: socket.channel});
}
+ // stats are fun
core.managers.stats.increment('messages-sent');
};
diff --git a/server/src/commands/core/help.js b/server/src/commands/core/help.js
index 7f63d3d..8fd02a1 100644
--- a/server/src/commands/core/help.js
+++ b/server/src/commands/core/help.js
@@ -5,6 +5,8 @@
const stripIndents = require('common-tags').stripIndents;
exports.run = async (core, server, socket, data) => {
+ // TODO: this module needs to be clean up badly :(
+
// verify passed arguments
let typeDt = typeof data.type;
let catDt = typeof data.category;
@@ -23,6 +25,7 @@ exports.run = async (core, server, socket, data) => {
Show all commands in category -> { cmd: 'help', category: '<category name>' }
Show specific command -> { cmd: 'help', command: '<command name>' }`;
+ // change reply based on query
if (typeDt !== 'undefined') {
let categories = core.commands.categories().sort();
reply = `Command Categories:\n${categories.map(c => `- ${c.replace('../src/commands/', '')}`).join('\n')}`;
@@ -36,6 +39,7 @@ exports.run = async (core, server, socket, data) => {
Description: ${command.info.description || '¯\_(ツ)_/¯'}`;
}
+ // output reply
server.reply({
cmd: 'info',
text: reply
@@ -46,4 +50,4 @@ exports.info = {
name: 'help',
usage: 'help ([ type:categories] | [category:<category name> | command:<command name> ])',
description: 'Outputs information about the servers current protocol'
-}; \ No newline at end of file
+};
diff --git a/server/src/commands/core/invite.js b/server/src/commands/core/invite.js
index bcf9097..d616193 100644
--- a/server/src/commands/core/invite.js
+++ b/server/src/commands/core/invite.js
@@ -2,11 +2,10 @@
Description: Generates a semi-unique channel name then broadcasts it to each client
*/
-const verifyNickname = (nick) => {
- return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
-};
+const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
exports.run = async (core, server, socket, data) => {
+ // check for spam
if (server._police.frisk(socket.remoteAddress, 2)) {
server.reply({
cmd: 'warn',
@@ -16,22 +15,20 @@ exports.run = async (core, server, socket, data) => {
return;
}
- if (typeof data.nick !== 'string') {
- return;
- }
-
- if (!verifyNickname(data.nick)) {
- // Not a valid nickname? Chances are we won't find them
+ // verify user input
+ if (typeof data.nick !== 'string' || !verifyNickname(data.nick)) {
return;
}
+ // why would you invite yourself?
if (data.nick == socket.nick) {
- // They invited themself
return;
}
-
+
+ // generate common channel
let channel = Math.random().toString(36).substr(2, 8);
+ // build and send invite
let payload = {
cmd: 'info',
invite: channel,
@@ -39,6 +36,7 @@ exports.run = async (core, server, socket, data) => {
};
let inviteSent = server.broadcast( payload, { channel: socket.channel, nick: data.nick });
+ // server indicates the user was not found
if (!inviteSent) {
server.reply({
cmd: 'warn',
@@ -48,11 +46,13 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // reply with common channel
server.reply({
cmd: 'info',
text: `You invited ${data.nick} to ?${channel}`
}, socket);
+ // stats are fun
core.managers.stats.increment('invites-sent');
};
@@ -62,4 +62,4 @@ exports.info = {
name: 'invite',
usage: 'invite {nick}',
description: 'Generates a unique (more or less) room name and passes it to two clients'
-}; \ No newline at end of file
+};
diff --git a/server/src/commands/core/join.js b/server/src/commands/core/join.js
index 4875b0e..31458a2 100644
--- a/server/src/commands/core/join.js
+++ b/server/src/commands/core/join.js
@@ -10,11 +10,10 @@ const hash = (password) => {
return sha.digest('base64').substr(0, 6);
};
-const verifyNickname = (nick) => {
- return /^[a-zA-Z0-9_]{1,24}$/.test(nick);
-};
+const verifyNickname = (nick) => /^[a-zA-Z0-9_]{1,24}$/.test(nick);
exports.run = async (core, server, socket, data) => {
+ // check for spam
if (server._police.frisk(socket.remoteAddress, 3)) {
server.reply({
cmd: 'warn',
@@ -24,22 +23,23 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // calling socket already in a channel
if (typeof socket.channel !== 'undefined') {
- // Calling socket already in a channel
return;
}
+ // check user input
if (typeof data.channel !== 'string' || typeof data.nick !== 'string') {
return;
}
let channel = data.channel.trim();
if (!channel) {
- // Must join a non-blank channel
+ // must join a non-blank channel
return;
}
- // Process nickname
+ // process nickname
let nick = data.nick;
let nickArray = nick.split('#', 2);
nick = nickArray[0].trim();
@@ -53,13 +53,14 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // check if the nickname already exists in the channel
let userExists = server.findSockets({
channel: data.channel,
nick: (targetNick) => targetNick.toLowerCase() === nick.toLowerCase()
});
if (userExists.length > 0) {
- // That nickname is already in that channel
+ // that nickname is already in that channel
server.reply({
cmd: 'warn',
text: 'Nickname taken'
@@ -68,7 +69,7 @@ exports.run = async (core, server, socket, data) => {
return;
}
- // TODO: Should we check for mod status first to prevent overwriting of admin status somehow? Meh, w/e, cba.
+ // TODO: should we check for mod status first to prevent overwriting of admin status somehow? Meh, w/e, cba.
let uType = 'user';
let trip = null;
let password = nickArray[1];
@@ -90,40 +91,47 @@ exports.run = async (core, server, socket, data) => {
trip = hash(password + core.config.tripSalt);
}
- // TODO: Disallow moderator impersonation
+ // TODO: disallow moderator impersonation
for (let mod of core.config.mods) {
if (trip === mod.trip) {
uType = 'mod';
}
}
- // Reply with online user list
+ // prepare to notify channel peers
let newPeerList = server.findSockets({ channel: data.channel });
+ let userHash = server.getSocketHash(socket);
+ let nicks = [];
+
let joinAnnouncement = {
cmd: 'onlineAdd',
nick: nick,
trip: trip || 'null',
- hash: server.getSocketHash(socket)
+ hash: userHash
};
- let nicks = [];
+ // send join announcement and prep online set
for (let i = 0, l = newPeerList.length; i < l; i++) {
server.reply(joinAnnouncement, newPeerList[i]);
nicks.push(newPeerList[i].nick);
}
+ // store user info
socket.uType = uType;
socket.nick = nick;
socket.channel = channel;
- socket.hash = server.getSocketHash(socket);
+ socket.hash = userHash;
if (trip !== null) socket.trip = trip;
+
nicks.push(socket.nick);
+ // reply with channel peer list
server.reply({
cmd: 'onlineSet',
nicks: nicks
}, socket);
+ // stats are fun
core.managers.stats.increment('users-joined');
};
diff --git a/server/src/commands/core/morestats.js b/server/src/commands/core/morestats.js
index 5510cb1..b64d478 100644
--- a/server/src/commands/core/morestats.js
+++ b/server/src/commands/core/morestats.js
@@ -12,10 +12,15 @@ const formatTime = (time) => {
let hours = Math.floor(minutes / 60);
minutes = minutes % 60;
- return `${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`;
+
+ let days = Math.floor(hours / 24);
+ hours = hours % 24;
+
+ return `${days.toFixed(0)}d ${hours.toFixed(0)}h ${minutes.toFixed(0)}m ${seconds.toFixed(0)}s`;
};
exports.run = async (core, server, socket, data) => {
+ // gather connection and channel count
let ips = {};
let channels = {};
for (let client of server.clients) {
@@ -31,6 +36,7 @@ exports.run = async (core, server, socket, data) => {
ips = null;
channels = null;
+ // dispatch info
server.reply({
cmd: 'info',
text: stripIndents`current-connections: ${uniqueClientCount}
@@ -44,10 +50,11 @@ exports.run = async (core, server, socket, data) => {
server-uptime: ${formatTime(process.hrtime(core.managers.stats.get('start-time')))}`
}, socket);
+ // stats are fun
core.managers.stats.increment('stats-requested');
};
exports.info = {
name: 'morestats',
description: 'Sends back current server stats to the calling client'
-}; \ No newline at end of file
+};
diff --git a/server/src/commands/core/move.js b/server/src/commands/core/move.js
index c5efafd..284a38d 100644
--- a/server/src/commands/core/move.js
+++ b/server/src/commands/core/move.js
@@ -1,8 +1,9 @@
/*
- Description: Generates a semi-unique channel name then broadcasts it to each client
+ Description: Changes the current channel of the calling socket
*/
exports.run = async (core, server, socket, data) => {
+ // check for spam
if (server._police.frisk(socket.remoteAddress, 6)) {
server.reply({
cmd: 'warn',
@@ -12,15 +13,17 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // check user input
if (typeof data.channel !== 'string') {
return;
}
if (data.channel === socket.channel) {
- // They are trying to rejoin the channel
+ // they are trying to rejoin the channel
return;
}
+ // check that the nickname isn't already in target channel
const currentNick = socket.nick.toLowerCase();
let userExists = server.findSockets({
channel: data.channel,
@@ -32,6 +35,7 @@ exports.run = async (core, server, socket, data) => {
return;
}
+ // broadcast leave notice to peers
let peerList = server.findSockets({ channel: socket.channel });
if (peerList.length > 1) {
@@ -50,12 +54,13 @@ exports.run = async (core, server, socket, data) => {
}
}
+ // broadcast join notice to new peers
let newPeerList = server.findSockets({ channel: data.channel });
let moveAnnouncement = {
cmd: 'onlineAdd',
nick: socket.nick,
trip: socket.trip || 'null',
- hash: server.getSocketHash(socket)
+ hash: socket.hash
};
let nicks = [];
@@ -66,11 +71,13 @@ exports.run = async (core, server, socket, data) => {
nicks.push(socket.nick);
+ // reply with new user list
server.reply({
cmd: 'onlineSet',
nicks: nicks
}, socket);
+ // commit change
socket.channel = data.channel;
};
@@ -80,4 +87,4 @@ exports.info = {
name: 'move',
usage: 'move {channel}',
description: 'This will change the current channel to the new one provided'
-}; \ No newline at end of file
+};
diff --git a/server/src/commands/core/stats.js b/server/src/commands/core/stats.js
index b9dc002..74bd4a5 100644
--- a/server/src/commands/core/stats.js
+++ b/server/src/commands/core/stats.js
@@ -3,6 +3,7 @@
*/
exports.run = async (core, server, socket, data) => {
+ // gather connection and channel count
let ips = {};
let channels = {};
for (let client of server.clients) {
@@ -18,15 +19,17 @@ exports.run = async (core, server, socket, data) => {
ips = null;
channels = null;
+ // dispatch info
server.reply({
cmd: 'info',
text: `${uniqueClientCount} unique IPs in ${uniqueChannels} channels`
}, socket);
+ // stats are fun
core.managers.stats.increment('stats-requested');
};
exports.info = {
name: 'stats',
description: 'Sends back legacy server stats to the calling client'
-}; \ No newline at end of file
+};