From 7d1c3ee9f22c585073db4c1b0cae43cee2bffe31 Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Tue, 19 Jul 2011 06:20:58 +0100 Subject: [PATCH] Working Kiwi IRC with no PHP needed. --- index.php | 2 +- js/gateway.js | 24 ++++++++------ node/client/index.html.jade | 4 +-- node/config.json | 3 +- node/kiwi.js | 66 +++++++++++++++++++++++++++++++++++-- 5 files changed, 82 insertions(+), 17 deletions(-) diff --git a/index.php b/index.php index 1c42acd..6e09f2f 100644 --- a/index.php +++ b/index.php @@ -66,7 +66,7 @@ var agent = ''; var touchscreen = ; var init_data = {}; - var kiwi_server = ':7777/'; + var kiwi_server = ':7777/kiwi'; var touch_scroll; $(document).ready(function(){ diff --git a/js/gateway.js b/js/gateway.js index 97f6800..5fe294c 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -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); diff --git a/node/client/index.html.jade b/node/client/index.html.jade index ca688ca..c76a7b9 100644 --- a/node/client/index.html.jade +++ b/node/client/index.html.jade @@ -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(){ diff --git a/node/config.json b/node/config.json index 66b6c2b..a80939f 100644 --- a/node/config.json +++ b/node/config.json @@ -5,5 +5,6 @@ "ssl_key": "server.key", "ssl_cert": "cert.pem", "quit_message": "KiwiIRC", - "cap_options": [] + "cap_options": [], + "handle_http": true } diff --git a/node/kiwi.js b/node/kiwi.js index b52a8a9..f40ae80 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -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 -- 2.25.1