From 32a09dc1534458aeb4fe913c85d11d9040dbd5ad Mon Sep 17 00:00:00 2001 From: Darren Date: Sat, 25 May 2013 17:21:59 +0100 Subject: [PATCH] Server code cleaning --- server/irc/channel.js | 34 +++++++++++++-------------- server/irc/connection.js | 37 ++++++++++++++--------------- server/irc/server.js | 51 ++++++++++++++++++++-------------------- server/irc/user.js | 36 ++++++++++++++-------------- 4 files changed, 78 insertions(+), 80 deletions(-) diff --git a/server/irc/channel.js b/server/irc/channel.js index 261bf23..fcdeec2 100644 --- a/server/irc/channel.js +++ b/server/irc/channel.js @@ -28,7 +28,7 @@ var IrcChannel = function(irc_connection, name) { mode: onMode }; EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection); -} +}; module.exports = IrcChannel; @@ -53,7 +53,7 @@ function onJoin(event) { if (event.nick === this.irc_connection.nick) { this.irc_connection.write('NAMES ' + this.name); } -}; +} function onPart(event) { @@ -64,7 +64,7 @@ function onPart(event) { channel: this.name, message: event.message }); -}; +} function onKick(event) { @@ -76,7 +76,7 @@ function onKick(event) { channel: this.name, message: event.message }); -}; +} function onQuit(event) { @@ -86,7 +86,7 @@ function onQuit(event) { hostname: event.hostname, message: event.message }); -}; +} function onMsg(event) { @@ -97,7 +97,7 @@ function onMsg(event) { channel: this.name, msg: event.msg }); -}; +} function onNotice(event) { @@ -109,7 +109,7 @@ function onNotice(event) { target: event.target, msg: event.msg }); -}; +} function onCtcpRequest(event) { @@ -121,7 +121,7 @@ function onCtcpRequest(event) { type: event.type, msg: event.msg }); -}; +} function onCtcpResponse(event) { @@ -133,7 +133,7 @@ function onCtcpResponse(event) { type: event.type, msg: event.msg }); -}; +} // TODO: Split event.users into batches of 50 @@ -144,7 +144,7 @@ function onNicklist(event) { }); // TODO: uncomment when using an IrcUser per nick //updateUsersList.call(this, event.users); -}; +} function onNicklistEnd(event) { @@ -154,7 +154,7 @@ function onNicklistEnd(event) { }); // TODO: uncomment when using an IrcUser per nick //updateUsersList.call(this, event.users); -}; +} function updateUsersList(users) { var that = this; @@ -174,12 +174,12 @@ function onTopic(event) { channel: this.name, topic: event.topic }); -}; +} function onBanList(event) { this.ban_list_buffer.push(event); -}; +} function onBanListEnd(event) { var that = this; @@ -187,14 +187,14 @@ function onBanListEnd(event) { that.irc_connection.clientEvent('banlist', ban); }); this.ban_list_buffer = []; -}; +} function onTopic(event) { this.irc_connection.clientEvent('topic', { channel: event.channel, topic: event.topic }); -}; +} function onTopicSetBy(event) { this.irc_connection.clientEvent('topicsetby', { @@ -202,7 +202,7 @@ function onTopicSetBy(event) { channel: event.channel, when: event.when }); -}; +} function onMode(event) { this.irc_connection.clientEvent('mode', { @@ -210,4 +210,4 @@ function onMode(event) { nick: event.nick, modes: event.modes }); -}; +} diff --git a/server/irc/connection.js b/server/irc/connection.js index b2ec4dd..9e9ab3b 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -8,8 +8,8 @@ var net = require('net'), IrcChannel = require('./channel.js'), IrcUser = require('./user.js'), Socks; - - + + // Break the Node.js version down into usable parts var version_values = process.version.substr(1).split('.').map(function (item) { return parseInt(item, 10); @@ -28,7 +28,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { delimiter: ' ' }); this.setMaxListeners(0); - + // Socket state this.connected = false; @@ -43,13 +43,13 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { this.user = user; // Contains users real hostname and address this.username = this.nick.replace(/[^0-9a-zA-Z\-_.\/]/, ''); this.password = pass; - + // State object this.state = state; - + // IrcServer object this.server = new IrcServer(this, hostname, port); - + // IrcUser objects this.irc_users = Object.create(null); @@ -62,7 +62,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { // IRC connection information this.irc_host = {hostname: hostname, port: port}; this.ssl = !(!ssl); - + // SOCKS proxy details // TODO: Wildcard matching of hostnames and/or CIDR ranges of IP addresses if ((global.config.socks_proxy && global.config.socks_proxy.enabled) && ((global.config.socks_proxy.all) || (_.contains(global.config.socks_proxy.proxy_hosts, this.irc_host.hostname)))) { @@ -82,7 +82,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { // Is SASL supported on the IRCd this.sasl = false; - + // Buffers for data sent from the IRCd this.hold_last = false; this.held_data = ''; @@ -136,7 +136,7 @@ IrcConnection.prototype.connect = function () { // Are we connecting through a SOCKS proxy? if (this.socks) { this.socket = Socks.connect({ - host: this.irc_host.hostname, + host: this.irc_host.hostname, port: this.irc_host.port, ssl: this.ssl, rejectUnauthorized: global.config.reject_unauthorised_certificates @@ -145,7 +145,7 @@ IrcConnection.prototype.connect = function () { user: this.socks.user, pass: this.socks.pass }); - + } else if (this.ssl) { this.socket = tls.connect({ host: this.irc_host.hostname, @@ -161,23 +161,22 @@ IrcConnection.prototype.connect = function () { port: this.irc_host.port }); } - + this.socket.on(socket_connect_event_name, function () { that.connected = true; socketConnectHandler.call(that); }); - + this.socket.setEncoding('utf-8'); - + this.socket.on('error', function (event) { that.emit('error', event); - }); - + this.socket.on('data', function () { parse.apply(that, arguments); }); - + this.socket.on('close', function (had_error) { that.connected = false; that.emit('close'); @@ -210,7 +209,7 @@ IrcConnection.prototype.write = function (data, callback) { IrcConnection.prototype.end = function (data, callback) { if (data) this.write(data); - + this.socket.end(); }; @@ -359,10 +358,10 @@ var socketConnectHandler = function () { if (that.password) that.write('PASS ' + that.password); - + that.write('NICK ' + that.nick); that.write('USER ' + that.username + ' 0 0 :' + '[www.kiwiirc.com] ' + that.nick); - + that.emit('connected'); }); }; diff --git a/server/irc/server.js b/server/irc/server.js index 2a989ec..49f06d8 100755 --- a/server/irc/server.js +++ b/server/irc/server.js @@ -7,7 +7,7 @@ var IrcServer = function (irc_connection) { this.list_buffer = []; this.motd_buffer = ''; - + this.irc_events = { connect: onConnect, options: onOptions, @@ -33,7 +33,6 @@ var IrcServer = function (irc_connection) { nickname_in_use: onNicknameInUse }; EventBinder.bindIrcEvents('server *', this.irc_events, this, this.irc_connection); - }; @@ -52,20 +51,20 @@ function onConnect(event) { this.irc_connection.clientEvent('connect', { nick: event.nick }); -}; +} function onOptions(event) { this.irc_connection.clientEvent('options', { options: event.options, cap: event.cap }); -}; +} function onListStart(event) { this.irc_connection.clientEvent('list_start', {}); this.list_buffer = []; this.busy_listing = true; -}; +} function onListChannel(event) { var buf; @@ -77,7 +76,7 @@ function onListChannel(event) { num_users: event.num_users, topic: event.topic }); - + if (this.list_buffer.length > 200) { buf = _.sortBy(this.list_buffer, function (channel) { // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users. @@ -88,11 +87,11 @@ function onListChannel(event) { }); this.list_buffer = []; } -}; +} function onListEnd(event) { var buf; - + buf = _.sortBy(this.list_buffer, function (channel) { // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users. return 0 - channel.num_users; @@ -102,30 +101,30 @@ function onListEnd(event) { }); this.list_buffer = []; this.busy_listing = false; - + this.irc_connection.clientEvent('list_end', {}); -}; +} function onMotdStart(event) { this.motd_buffer = ''; -}; +} function onMotd(event) { this.motd_buffer += event.motd; -}; +} function onMotdEnd(event) { this.irc_connection.clientEvent('motd', { msg: this.motd_buffer }); -}; +} function onError(event) { this.irc_connection.clientEvent('irc_error', { error: 'error', reason: event.reason }); -}; +} function onPasswordMismatch(event) { this.irc_connection.clientEvent('irc_error', { @@ -138,7 +137,7 @@ function onChannelRedirect(event) { from: event.from, to: event.to }); -}; +} function onNoSuchNick(event) { this.irc_connection.clientEvent('irc_error', { @@ -146,7 +145,7 @@ function onNoSuchNick(event) { nick: event.nick, reason: event.reason }); -}; +} function onCannotSendToChan(event) { this.irc_connection.clientEvent('irc_error', { @@ -154,7 +153,7 @@ function onCannotSendToChan(event) { channel: event.channel, reason: event.reason }); -}; +} function onTooManyChannels(event) { this.irc_connection.clientEvent('irc_error', { @@ -162,7 +161,7 @@ function onTooManyChannels(event) { channel: event.channel, reason: event.reason }); -}; +} function onUserNotInChannel(event) { this.irc_connection.clientEvent('irc_error', { @@ -171,7 +170,7 @@ function onUserNotInChannel(event) { channel: event.channel, reason: event.reason }); -}; +} function onNotOnChannel(event) { this.irc_connection.clientEvent('irc_error', { @@ -179,7 +178,7 @@ function onNotOnChannel(event) { channel: event.channel, reason: event.reason }); -}; +} function onChannelIsFull(event) { this.irc_connection.clientEvent('irc_error', { @@ -187,7 +186,7 @@ function onChannelIsFull(event) { channel: event.channel, reason: event.reason }); -}; +} function onInviteOnlyChannel(event) { this.irc_connection.clientEvent('irc_error', { @@ -195,7 +194,7 @@ function onInviteOnlyChannel(event) { channel: event.channel, reason: event.reason }); -}; +} function onBannedFromChannel(event) { this.irc_connection.clientEvent('irc_error', { @@ -203,7 +202,7 @@ function onBannedFromChannel(event) { channel: event.channel, reason: event.reason }); -}; +} function onBadChannelKey(event) { this.irc_connection.clientEvent('irc_error', { @@ -211,7 +210,7 @@ function onBadChannelKey(event) { channel: event.channel, reason: event.reason }); -}; +} function onChanopPrivsNeeded(event) { this.irc_connection.clientEvent('irc_error', { @@ -219,7 +218,7 @@ function onChanopPrivsNeeded(event) { channel: event.channel, reason: event.reason }); -}; +} function onNicknameInUse(event) { this.irc_connection.clientEvent('irc_error', { @@ -227,4 +226,4 @@ function onNicknameInUse(event) { nick: event.nick, reason: event.reason }); -}; +} diff --git a/server/irc/user.js b/server/irc/user.js index cca8b53..499f691 100755 --- a/server/irc/user.js +++ b/server/irc/user.js @@ -4,7 +4,7 @@ var util = require('util'), var IrcUser = function (irc_connection, nick) { this.irc_connection = irc_connection; this.nick = nick; - + this.irc_events = { nick: onNick, away: onAway, @@ -48,14 +48,14 @@ function onNick(event) { //EventBinder.unbindIrcEvents('user ' + this.nick, this.irc_events, irc_connection); //this.nick = event.newnick; //EventBinder.bindIrcEvents('user ' + this.nick, this.irc_events, this, irc_connection); -}; +} function onAway(event) { this.irc_connection.clientEvent('away', { nick: event.nick, msg: event.msg }); -}; +} function onQuit(event) { this.irc_connection.clientEvent('quit', { @@ -64,7 +64,7 @@ function onQuit(event) { hostname: event.hostname, message: event.trailing }); -}; +} function onWhoisUser(event) { this.irc_connection.clientEvent('whois', { @@ -74,7 +74,7 @@ function onWhoisUser(event) { msg: event.msg, end: false }); -}; +} function onWhoisAway(event) { this.irc_connection.clientEvent('whois', { @@ -82,7 +82,7 @@ function onWhoisAway(event) { away_reason: event.reason, end: false }); -}; +} function onWhoisServer(event) { this.irc_connection.clientEvent('whois', { @@ -90,7 +90,7 @@ function onWhoisServer(event) { irc_server: event.irc_server, end: false }); -}; +} function onWhoisOperator(event) { this.irc_connection.clientEvent('whois', { @@ -98,7 +98,7 @@ function onWhoisOperator(event) { msg: event.msg, end: false }); -}; +} function onWhoisChannels(event) { this.irc_connection.clientEvent('whois', { @@ -106,7 +106,7 @@ function onWhoisChannels(event) { chans: event.chans, end: false }); -}; +} function onWhoisModes(event) { this.irc_connection.clientEvent('whois', { @@ -114,7 +114,7 @@ function onWhoisModes(event) { msg: event.msg, end: false }); -}; +} function onWhoisIdle(event) { this.irc_connection.clientEvent('whois', { @@ -123,7 +123,7 @@ function onWhoisIdle(event) { logon: event.logon || undefined, end: false }); -}; +} function onWhoisRegNick(event) { this.irc_connection.clientEvent('whois', { @@ -131,7 +131,7 @@ function onWhoisRegNick(event) { msg: event.msg, end: false }); -}; +} function onWhoisEnd(event) { this.irc_connection.clientEvent('whois', { @@ -139,7 +139,7 @@ function onWhoisEnd(event) { msg: event.msg, end: true }); -}; +} function onNotice(event) { this.irc_connection.clientEvent('notice', { @@ -150,7 +150,7 @@ function onNotice(event) { target: event.target, msg: event.msg }); -}; +} function onCtcpResponse(event) { this.irc_connection.clientEvent('ctcp_response', { @@ -160,7 +160,7 @@ function onCtcpResponse(event) { channel: event.channel, msg: event.msg }); -}; +} function onPrivmsg(event) { this.irc_connection.clientEvent('msg', { @@ -170,7 +170,7 @@ function onPrivmsg(event) { channel: event.channel, msg: event.msg }); -}; +} function onCtcpRequest(event) { this.irc_connection.clientEvent('ctcp_request', { @@ -181,7 +181,7 @@ function onCtcpRequest(event) { type: event.type, msg: event.msg }); -}; +} function onMode(event) { this.irc_connection.clientEvent('mode', { @@ -189,4 +189,4 @@ function onMode(event) { nick: event.nick, modes: event.modes }); -}; +} -- 2.25.1