From bc5e5ee5fca3f7fb6f9822f737054e0d7ec96f0e Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 13 Mar 2014 01:38:20 +0100 Subject: [PATCH] Added /nick command --- lib/models.js | 10 +++++----- lib/server.js | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/models.js b/lib/models.js index 9a623f19..22ba586d 100644 --- a/lib/models.js +++ b/lib/models.js @@ -50,13 +50,13 @@ models.Channel = Backbone.Model.extend({ this.set("users", new models.UserCollection()); this.get("users").on( - "add remove", - function(model, collection) { + "add change remove", + function() { // Bubble event this.trigger( "users", { target: this.get("id"), - data: collection + data: this.get("users") } ); }, @@ -66,12 +66,12 @@ models.Channel = Backbone.Model.extend({ this.set("messages", new models.MessageCollection()); this.get("messages").on( "add", - function(model, collection) { + function() { // Bubble event this.trigger( "messages", { target: this.get("id"), - data: collection.last() + data: this.get("messages").last() } ); }, diff --git a/lib/server.js b/lib/server.js index 8b60d7e3..e71629ec 100644 --- a/lib/server.js +++ b/lib/server.js @@ -20,11 +20,12 @@ Server.prototype.listen = function(port) { this.networks.on( "all", function(type, data) { - if (type == "users" || type == "messages") { - self.sockets.emit(type, data); - } else { - self.sockets.emit("networks", self.networks); + if (type != "users" && type != "messages") { + type = "networks"; + data = self.networks; } + self.sockets + .emit(type, data); } ); @@ -126,6 +127,13 @@ function handleInput(input) { } break; + case "NICK": + var irc = target.network.irc; + if (argv[1] && typeof irc !== "undefined") { + irc.send("NICK", argv[1]); + } + break; + case "TOPIC": var irc = target.network.irc; if (typeof irc !== "undefined") { @@ -207,8 +215,8 @@ function handleEvent(argv) { user: from, text: "notice: " + argv.args[1] }); - channels.each(function(c) { - c.get("messages").add(message); + channels.each(function(channel) { + channel.get("messages").add(message); }); break; @@ -258,8 +266,25 @@ function handleEvent(argv) { } break; + case "NICK": + var message = new models.Message({ + user: argv.nick, + text: "changed name to " + argv.args[0] + }); + if (network.get("nick") == argv.nick) { + network.set("nick", argv.nick); + } + channels.each(function(channel) { + var user = channel.get("users").findWhere({name: argv.nick}); + if (typeof user !== "undefined") { + user.set("name", argv.args[0]); + channel.get("messages").add(message); + } + }); + break; + case "TOPIC": - var channel = channels.findWhere({name: argv.args[0]}); + var channel = channels.findWhere({name: argv.args[0]}); var messages = channel.get("messages"); messages.add( new models.Message({ @@ -309,6 +334,7 @@ function handleEvent(argv) { ); break; + case "err_nicknameinuse": case "err_chanoprivsneeded": case "err_notonchannel": case "err_nosuchchannel": @@ -316,7 +342,7 @@ function handleEvent(argv) { case "err_nosuchnick": channels.first().get("messages").add( new models.Message({ - text: argv.args[2] + text: argv.args.slice(2).join(" ") }) ); break; @@ -325,7 +351,7 @@ function handleEvent(argv) { channels.first().get("messages").add( new models.Message({ user: "-!-", - text: argv.args[1] + text: argv.args.slice(1).join(" ") }) ); }