diff --git a/client/js/chat.js b/client/js/chat.js index 807c7e4..cd93c5a 100644 --- a/client/js/chat.js +++ b/client/js/chat.js @@ -17,7 +17,6 @@ $(function() { function event(type, json) { console.log(json); - console.log(type); switch (type) { diff --git a/lib/models.js b/lib/models.js index 414860e..705f8e0 100644 --- a/lib/models.js +++ b/lib/models.js @@ -58,10 +58,10 @@ models.Channel = Backbone.Model.extend({ name: "", }, addUser: function(models) { - this.get("users").add(models); + return this.get("users").add(models); }, addMessage: function(models) { - this.get("messages").add(models); + return this.get("messages").add(models); }, initialize: function() { this.set({ @@ -79,11 +79,12 @@ models.Channel = Backbone.Model.extend({ }); }, this); - this.get("users").on("all", function(action, data) { + var users = this.get("users"); + users.on("all", function(action, data) { this.trigger("user", { target: this.get("id"), type: "user", - data: data, + data: users, action: action, }); }, this); @@ -96,10 +97,11 @@ models.Channels = Backbone.Collection.extend({ models.Network = Backbone.Model.extend({ defaults: { - host: "" + host: "", + client: null, }, addChannel: function(models) { - this.get("channels").add(models); + return this.get("channels").add(models); }, initialize: function() { this.set({ @@ -124,6 +126,9 @@ models.Network = Backbone.Model.extend({ type: "network", name: this.get("host") }); + }, + toJSON: function() { + return _.omit(this.attributes, "client"); } }); diff --git a/lib/server.js b/lib/server.js index 14c88a8..d9dd4ec 100644 --- a/lib/server.js +++ b/lib/server.js @@ -40,9 +40,6 @@ module.exports = function listen() { }; function connect(host) { - var network = networks.add({host: host}, {silent: true}); - networks.trigger("network", networks); - var stream = net.connect({ port: 6667, host: host, @@ -53,6 +50,12 @@ function connect(host) { }); var client = irc(stream); + var network = networks.add({ + host: host, + client: client, + }, {silent: true}); + + networks.trigger("network", networks); client.nick("t_user"); client.user("t_user", "temp user"); @@ -104,13 +107,24 @@ function input(json) { networks.remove(network); break; + case "join": + if (args[1]) { + network.get("client").join(args[1]); + } + break; + + case "part": + if (args[1]) { + network.get("client").part(args[1]); + } + break; + } } function event(event, data) { - var network = this; - var channels = network.get("channels"); - + var channels = this.get("channels"); + switch (event) { case "away": @@ -118,7 +132,12 @@ function event(event, data) { break; case "join": - console.log(event); + var chan = channels.findWhere({name: data.channel}) || channels.add({name: data.channel}); + chan.addUser({name: data.nick}); + chan.addMessage({ + user: data.nick, + type: "join", + }); break; case "kick": @@ -126,7 +145,8 @@ function event(event, data) { break; case "names": - console.log(event); + var chan = channels.findWhere({name: data.channel}); + chan.get("users").reset(_.map(data.names, function(n) { return {name: n}; })); break; case "nick": @@ -138,7 +158,21 @@ function event(event, data) { break; case "part": - console.log(event); + var len = data.channels.length; + var name = data.nick; + for (var i = 0; i < len; i++) { + var chan = channels.findWhere({name: data.channels[i]}); + if (name == this.get("client").me) { + channels.remove(chan); + return; + } + var users = channel.get("users"); + users.remove(users.findWhere({name: name})); + chan.addMessage({ + user: data.nick, + type: "part", + }); + } break; case "pong": @@ -158,7 +192,7 @@ function event(event, data) { break; case "welcome": - console.log(event); + channels.first().addMessage({message: "Connected."}); break; case "whois":