From 2a8e95d1e94493e0b57fccd478d4253e7378c994 Mon Sep 17 00:00:00 2001 From: Darren Date: Sun, 21 Oct 2012 18:38:12 +0100 Subject: [PATCH] More naming conventions --- server/client.js | 14 +- .../{client-commands.js => clientcommands.js} | 6 +- server/{irc-commands.js => irc/commands.js} | 9 +- server/irc/connection.js | 130 ++++++++++++++++++ 4 files changed, 146 insertions(+), 13 deletions(-) rename server/{client-commands.js => clientcommands.js} (92%) rename server/{irc-commands.js => irc/commands.js} (99%) mode change 100755 => 100644 create mode 100644 server/irc/connection.js diff --git a/server/client.js b/server/client.js index 0ed76b8..fb326e8 100755 --- a/server/client.js +++ b/server/client.js @@ -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')"); diff --git a/server/client-commands.js b/server/clientcommands.js similarity index 92% rename from server/client-commands.js rename to server/clientcommands.js index 4f0b3f7..065aac2 100644 --- a/server/client-commands.js +++ b/server/clientcommands.js @@ -3,12 +3,12 @@ var _ = require('underscore'); -var ClientCommandset = function (client) { +var ClientCommands = function (client) { this.client = client; }; -module.exports.ClientCommandset = ClientCommandset; +module.exports = ClientCommands; -ClientCommandset.prototype.run = function (command, args, irc_connection, callback) { +ClientCommands.prototype.run = function (command, args, irc_connection, callback) { // Do we have a function to handle this command? if (!listeners[command.toUpperCase()]) { return; diff --git a/server/irc-commands.js b/server/irc/commands.js old mode 100755 new mode 100644 similarity index 99% rename from server/irc-commands.js rename to server/irc/commands.js index f4a4ed2..a78b3d4 --- a/server/irc-commands.js +++ b/server/irc/commands.js @@ -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 index 0000000..7ab3d02 --- /dev/null +++ b/server/irc/connection.js @@ -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+$/, '')); + } + } + } +}; -- 2.25.1