From 0d880802a947cc92cbba41010961b493bdef0eed Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Thu, 13 Mar 2014 18:50:02 +0100 Subject: [PATCH] Added option to auto connect --- config.js | 6 ++++-- lib/models.js | 31 ++++++++++++++++--------------- lib/server.js | 49 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 52 insertions(+), 34 deletions(-) diff --git a/config.js b/config.js index 55d222f..a88777c 100644 --- a/config.js +++ b/config.js @@ -1,7 +1,9 @@ module.exports = { + nick: "temp_name", + fullname: "Temporary Name", + autoConnect: true, host: "irc.freenode.org", - nick: "temp_user", channels: [ "#temp_chan" - ] + ], }; \ No newline at end of file diff --git a/lib/models.js b/lib/models.js index 55bc1c5..547a22a 100644 --- a/lib/models.js +++ b/lib/models.js @@ -77,8 +77,7 @@ models.ChannelCollection = Backbone.Collection.extend({ models.Network = Backbone.Model.extend({ defaults: { host: "", - nick: config.nick, - connect: true + nick: config.nick }, initialize: function() { this.set({ @@ -97,26 +96,28 @@ models.Network = Backbone.Model.extend({ type: "network", name: this.get("host") })); + }, + connect: function(channels) { + var client = new irc.Client( + this.get("host"), + this.get("nick"), { + fullname: config.fullname, + channels: channels + } + ); - if (this.get("connect")) { - this.irc = new irc.Client( - this.get("host"), - this.get("nick"), { - channels: config.channels - } - ); - this.irc.addListener( - "error", function() { - // .. - } - ); - } + this.irc = client; + this.irc.addListener("error", function() { + // .. + }); this.on("remove", function() { if (typeof this.irc !== "undefined") { this.irc.disconnect(); } }); + + return this.irc; } }); diff --git a/lib/server.js b/lib/server.js index 2ee3ec7..da61787 100644 --- a/lib/server.js +++ b/lib/server.js @@ -13,6 +13,7 @@ function Server() { } Server.prototype.listen = function(port) { + var self = this; var http = connect() .use(connect.static("client")) .listen(port); @@ -29,11 +30,7 @@ Server.prototype.listen = function(port) { } }, this); - this.sockets = io - .listen(http, {log: false}) - .sockets; - - var self = this; + this.sockets = io.listen(http, {log: false}).sockets; this.sockets.on("connection", function(socket) { socket.emit( "networks", self.networks @@ -46,9 +43,23 @@ Server.prototype.listen = function(port) { ); }); + if (config.autoConnect) { + this.connect(config.host, config.channels); + } + return this; }; +Server.prototype.connect = function(host, channels) { + var network = new models.Network({ + host: host + }); + this.networks.add(network); + network.connect(channels).addListener("raw", function() { + handleEvent.apply(network, arguments); + }); +}; + function handleInput(input) { var target = this.networks.find(input.id); if (!target) { @@ -79,14 +90,7 @@ function handleInput(input) { case "SERVER": case "CONNECT": - var network = new models.Network({ - host: argv[1] || config.host - }); - this.networks.add(network); - network.irc.addListener("raw", function() { - handleEvent.apply(network, arguments); - } - ); + this.connect(argv[1]); break; case "QUIT": @@ -183,6 +187,15 @@ function handleEvent(argv) { switch (event) { + case "ERROR": + channels.first().get("messages").add( + new models.Message({ + text: argv.args.slice(2).join(" "), + type: "error" + }) + ); + break; + case "PRIVMSG": var target = argv.args[0]; if (target.charAt(0) != "#") { @@ -209,7 +222,7 @@ function handleEvent(argv) { break; case "NOTICE": - var from = argv.nick ? argv.nick : argv.prefix; + var from = argv.nick ? argv.nick : "-!-"; var message = new models.Message({ user: from, text: "notice: " + argv.args[1], @@ -336,11 +349,13 @@ function handleEvent(argv) { ); break; - case "ERROR": + case "rpl_motdstart": + case "rpl_endofmotd": + case "rpl_motd": channels.first().get("messages").add( new models.Message({ - text: argv.args.slice(2).join(" "), - type: "error" + user: "-!-", + text: argv.args[1] }) ); break;