From d2c7c72c38c83ba967dc277efb5c2c8220f92e5a Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Sat, 14 Jun 2014 17:57:26 +0200 Subject: [PATCH] Handle nick collision. Close #11 --- lib/models/network.js | 6 +++++- lib/server.js | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/models/network.js b/lib/models/network.js index f984fee4..7a8b5e2a 100644 --- a/lib/models/network.js +++ b/lib/models/network.js @@ -7,6 +7,7 @@ function Network(attr) { _.merge(this, _.extend({ id: global.id = ++global.id || 1, client: null, + connected: false, host: "", nick: "", channels: [], @@ -19,7 +20,10 @@ function Network(attr) { }; Network.prototype.toJSON = function() { - var clone = _.omit(this, "client"); + var clone = _.omit(this, [ + "client", + "connected", + ]); clone.name = clone.host.split(".")[1] || clone.host; return clone; }; diff --git a/lib/server.js b/lib/server.js index aeac12a2..0a263d06 100644 --- a/lib/server.js +++ b/lib/server.js @@ -387,6 +387,12 @@ function event(e, data) { text: data.message, }), }); + if (!this.connected) { + if (data.cmd == "ERR_NICKNAMEINUSE") { + var random = config.defaults.nick + Math.floor(10 + (Math.random() * 89)); + this.client.nick(random); + } + } break; case "join": @@ -662,7 +668,17 @@ function event(e, data) { break; case "welcome": - // Leaving this empty for now. + this.connected = true; + var chan = channels[0]; + var msg = new Msg({ + from: "-!-", + text: "You're now known as " + data, + }); + chan.messages.push(msg); + sockets.emit("msg", { + id: chan.id, + msg: msg, + }); break; case "whois":