From cced0091513db24128650494f79784e22f7538c5 Mon Sep 17 00:00:00 2001 From: Darren Date: Fri, 26 Jul 2013 22:49:12 +0100 Subject: [PATCH] Server: Splitting State / IrcConnection / IrcCommands better --- server/irc/commands.js | 11 ++++------- server/irc/connection.js | 9 ++++++++- server/irc/state.js | 11 ++++------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/server/irc/commands.js b/server/irc/commands.js index 6979af8..96943a3 100644 --- a/server/irc/commands.js +++ b/server/irc/commands.js @@ -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: '', diff --git a/server/irc/connection.js b/server/irc/connection.js index 8f9ee6a..67b05a2 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -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); diff --git a/server/irc/state.js b/server/irc/state.js index 776ab5d..e63451a 100755 --- a/server/irc/state.js +++ b/server/irc/state.js @@ -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); -- 2.25.1