From: Jack Allnutt Date: Sun, 27 Jan 2013 02:59:39 +0000 (+0000) Subject: Get codebase into semi-working state X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cefa09007ec2b6a0777188df73ead6366cb05a0b;p=KiwiIRC.git Get codebase into semi-working state --- diff --git a/server/irc/channel.js b/server/irc/channel.js index e75bcf0..b7aaa97 100644 --- a/server/irc/channel.js +++ b/server/irc/channel.js @@ -1,5 +1,6 @@ -var util = require('util'), - EventBinder = require('./eventbinder.js'); +var util = require('util'), + EventBinder = require('./eventbinder.js'), + IrcUser = require('./user.js'); function IrcChannel(irc_connection, name) { this.irc_connection = irc_connection; @@ -19,11 +20,11 @@ function IrcChannel(irc_connection, name) { ctcp_request: onCtcpRequest, ctcp_response: onCtcpResponse, topic: onTopic, - nicklist: onNicklist, - nicklistEnd: onNicklistEnd, + userlist: onNicklist, + userlist_end: onNicklistEnd, banlist: onBanList, banlist_end: onBanListEnd, - topicsetby: onTopicSetby, + topicsetby: onTopicSetBy, mode: onMode }; EventBinder.bindIrcEvents('channel:' + this.name, this.irc_events, this, irc_connection); @@ -34,7 +35,7 @@ module.exports = IrcChannel; IrcChannel.prototype.dispose = function (){ - EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events); + EventBinder.unbindIrcEvents('channel:' + this.name, this.irc_events, this.irc_connection); this.irc_connection = undefined; }; @@ -45,12 +46,12 @@ function onJoin(event) { channel: this.name, nick: event.nick, ident: event.ident, - hostname: event.hostname, + hostname: event.hostname }); // If we've just joined this channel then request get a nick list if (event.nick === this.irc_connection.nick) { - this.irc_connection.write('NAMES ' + channel); + this.irc_connection.write('NAMES ' + this.name); } }; @@ -100,7 +101,7 @@ function onMsg(event) { ident: event.ident, hostname: event.hostname, channel: this.name, - msg: event.message + msg: event.msg }); }; @@ -146,6 +147,7 @@ function onNicklist(event) { users: event.users, channel: this.name }); + updateUsersList.call(this, event.users); }; @@ -154,8 +156,20 @@ function onNicklistEnd(event) { users: event.users, channel: this.name }); + updateUsersList.call(this, event.users); }; +function updateUsersList(users) { + var that = this; + if (users) { + users.forEach(function (user) { + if (!that.irc_connection.irc_users[user.nick]) { + that.irc_connection.irc_users[user.nick] = new IrcUser(that.irc_connection, user.nick); + } + }); + } +} + function onTopic(event) { this.irc_connection.clientEvent('topic', { diff --git a/server/irc/commands.js b/server/irc/commands.js index e806ddb..804b6c7 100644 --- a/server/irc/commands.js +++ b/server/irc/commands.js @@ -150,7 +150,7 @@ var listeners = { 'RPL_WHOISCHANNELS': function (command) { this.irc_connection.emit('user:' + command.params[1] + ':whoischannels', { nick: command.params[1], - chans: commnd.trailing + chans: command.trailing }); }, 'RPL_WHOISMODES': function (command) { @@ -452,8 +452,8 @@ var listeners = { } } else { // A message to a user (private message) or to a channel? - namespace = (command.target == this.irc_connection.nick) ? 'user' : 'channel'; - this.irc_connection.emit(namespace + ':' + command.nick + ':privmsg', { + namespace = (command.params[0] === this.irc_connection.nick) ? 'user:' + command.nick : 'channel:' + command.params[0]; + this.irc_connection.emit(namespace + ':privmsg', { nick: command.nick, ident: command.ident, hostname: command.hostname, diff --git a/server/irc/connection.js b/server/irc/connection.js index 0657a27..6c567e4 100644 --- a/server/irc/connection.js +++ b/server/irc/connection.js @@ -1,13 +1,19 @@ -var net = require('net'), - tls = require('tls'), - events = require('events'), - util = require('util'), - _ = require('lodash'); +var net = require('net'), + tls = require('tls'), + util = require('util'), + _ = require('lodash'), + EventEmitter2 = require('eventemitter2').EventEmitter2, + IrcServer = require('./server.js'), + IrcChannel = require('./channel.js'), + IrcUser = require('./user.js'); var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { var that = this; - events.EventEmitter.call(this); + EventEmitter2.call(this,{ + wildcard: true, + delimiter: ':' + }); // Socket state this.connected = false; @@ -26,6 +32,41 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { // State object this.state = state; + + // IrcServer object + this.server = new IrcServer(this, hostname, port); + + // IrcUser objects + this.irc_users = Object.create(null); + + // IrcChannel objects + this.irc_channels = Object.create(null); + + // Create IrcUser and IrcChannel objects when needed + // TODO: Remove IrcUser objects when they are no longer needed + this.on('server:*:connect', function (event) { + that.nick = event.nick; + that.irc_users[event.nick] = new IrcUser(that, event.nick); + }); + this.on('channel:*:join', function (event) { + var chan; + if (event.nick === that.nick) { + chan = new IrcChannel(that, event.channel); + that.irc_channels[event.channel] = chan; + chan.irc_events.join.call(chan, event); + } + }); + + this.on('user:*:privmsg', function (event) { + var user; + if (event.channel === that.nick) { + if (!that.irc_users[event.nick]) { + user = new IrcUser(that, event.nick); + that.irc_users[event.nick] = user; + user.irc_events.privmsg.call(user, event); + } + } + }); // IRC connection information this.irc_host = {hostname: hostname, port: port}; @@ -49,7 +90,7 @@ var IrcConnection = function (hostname, port, ssl, nick, user, pass, state) { that.connect(); }); }; -util.inherits(IrcConnection, events.EventEmitter); +util.inherits(IrcConnection, EventEmitter2); module.exports.IrcConnection = IrcConnection; @@ -144,6 +185,14 @@ IrcConnection.prototype.end = function (data, callback) { * Clean up this IrcConnection instance and any sockets */ IrcConnection.prototype.dispose = function () { + _.each(this.irc_users, function (user) { + user.dispose(); + }); + _.each(this.irc_channels, function (chan) { + chan.dispose(); + }); + this.irc_users = null; + this.irc_channels = null; this.disposeSocket(); this.removeAllListeners(); }; diff --git a/server/irc/user.js b/server/irc/user.js index 40fdf90..de66a5a 100755 --- a/server/irc/user.js +++ b/server/irc/user.js @@ -8,14 +8,14 @@ var IrcUser = function (irc_connection, nick) { this.irc_events = { nick: onNick, away: onAway, - quit: onKick, + quit: onQuit, whoisuser: onWhoisUser, whoisoperator: onWhoisOperator, whoischannels: onWhoisChannels, whoismodes: onWhoisModes, whoisidle: onWhoisIdle, - whoisregnick: onRegNick, - endofwhois: onEhoisEnd, + whoisregnick: onWhoisRegNick, + endofwhois: onWhoisEnd, notice: onNotice, ctcp_response: onCtcpResponse, privmsg: onPrivmsg, @@ -29,7 +29,7 @@ var IrcUser = function (irc_connection, nick) { module.exports = IrcUser; -IrcUser.prototype.dispose = function (){ +IrcUser.prototype.dispose = function () { EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events); this.irc_connection = undefined; }; @@ -42,6 +42,9 @@ function onNick(event) { hostname: event.hostname, newnick: event.newnick }); + EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events); + this.nick = event.newnick; + EventBinder.bindIrcEvents('user:' + this.nick, this.irc_events, this, irc_connection); }; function onAway(event) { @@ -102,7 +105,7 @@ function onWhoisModes(event) { }); }; -function onWhoisUser(event) { +function onWhoisIdle(event) { this.irc_connection.clientEvent('whois', { nick: event.nick, idle: event.idle, @@ -148,7 +151,7 @@ function onCtcpResponse(event) { }; function onPrivmsg(event) { - this.irc_connection.clientEvent('privmsg', { + this.irc_connection.clientEvent('msg', { nick: event.nick, ident: event.ident, hostname: event.hostname,