From 7f3656e45835fc0ce73ca12eed096f3bdb973e11 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 6 Mar 2014 10:02:43 -0800 Subject: [PATCH] Added node-irc --- client/css/style.css | 21 ++++---- client/index.html | 2 +- client/js/chat.js | 3 +- lib/models.js | 6 +++ lib/server.js | 124 ++++++++++++++++++++++++++++++++++--------- package.json | 1 + 6 files changed, 122 insertions(+), 35 deletions(-) diff --git a/client/css/style.css b/client/css/style.css index 65992539..7884c71a 100644 --- a/client/css/style.css +++ b/client/css/style.css @@ -27,22 +27,25 @@ h2 { border-right: 1px solid #ccc; float: left; height: 100%; - line-height: 20px; + line-height: 36px; width: 199px; } #sidebar .channel { - clear: both; + border-bottom: 1px solid transparent; color: #f00; cursor: pointer; - float: left; - margin-left: 20px; + padding: 0 12px; +} +#sidebar .channel[data-id='0'] { + background: #eaeaea; + border-bottom-color: #e5e5e5; +} +#sidebar .channel:first-child { + color: #333; + overflow: hidden; } #sidebar .channel:hover { - color: #999; -} -#sidebar .channel.network { - margin-top: 10px; - overflow: hidden; + text-decoration: underline; } #chat { bottom: 0; diff --git a/client/index.html b/client/index.html index 0203d051..f6630e3c 100644 --- a/client/index.html +++ b/client/index.html @@ -14,7 +14,7 @@ {{#networks}}
{{#channels}} -
+
{{name}}
{{/channels}} diff --git a/client/js/chat.js b/client/js/chat.js index de6f01ff..ca9a1533 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -3,6 +3,7 @@ $(function() { socket.on( "event", function(event) { + console.log(event); View[event.action](event); } ); @@ -18,7 +19,7 @@ $(function() { var View = {}; - View.redraw = function(event) { + View.refresh = function(event) { if (event.data == undefined || event.data == []) { return; } diff --git a/lib/models.js b/lib/models.js index 83721805..8a08dca1 100644 --- a/lib/models.js +++ b/lib/models.js @@ -1,3 +1,5 @@ +var _ = require("lodash"); + var models = exports; var id = 0; @@ -8,6 +10,10 @@ models.Network = function() { this.channels = []; }; +models.Network.prototype.toJSON = function() { + return _.omit(this, "irc"); +}; + models.Channel = function() { this.id = id++; this.name = ""; diff --git a/lib/server.js b/lib/server.js index b32154c1..16ec4330 100644 --- a/lib/server.js +++ b/lib/server.js @@ -1,5 +1,6 @@ var _ = require("lodash"); var connect = require("connect"); +var irc = require("irc"); var io = require("socket.io"); var models = require("./models.js"); @@ -8,14 +9,7 @@ exports.listen = listen; var sockets; var networks = []; -var network = new models.Network; -var chan = _.assign(new models.Channel, { - name: "Network", - type: "network" -}); - -network.channels.push(chan); -networks.push(network); +addNetwork("Lobby", false); function listen(port) { var http = connect() @@ -25,39 +19,121 @@ function listen(port) { sockets.on("connection", function(socket) { init(socket); }); -}; +} function init(socket) { + refresh(); socket.on( "input", function(input) { handleUserInput(input) } ); +} + +function refresh() { + if (typeof sockets === "undefined") { + return; + } sockets.emit("event", _.assign(new models.Event, { - action: "redraw", + action: "refresh", data: networks })); -}; +} function handleUserInput(input) { - var id = input.id; var text = input.text; + var target = getChannel(input.id); + if (text.charAt(0) != "/") { + return addMessage(target, text); + } + + var args = text.substr(1).split(" "); + switch (args[0]) { + + case "connect": + if (typeof args[1] !== "undefined") { + addNetwork(args[1], true); + } + break; + + case "join": + console.log(args); + if (typeof args[1] === "undefined") { + return; + } + target.network.channels.push( + _.assign(new models.Channel, { + name: args[1] + }) + ); + refresh(); + break; + + default: + console.log("DEFAULT"); + addMessage( + target, + "Command '/" + args[0] + "' does not exist." + ); + break; + + } +} + +function addNetwork(addr, bool) { + bool = bool || false; + + var chan = _.assign(new models.Channel, {name: addr, type: "network"}); + var network = _.assign( + new models.Network, {channels: [chan]} + ); + + if (bool) { + console.log("IRC"); + network.irc = new irc.Client(addr, "default_user", { + channels: ["#default_channel"] + }); + network.irc.addListener("raw", function() { + handleEvent.apply(this, [network].concat(arguments)); + }); + } + + networks.push(network); + refresh(); +} + +function handleEvent(network) { + var args = arguments; + var target = { + network: network, + channel: network.channels[0] + }; + + console.log(args[1]); + addMessage(target, args[1][0].args); +} + +function addMessage(target, text) { var message = _.assign(new models.Message, {text: text}); - var event = _.assign(new models.Event, { + target.channel.messages.push(message); + sockets.emit("event", _.assign(new models.Event, { action: "add", type: "message", - data: message, - target: id - }); + target: target.channel.id, + data: message + })); +} - sockets.emit("event", event); - - _.each(networks, function(n) { - var chan = _.findWhere(n.channels, {id: id}); - if (chan !== "undefined") { - chan.messages.push(message); +function getChannel(id) { + for (var i = 0; i < networks.length; i++) { + var find = { + network: networks[i], + channel: _.findWhere(networks[i].channels, {id: id}) + }; + if (typeof find.channel !== "undefined") { + return find; } - }); -}; + } +} diff --git a/package.json b/package.json index 6e80df36..6755da51 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dependencies": { "commander": "2.1.0", "connect": "2.13.0", + "irc": "0.3.6", "lodash": "2.4.1", "socket.io": "0.9.16" }