/list response now shows in a new tab
[KiwiIRC.git] / node / app.js
index 6ef776b61d98ce92bbe19df1ed16f330f6212587..6025dd41a62a7bfd6f632d8f27142b224e87ddff 100644 (file)
@@ -1,3 +1,5 @@
+/*jslint sloppy: true, continue: true, forin: true, regexp: true, undef: false, node: true, nomen: true, plusplus: true, maxerr: 50, indent: 4 */
+/*globals kiwi_root */
 var tls = null;
 var net = null;
 var http = null;
@@ -28,7 +30,7 @@ this.init = function (objs) {
        _ = objs._;
        starttls = objs.starttls;
        kiwi = require('./kiwi.js');
-}
+};
 
 
 
@@ -40,7 +42,7 @@ this.init = function (objs) {
  */
 this.setTitle = function () {
        process.title = 'kiwiirc';
-}
+};
 
 this.changeUser = function () {
     if (typeof kiwi.config.group !== 'undefined' && kiwi.config.group !== '') {
@@ -80,6 +82,9 @@ var ircNumerics = {
     RPL_WHOISIDLE:          '317',
     RPL_ENDOFWHOIS:         '318',
     RPL_WHOISCHANNELS:      '319',
+    RPL_LISTSTART:          '321',
+    RPL_LIST:               '322',
+    RPL_LISTEND:            '323',
     RPL_NOTOPIC:            '331',
     RPL_TOPIC:              '332',
     RPL_NAMEREPLY:          '353',
@@ -106,7 +111,7 @@ var ircNumerics = {
 
 this.parseIRCMessage = function (websocket, ircSocket, data) {
     /*global ircSocketDataHandler */
-    var msg, regex, opts, options, opt, i, j, matches, nick, users, chan, channel, params, prefix, prefixes, nicklist, caps, rtn, obj;
+    var msg, regex, opts, options, opt, i, j, matches, nick, users, chan, channel, params, prefix, prefixes, nicklist, caps, rtn, obj, tmp, namespace;
     //regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?([a-z0-9]+)(?:(?: ([^:]+))?(?: :(.+))?)$/i;
     //regex = /^(?::(\S+) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
     regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
@@ -172,6 +177,38 @@ this.parseIRCMessage = function (websocket, ircSocket, data) {
         case ircNumerics.RPL_WHOISMODES:
             websocket.sendClientEvent('whois', {server: '', nick: msg.params.split(" ", 3)[1], "msg": msg.trailing});
             break;
+
+        case ircNumerics.RPL_LISTSTART:
+            (function () {
+                websocket.sendClientEvent('list_start', {server: ''});
+            }());
+            break;
+        case ircNumerics.RPL_LISTEND:
+            (function () {
+                websocket.sendClientEvent('list_end', {server: ''});
+            }());
+            break;
+        
+        case ircNumerics.RPL_LIST:
+            (function () {
+                var parts, channel, num_users, modes, topic;
+
+                parts = msg.params.split(' ');
+                channel = parts[1];
+                num_users = parts[2];
+                modes = msg.trailing.split(' ', 1);
+                topic = msg.trailing.substring(msg.trailing.indexOf(' ') + 1);
+
+                websocket.sendClientEvent('list_channel', {
+                    server: '',
+                    channel: channel,
+                    topic: topic,
+                    modes: modes,
+                    num_users: num_users
+                });
+            }());
+            break;
+
         case ircNumerics.RPL_WHOISIDLE:
             params = msg.params.split(" ", 4);
             rtn = {server: '', nick: params[1], idle: params[2]};
@@ -293,6 +330,11 @@ this.parseIRCMessage = function (websocket, ircSocket, data) {
                 // It's a CTCP request
                 if (msg.trailing.substr(1, 6) === 'ACTION') {
                     websocket.sendClientEvent('action', {nick: msg.nick, ident: msg.ident, hostname: msg.hostname, channel: msg.params.trim(), msg: msg.trailing.substr(7, msg.trailing.length - 2)});
+                } else if (msg.trailing.substr(1, 4) === 'KIWI') {
+                    tmp = msg.trailing.substr(6, msg.trailing.length - 2);
+                    namespace = tmp.split(' ', 1)[0];
+                    websocket.sendClientEvent('kiwi', {namespace: namespace, data: tmp.substr(namespace.length + 1)});
+                    
                 } else if (msg.trailing.substr(1, 7) === 'VERSION') {
                     ircSocket.write('NOTICE ' + msg.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1) + '\r\n');
                 } else {
@@ -462,11 +504,12 @@ this.httpHandler = function (request, response) {
             if (kiwi.cache.alljs === '') {
                 public_http_path = kiwi.kiwi_root + '/' + kiwi.config.public_http;
 
+                               min.underscore = fs.readFileSync(public_http_path + 'js/underscore.min.js');
                 min.util = fs.readFileSync(public_http_path + 'js/util.js');
                 min.gateway = fs.readFileSync(public_http_path + 'js/gateway.js');
                 min.front = fs.readFileSync(public_http_path + 'js/front.js');
                 min.iscroll = fs.readFileSync(public_http_path + 'js/iscroll.js');
-                min.ast = jsp.parse(min.util + min.gateway + min.front + min.iscroll);
+                min.ast = jsp.parse(min.underscore + min.util + min.gateway + min.front + min.iscroll);
                 min.ast = pro.ast_mangle(min.ast);
                 min.ast = pro.ast_squeeze(min.ast);
                 min.final_code = pro.gen_code(min.ast);
@@ -486,7 +529,7 @@ this.httpHandler = function (request, response) {
                 kiwi.fileServer.serve(request, response);
             });
         } else if (uri.pathname === '/' || uri_parts[1] === 'client') {
-            useragent = (request.headers) ? request.headers['user-agent'] : '';
+            useragent = (typeof request.headers === 'string') ? request.headers['user-agent'] : '';
             if (useragent.match(/android/i) !== -1) {
                 agent = 'android';
                 touchscreen = true;
@@ -610,7 +653,9 @@ this.websocketConnection = function (websocket) {
 
         websocket.sendClientEvent = function (event_name, data) {
             var ev = kiwi.kiwi_mod.run(event_name, data, {websocket: this});
-            if(ev === null) return;
+            if (ev === null) {
+                return;
+            }
 
             data.event = event_name;
             websocket.emit('message', data);
@@ -618,7 +663,10 @@ this.websocketConnection = function (websocket) {
 
         websocket.sendServerLine = function (data, eol) {
             eol = (typeof eol === 'undefined') ? '\r\n' : eol;
-            websocket.ircSocket.write(data + eol);
+
+            try {
+                websocket.ircSocket.write(data + eol);
+            } catch (e) { }
         };
 
         websocket.on('irc connect', kiwi.websocketIRCConnect);
@@ -692,9 +740,16 @@ this.websocketMessage = function (websocket, msg, callback) {
             break;
         case 'action':
             if ((args.target) && (args.msg)) {
-                websocket.sendServerLine('PRIVMSG ' + args.target + ' :\ 1' + String.fromCharCode(1) + 'ACTION ' + args.msg + String.fromCharCode(1));
+                websocket.sendServerLine('PRIVMSG ' + args.target + ' :' + String.fromCharCode(1) + 'ACTION ' + args.msg + String.fromCharCode(1));
+            }
+            break;
+
+        case 'kiwi':
+            if ((args.target) && (args.data)) {
+                websocket.sendServerLine('PRIVMSG ' + args.target + ' :' + String.fromCharCode(1) + 'KIWI ' + args.data + String.fromCharCode(1));
             }
             break;
+
         case 'raw':
             websocket.sendServerLine(args.data);
             break;
@@ -817,7 +872,9 @@ this.rehash = function () {
  * KiwiIRC controlling via STDIN
  */
 this.manageControll = function (data) {
-    var parts = data.toString().trim().split(' ');
+    var parts = data.toString().trim().split(' '),
+        connections_cnt = 0,
+        i;
     switch (parts[0]) {
     case 'rehash':
         console.log('Rehashing...');
@@ -845,8 +902,7 @@ this.manageControll = function (data) {
         break;
 
     case 'status':
-        var connections_cnt = 0;
-        for (var i in kiwi.connections) {
+        for (i in kiwi.connections) {
             connections_cnt = connections_cnt + parseInt(kiwi.connections[i].count, 10);
         }
         console.log(connections_cnt.toString() + ' connected clients');
@@ -855,4 +911,4 @@ this.manageControll = function (data) {
     default:
         console.log('Unknown command \'' + parts[0] + '\'');
     }
-};
\ No newline at end of file
+};