From 98b4e68285feacd583453d88dca945589e0ccb36 Mon Sep 17 00:00:00 2001 From: Evert Date: Mon, 26 Sep 2016 18:41:48 +0300 Subject: [PATCH] better command handlig --- public/css/main.css | 4 + public/js/main.js | 243 ++++++++++++++++++++++++++++---------------- public/main.styl | 3 + 3 files changed, 162 insertions(+), 88 deletions(-) diff --git a/public/css/main.css b/public/css/main.css index 0657c3b..7ea4cfc 100644 --- a/public/css/main.css +++ b/public/css/main.css @@ -486,6 +486,10 @@ body { color: #f00; font-weight: bold; } +.message.m_help .content { + color: #008000; + font-weight: bold; +} .message .irc-bg00, .topicbar .irc-bg00, .message .irc-bg0, diff --git a/public/js/main.js b/public/js/main.js index a2f2d8e..0325deb 100644 --- a/public/js/main.js +++ b/public/js/main.js @@ -63,6 +63,10 @@ window.validators.nickname = function(str) { return false; } +window.validators.escapeHTML = function(str) { + return str.replace(/\/, '>'); +} + Date.prototype.format = function (format, utc){ var date = this; var MMMM = ["\x00", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; @@ -147,7 +151,7 @@ irc.whoisMessage = function(whoisData, buffer) { for(let key in whoisData) { switch(key) { case "hostmask": - messages.push(""+whoisData[key]+": "+whoisData['realname']); + messages.push(""+whoisData[key]+": "+validators.escapeHTML(whoisData['realname'])); break; case "idleSeconds": let msgs = "is idle for "+whoisData[key]+" seconds"; @@ -160,7 +164,7 @@ irc.whoisMessage = function(whoisData, buffer) { case "connectingFrom": case "usingModes": case "title": - messages.push(whoisData[key]); + messages.push(validators.escapeHTML(whoisData[key])); break; case "channels": messages.push(whoisData[key].join(" ")); @@ -168,7 +172,7 @@ irc.whoisMessage = function(whoisData, buffer) { case "server": let adfd = "is on "+whoisData[key]+""; if(whoisData['server_name']) - adfd += " "+whoisData['server_name']+""; + adfd += " "+validators.escapeHTML(whoisData['server_name'])+""; messages.push(adfd); break; case "secure": @@ -240,7 +244,7 @@ function linkify(text) { if (url.indexOf('http') !== 0) { href = 'http://' + url; } - return '' + url + ''; + return '' + url + ''; }); return parsed; } @@ -337,6 +341,125 @@ let composer = { } } +/*****************************\ +|** **| +|** CLIENT COMMANDS **| +|** **| +\*****************************/ + +// commandName: {execute: function(buffer, handler, command, message, listargs) {}, description: ""} +let commands = { + join: {execute: function(buffer, handler, command, message, listargs) { + if (!listargs[1]) { + if(!buffer.alive) { + irc.socket.emit("userinput", {command: "join", server: buffer.server, message: "", arguments: [buffer.name]}); + } else { + handler.commandError(buffer, listargs[0].toUpperCase()+': Missing parameters!'); + } + } else { + irc.socket.emit("userinput", {command: "join", server: buffer.server, message: "", arguments: [listargs[1]]}); + } + }, description: " - Join a channel"}, + + part: {execute: function(buffer, handler, command, message, listargs) { + if (!listargs[1] && buffer.type == "channel") { + irc.socket.emit("userinput", {command: "part", server: buffer.server, message: "", arguments: [buffer.name]}); + } else if(buffer.type != "channel") { + handler.commandError(buffer, listargs[0].toUpperCase()+': Buffer is not a channel.'); + } else if(listargs[1]) { + if(listargs[1].indexOf('#')) { + let msg = ""; + if(listargs[2]) + msg = listargs.slice(2).join(" "); + irc.socket.emit("userinput", {command: "part", server: buffer.server, message: msg, arguments: [listargs[1]]}); + } else { + if(buffer.type == "channel") { + irc.socket.emit("userinput", {command: "part", server: buffer.server, message: listargs.slice(1).join(" "), arguments: [buffer.name]}); + } else { + handler.commandError(buffer, listargs[0].toUpperCase()+': Buffer is not a channel.'); + } + } + } + }, description: "[<#channel>|] [message] - Leave the channel. If no channel specified, leave the current buffer.", aliases: ['leave']}, + + quit: {execute: function(buffer, handler, command, message, listargs) { + irc.socket.emit("userinput", {command: "quit", server: buffer.server, message: listargs.slice(1).join(" "), arguments: []}); + }, description: "[] - Quit the current server with message.", aliases: ['exit']}, + + privmsg: {execute: function(buffer, handler, command, message, listargs) { + if(!listargs[1] || !listargs[2]) + return handler.commandError(buffer, listargs[0].toUpperCase()+': Missing parameters!'); + if(listargs[1] == '*') + listargs[1] = buffer.name; + irc.socket.emit("userinput", {command: "privmsg", server: buffer.server, message: listargs.slice(2).join(" "), arguments: [listargs[1]]}); + }, description: " - Sends a message to target.", aliases: ['msg', 'q', 'query', 'say']}, + + notice: {execute: function(buffer, handler, command, message, listargs) { + if(!listargs[1] || !listargs[2]) + return handler.commandError(buffer, listargs[0].toUpperCase()+': Missing parameters!'); + if(listargs[1] == '*') + listargs[1] = buffer.name; + irc.socket.emit("userinput", {command: "notice", server: buffer.server, message: listargs.slice(2).join(" "), arguments: [listargs[1]]}); + }, description: " - Sends a NOTICE to target."}, + + action: {execute: function(buffer, handler, command, message, listargs) { + irc.socket.emit("userinput", {command: "privmsg", server: buffer.server, message: "\x01ACTION "+message.substring(command.length+2)+"\x01", arguments: [buffer.name]}); + }, description: " - 'act' as yourself"}, + + list: {execute: function(buffer, handler, command, message, listargs) { + irc.socket.emit("userinput", {command: "list", server: buffer.server, message: "", arguments: listargs.splice(1)}); + }, description: "- List all channels on the current server."}, + + quote: {execute: function(buffer, handler, command, message, listargs) { + irc.socket.emit("userinput", {command: listargs[1], server: buffer.server, message: listargs.slice(2).join(" "), arguments: listargs.splice(2)}); + }, description: " [args] - Send a raw command to the server.", aliases: ['raw']}, + + whois: {execute: function(buffer, handler, command, message, listargs) { + if(!listargs[1]) + return handler.commandError(buffer, listargs[0].toUpperCase()+': Missing parameters!'); + + irc.socket.emit("userinput", {command: "whois", server: buffer.server, message: "", arguments: [listargs[1]]}); + }, description: " - Display information about a user."}, + + connect: {execute: function(buffer, handler, command, message, listargs) { + clientdom.connector.frame.style.display = "block"; + irc.auther.authMessage("Create a new connection", false); + irc.auther.canClose = true; + }, description: "- Create a new connection."}, + + help: {execute: function(buffer, handler, command, message, listargs) { + if(!listargs[1]) + return handler.commandError(buffer, listargs[0].toUpperCase()+': Missing parameters!'); + + let cmd = listargs[1].toLowerCase(); + if(cmd.indexOf('/') === 0) + cmd = cmd.substring(1); + + if(cmd in commands) { + if("description" in commands[cmd]) + buffer.addMessage("/"+cmd.toUpperCase()+" "+ + validators.escapeHTML(commands[cmd].description), null, "help"); + else + buffer.addMessage("/"+cmd.toUpperCase()+" - No description provided", null, "help"); + } else { + let foundAliased = null; + for(let cmd2 in commands) { + if(!commands[cmd2]['aliases']) continue; + if(commands[cmd2].aliases.indexOf(cmd) != -1) foundAliased = cmd2; + } + if(foundAliased) { + if("description" in commands[foundAliased]) + buffer.addMessage("/"+cmd.toUpperCase()+" "+ + validators.escapeHTML(commands[foundAliased].description), null, "help"); + else + buffer.addMessage("/"+cmd.toUpperCase()+" - No description provided", null, "help"); + } else { + handler.commandError(buffer, '/'+cmd.toUpperCase()+': Unknown command!'); + } + } + }, description: " - Display help for command"} +} + /*********************\ |** **| |** CLASSES **| @@ -1123,98 +1246,37 @@ class InputHandler { } handleInput() { - let inp = clientdom.input.value; - let buf = irc.chat.getActiveBuffer(); + let message = clientdom.input.value; + let buffer = irc.chat.getActiveBuffer(); - if(!buf) return; - if(inp.trim() == "") return; + if(!buffer) return; + if(message.trim() == "") return; - let listargs = inp.split(' '); + let listargs = message.split(' '); if(listargs[0].indexOf('/') == 0) { - let cmd = listargs[0].substring(1).toLowerCase(); - switch(cmd) { - case "join": - if (!listargs[1]) { - if(!buf.alive) { - irc.socket.emit("userinput", {command: "join", server: buf.server, message: "", arguments: [buf.name]}); - } else { - this.commandError(buf, listargs[0].toUpperCase()+': Missing parameters!'); - } - } else { - irc.socket.emit("userinput", {command: "join", server: buf.server, message: "", arguments: [listargs[1]]}); - } - break; - case "part": - if (!listargs[1] && buf.type == "channel") { - irc.socket.emit("userinput", {command: "part", server: buf.server, message: "", arguments: [buf.name]}); - } else if(buf.type != "channel") { - this.commandError(buf, listargs[0].toUpperCase()+': Buffer is not a channel.'); - } else if(listargs[1]) { - if(listargs[1].indexOf('#')) { - let msg = ""; - if(listargs[2]) - msg = listargs.slice(2).join(" "); - irc.socket.emit("userinput", {command: "part", server: buf.server, message: msg, arguments: [listargs[1]]}); - } else { - if(buf.type == "channel") { - irc.socket.emit("userinput", {command: "part", server: buf.server, message: listargs.slice(1).join(" "), arguments: [buf.name]}); - } else { - this.commandError(buf, listargs[0].toUpperCase()+': Buffer is not a channel.'); - } - } - } - break; - case "quit": - irc.socket.emit("userinput", {command: "quit", server: buf.server, message: listargs.slice(1).join(" "), arguments: []}); - break; - case "msg": - case "privmsg": - case "say": - if(!listargs[1] || !listargs[2]) - return this.commandError(buf, listargs[0].toUpperCase()+': Missing parameters!'); - if(listargs[1] == '*') - listargs[1] = buf.name; - irc.socket.emit("userinput", {command: "privmsg", server: buf.server, message: listargs.slice(2).join(" "), arguments: [listargs[1]]}); - break; - case "notice": - if(!listargs[1] || !listargs[2]) - return this.commandError(buf, listargs[0].toUpperCase()+': Missing parameters!'); - if(listargs[1] == '*') - listargs[1] = buf.name; - irc.socket.emit("userinput", {command: "notice", server: buf.server, message: listargs.slice(2).join(" "), arguments: [listargs[1]]}); - break; - case "me": - case "action": - irc.socket.emit("userinput", {command: "privmsg", server: buf.server, message: "\x01ACTION "+inp.substring(cmd.length+2)+"\x01", arguments: [buf.name]}); - break; - case "nick": - case "list": - irc.socket.emit("userinput", {command: cmd, server: buf.server, message: "", arguments: listargs.splice(1)}); - break; - case "quote": - case "raw": - irc.socket.emit("userinput", {command: listargs[1], server: buf.server, message: listargs.slice(2).join(" "), arguments: listargs.splice(2)}); - break; - case "whois": - if(!listargs[1]) - return this.commandError(buf, listargs[0].toUpperCase()+': Missing parameters!'); - - irc.socket.emit("userinput", {command: "whois", server: buf.server, message: "", arguments: [listargs[1]]}); - break; - case "connect": - clientdom.connector.frame.style.display = "block"; - irc.auther.authMessage("Create a new connection", false); - irc.auther.canClose = true; - break; - default: - this.commandError(buf, listargs[0].toUpperCase()+': Unknown command!'); + let command = listargs[0].substring(1).toLowerCase(); + if(command.toLowerCase() in commands) { + let cmd = commands[command]; + if("execute" in cmd) + cmd.execute(buffer, this, command, message, listargs); + } else { + let foundAliased = null; + for(let cmd in commands) { + if(!commands[cmd]['aliases']) continue; + if(commands[cmd].aliases.indexOf(command) != -1) foundAliased = commands[cmd]; + } + if(foundAliased) + foundAliased.execute(buffer, this, command, message, listargs); + else + this.commandError(buffer, listargs[0].toUpperCase()+': Unknown command!'); } + } else { - irc.socket.emit("userinput", {command: "privmsg", server: buf.server, message: inp, arguments: [buf.name]}); + irc.socket.emit("userinput", {command: "privmsg", server: buffer.server, message: message, arguments: [buffer.name]}); } - this.history.push(inp); + this.history.push(message); this.historyCaret = this.history.length; clientdom.input.value = ""; } @@ -1654,6 +1716,11 @@ window.onload = function() { // Does everything irc.socket.on('act_client', function (data) { + if(data['message']) + data.message = validators.escapeHTML(data.message); + if(data['reason']) + data.reason = validators.escapeHTML(data.reason); + switch(data.type) { case "event_connect": irc.auther.authComplete(); diff --git a/public/main.styl b/public/main.styl index 101e776..a507c71 100644 --- a/public/main.styl +++ b/public/main.styl @@ -406,6 +406,9 @@ body &.m_error .content color: red; font-weight: bold; + &.m_help .content + color: #008000; + font-weight: bold; .message, .topicbar .irc-bg00,.irc-bg0