More naming conventions
authorDarren <darren@darrenwhitlen.com>
Sun, 21 Oct 2012 17:38:12 +0000 (18:38 +0100)
committerDarren <darren@darrenwhitlen.com>
Sun, 21 Oct 2012 17:38:12 +0000 (18:38 +0100)
server/client.js
server/clientcommands.js [moved from server/client-commands.js with 92% similarity]
server/irc/commands.js [moved from server/irc-commands.js with 99% similarity, mode: 0644]
server/irc/connection.js [new file with mode: 0644]

index 0ed76b88d4c3c82a77e67bd00ed648a414b35bed..fb326e8e408310995ca2d62c740f57b5da9116c8 100755 (executable)
@@ -1,9 +1,9 @@
 var util             = require('util'),
     events           = require('events'),
     _                = require('underscore'),
-    IrcConnection    = require('./ircconnection.js').IrcConnection,
-    IrcCommands      = require('./irc-commands.js'),
-    ClientCommandset = require('./client-commands.js').ClientCommandset;
+    IrcConnection    = require('./irc/connection.js').IrcConnection,
+    IrcCommands      = require('./irc/commands.js'),
+    ClientCommands = require('./clientcommands.js');
 
 
 var Client = function (websocket) {
@@ -21,7 +21,7 @@ var Client = function (websocket) {
     };
     
     // Handler for any commands sent from the client
-    this.client_commands = new ClientCommandset(this);
+    this.client_commands = new ClientCommands(this);
 
     websocket.on('irc', function () {
         handleClientMessage.apply(that, arguments);
@@ -95,7 +95,7 @@ function kiwiCommand(command, callback) {
     }
     switch (command.command) {
                case 'connect':
-                       if ((command.hostname) && (command.port) && (command.nick)) {
+                       if (command.hostname && command.port && command.nick) {
                                var con = new IrcConnection(command.hostname, command.port, command.ssl,
                                        command.nick, {hostname: this.websocket.handshake.revdns, address: this.websocket.handshake.address.address},
                                        command.password, null);
@@ -103,8 +103,8 @@ function kiwiCommand(command, callback) {
                                var con_num = this.next_connection++;
                                this.irc_connections[con_num] = con;
 
-                               var binder = new IrcCommands.Binder(con, con_num, this);
-                               binder.bind_irc_commands();
+                               var irc_commands = new IrcCommands(con, con_num, this);
+                               irc_commands.bindEvents();
                                
                                con.on('connected', function () {
                     console.log("con.on('connected')");
similarity index 92%
rename from server/client-commands.js
rename to server/clientcommands.js
index 4f0b3f751ecd36ca40616386b09623ea1f6dcc50..065aac2b2e676c44aee67864dbda89e57ebcc63d 100644 (file)
@@ -3,12 +3,12 @@ var _ = require('underscore');
 \r
 \r
 \r
-var ClientCommandset = function (client) {\r
+var ClientCommands = function (client) {\r
     this.client = client;\r
 };\r
-module.exports.ClientCommandset = ClientCommandset;\r
+module.exports = ClientCommands;\r
 \r
-ClientCommandset.prototype.run = function (command, args, irc_connection, callback) {\r
+ClientCommands.prototype.run = function (command, args, irc_connection, callback) {\r
     // Do we have a function to handle this command?\r
     if (!listeners[command.toUpperCase()]) {\r
         return;\r
old mode 100755 (executable)
new mode 100644 (file)
similarity index 99%
rename from server/irc-commands.js
rename to server/irc/commands.js
index f4a4ed2..a78b3d4
@@ -42,15 +42,16 @@ var irc_numerics = {
 };
 
 
-var Binder = function (irc_connection, con_num, client) {
+var IrcCommands = function (irc_connection, con_num, client) {
     this.irc_connection = irc_connection;
     this.con_num = con_num;
     this.client = client;
 };
-module.exports.Binder = Binder;
+module.exports = IrcCommands;
 
-Binder.prototype.bind_irc_commands = function () {
+IrcCommands.prototype.bindEvents = function () {
     var that = this;
+
     _.each(listeners, function (listener, command) {
         var s = command.substr(0, 4);
         if ((s === 'RPL_') || (s === 'ERR_')) {
@@ -62,6 +63,8 @@ Binder.prototype.bind_irc_commands = function () {
     });
 };
 
+
+
 var listeners = {
     'RPL_WELCOME':            function (command) {
                 var nick =  command.params[0];
diff --git a/server/irc/connection.js b/server/irc/connection.js
new file mode 100644 (file)
index 0000000..7ab3d02
--- /dev/null
@@ -0,0 +1,130 @@
+var net     = require('net'),
+    tls     = require('tls'),
+    events  = require('events'),
+    util    = require('util');
+
+var IrcConnection = function (hostname, port, ssl, nick, user, pass, webirc) {
+    var that = this;
+    events.EventEmitter.call(this);
+    
+    if (ssl) {
+        this.socket = tls.connect(port, hostname, {}, connect_handler);
+    } else {
+        this.socket = net.createConnection(port, hostname);
+        this.socket.on('connect', function () {
+            connect_handler.apply(that, arguments);
+        });
+    }
+    
+    this.socket.on('error', function () {
+        var a = Array.prototype.slice.call(arguments);
+        a.unshift('error');
+        that.emit.apply(this, a);
+    });
+    
+    this.socket.setEncoding('utf-8');
+    
+    this.socket.on('data', function () {
+        parse.apply(that, arguments);
+    });
+    
+    this.socket.on('close', function () {
+        that.emit('close');
+    });
+    
+    this.connected = false;
+    this.registered = false;
+    this.nick = nick;
+    this.user = user;
+    this.ssl = !(!ssl);
+    this.options = Object.create(null);
+    
+    this.webirc = webirc;
+    this.password = pass;
+    this.hold_last = false;
+    this.held_data = '';
+};
+util.inherits(IrcConnection, events.EventEmitter);
+
+module.exports.IrcConnection = IrcConnection;
+
+
+IrcConnection.prototype.write = function (data, callback) {
+    write.call(this, data + '\r\n', 'utf-8', callback);
+};
+
+IrcConnection.prototype.end = function (data, callback) {
+    console.log('Closing IRC socket');
+    end.call(this, data + '\r\n', 'utf-8', callback);
+};
+
+var write = function (data, encoding, callback) {
+    this.socket.write(data, encoding, callback);
+};
+
+var end = function (data, encoding, callback) {
+    this.socket.end(data, encoding, callback);
+};
+
+
+var connect_handler = function () {
+    if (this.webirc) {
+        this.write('WEBIRC ' + this.webirc.pass + ' KiwiIRC ' + this.user.hostname + ' ' + this.user.address);
+    }
+    if (this.password) {
+        this.write('PASS ' + this.password);
+    }
+    //this.write('CAP LS');
+    this.write('NICK ' + this.nick);
+    this.write('USER kiwi_' + this.nick.replace(/[^0-9a-zA-Z\-_.]/, '') + ' 0 0 :' + this.nick);
+    
+    this.connected = true;
+    console.log("IrcConnection.emit('connected')");
+    this.emit('connected');
+};
+
+parse_regex = /^(?::(?:([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)|([a-z0-9\x5B-\x60\x7B-\x7D\.\-]+)!([a-z0-9~\.\-_|]+)@?([a-z0-9\.\-:\/]+)?) )?(\S+)(?: (?!:)(.+?))?(?: :(.+))?$/i;
+
+var parse = function (data) {
+    var i,
+        msg,
+               msg2,
+               trm;
+    
+    if ((this.hold_last) && (this.held_data !== '')) {
+        data = this.held_data + data;
+        this.hold_last = false;
+        this.held_data = '';
+    }
+    if (data.substr(-1) !== '\n') {
+        this.hold_last = true;
+    }
+    data = data.split("\n");
+    for (i = 0; i < data.length; i++) {
+        if (data[i]) {
+            if ((this.hold_last) && (i === data.length - 1)) {
+                this.held_data = data[i];
+                break;
+            }
+
+            // We have a complete line of data, parse it!
+            msg = parse_regex.exec(data[i].replace(/^\r+|\r+$/, ''));
+            if (msg) {
+                msg = {
+                    prefix:     msg[1],
+                    nick:       msg[2],
+                    ident:      msg[3],
+                    hostname:   msg[4] || '',
+                    command:    msg[5],
+                    params:     msg[6] || '',
+                    trailing:   (msg[7]) ? msg[7].trim() : ''
+                };
+                msg.params = msg.params.split(' ');
+
+                this.emit('irc_' + msg.command.toUpperCase(), msg);
+            } else {
+                console.log("Malformed IRC line: " + data[i].replace(/^\r+|\r+$/, ''));
+            }
+        }
+    }
+};