Working Kiwi IRC with no PHP needed.
authorJack Allnutt <m2ys4u@Gmail.com>
Tue, 19 Jul 2011 05:20:58 +0000 (06:20 +0100)
committerJack Allnutt <m2ys4u@Gmail.com>
Tue, 19 Jul 2011 05:20:58 +0000 (06:20 +0100)
index.php
js/gateway.js
node/client/index.html.jade
node/config.json
node/kiwi.js

index 1c42acd86443e82ef05b8220c4e9562b6c8e7c81..6e09f2f7aff02b715d782b65d87d945a9029756a 100644 (file)
--- a/index.php
+++ b/index.php
@@ -66,7 +66,7 @@
     var agent = '<?php echo $agent; ?>';\r
     var touchscreen = <?php echo ($touchscreen) ? 'true' : 'false'; ?>;\r
     var init_data = {};\r
-    var kiwi_server = '<?php echo ($node_config['listen_ssl'] ? "https" : "http")."://".$node_server; ?>:7777/';\r
+    var kiwi_server = '<?php echo ($node_config['listen_ssl'] ? "https" : "http")."://".$node_server; ?>:7777/kiwi';\r
     var touch_scroll;\r
     \r
     $(document).ready(function(){\r
index 97f6800f7d4c325bbe58ebe802496456a878305d..5fe294c224011ca63e91f95debe2cb0135c0f9cf 100644 (file)
@@ -13,17 +13,20 @@ var gateway = {
     socket: null,
        
     start: function (kiwi_server) {
-        gateway.socket = io.connect(kiwi_server);
-        gateway.socket.on('connect', function () {
-            gateway.sendData = function (data, callback) {
-                gateway.socket.emit('message', {sid: this.session_id, data: $.toJSON(data)}, callback);
-            };
-            gateway.socket.on('message', gateway.parse);
-            gateway.socket.on('disconnect', function () {
-                // Teardown procedure here
-                $(gateway).trigger("ondisconnect", {});
+        if (typeof kiwi_server !== 'undefined') {
+            gateway.socket = io.connect(kiwi_server);
+            gateway.socket.on('connect', function () {
+                console.debug("Connecting to " + kiwi_server);
+                gateway.sendData = function (data, callback) {
+                    gateway.socket.emit('message', {sid: this.session_id, data: $.toJSON(data)}, callback);
+                };
+                gateway.socket.on('message', gateway.parse);
+                gateway.socket.on('disconnect', function () {
+                    // Teardown procedure here
+                    $(gateway).trigger("ondisconnect", {});
+                });
             });
-        });
+        }
     },
 
     connect: function (host, port, ssl, callback) {
@@ -50,6 +53,7 @@ var gateway = {
                    debug
     */
     parse: function (item) {
+        console.debug('a',item);
            if (item.event !== undefined) {
                    $(gateway).trigger("on" + item.event, item);
                
index ca688ca59480373c32da6630c145e5dbfd348ea7..c76a7b9139dcb9cec9bdae265fcc633a6444c911 100644 (file)
@@ -13,7 +13,7 @@ html(xmlns="http://www.w3.org/1999/xhtml")
         - if (touchscreen)
             link(rel="stylesheet", type="text/css", href="css/touchscreen_tweaks.css")
 
-        script(src="http://192.168.1.127:7777/socket.io/socket.io.js")
+        script(type="text/javascript", src="/socket.io/socket.io.js")
         script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")
         script(type="text/javascript", src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js")
         script(type="text/javascript", src="js/jquery.json-2.2.min.js")
@@ -34,7 +34,7 @@ html(xmlns="http://www.w3.org/1999/xhtml")
             var agent = '#{agent}';
             var touchscreen = #{touchscreen};
             var init_data = {};
-            var kiwi_server = '#{proto}://192.168.1.127:7777/';
+            var kiwi_server = '#{proto}://192.168.1.127:7777/kiwi';
             var touch_scroll;
             
             $(document).ready(function(){
index 66b6c2be94bbe20c65a91bcdd33246ff86f1f181..a80939fc758c20002123e7057c71d3b7d54cb416 100644 (file)
@@ -5,5 +5,6 @@
     "ssl_key":          "server.key",
     "ssl_cert":         "cert.pem",
     "quit_message":     "KiwiIRC",
-    "cap_options":      []
+    "cap_options":      [],
+    "handle_http":      true
 }
index b52a8a9d678f9df598e0701c6ccae851ec6af4b5..f40ae8058dfba31ee1fb10bebc6b0c48669f2c82 100644 (file)
@@ -5,7 +5,10 @@ var tls = require('tls'),
     http = require('http'),
     https = require('https'),
     fs = require('fs'),
+    url = require('url'),
+    static_server = require('node-static'),
     ws = require('socket.io'),
+    jade = require('jade'),
     _ = require('./lib/underscore.min.js'),
     starttls = require('./lib/starttls.js');
 
@@ -271,17 +274,74 @@ var ircSocketDataHandler = function (data, websocket, ircSocket) {
     }
 };
 
+var fileServer = new (static_server.Server)(__dirname + '/client');
+
+var httpHandler = function (request, response) {
+    var uri, subs, useragent, agent, server_set, server, nick, debug, touchscreen;
+    if (config.handle_http) {
+        uri = url.parse(request.url);
+        subs = uri.pathname.substr(0, 4);
+        if ((subs === '/js/') || (subs === '/css') || (subs === '/img')) {
+            request.addListener('end', function () {
+                fileServer.serve(request, response);
+            });
+        } else if (uri.pathname === '/') {
+            useragent = (response.headers) ? response.headers['user-agent']: '';
+            if (useragent.indexOf('android') !== -1) {
+                agent = 'android';
+                touchscreen = true;
+            } else if (useragent.indexOf('iphone') !== -1) {
+                agent = 'iphone';
+                touchscreen = true;
+            } else if (useragent.indexOf('ipad') !== -1) {
+                agent = 'ipad';
+                touchscreen = true;
+            } else if (useragent.indexOf('ipod') !== -1) {
+                agent = 'ipod';
+                touchscreen = true;
+            } else {
+                agent = 'normal';
+                touchscreen = false;
+            }
+            if (uri.query) {
+                server_set = (uri.query.server !== '');
+                server = uri.query.server || 'irc.anonnet.org';
+                nick = uri.query.nick || '';
+                debug = (uri.query.debug !== '');
+            } else {
+                server = 'irc.anonnet.org';
+                nick = '';
+            }
+            response.setHeader('Connection', 'close');
+            response.setHeader('X-Generated-By', 'KiwiIRC');
+            jade.renderFile(__dirname + '/client/index.html.jade', { locals: { "touchscreen": touchscreen, "debug": debug, "server_set": server_set, "server": server, "nick": nick, "agent": agent, "config": config }}, function (err, html) {
+                if (!err) {
+                    response.write(html);
+                } else {
+                    response.statusCode = 500;
+                }
+                response.end();
+            });
+        } else if (uri.pathname.substr(0, 10) === '/socket.io') {
+            // Do nothing!
+        } else {
+            response.statusCode = 404;
+            response.end();
+        }
+    }
+};
+
 //setup websocket listener
 if (config.listen_ssl) {
-    var httpServer = https.createServer({key: fs.readFileSync(__dirname + '/' + config.ssl_key), cert: fs.readFileSync(__dirname + '/' + config.ssl_cert)});
+    var httpServer = https.createServer({key: fs.readFileSync(__dirname + '/' + config.ssl_key), cert: fs.readFileSync(__dirname + '/' + config.ssl_cert)}, httpHandler);
     var io = ws.listen(httpServer, {secure: true});
     httpServer.listen(config.port, config.bind_address);
 } else {
-    var httpServer = http.createServer();
+    var httpServer = http.createServer(httpHandler);
     var io = ws.listen(httpServer, {secure: false});
     httpServer.listen(config.port, config.bind_address);
 }
-io.sockets.on('connection', function (websocket) {
+io.of('/kiwi').on('connection', function (websocket) {
     websocket.on('irc connect', function (nick, host, port, ssl, callback) {
         var ircSocket;
         //setup IRC connection