Handle nick collision. Close #11

This commit is contained in:
Mattias Erming 2014-06-14 17:57:26 +02:00
parent ba559c8fed
commit d2c7c72c38
2 changed files with 22 additions and 2 deletions

View File

@ -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;
};

View File

@ -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":