Emit 'dispose' on a dispose call instead of 'destroy'
[KiwiIRC.git] / server / irc / state.js
index df1ba056e754fbb2db8982296d61237a1fac1c12..aa531bee12fc87bf60cf2abe0aed1b5e2b34ba6a 100755 (executable)
@@ -1,7 +1,11 @@
-var util = require('util'),
-    IrcConnection = require('./connection.js');
+var util            = require('util'),
+    events          = require('events'),
+    _               = require('lodash'),
+    IrcConnection   = require('./connection.js').IrcConnection;
 
 var State = function (client, save_state) {
+    var that = this;
+
     events.EventEmitter.call(this);
     this.client = client;
     this.save_state = save_state || false;
@@ -9,9 +13,9 @@ var State = function (client, save_state) {
     this.irc_connections = [];
     this.next_connection = 0;
     
-    this.client.on('disconnect', function () {
-        if (!this.save_state) {
-            _.each(this.irc_connections, function (irc_connection, i, cons) {
+    this.client.on('dispose', function () {
+        if (!that.save_state) {
+            _.each(that.irc_connections, function (irc_connection, i, cons) {
                 if (irc_connection) {
                     irc_connection.end('QUIT :' + (global.config.quit_message || ''));
                     irc_connection.dispose();
@@ -19,7 +23,7 @@ var State = function (client, save_state) {
                 }
             });
             
-            this.dispose();
+            that.dispose();
         }
     });
 };
@@ -36,18 +40,20 @@ State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callb
             global.config.restrict_server,
             global.config.restrict_server_port,
             global.config.restrict_server_ssl,
-            command.nick,
+            nick,
             user,
-            global.config.restrict_server_password);
+            global.config.restrict_server_password,
+            this);
 
     } else {
         con = new IrcConnection(
-            command.hostname,
-            command.port,
-            command.ssl,
-            command.nick,
+            hostname,
+            port,
+            ssl,
+            nick,
             user,
-            command.password);
+            pass,
+            this);
     }
     
     con_num = this.next_connection++;
@@ -61,7 +67,7 @@ State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callb
     });
     
     con.on('error', function (err) {
-        console.log('irc_connection error (' + command.hostname + '):', err);
+        console.log('irc_connection error (' + hostname + '):', err);
         return callback(err.code, {server: con_num, error: err});
     });
 
@@ -79,6 +85,6 @@ State.prototype.sendKiwiCommand = function () {
 };
 
 State.prototype.dispose = function () {
-    this.emit('destroy');
+    this.emit('dispose');
     this.removeAllListeners();
 };