Server: Splitting State / IrcConnection / IrcCommands better
authorDarren <darren@darrenwhitlen.com>
Fri, 26 Jul 2013 21:49:12 +0000 (22:49 +0100)
committerDarren <darren@darrenwhitlen.com>
Fri, 26 Jul 2013 21:49:12 +0000 (22:49 +0100)
server/irc/commands.js
server/irc/connection.js
server/irc/state.js

index 6979af870c18ec9a9935406e34f3be4d2838d022..96943a39992ea2342c3f89fa98ca0072b3fe645e 100644 (file)
@@ -72,10 +72,8 @@ irc_numerics = {
 };
 
 
-IrcCommands = function (irc_connection, con_num, client) {
+IrcCommands = function (irc_connection) {
     this.irc_connection = irc_connection;
-    this.con_num = con_num;
-    this.client = client;
 };
 module.exports = IrcCommands;
 
@@ -503,11 +501,11 @@ handlers = {
         if ((command.trailing.charAt(0) === String.fromCharCode(1)) && (command.trailing.charAt(command.trailing.length - 1) === String.fromCharCode(1))) {
             //CTCP request
             if (command.trailing.substr(1, 6) === 'ACTION') {
-                this.client.sendIrcCommand('action', {server: this.con_num, nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(7, command.trailing.length - 2)});
+                this.irc_connection.clientEvent('action', {nick: command.nick, ident: command.ident, hostname: command.hostname, channel: command.params[0], msg: command.trailing.substr(7, command.trailing.length - 2)});
             } else if (command.trailing.substr(1, 4) === 'KIWI') {
                 tmp = command.trailing.substr(6, command.trailing.length - 2);
                 namespace = tmp.split(' ', 1)[0];
-                this.client.sendIrcCommand('kiwi', {server: this.con_num, namespace: namespace, data: tmp.substr(namespace.length + 1)});
+                this.irc_connection.clientEvent('kiwi', {namespace: namespace, data: tmp.substr(namespace.length + 1)});
             } else if (command.trailing.substr(1, 7) === 'VERSION') {
                 this.irc_connection.write('NOTICE ' + command.nick + ' :' + String.fromCharCode(1) + 'VERSION KiwiIRC' + String.fromCharCode(1));
             } else if (command.trailing.substr(1, 6) === 'SOURCE') {
@@ -850,8 +848,7 @@ function genericNotice (command, msg, is_error) {
     if (typeof is_error !== 'boolean')
         is_error = true;
 
-    this.client.sendIrcCommand('notice', {
-        server: this.con_num,
+    this.irc_connection.clientEvent('notice', {
         from_server: true,
         nick: command.prefix,
         ident: '',
index 8f9ee6ae57aa9e50defc2783b9af52089e485b94..67b05a2dcf6721881873afdd0238d42441d4f382 100644 (file)
@@ -5,6 +5,7 @@ var net             = require('net'),
     _               = require('lodash'),
     EventBinder     = require('./eventbinder.js'),
     IrcServer       = require('./server.js'),
+    IrcCommands     = require('./commands.js'),
     IrcChannel      = require('./channel.js'),
     IrcUser         = require('./user.js'),
     EE              = require('../ee.js'),
@@ -22,7 +23,7 @@ if (version_values[1] >= 10) {
     Socks = require('socksjs');
 }
 
-var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
+var IrcConnection = function (hostname, port, ssl, nick, user, pass, state, con_num) {
     var that = this;
 
     EE.call(this,{
@@ -61,6 +62,12 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) {
     // State object
     this.state = state;
 
+    // Connection ID in the state
+    this.con_num = con_num;
+
+    // IRC protocol handling
+    this.irc_commands = new IrcCommands(this);
+
     // IrcServer object
     this.server = new IrcServer(this, hostname, port);
 
index 776ab5db2aa73104ec6e886df9a4dbb9e018b95d..e63451aa1232d52b449bd238233b1ad6bc7164e4 100755 (executable)
@@ -1,8 +1,7 @@
 var util            = require('util'),
     events          = require('events'),
     _               = require('lodash'),
-    IrcConnection   = require('./connection.js').IrcConnection,
-    IrcCommands     = require('./commands.js');
+    IrcConnection   = require('./connection.js').IrcConnection;
 
 var State = function (client, save_state) {
     var that = this;
@@ -48,6 +47,7 @@ State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callb
         return callback('Too many connections to host', {host: hostname, limit: global.config.max_server_conns});
     }
 
+    con_num = this.next_connection++;
     con = new IrcConnection(
         hostname,
         port,
@@ -55,13 +55,10 @@ State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callb
         nick,
         user,
         pass,
-        this);
+        this,
+        con_num);
 
-    con_num = this.next_connection++;
     this.irc_connections[con_num] = con;
-    con.con_num = con_num;
-
-    con.irc_commands = new IrcCommands(con, con_num, this);
 
     con.on('connected', function () {
         global.servers.addConnection(this);