Client: Encoding within the URL
[KiwiIRC.git] / client / assets / src / models / gateway.js
index f0d8db5c5123f8f80805d1fe6292b8484bac9685..a2cc8b2e5a1f16584e323d14b4444cc0b19b74c3 100644 (file)
@@ -50,11 +50,14 @@ _kiwi.model.Gateway = function () {
 \r
     this.initialize = function () {\r
         that = this;\r
-        \r
+\r
         // For ease of access. The socket.io object\r
         this.socket = this.get('socket');\r
 \r
         this.applyEventHandlers();\r
+\r
+        // Used to check if a disconnection was unplanned\r
+        this.disconnect_requested = false;\r
     };\r
 \r
 \r
@@ -71,7 +74,7 @@ _kiwi.model.Gateway = function () {
         kiwi.gateway.on('quit', my_function);\r
         */\r
         var that = this;\r
-        \r
+\r
         // Some easier handler events\r
         this.on('onmsg', function (event) {\r
             var source,\r
@@ -79,7 +82,7 @@ _kiwi.model.Gateway = function () {
                 is_pm = (event.channel == connection.get('nick'));\r
 \r
             source = is_pm ? event.nick : event.channel;\r
-            \r
+\r
             that.trigger('message:' + source, event);\r
             that.trigger('message', event);\r
 \r
@@ -105,7 +108,7 @@ _kiwi.model.Gateway = function () {
                 is_pm = (event.channel == connection.get('nick'));\r
 \r
             source = is_pm ? event.nick : event.channel;\r
-            \r
+\r
             that.trigger('action:' + source, event);\r
 \r
             if (is_pm) {\r
@@ -130,6 +133,31 @@ _kiwi.model.Gateway = function () {
 \r
 \r
 \r
+    this.reconnect = function (callback) {\r
+        var that = this,\r
+            transport_path;\r
+\r
+        this.disconnect_requested = true;\r
+        this.socket.disconnect();\r
+\r
+        // To get around the allow-origin issues for requests, completely reload the\r
+        // transport source from the new server\r
+        window.io = null;\r
+\r
+        // Path to get the socket.io transport code\r
+        transport_path = _kiwi.app.kiwi_server + _kiwi.app.get('base_path') + '/transport/socket.io.js?ts='+(new Date().getTime());\r
+        $script(transport_path, function() {\r
+            if (!window.io) {\r
+                return callback('err_kiwi_server_not_found');\r
+            }\r
+\r
+            that.set('kiwi_server', _kiwi.app.kiwi_server + '/kiwi');\r
+            that.connect(callback);\r
+        });\r
+    };\r
+\r
+\r
+\r
     /**\r
     *   Connects to the server\r
     *   @param  {Function}  callback    A callback function to be invoked once Kiwi's server has connected to the IRC server\r
@@ -177,18 +205,10 @@ _kiwi.model.Gateway = function () {
          * IRCD and the nick has been accepted.\r
          */\r
         this.socket.on('connect', function () {\r
+            // Reset the disconnect_requested flag\r
+            that.disconnect_requested = false;\r
+\r
             callback && callback();\r
-            /*\r
-            this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {\r
-                if (!err) {\r
-                    that.server_num = server_num;\r
-                    console.log("_kiwi.gateway.socket.on('connect')");\r
-                } else {\r
-                    console.log("_kiwi.gateway.socket.on('error')", {reason: err});\r
-                    callback(err);\r
-                }\r
-            });\r
-            */\r
         });\r
 \r
         this.socket.on('too_many_connections', function () {\r
@@ -223,23 +243,32 @@ _kiwi.model.Gateway = function () {
     };\r
 \r
 \r
-\r
+    /**\r
+     * Return a new network object with the new connection details\r
+     */\r
     this.newConnection = function(connection_info, callback_fn) {\r
-        var that = this,\r
-            h = connection_info;\r
+        var that = this;\r
 \r
-        this.socket.emit('kiwi', {command: 'connect', nick: h.nick, hostname: h.host, port: h.port, ssl: h.ssl, password: h.password}, function (err, server_num) {\r
+        this.makeIrcConnection(connection_info, function(err, server_num) {\r
             var connection;\r
 \r
             if (!err) {\r
                 if (!_kiwi.app.connections.getByConnectionId(server_num)){\r
-                    connection = new _kiwi.model.Network({connection_id: server_num});\r
+                    var inf = {\r
+                        connection_id: server_num,\r
+                        nick: connection_info.nick,\r
+                        address: connection_info.host,\r
+                        port: connection_info.port,\r
+                        ssl: connection_info.ssl,\r
+                        password: connection_info.password\r
+                    };\r
+                    connection = new _kiwi.model.Network(inf);\r
                     _kiwi.app.connections.add(connection);\r
                 }\r
 \r
-                console.log("_kiwi.gateway.socket.on('connect')");\r
+                console.log("_kiwi.gateway.socket.on('connect')", connection);\r
                 callback_fn && callback_fn(err, connection);\r
-                \r
+\r
             } else {\r
                 console.log("_kiwi.gateway.socket.on('error')", {reason: err});\r
                 callback_fn && callback_fn(err);\r
@@ -247,6 +276,35 @@ _kiwi.model.Gateway = function () {
         });\r
     };\r
 \r
+\r
+    /**\r
+     * Make a new IRC connection and return its connection ID\r
+     */\r
+    this.makeIrcConnection = function(connection_info, callback_fn) {\r
+        var server_info = {\r
+            command:    'connect',\r
+            nick:       connection_info.nick,\r
+            hostname:   connection_info.host,\r
+            port:       connection_info.port,\r
+            ssl:        connection_info.ssl,\r
+            password:   connection_info.password\r
+        };\r
+\r
+        // A few optional parameters\r
+        if (connection_info.options.encoding)\r
+            server_info.encoding = connection_info.options.encoding;\r
+\r
+        this.socket.emit('kiwi', server_info, function (err, server_num) {\r
+            if (!err) {\r
+                callback_fn && callback_fn(err, server_num);\r
+\r
+            } else {\r
+                callback_fn && callback_fn(err);\r
+            }\r
+        });\r
+    };\r
+\r
+\r
     this.isConnected = function () {\r
         return this.socket.socket.connected;\r
     };\r