summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--README.md9
-rw-r--r--commands.js8
-rw-r--r--commands/motd.js7
-rw-r--r--config.json2
-rw-r--r--index.js18
-rw-r--r--package.json2
6 files changed, 42 insertions, 4 deletions
diff --git a/README.md b/README.md
index 3d5fd77..bc14503 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,12 @@ node index.js
 
 - ~~Make commands be in an object instead of a switch statement~~
 - ~~/help command~~
-- /about command
+- ~~/about command~~
 - ~~Config (JSON)~~
 - ~~MOTD~~
-- Custom commands (plugins)
+- ~~Custom commands (plugins)~~
+- Auth (optional)
+
+## IN PROGRESS
+
+- JSON support (for clients and stuff)
diff --git a/commands.js b/commands.js
index 8aa80a3..5c7a440 100644
--- a/commands.js
+++ b/commands.js
@@ -10,6 +10,7 @@ export const commands = {
             if(!server.channels.includes(args[0].replace("#", ""))) return user.socket.send("Error: Channel not found, run /channels to see a list of channels.");
             sendInChannel(`${user.username} left #${user.channel}.`, user.channel)
             user.channel = args[0].replace("#", "");
+            console.info(`${user.username} went to #${user.channel}`)
             sendInChannel(`${user.username} joined #${user.channel}!`, user.channel)
         }
     },
@@ -68,4 +69,11 @@ export const commands = {
 
 export function register(cmd, data) {
     commands[cmd] = data
+}
+
+let commandFiles = fs.readdirSync("commands").filter(filename => filename.endsWith(".js")).map(file => file.replace(/\.js$/gmi, ''))
+for (let i = 0; i < commandFiles.length; i++) {
+    const cmdName = commandFiles[i];
+    const cmd = (await import(`./commands/${cmdName}.js`)).default
+    register(cmdName, cmd)
 }
\ No newline at end of file
diff --git a/commands/motd.js b/commands/motd.js
new file mode 100644
index 0000000..740cb07
--- /dev/null
+++ b/commands/motd.js
@@ -0,0 +1,7 @@
+export default {
+    name: 'motd',
+    aliases: [],
+    command: function({user, server}) {
+        user.socket.send("MOTD: " + server.format(server.config.motd));
+    }
+}
\ No newline at end of file
diff --git a/config.json b/config.json
index 9524704..6d94004 100644
--- a/config.json
+++ b/config.json
@@ -1,6 +1,6 @@
 {
     "name": "wsChat server",
-    "motd": "Welcome to $(serverName)$",
+    "motd": "Welcome to $(serverName)$\nRun /help to see a list of commands",
     "max": 20,
     "fullMessage": "Sorry, but the server is full right now, come back later"
 }
\ No newline at end of file
diff --git a/index.js b/index.js
index ee03570..c17c134 100644
--- a/index.js
+++ b/index.js
@@ -29,6 +29,8 @@ function format(txt) {
     return txt
 }
 
+server.format = format
+
 ws.on('connection', (socket, request) => {
     if (server.config.max && Object.keys(server.users).length >= server.config.max) {
         socket.send(format(server.config.fullMessage ?? "Sorry, but the server is full right now, come back later"))
@@ -48,6 +50,7 @@ ws.on('connection', (socket, request) => {
         },
         channel: 'home'
     }
+    console.info(`${server.users[userID].username} joined the server!`)
     sendInChannel(`${server.users[userID].username} joined #${server.users[userID].channel}!`, server.users[userID].channel)
     socket.on('close', function (code, reason) {
         sendInChannel(`${server.users[userID].username} left.`, server.users[userID].channel)
@@ -76,6 +79,21 @@ ws.on('connection', (socket, request) => {
             server.users[userID].client = client;
             return
         }
+        if (rawData.toString().startsWith(":jsonGet")) {
+            let params = String(rawData).replace(":jsonGet", "").split(" ");
+            switch (params[0]) {
+                case 'channels':
+                    socket.send(JSON.stringify(server.channels));
+                    break;
+                case 'users':
+                    socket.send(JSON.stringify(server.users));
+                    break;
+            
+                default:
+                    break;
+            }
+            return
+        }
         if (rawData.length < 1) return socket.send("Error: message too short!")
         if (rawData.length >= 2000) return socket.send("Error: message too long!")
         sendInChannel(`<${server.users[userID].username}> ${rawData}`, server.users[userID].channel)
diff --git a/package.json b/package.json
index 1c6f0b2..94d14c0 100644
--- a/package.json
+++ b/package.json
@@ -1,7 +1,7 @@
 {
   "name": "wschat",
   "description": "A simple IRC-like chat made using nodejs and webSockets",
-  "version": "1.1.0",
+  "version": "1.2.0",
   "author": {
     "name": "WlodekM",
     "email": "[email protected]",