Server: Seperated connection method from constructor
authorDarren <darren@darrenwhitlen.com>
Wed, 12 Dec 2012 21:12:25 +0000 (21:12 +0000)
committerDarren <darren@darrenwhitlen.com>
Wed, 12 Dec 2012 21:12:25 +0000 (21:12 +0000)
server/irc/connection.js

index 66d5c426d40c865f82edd7e777279c6b7690b7de..101872b47c3ef04117ad8184420f064811c25b3f 100644 (file)
@@ -7,17 +7,45 @@ var net     = require('net'),
 var IrcConnection = function (hostname, port, ssl, nick, user, pass) {
     var that = this;
     events.EventEmitter.call(this);
+    
+    this.connected = false;
+    this.registered = false;
+    this.cap_negotiation = true;
+    this.nick = nick;
+    this.user = user;  // Contains users real hostname and address
+    this.username = this.nick.replace(/[^0-9a-zA-Z\-_.]/, ''),
+    this.irc_host = {hostname: hostname, port: port};
+    this.ssl = !(!ssl);
+    this.options = Object.create(null);
+    this.cap = {requested: [], enabled: []};
+    this.sasl = false;
+    
+    this.password = pass;
+    this.hold_last = false;
+    this.held_data = '';
+
+    global.modules.emit('irc:connecting', {connection: this}).done(function () {
+        that.connect();
+    });
+};
+util.inherits(IrcConnection, events.EventEmitter);
+
+module.exports.IrcConnection = IrcConnection;
+
 
-    if (ssl) {
+IrcConnection.prototype.connect = function () {
+    var that = this;
+
+    if (this.ssl) {
         this.socket = tls.connect({
-            host: hostname,
-            port: port,
+            host: this.irc_host.hostname,
+            port: this.irc_host.port,
             rejectUnauthorized: global.config.reject_unauthorised_certificates
         }, function () {
             connect_handler.apply(that, arguments);
         });
     } else {
-        this.socket = net.createConnection(port, hostname);
+        this.socket = net.createConnection(this.irc_host.port, this.irc_host.hostname);
         this.socket.on('connect', function () {
             connect_handler.apply(that, arguments);
         });
@@ -36,27 +64,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass) {
     this.socket.on('close', function () {
         that.emit('close');
     });
-    
-    this.connected = false;
-    this.registered = false;
-    this.cap_negotiation = true;
-    this.nick = nick;
-    this.user = user;  // Contains users real hostname and address
-    this.username = this.nick.replace(/[^0-9a-zA-Z\-_.]/, ''),
-    this.irc_host = {hostname: hostname, port: port};
-    this.ssl = !(!ssl);
-    this.options = Object.create(null);
-    this.cap = {requested: [], enabled: []};
-    this.sasl = false;
-    
-    this.password = pass;
-    this.hold_last = false;
-    this.held_data = '';
 };
-util.inherits(IrcConnection, events.EventEmitter);
-
-module.exports.IrcConnection = IrcConnection;
-
 
 IrcConnection.prototype.write = function (data, callback) {
     write.call(this, data + '\r\n', 'utf-8', callback);