-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;
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);
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;
};
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);
}
};
ident: event.ident,
hostname: event.hostname,
channel: this.name,
- msg: event.message
+ msg: event.msg
});
};
users: event.users,
channel: this.name
});
+ updateUsersList.call(this, event.users);
};
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', {
'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) {
}
} 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,
-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;
// 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};
that.connect();
});
};
-util.inherits(IrcConnection, events.EventEmitter);
+util.inherits(IrcConnection, EventEmitter2);
module.exports.IrcConnection = IrcConnection;
* 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();
};
this.irc_events = {\r
nick: onNick,\r
away: onAway,\r
- quit: onKick,\r
+ quit: onQuit,\r
whoisuser: onWhoisUser,\r
whoisoperator: onWhoisOperator,\r
whoischannels: onWhoisChannels,\r
whoismodes: onWhoisModes,\r
whoisidle: onWhoisIdle,\r
- whoisregnick: onRegNick,\r
- endofwhois: onEhoisEnd,\r
+ whoisregnick: onWhoisRegNick,\r
+ endofwhois: onWhoisEnd,\r
notice: onNotice,\r
ctcp_response: onCtcpResponse,\r
privmsg: onPrivmsg,\r
module.exports = IrcUser;\r
\r
\r
-IrcUser.prototype.dispose = function (){\r
+IrcUser.prototype.dispose = function () {\r
EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events);\r
this.irc_connection = undefined;\r
};\r
hostname: event.hostname,\r
newnick: event.newnick\r
});\r
+ EventBinder.unbindIrcEvents('user:' + this.nick, this.irc_events);\r
+ this.nick = event.newnick;\r
+ EventBinder.bindIrcEvents('user:' + this.nick, this.irc_events, this, irc_connection);\r
};\r
\r
function onAway(event) {\r
});\r
};\r
\r
-function onWhoisUser(event) {\r
+function onWhoisIdle(event) {\r
this.irc_connection.clientEvent('whois', {\r
nick: event.nick,\r
idle: event.idle,\r
};\r
\r
function onPrivmsg(event) {\r
- this.irc_connection.clientEvent('privmsg', {\r
+ this.irc_connection.clientEvent('msg', {\r
nick: event.nick,\r
ident: event.ident,\r
hostname: event.hostname,\r