From bad1ea63aeeec92930fd16abfcb5195c62dcf04e Mon Sep 17 00:00:00 2001 From: Jack Allnutt Date: Thu, 14 Jul 2011 03:30:49 +0100 Subject: [PATCH] Now a useable back-end to the client! \o/ Started work on formatting front.js so that it passes some jslint tests. Options: /*jslint regexp: true, browser: true, confusion: true, undef: true, sloppy: true, es5: true, vars: true, forin: true, newcap: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */ --- js/front.js | 571 ++++++++++++++++++++++++++------------------------ js/gateway.js | 248 +++++----------------- node/kiwi.js | 145 +++++++++++-- 3 files changed, 476 insertions(+), 488 deletions(-) diff --git a/js/front.js b/js/front.js index 172749a..a86d17a 100644 --- a/js/front.js +++ b/js/front.js @@ -9,8 +9,8 @@ var front = { buffer: [], buffer_pos: 0, - init: function(){ - gateway.nick = 'kiwi_'+Math.ceil(100*Math.random())+Math.ceil(100*Math.random()); + init: function () { + gateway.nick = 'kiwi_' + Math.ceil(100 * Math.random()) + Math.ceil(100 * Math.random()); gateway.session_id = null; $(gateway).bind("onmsg", front.onMsg); @@ -32,7 +32,7 @@ var front = { $(gateway).bind("onchannel_redirect", front.onChannelRedirect); $(gateway).bind("ondebug", front.onDebug); - this.buffer = new Array(); + this.buffer = []; // Build the about box front.boxes.about = new box("about"); @@ -40,23 +40,25 @@ var front = { front.boxes.about.content.append("

An alternative to downloading an irc client. Kiwi IRC is the best web app you'll use for the next couple years.

"); front.boxes.about.content.append(''); - var about_info = 'UI adapted for '+agent; - if(touchscreen) about_info += ' touchscreen '; + var about_info = 'UI adapted for ' + agent; + if (touchscreen) { + about_info += ' touchscreen '; + } about_info += 'usage'; - front.boxes.about.content.append('

'+about_info+'

'); - front.boxes.about.content.append('

Front: '+front.revision+'
Gateway: '+gateway.revision+'

'); + front.boxes.about.content.append('

' + about_info + '

'); + front.boxes.about.content.append('

Front: ' + front.revision + '
Gateway: ' + gateway.revision + '

'); //$(window).bind("beforeunload", function(){ gateway.quit(); }); front.registerKeys(); - $('#kiwi .formconnectwindow').submit(function(){ + $('#kiwi .formconnectwindow').submit(function () { var netsel = $('#kiwi .formconnectwindow .network'); var nick = $('#kiwi .formconnectwindow .nick'); - if(nick.val() == ''){ + if (nick.val() === '') { nick.val('Nick please!'); nick.focus(); return false; @@ -66,8 +68,8 @@ var front = { gateway.nick = tmp[0]; front.doLayout(); try { - front.run('/connect '+netsel.val()); - } catch(e){ + front.run('/connect ' + netsel.val()); + } catch (e) { alert(e); } @@ -85,7 +87,9 @@ var front = { front.tabviewAdd('server'); // Any pre-defined nick? - if(typeof init_data.nick == "string") $('#kiwi .formconnectwindow .nick').val(init_data.nick); + if (typeof init_data.nick === "string") { + $('#kiwi .formconnectwindow .nick').val(init_data.nick); + } //gateway.session_id = 'testses'; @@ -93,13 +97,13 @@ var front = { //front.sync(); }, - doLayoutSize: function() { + doLayoutSize: function () { var kiwi = $('#kiwi'); - if(kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')){ + if (kiwi.width() < 330 && !kiwi.hasClass('small_kiwi')) { console.log("switching to small kiwi"); kiwi.removeClass('large_kiwi'); kiwi.addClass('small_kiwi'); - } else if(kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) { + } else if (kiwi.width() >= 330 && !kiwi.hasClass('large_kiwi')) { console.log("switching to large kiwi"); kiwi.removeClass('small_kiwi'); kiwi.addClass('large_kiwi'); @@ -109,25 +113,26 @@ var front = { var ul = $('#kiwi .userlist'); var top = ul.css('margin-top').replace('px', '') + ct.css('border-bottom-width').replace('px', ''); //top = parseInt(ct.offset().top) + parseInt(ct.height()) + parseInt(top); - top = parseInt(ct.height()) + parseInt(top); + top = parseInt(ct.height(), 10) + parseInt(top, 10); - $('#kiwi .messages').css('top', top+"px"); - $('#kiwi .userlist').css('top', top+"px"); + $('#kiwi .messages').css('top', top + "px"); + $('#kiwi .userlist').css('top', top + "px"); }, - doLayout: function(){ + doLayout: function () { $('#kiwi .msginput .nick a').text(gateway.nick); $('#kiwi_msginput').val(' '); $('#kiwi_msginput').focus(); }, - joinChannel: function(chan_name){ - var chans = chan_name.split(','); - for(var i in chans){ + joinChannel: function (chan_name) { + var chans = chan_name.split(','), + i; + for (i in chans) { chan = chans[i]; - if(front.tabviews[chan.toLowerCase()] == undefined){ + if (front.tabviews[chan.toLowerCase()] === undefined) { gateway.join(chan); front.tabviewAdd(chan); } else { @@ -137,315 +142,328 @@ var front = { }, - run: function(msg){ - if(msg.substring(0,1) == '/'){ + run: function (msg) { + if (msg.substring(0, 1) === '/') { var parts = msg.split(' '); - switch(parts[0]){ - case '/j': - case '/join': - front.joinChannel(parts[1]); - break; - - case '/connect': - case '/server': - if(parts[1] == undefined){ - alert('Usage: /connect servername [port]'); - break; - } - - if(parts[2] == undefined) parts[2] = 6667; - front.cur_channel.addMsg(null, ' ', '=== Connecting to '+parts[1]+'...', 'status'); - gateway.connect(parts[1], parts[2], 0); - break; - - case '/part': - if(typeof parts[1] == "undefined"){ - gateway.raw(msg.substring(1)+' '+front.cur_channel.name); - } else { - gateway.raw(msg.substring(1)); - } - break; - - case '/names': - if(typeof parts[1] != "undefined"){ - gateway.raw(msg.substring(1)); - } - break; - - case '/debug': - gateway.debug(); - break; - - case '/q': - case '/query': - if(typeof parts[1] != "undefined"){ - front.tabviewAdd(parts[1]); - } - break; - - case '/quote': - gateway.raw(msg.replace(/^\/quote /i,'')); - break; - - case '/me': - gateway.action(front.cur_channel.name, msg.substring(4)); - //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;'); + switch (parts[0]) { + case '/j': + case '/join': + front.joinChannel(parts[1]); + break; + + case '/connect': + case '/server': + if (parts[1] === undefined) { + alert('Usage: /connect servername [port]'); break; - - default: - //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1)); + } + + if (parts[2] === undefined) { + parts[2] = 6667; + } + front.cur_channel.addMsg(null, ' ', '=== Connecting to ' + parts[1] + '...', 'status'); + gateway.connect(parts[1], parts[2], 0); + break; + + case '/part': + if (typeof parts[1] === "undefined") { + gateway.raw(msg.substring(1) + ' ' + front.cur_channel.name); + } else { gateway.raw(msg.substring(1)); + } + break; + + case '/names': + if (typeof parts[1] !== "undefined") { + gateway.raw(msg.substring(1)); + } + break; + + case '/debug': + gateway.debug(); + break; + + case '/q': + case '/query': + if (typeof parts[1] !== "undefined") { + front.tabviewAdd(parts[1]); + } + break; + + case '/quote': + gateway.raw(msg.replace(/^\/quote /i, '')); + break; + + case '/me': + gateway.action(front.cur_channel.name, msg.substring(4)); + //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; + + default: + //front.cur_channel.addMsg(null, ' ', '--> Invalid command: '+parts[0].substring(1)); + gateway.raw(msg.substring(1)); } } else { //alert('Sending message: '+msg); - if(msg.trim() == '') return; + if (msg.trim() === '') { + return; + } gateway.msg(front.cur_channel.name, msg); var d = new Date(); - var d = d.getHours() + ":" + d.getMinutes(); + d = d.getHours() + ":" + d.getMinutes(); //front.addMsg(d, gateway.nick, msg); front.cur_channel.addMsg(null, gateway.nick, msg); } }, - onMsg: function(e, data){ + onMsg: function (e, data) { + var destination; // Is this message from a user? - if(data.channel == gateway.nick){ - var destination = data.nick.toLowerCase(); + if (data.channel === gateway.nick) { + destination = data.nick.toLowerCase(); } else { - var destination = data.channel.toLowerCase(); + destination = data.channel.toLowerCase(); } - if(!front.tabviewExists(destination)) front.tabviewAdd(destination); + if (!front.tabviewExists(destination)) { + front.tabviewAdd(destination); + } front.tabviews[destination].addMsg(null, data.nick, data.msg); }, - onDebug: function(e, data){ - if(!front.tabviewExists('kiwi_debug')) front.tabviewAdd('kiwi_debug'); - front.tabviews['kiwi_debug'].addMsg(null, ' ', data.msg); + onDebug: function (e, data) { + if (!front.tabviewExists('kiwi_debug')) { + front.tabviewAdd('kiwi_debug'); + } + front.tabviews.kiwi_debug.addMsg(null, ' ', data.msg); }, - onAction: function(e, data){ + onAction: function (e, data) { + var destination; // Is this message from a user? - if(data.channel == gateway.nick){ - var destination = data.nick; + if (data.channel === gateway.nick) { + destination = data.nick; } else { - var destination = data.channel; + destination = data.channel; } - if(!front.tabviewExists(destination)) front.tabviewAdd(destination); - front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* '+data.nick+' '+data.msg, 'action', 'color:#555;'); + if (!front.tabviewExists(destination)) { + front.tabviewAdd(destination); + } + front.tabviews[destination.toLowerCase()].addMsg(null, ' ', '* ' + data.nick + ' ' + data.msg, 'action', 'color:#555;'); }, - onTopic: function(e, data){ - if(front.tabviewExists(data.channel)){ + onTopic: function (e, data) { + if (front.tabviewExists(data.channel)) { front.tabviews[data.channel.toLowerCase()].changeTopic(data.topic); } }, - onNotice: function(e, data){ - var nick = (data.nick=="") ? "" : '['+data.nick+']'; - if(data.channel != undefined){ + onNotice: function (e, data) { + var nick = (data.nick === "") ? "" : '[' + data.nick + ']'; + if (data.channel !== undefined) { //alert('notice for '+data.channel); - if(front.tabviewExists(data.channel)){ + if (front.tabviewExists(data.channel)) { front.tabviews[data.channel.toLowerCase()].addMsg(null, nick, data.msg, 'notice'); } } else { //alert('direct notice'); - front.tabviews['server'].addMsg(null, nick, data.msg, 'notice'); + front.tabviews.server.addMsg(null, nick, data.msg, 'notice'); } }, - onConnect: function(e, data){ - if(data.connected){ - front.tabviews['server'].addMsg(null, ' ', '=== Connected OK :)', 'status'); - if(typeof init_data.channel == "string"){ + onConnect: function (e, data) { + if (data.connected) { + front.tabviews.server.addMsg(null, ' ', '=== Connected OK :)', 'status'); + if (typeof init_data.channel === "string") { front.joinChannel(init_data.channel); } } else { - front.tabviews['server'].addMsg(null, ' ', '=== Failed to connect :(', 'status'); + front.tabviews.server.addMsg(null, ' ', '=== Failed to connect :(', 'status'); } }, - onOptions: function(e, data){ - if(typeof gateway.network_name == "string" && gateway.network_name != ""){ - front.tabviews['server'].tab.text(gateway.network_name); + onOptions: function (e, data) { + if (typeof gateway.network_name === "string" && gateway.network_name !== "") { + front.tabviews.server.tab.text(gateway.network_name); } }, - onMOTD: function(e, data){ - front.tabviews['server'].addMsg(null, data.server, data.msg, 'motd'); + onMOTD: function (e, data) { + front.tabviews.server.addMsg(null, data.server, data.msg, 'motd'); }, - onWhois: function(e, data){ + onWhois: function (e, data) { front.cur_channel.addMsg(null, data.nick, data.msg, 'whois'); }, - onUserList: function(e, data){ - if(front.tabviews[data.channel.toLowerCase()] == undefined) return; - + onUserList: function (e, data) { + if (front.tabviews[data.channel.toLowerCase()] === undefined) { + return; + } var ul = front.tabviews[data.channel.toLowerCase()].userlist; - if(!document.userlist_updating){ + if (!document.userlist_updating) { document.userlist_updating = true; ul.empty(); } - $.each(data.users, function(i,item){ + $.each(data.users, function (i, item) { var nick = i; //i.match(/^.+!/g); var mode = item; - $('
  • '+mode+nick+'
  • ').appendTo(ul); + $('
  • ' + mode + nick + '
  • ').appendTo(ul); }); front.tabviews[data.channel.toLowerCase()].userlistSort(); }, - onUserListEnd: function(e, data){ + onUserListEnd: function (e, data) { document.userlist_updating = false; }, - onJoin: function(e, data){ - if(!front.tabviewExists(data.channel)){ + onJoin: function (e, data) { + if (!front.tabviewExists(data.channel)) { front.tabviewAdd(data.channel.toLowerCase()); } - if(data.nick == gateway.nick) return; // Not needed as it's already in nicklist - front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> '+data.nick+' has joined', 'action', 'color:#009900;'); - $('
  • '+data.nick+'
  • ').appendTo(front.tabviews[data.channel.toLowerCase()].userlist); + if (data.nick === gateway.nick) { + return; // Not needed as it's already in nicklist + } + front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '--> ' + data.nick + ' has joined', 'action', 'color:#009900;'); + $('
  • ' + data.nick + '
  • ').appendTo(front.tabviews[data.channel.toLowerCase()].userlist); front.tabviews[data.channel.toLowerCase()].userlistSort(); }, - onPart: function(e, data){ - if(front.tabviewExists(data.channel)){ + onPart: function (e, data) { + if (front.tabviewExists(data.channel)) { // If this is us, close the tabview - if(data.nick == gateway.nick){ + if (data.nick === gateway.nick) { front.tabviews[data.channel.toLowerCase()].close(); - front.tabviews['server'].show(); + front.tabviews.server.show(); return; } - front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- '+data.nick+' has left ('+data.message+')', 'action', 'color:#990000;'); - front.tabviews[data.channel.toLowerCase()].userlist.children().each(function(){ - if($(this).text() == data.nick){ + front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.nick + ' has left (' + data.message + ')', 'action', 'color:#990000;'); + front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () { + if ($(this).text() === data.nick) { $(this).remove(); } }); } }, - onKick: function(e, data){ - if(front.tabviewExists(data.channel)){ - // If this is us, close the tabview - if(data.kicked == gateway.nick){ + onKick: function (e, data) { + if (front.tabviewExists(data.channel)) { + // If this is us, close the tabvi ew + if (data.kicked === gateway.nick) { front.tabviews[data.channel.toLowerCase()].close(); return; } - front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- '+data.kicked+' kicked by '+data.nick+'('+data.message+')', 'action', 'color:#990000;'); - front.tabviews[data.channel.toLowerCase()].userlist.children().each(function(){ - if($(this).text() == data.nick){ + front.tabviews[data.channel.toLowerCase()].addMsg(null, ' ', '<-- ' + data.kicked + ' kicked by ' + data.nick + '(' + data.message + ')', 'action', 'color:#990000;'); + front.tabviews[data.channel.toLowerCase()].userlist.children().each(function () { + if ($(this).text() === data.nick) { $(this).remove(); } }); } }, - onNick: function(e, data){ - if(data.nick == gateway.nick){ + onNick: function (e, data) { + if (data.nick === gateway.nick) { gateway.nick = data.newnick; front.doLayout(); } - $.each(front.tabviews, function(i,item){ - $.each(front.tabviews, function(i,item){ + $.each(front.tabviews, function (i, item) { + $.each(front.tabviews, function (i, item) { item.changeNick(data.newnick, data.nick); }); }); }, - onQuit: function(e, data){ - $.each(front.tabviews, function(i,item){ - $.each(front.tabviews, function(i,item){ - item.userlist.children().each(function(){ - if($(this).text() == data.nick){ + onQuit: function (e, data) { + $.each(front.tabviews, function (i, item) { + $.each(front.tabviews, function (i, item) { + item.userlist.children().each(function () { + if ($(this).text() === data.nick) { $(this).remove(); - item.addMsg(null, ' ', '<-- '+data.nick+' has quit ('+data.message+')', 'action', 'color:#990000;'); + item.addMsg(null, ' ', '<-- ' + data.nick + ' has quit (' + data.message + ')', 'action', 'color:#990000;'); } }); }); }); }, - onChannelRedirect: function(e, data){ + onChannelRedirect: function (e, data) { front.tabviews[data.from.toLowerCase()].close(); front.tabviewAdd(data.to.toLowerCase()); - front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from '+data.from, 'action'); + front.tabviews[data.to.toLowerCase()].addMsg(null, ' ', '=== Redirected from ' + data.from, 'action'); }, - registerKeys: function(){ - $('#kiwi_msginput').bind('keydown', function(e){ + registerKeys: function () { + $('#kiwi_msginput').bind('keydown', function (e) { //$('input').keypress(function(e){ - switch(e.which){ - case 27: // escape - return false; - break; - case 13: // return - var msg = $('#kiwi_msginput').val(); - msg = msg.trim(); - - front.buffer.push(msg); - front.buffer_pos = front.buffer.length; - - front.run(msg); - $('#kiwi_msginput').val(''); - - break; + switch (e.which) { + case 27: // escape + return false; + case 13: // return + var msg = $('#kiwi_msginput').val(); + msg = msg.trim(); + + front.buffer.push(msg); + front.buffer_pos = front.buffer.length; + + front.run(msg); + $('#kiwi_msginput').val(''); + + break; + + case 38: // up + if (front.buffer_pos > 0) { + front.buffer_pos--; + $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); + } + break; + case 40: // down + if (front.buffer_pos < front.buffer.length) { + front.buffer_pos++; + $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); + } + break; + + case 9: // tab + // Get possible autocompletions + var data = []; + front.cur_channel.userlist.children().each(function () { + nick = front.nickStripPrefix($('a.nick', this).text()); + data.push(nick); + }); + + // Do the autocomplete + if (this.value.length === this.selectionStart && this.value.length === this.selectionEnd) { + var candidates = []; - case 38: // up - if(front.buffer_pos > 0){ - front.buffer_pos--; - $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); - } - break; - case 40: // down - if(front.buffer_pos < front.buffer.length){ - front.buffer_pos++; - $('#kiwi_msginput').val(front.buffer[front.buffer_pos]); + var word_pos = this.value.lastIndexOf(' '); + var word = ""; + if (word_pos === -1) { + word = this.value; + } else { + word = this.value.substr(word_pos); } - break; + word = word.trim(); - case 9: // tab - // Get possible autocompletions - var data = []; - front.cur_channel.userlist.children().each(function(){ - nick = front.nickStripPrefix($('a.nick', this).text()); - data.push(nick); - }); - - // Do the autocomplete - if (this.value.length == this.selectionStart && this.value.length == this.selectionEnd) { - var candidates = []; - - var word_pos = this.value.lastIndexOf(' '); - var word = ""; - if(word_pos == -1){ - word = this.value; - } else { - word = this.value.substr(word_pos); - } - word = word.trim(); - - // filter data to find only strings that start with existing value - for (var i=0; i < data.length; i++) { - if (data[i].indexOf(word) == 0 && data[i].length > word.length) - candidates.push(data[i]); - } - - if (candidates.length > 0) { - // some candidates for autocompletion are found - this.value = this.value.substring(0, word_pos) + ' ' + candidates[0]+': '; - this.selectionStart = this.value.length; - } + // filter data to find only strings that start with existing value + for (i = 0; i < data.length; i++) { + if (data[i].indexOf(word) === 0 && data[i].length > word.length) { + candidates.push(data[i]); + } } - return false; - break; + if (candidates.length > 0) { + // some candidates for autocompletion are found + this.value = this.value.substring(0, word_pos) + ' ' + candidates[0] + ': '; + this.selectionStart = this.value.length; + } + } + return false; } }); - $('#kiwi .control .msginput .nick').click(function(){ + $('#kiwi .control .msginput .nick').click(function () { var html = '
    \ Your new nick:
    \
    \ @@ -454,12 +472,12 @@ var front = {
    \
    '; $('#kiwi').append(html); - $('#kiwi .form_newnick').submit(function(){ - front.run('/NICK '+$('#kiwi .txtnewnick').val()); + $('#kiwi .form_newnick').submit(function () { + front.run('/NICK ' + $('#kiwi .txtnewnick').val()); $('#kiwi .newnick').remove(); return false; }); - $('#kiwi .cancelnewnick').click(function(){ + $('#kiwi .cancelnewnick').click(function () { $('#kiwi .newnick').remove(); }); @@ -471,8 +489,10 @@ var front = { - $('#kiwi .plugins .load_plugin_file').click(function(){ - if(typeof front.boxes.plugins != "undefined") return; + $('#kiwi .plugins .load_plugin_file').click(function () { + if (typeof front.boxes.plugins !== "undefined") { + return; + } var html = '
    \

    Kiwi plugins

    \ @@ -491,40 +511,43 @@ var front = {
    '; front.boxes.plugins = new box("plugin_file"); front.boxes.plugins.content.html(html); - front.boxes.plugins.box.css('top', -(front.boxes.plugins.height+40)); + front.boxes.plugins.box.css('top', -(front.boxes.plugins.height + 40)); // Populate the plugin list.. var lst = $('#plugin_list'); lst.find('option').remove(); - for(var i in plugins.privmsg){ - var txt = plugins.privmsg[i].name; - lst.append(''); + var j; + for (j in plugins.privmsg) { + var txt = plugins.privmsg[j].name; + lst.append(''); } // Event bindings - $('#kiwi .plugin_file').submit(function(){ - $.getJSON($('.txtpluginfile').val(), function(data) { + $('#kiwi .plugin_file').submit(function () { + $.getJSON($('.txtpluginfile').val(), function (data) { var plg = {}; plg.name = data.name; - eval("plg.onprivmsg = "+data.onprivmsg); - eval("plg.onload = "+data.onload); - eval("plg.onunload = "+data.onunload); + eval("plg.onprivmsg = " + data.onprivmsg); + eval("plg.onload = " + data.onload); + eval("plg.onunload = " + data.onunload); plugins.privmsg.push(plg); - if(plg.onload instanceof Function) plg.onload(); + if (plg.onload instanceof Function) { + plg.onload(); + } }); return false; }); - $('#kiwi .cancelpluginfile').click(function(){ + $('#kiwi .cancelpluginfile').click(function () { front.boxes.plugins.destroy(); }); - $('#kiwi #plugins_list_unload').click(function(){ + $('#kiwi #plugins_list_unload').click(function () { var selected_plugin = $('#plugin_list').val(); console.log("removing plugin: "+selected_plugin); - for(var i in plugins.privmsg){ - if(plugins.privmsg[i].name == selected_plugin){ - if(plugins.privmsg[i].onunload instanceof Function) + for (var i in plugins.privmsg) { + if (plugins.privmsg[i].name === selected_plugin) { + if (plugins.privmsg[i].onunload instanceof Function) plugins.privmsg[i].onunload(); delete plugins.privmsg[i]; @@ -536,11 +559,11 @@ var front = { }); - $('#kiwi .plugins .reload_css').click(function(){ + $('#kiwi .plugins .reload_css').click(function () { var links = document.getElementsByTagName("link"); - for (var i=0; i < links.length; i++){ - if(links[i].rel === "stylesheet"){ - if(links[i].href.indexOf("?")===-1){ + for (var i=0; i < links.length; i++) { + if (links[i].rel === "stylesheet") { + if (links[i].href.indexOf("?")===-1) { links[i].href += "?"; } links[i].href += "x"; @@ -549,38 +572,38 @@ var front = { }); - $('#kiwi .about .about_close').click(function(){ + $('#kiwi .about .about_close').click(function () { $('#kiwi .about').css('display', 'none'); }); - $('#kiwi .poweredby').click(function(){ + $('#kiwi .poweredby').click(function () { $('#kiwi .about').css('display', 'block'); }); }, - tabviewExists: function(name){ + tabviewExists: function (name) { return !(front.tabviews[name.toLowerCase()] == undefined); }, - tabviewAdd: function(v_name){ - if(v_name.charAt(0) == gateway.channel_prefix){ + tabviewAdd: function (v_name) { + if (v_name.charAt(0) == gateway.channel_prefix) { var re = new RegExp(gateway.channel_prefix,"g"); var htmlsafe_name = v_name.replace(re, 'pre'); - htmlsafe_name = "chan_"+htmlsafe_name; + htmlsafe_name = "chan_" + htmlsafe_name; } else { - var htmlsafe_name = 'query_'+v_name; + var htmlsafe_name = 'query_' + v_name; } - var tmp_divname = 'kiwi_window_'+htmlsafe_name; - var tmp_userlistname = 'kiwi_userlist_'+htmlsafe_name; - var tmp_tabname = 'kiwi_tab_'+htmlsafe_name + var tmp_divname = 'kiwi_window_' + htmlsafe_name; + var tmp_userlistname = 'kiwi_userlist_' + htmlsafe_name; + var tmp_tabname = 'kiwi_tab_' + htmlsafe_name - $('#kiwi').append('
    '); - $('#kiwi .userlist').append(''); - $('#kiwi .windowlist ul').append('
  • '+v_name+'
  • '); + $('#kiwi').append('
    '); + $('#kiwi .userlist').append(''); + $('#kiwi .windowlist ul').append('
  • ' + v_name + '
  • '); //$('#kiwi .windowlist ul .window_'+v_name).click(function(){ front.windowShow(v_name); }); //front.windowShow(v_name); @@ -591,7 +614,7 @@ var front = { front.tabviews[v_name.toLowerCase()].tab = $('#'+tmp_tabname); front.tabviews[v_name.toLowerCase()].show(); - if(typeof registerTouches == "function"){ + if (typeof registerTouches === "function") { //alert("Registering touch interface"); //registerTouches($('#'+tmp_divname)); registerTouches(document.getElementById(tmp_divname)); @@ -606,46 +629,46 @@ var front = { }, - userClick: function(item){ + userClick: function (item) { // Remove any existing userboxes $('#kiwi .userbox').remove(); var li = $(item).parent(); var html = '
    \ - \ + \ Message\ Info\
    '; li.append(html); - $('#kiwi .userbox .userbox_query').click(function(ev){ + $('#kiwi .userbox .userbox_query').click(function (ev) { var nick = $('#kiwi .userbox_nick').val(); - front.run('/query '+nick); + front.run('/query ' + nick); }); - $('#kiwi .userbox .userbox_whois').click(function(ev){ + $('#kiwi .userbox .userbox_whois').click(function (ev) { var nick = $('#kiwi .userbox_nick').val(); - front.run('/whois '+nick); + front.run('/whois ' + nick); }); }, - sync: function(){ + sync: function () { gateway.sync(); }, - onSync: function(e, data){ + onSync: function (e, data) { // Set any settings - if(data.nick != undefined) gateway.nick = data.nick; + if (data.nick != undefined) gateway.nick = data.nick; // Add the tabviews - if(data.tabviews != undefined){ - $.each(data.tabviews, function(i,tab){ + if (data.tabviews != undefined) { + $.each(data.tabviews, function (i, tab) { if(!front.tabviewExists(tab.name)){ - front.tabviewAdd(gateway.channel_prefix+tab.name); + front.tabviewAdd(gateway.channel_prefix + tab.name); - if(tab.userlist != undefined) - front.onUserList({'channel':gateway.channel_prefix+tab.name, 'users':tab.userlist}); + if (tab.userlist !== undefined) + front.onUserList({'channel':gateway.channel_prefix + tab.name, 'users':tab.userlist}); } }); } @@ -654,7 +677,7 @@ var front = { }, - setTopicText: function(new_topic){ + setTopicText: function (new_topic) { $('#kiwi .cur_topic').text(new_topic); }, @@ -664,29 +687,29 @@ var front = { - nickStripPrefix: function(nick){ + nickStripPrefix: function (nick) { var tmp = nick; prefix = tmp.charAt(0); - if(typeof gateway.user_prefixes[prefix] != "undefined") tmp = tmp.substring(1); + if (typeof gateway.user_prefixes[prefix] != "undefined") tmp = tmp.substring(1); return tmp; }, - nickGetPrefix: function(nick){ + nickGetPrefix: function (nick) { var tmp = nick; prefix = tmp.charAt(0); - if(typeof gateway.user_prefixes[prefix] == "undefined"){ + if (typeof gateway.user_prefixes[prefix] == "undefined") { prefix = ""; } return prefix; }, - isChannel: function(name){ + isChannel: function (name) { prefix = name.charAt(0); - if(gateway.channel_prefix.indexOf(prefix) > -1){ + if (gateway.channel_prefix.indexOf(prefix) > -1) { is_chan = true; } else { is_chan = false; @@ -950,4 +973,4 @@ box.prototype.destroy = function(){ this.box.remove(); for (var name in front.boxes) if(front.boxes[name].id = this.id) delete front.boxes[name]; } -box.prototype.height = function(){ return this.box.height(); } \ No newline at end of file +box.prototype.height = function(){ return this.box.height(); } diff --git a/js/gateway.js b/js/gateway.js index 065977f..200ade6 100644 --- a/js/gateway.js +++ b/js/gateway.js @@ -9,140 +9,21 @@ var gateway = { network_name: '', user_prefixes: [], socket: null, - - - /*connect: function(s_host, s_port, s_ssl, callback){ - var data = { - method: 'connect', - args: { - server: s_host, - port: s_port, - ssl: s_ssl, - nick: this.nick - } - }; - - //$.post('poll.php', data, callback, 'json'); - gateway.sendData(data, callback); - },*/ - connect: function(host,port,ssl,callback) { - socket.emit('irc connect', this.nick, host, port, ssl, function() { console.log('callback'); callback}); - }, - - - /*start: function(){ - if(typeof WebSocket != "undefined"){ - //alert("Starting websocket support.."); - gateway.socket(); - } else { - //alert("Starting polling support.."); - gateway.poll(); - } - },*/ - start: function() { - socket = io.connect('http://192.168.1.127:7777/'); - socket.on('connection',function() { - gateway.sendData = function(data,callback) { - socket.send({sid:this.session_id,data:$.toJSON(data)},function(){console.log('callbacl'); callback}); - } - socket.on('message', function(msg) { - gateway.buffer += msg; - - if(gateway.buffer.indexOf("\n") > 0){ - var msgs = gateway.buffer.split("\n"); - for(var i=0; i 0){ - var msgs = gateway.buffer.split("\n"); - for(var i=0; i= 50) { + if (i++ >= 50) { websocket.emit('message', {event: 'userlist', server: '', "users": nicklist, channel: chan}); nicklist = {}; i = 0; } - i++; }); - if (nicklist.length > 0) { + if (i > 0) { websocket.emit('message', {event: 'userlist', server: '', "users": nicklist, channel: chan}); + } else { + console.log("oops"); } break; - case RPL_ENDOFNAMES: - chan = msg.params.split(" ")[1]; - websocket.emit('message', {event: 'userlist_end', server: '', channel: chan}); + case ircNumerics.RPL_ENDOFNAMES: + websocket.emit('message', {event: 'userlist_end', server: '', channel: msg.params.split(" ")[1]}); + break; + case ircNumerics.ERR_LINKCHANNEL: + params = msg.params.split(" "); + websocket.emit('message', {event: 'channel_redirect', from: params[1], to: params[2]}); + break; + case ircNumerics.ERR_NOSUCHNICK: + //TODO: shit + break; + case 'JOIN': + websocket.emit('message', {event: 'join', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.trailing}); + if (msg.nick === ircSocket.IRC.nick) { + ircSocket.write('NAMES ' + msg.trailing + '\r\n'); + } + break; + case 'PART': + websocket.emit('message', {event: 'part', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), message: msg.trailing}); + break; + case 'KICK': + params = msg.params.split(" "); + websocket.emit('message', {event: 'kick', kicked: params[1], nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: params[0].trim(), message: msg.trailing}); + break; + case 'QUIT': + websocket.emit('message', {event: 'quit', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, message: msg.trailing}); + break; + case 'NOTICE': + websocket.emit('message', {event: 'notice', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing}); + break; + case 'NICK': + websocket.emit('message', {event: 'nick', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, newnick: msg.trailing}); + break; + case 'TOPIC': + websocket.emit('message', {event: 'topic', nick: msg.nick, channel: msg.params, topic: msg.trailing}); + break; + case ircNumerics.RPL_TOPIC: + websocket.emit('message', {event: 'topic', nick: '', channel: msg.params.split(" ")[1], topic: msg.trailing}); + break; + case 'MODE': + opts = msg.params.split(" "); + params = {event: 'mode', nick: msg.nick}; + switch (opts.length) { + case 1: + params.effected_nick = opts[0]; + params.mode = msg.trailing; + break; + case 2: + params.channel = opts[0]; + params.mode = opts[1]; + break; + default: + params.channel = opts[0]; + params.mode = opts[1]; + params.effected_nick = opts[2]; + break; + } + websocket.emit('message', params); + break; + case 'PRIVMSG': + websocket.emit('message', {event: 'msg', nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing}); break; } } else { @@ -118,18 +185,20 @@ io.sockets.on('connection', function (websocket) { ircSocket = tls.connect(port, host); } ircSocket.setEncoding('ascii'); - ircSocket.ircServer = {options: {}}; + ircSocket.IRC = {options: {}}; + websocket.ircSocket = ircSocket; ircSocket.on('data', function (data) { data = data.split("\r\n"); for (i = 0; i < data.length; i++) { if (data[i]) { - console.log(data[i] + '\r\n'); + console.log("->" + data[i]); parseIRCMessage(websocket, ircSocket, data[i]); } } }); + ircSocket.IRC.nick = nick; // Send the login data ircSocket.write('NICK ' + nick + '\r\n'); ircSocket.write('USER ' + nick + '_kiwi 0 0 :' + nick + '\r\n'); @@ -139,7 +208,37 @@ io.sockets.on('connection', function (websocket) { } }); websocket.on('message', function (msg, callback) { - console.log(msg); + 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'); + } + 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.msg + '\r\n'); + websocket.ircSocket.destroySoon(); + websocket.disconnect(); + break; + default: + } if ((callback) && (typeof (callback) === 'function')) { callback(); } -- 2.25.1