Kiwi client/server API fully reverted
[KiwiIRC.git] / client_backbone / dev / model_gateway.js
old mode 100644 (file)
new mode 100755 (executable)
index 31863e8..75dc73b
@@ -36,8 +36,7 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
         *   The URL to the Kiwi server\r
         *   @type   String\r
         */\r
-        //kiwi_server: '//kiwi'\r
-        kiwi_server: 'http://localhost:7778/kiwi'\r
+        kiwi_server: '//kiwi'\r
     };\r
 \r
 \r
@@ -91,7 +90,15 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
         });\r
 \r
         this.socket.on('connect', function () {\r
-            this.emit('irc connect', that.get('nick'), host, port, ssl, password, callback);\r
+            this.emit('kiwi', {command: 'connect', nick: that.get('nick'), hostname: host, port: port, ssl: ssl, password:password}, function (err, server_num) {\r
+                console.log('err, server_num', 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
+                }\r
+            });\r
             that.trigger('connect', {});\r
         });\r
 \r
@@ -99,7 +106,9 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
             this.emit("connect_fail", {reason: 'too_many_connections'});\r
         });\r
 \r
-        this.socket.on('message', this.parse);\r
+        this.socket.on('irc', function (data, callback) {\r
+            that.parse(data.command, data.data);\r
+        });\r
 \r
         this.socket.on('disconnect', function () {\r
             that.trigger("disconnect", {});\r
@@ -143,18 +152,18 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
     /**\r
     *   Parses the response from the server\r
     */\r
-    this.parse = function (item) {\r
-        //console.log('gateway event', item);\r
-        if (item.event !== undefined) {\r
-            that.trigger('on' + item.event, item);\r
+    this.parse = function (command, data) {\r
+        //console.log('gateway event', command, data);\r
+        if (command !== undefined) {\r
+            that.trigger('on' + command, data);\r
 \r
-            switch (item.event) {\r
+            switch (command) {\r
             case 'options':\r
-                $.each(item.options, function (name, value) {\r
+                $.each(data.options, function (name, value) {\r
                     switch (name) {\r
                     case 'CHANTYPES':\r
                         // TODO: Check this. Why is it only getting the first char?\r
-                        that.set('channel_prefix', value.charAt(0));\r
+                        that.set('channel_prefix', value.join('').charAt(0));\r
                         break;\r
                     case 'NETWORK':\r
                         that.set('name', value);\r
@@ -167,12 +176,12 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
                 break;\r
 \r
             case 'connect':\r
-                that.set('nick', item.nick);\r
+                that.set('nick', data.nick);\r
                 break;\r
 \r
             case 'nick':\r
-                if (item.nick === that.get('nick')) {\r
-                    that.set('nick', item.newnick);\r
+                if (data.nick === that.get('nick')) {\r
+                    that.set('nick', data.newnick);\r
                 }\r
                 break;\r
             /*\r
@@ -185,7 +194,7 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
             */\r
 \r
             case 'kiwi':\r
-                this.emit('kiwi.' + item.namespace, item.data);\r
+                this.emit('kiwi.' + data.namespace, data.data);\r
                 break;\r
             }\r
         }\r
@@ -198,7 +207,7 @@ kiwi.model.Gateway = Backbone.Model.extend(new (function () {
     *   @param  {Function}  callback    A callback function\r
     */\r
     this.sendData = function (data, callback) {\r
-        this.socket.emit('message', {sid: this.session_id, data: JSON.stringify(data)}, callback);\r
+        this.socket.emit('irc', {server: 0, data: JSON.stringify(data)}, callback);\r
     };\r
 \r
     /**\r