From 2d823c8f8557f0a49e4307717e99a90b24052e9c Mon Sep 17 00:00:00 2001 From: lassulus Date: Fri, 8 Nov 2013 13:15:52 +0100 Subject: webchat: refactored code --- webchat/index.js | 69 +++++++++++++++++++------- webchat/public/client.js | 58 +++------------------- webchat/public/commands.js | 25 ++++++++++ webchat/public/functions.js | 117 ++++++++++++++++++++++++++++++++++++++++++++ webchat/public/krebs.png | Bin 956 -> 2583 bytes webchat/public/reset.css | 6 +-- 6 files changed, 203 insertions(+), 72 deletions(-) create mode 100644 webchat/public/commands.js create mode 100644 webchat/public/functions.js (limited to 'webchat') diff --git a/webchat/index.js b/webchat/index.js index 8ef737fc..d3bd0a71 100644 --- a/webchat/index.js +++ b/webchat/index.js @@ -11,6 +11,26 @@ Clients.broadcast = function(object) { //broadcast to all clients }); } +var serverCommands = {}; + +serverCommands.say = function (settings, params) { + var nick = settings.nick || settings.conn.remoteAddress + var message = params.message + irc_client.say("#krebs", nick + ' → ' + message); + return Clients.broadcast({ type: 'message', nick: nick, message: message }) +} + +serverCommands.nick = function (settings, params) { + var oldnick = settings.nick || settings.conn.remoteAddress + var newnick = params.nick + settings.nick = newnick + return Clients.broadcast({ type: 'nickchange', nick: oldnick, newnick: newnick }) +} + +serverCommands.badcommand = function (settings, params) { + settings.conn.write(JSON.stringify({ type: 'usererror', message: 'bad command' })) +} + var irc_reconnect = function() { //reconnt to irc console.log("reconnecting due to pingtimeout"); irc_client.disconnect(); @@ -45,10 +65,24 @@ irc_client.on('ping', function(server) { //restart timer on server ping irc_client.on('message#krebs', function(from, message) { console.log({ from: from, message: message }); - Clients.broadcast({ from: from, message: message }); //broadcast irc messages to all connected clients + Clients.broadcast({ type: 'message', from: from, message: message }); //broadcast irc messages to all connected clients clearTimeout(lastping); }); +irc_client.on('names#krebs', function(nicks) { + Clients.broadcast({type: 'nicklist', message: nicks}); +}); + +irc_client.on('join#krebs', function(nick, msg) { + if (nick !== 'kweb'){ + Clients.broadcast({type: 'join', from: nick}); + } +}); + +irc_client.on('part#krebs', function(nick, rs, msg) { + Clients.broadcast({type: 'quit', from: nick}); +}); + var web_serv_options = { //certificates for https key: fs.readFileSync(__dirname+'/local_config/server_npw.key'), cert: fs.readFileSync(__dirname+'/local_config/server.crt'), @@ -57,26 +91,22 @@ var web_serv_options = { //certificates for https var echo = sockjs.createServer(); echo.on('connection', function(conn) { var origin = conn.remoteAddress; + var settings = { + conn: conn + } Clients.push(conn); - Clients.broadcast({from: 'system', message: origin + ' has joined'}) + Clients.broadcast({type: 'join', from: origin}) // irc_client.say("#krebs", origin + ' has joined'); - conn.write(JSON.stringify({from: 'system', message: 'hello'})) //welcome message + if (typeof irc_client.chans['#krebs'] === 'object') { + conn.write(JSON.stringify({type: 'nicklist', message: irc_client.chans['#krebs'].users})); //send current nicklist + }; + conn.write(JSON.stringify({type: 'message', from: 'system', message: 'hello' })) //welcome message + console.log(irc_client.chans['#krebs']) conn.on('data', function(data) { console.log('data:',data); try { - var object = JSON.parse(data); - if (object.message.length > 0) { //if message is not empty - if (/^\/nick\s+(.+)$/.test(object.message)) { //if nick is send use nick instead of ip - object.from = origin; - } else if (typeof object.nick === 'string') { - object.from = object.nick; - } else { - object.from = origin; - }; - console.log(object.message); - irc_client.say("#krebs", object.from + ' → ' + object.message); - Clients.broadcast(object); - } + var command = JSON.parse(data); + return (serverCommands[command.method] || serverCommands.badcommand)(settings, command.params) } catch (error) { console.log(error); @@ -84,7 +114,7 @@ echo.on('connection', function(conn) { }); conn.on('close', function() { //propagate if client quits the page Clients.splice(Clients.indexOf(conn)); - Clients.broadcast({from: 'system', message: origin + ' has quit'}) + Clients.broadcast({type: 'quit', from: origin}) // irc_client.say("#krebs", origin + ' has quit'); }); }); @@ -99,6 +129,8 @@ var app = connect() page_template+='\n'; page_template+='\n'; page_template+='\n'; + page_template+='\n'; + page_template+='\n'; page_template+='\n'; page_template+='
'; page_template+='
'; @@ -106,7 +138,8 @@ var app = connect() page_template+='hello, this is the official krebs support:
\n'; page_template+='
\n'; page_template+='
'; - page_template+='