From f4f7781b8543d07555bc8ff193b49e566a2fbc1c Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Thu, 14 Jul 2011 16:33:18 +0100 Subject: [PATCH] /quit now handled properly. Fixed indentation in kiwi.js Updated gateway.js to use websocket encryption --- js/front.js | 5 +- js/gateway.js | 299 +++++++++++++++++++++++++------------------------- node/kiwi.js | 76 +++++++------ 3 files changed, 200 insertions(+), 180 deletions(-) diff --git a/js/front.js b/js/front.js index a86d17a..cb979d3 100644 --- a/js/front.js +++ b/js/front.js @@ -199,7 +199,10 @@ var front = { //front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'color:green;'); front.cur_channel.addMsg(null, ' ', '* ' + gateway.nick + ' ' + msg.substring(4), 'action', 'color:#555;'); break; - + case '/quit': + gateway.quit(msg.split(" ",2)[1]); + break; + default: //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1)); gateway.raw(msg.substring(1)); diff --git a/js/gateway.js b/js/gateway.js index 200ade6..3923768 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -1,168 +1,173 @@ +/*jslint browser: true, confusion: true, sloppy: true, maxerr: 50, indent: 4 */ +/*globals io, $ */ var gateway = { - revision: 16, + revision: 16, - nick: 'kiwi', - session_id: null, - syncing: false, - channel_prefix: '#', - network_name: '', - user_prefixes: [], + nick: 'kiwi', + session_id: null, + syncing: false, + channel_prefix: '#', + network_name: '', + user_prefixes: [], socket: null, - start: function () { - gateway.socket = io.connect('http://192.168.1.127:7777/'); + start: function () { + gateway.socket = io.connect('wss://192.168.1.127:7777/'); 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 + }); }); - }, + }, - connect: function (host, port, ssl, callback) { + connect: function (host, port, ssl, callback) { gateway.socket.emit('irc connect', this.nick, host, port, ssl, callback); }, - /* - Events: - msg - action - server_connect - options - motd - notice - userlist - nick - join - topic - part - kick - quit - whois - syncchannel_redirect - debug - */ - parse: function (item) { - if (item.event !== undefined) { - $(gateway).trigger("on" + item.event, item); - - switch (item.event) { - case 'options': - $.each(item.options, function (name, value) { - switch (name) { - case 'CHANTYPES': - gateway.channel_prefix = value.charAt(0); - break; - case 'NETWORK': - gateway.network_name = value; - break; - case 'PREFIX': - gateway.user_prefixes = value; - break; - } - }); - break; - - case 'sync': - if (gateway.onSync && gateway.syncing) { - gateway.syncing = false; - gateway.onSync(item); - } - break; - } - } - }, - - sendData: function () {}, - - sync: function (callback) { - if (this.session_id === null) { - return; - } + /* + Events: + msg + action + server_connect + options + motd + notice + userlist + nick + join + topic + part + kick + quit + whois + syncchannel_redirect + debug + */ + parse: function (item) { + if (item.event !== undefined) { + $(gateway).trigger("on" + item.event, item); - var data = { - method: 'sync', - args: {} - }; + switch (item.event) { + case 'options': + $.each(item.options, function (name, value) { + switch (name) { + case 'CHANTYPES': + gateway.channel_prefix = value.charAt(0); + break; + case 'NETWORK': + gateway.network_name = value; + break; + case 'PREFIX': + gateway.user_prefixes = value; + break; + } + }); + break; - gateway.syncing = true; - gateway.sendData(data, callback); - }, - - debug: function (callback) { - var data = { - method: 'debug', - args: {} - }; - - gateway.sendData(data, callback); - }, - - - msg: function (s_target, s_msg, callback) { - var data = { - method: 'msg', - args: { - target: s_target, - msg: s_msg - } - }; - - gateway.sendData(data, callback); - }, - - action: function (s_target, s_msg, callback) { - var data = { - method: 'action', - args: { - target: s_target, - msg: s_msg - } - }; - - gateway.sendData(data, callback); - }, - - - join: function (s_channel, callback) { - var data = { - method: 'join', - args: { - channel: s_channel - } - }; - - gateway.sendData(data, callback); - }, - - - raw: function (v_data, callback) { - var data = { - method: 'raw', - args: { - data: v_data - } - }; - - gateway.sendData(data, callback); - }, - - - quit: function (msg, callback) { - //alert("closing"); - msg = msg || ""; - var data = { - method: 'quit', - args: { - message: msg - } - }; - - gateway.sendData(data, callback); - } - + case 'sync': + if (gateway.onSync && gateway.syncing) { + gateway.syncing = false; + gateway.onSync(item); + } + break; + } + } + }, + + sendData: function () {}, + + sync: function (callback) { + if (this.session_id === null) { + return; + } + var data = { + method: 'sync', + args: {} + }; + gateway.syncing = true; + gateway.sendData(data, callback); + }, + + debug: function (callback) { + var data = { + method: 'debug', + args: {} + }; + + gateway.sendData(data, callback); + }, + + + msg: function (s_target, s_msg, callback) { + var data = { + method: 'msg', + args: { + target: s_target, + msg: s_msg + } + }; + + gateway.sendData(data, callback); + }, + + action: function (s_target, s_msg, callback) { + var data = { + method: 'action', + args: { + target: s_target, + msg: s_msg + } + }; + + gateway.sendData(data, callback); + }, + + + join: function (s_channel, callback) { + var data = { + method: 'join', + args: { + channel: s_channel + } + }; + + gateway.sendData(data, callback); + }, + + + raw: function (v_data, callback) { + var data = { + method: 'raw', + args: { + data: v_data + } + }; + + gateway.sendData(data, callback); + }, + + + quit: function (msg, callback) { + //alert("closing"); + msg = msg || ""; + var data = { + method: 'quit', + args: { + message: msg + } + }; + + gateway.sendData(data, callback); + } + + + }; diff --git a/node/kiwi.js b/node/kiwi.js index dc586d3..0558546 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -1,3 +1,5 @@ +/*jslint regexp: true, confusion: true, undef: false, node: true, sloppy: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */ + var tls = require('tls'), net = require('net'), http = require('http'), @@ -174,7 +176,7 @@ var parseIRCMessage = function (websocket, ircSocket, data) { }; //setup websocket listener -var io = ws.listen(7777); +var io = ws.listen(7777, {secure: true}); io.sockets.on('connection', function (websocket) { websocket.on('irc connect', function (nick, host, port, ssl, callback) { var ircSocket, i; @@ -209,41 +211,51 @@ io.sockets.on('connection', function (websocket) { }); websocket.on('message', function (msg, callback) { var args; - msg.data = JSON.parse(msg.data); - args = msg.data.args; - switch (msg.data.method) { - case 'msg': - if ((args.target) && (args.msg)) { - websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n'); + try { + msg.data = JSON.parse(msg.data); + args = msg.data.args; + switch (msg.data.method) { + case 'msg': + if ((args.target) && (args.msg)) { + websocket.ircSocket.write('PRIVMSG ' + args.target + ' :' + args.msg + '\r\n'); + } + break; + case 'action': + if ((args.target) && (args.msg)) { + websocket.ircSocket.write('PRIVMSG ' + args.target + ' :ACTION ' + args.msg + '\r\n'); + } + break; + case 'raw': + websocket.ircSocket.write(args.data + '\r\n'); + break; + case 'join': + if (args.channel) { + _.each(args.channel.split(","), function (chan) { + websocket.ircSocket.write('JOIN ' + chan + '\r\n'); + }); + } + break; + case 'quit': + websocket.ircSocket.end('QUIT :' + args.message + '\r\n'); + websocket.sentQUIT = true; + websocket.ircSocket.destroySoon(); + websocket.disconnect(); + break; + default: } - break; - case 'action': - if ((args.target) && (args.msg)) { - websocket.ircSocket.write('PRIVMSG ' + args.target + ' :ACTION ' + args.msg + '\r\n'); + if ((callback) && (typeof (callback) === 'function')) { + callback(); } - break; - case 'raw': - websocket.ircSocket.write(args.data + '\r\n'); - break; - case 'join': - if (args.channel) { - _.each(args.channel.split(","), function (chan) { - websocket.ircSocket.write('JOIN ' + chan + '\r\n'); - }); - } - break; - case 'quit': - websocket.ircSocket.end('QUIT :' + args.msg + '\r\n'); - websocket.ircSocket.destroySoon(); - websocket.disconnect(); - break; - default: + } catch (e) { + console.log("Caught error: " + e); } - if ((callback) && (typeof (callback) === 'function')) { - callback(); + }); + websocket.on('disconnect', function () { + if ((!websocket.sentQUIT) && (websocket.ircSocket)) { + websocket.ircSocket.end('QUIT :KiwiIRC\r\n'); + websocket.sentQUIT = true; + websocket.ircSocket.destroySoon(); } }); }); - - -- 2.25.1