+var util = require('util'),
+ EventBinder = require('./eventbinder.js'),
+ IrcUser = require('./user.js');
+
+var IrcChannel = function(irc_connection, name) {
+ this.irc_connection = irc_connection;
+ this.name = name;
+
+ this.members = [];
+ this.ban_list_buffer = [];
+
+ // Listen for events on the IRC connection
+ this.irc_events = {
+ join: onJoin,
+ part: onPart,
+ kick: onKick,
+ quit: onQuit,
+ privmsg: onMsg,
+ notice: onNotice,
+ ctcp_request: onCtcpRequest,
+ ctcp_response: onCtcpResponse,
+ topic: onTopic,
+ userlist: onNicklist,
+ userlist_end: onNicklistEnd,
+ banlist: onBanList,
+ banlist_end: onBanListEnd,
+ topicsetby: onTopicSetBy,
+ mode: onMode
+ };
+ EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
+};
-function IrcChannel(irc_connection, name) {
- var that = this;
-
- this.irc_connection = irc_connection;
- this.name = name;
-
- // Helper for binding listeners
- function bindEvent(event, fn) {
- irc_connection.on('channel:' + name + ':' + event, function () {
- fn.apply(that, arguments);
- });
- }
-
- bindEvent('join', this.onJoin);
- bindEvent('part', this.onPart);
- bindEvent('kick', this.onKick);
- bindEvent('quit', this.onQuit);
-
- bindEvent('privmsg', this.onMsg);
- bindEvent('notice', this.onNotice);
- bindEvent('ctcp_request', this.onCtcpRequest);
- bindEvent('ctcp_response', this.onCtcpResponse);
- bindEvent('nicklist', this.onNicklist);
- bindEvent('nicklistEnd', this.onNicklistEnd);
-}
+module.exports = IrcChannel;
-IrcChannel.prototype.clientEvent = function (event_name, event) {
- event.server = this.irc_connection.con_num;
- this.irc_connection.state.sendIrcCommand(event_name, event);
+IrcChannel.prototype.dispose = function (){
+ EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
+ this.irc_connection = undefined;
};
-IrcChannel.prototype.onJoin = function (event) {
- this.clientEvent('join', {
- channel: this.name,
- nick: event.nick,
- ident: event.ident,
- hostname: event.hostname,
- });
- // If we've just joined this channel then requesr=t get a nick list
+function onJoin(event) {
+ this.irc_connection.clientEvent('join', {
+ channel: this.name,
+ nick: event.nick,
+ ident: event.ident,
+ 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);
}
-};
+}
-IrcChannel.prototype.onPart = function (event) {
- this.clientEvent('part', {
+function onPart(event) {
+ this.irc_connection.clientEvent('part', {
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
channel: this.name,
message: event.message
});
-};
+}
-IrcChannel.prototype.onKick = function (event) {
- this.client.sendIrcCommand('kick', {
- kicked: event.params[1], // Nick of the kicked
+function onKick(event) {
+ this.irc_connection.clientEvent('kick', {
+ kicked: event.kicked, // Nick of the kicked
nick: event.nick, // Nick of the kicker
ident: event.ident,
hostname: event.hostname,
channel: this.name,
message: event.message
});
-};
+}
-IrcChannel.prototype.onQuit = function (event) {
- this.clientEvent('quit', {
+function onQuit(event) {
+ this.irc_connection.clientEvent('quit', {
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
message: event.message
});
-};
+}
-IrcChannel.prototype.onMsg = function (event) {
- this.clientEvent('msg', {
- server: this.con_num,
+function onMsg(event) {
+ this.irc_connection.clientEvent('msg', {
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
channel: this.name,
- msg: event.message
+ msg: event.msg
});
-};
+}
-IrcChannel.prototype.onNotice = function (event) {
- this.clientEvent('msg', {
- server: this.con_num,
+function onNotice(event) {
+ this.irc_connection.clientEvent('notice', {
+ from_server: event.from_server,
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
- channel: this.name,
- msg: event.trailing
+ target: event.target,
+ msg: event.msg
});
-};
+}
-IrcChannel.prototype.onCtcpRequest = function (event) {
- this.clientEvent('ctcp_request', {
+function onCtcpRequest(event) {
+ this.irc_connection.clientEvent('ctcp_request', {
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
type: event.type,
msg: event.msg
});
-};
+}
-IrcChannel.prototype.onCtcpResponse = function (event) {
- this.clientEvent('ctcp_response', {
+function onCtcpResponse(event) {
+ this.irc_connection.clientEvent('ctcp_response', {
nick: event.nick,
ident: event.ident,
hostname: event.hostname,
type: event.type,
msg: event.msg
});
-};
+}
// TODO: Split event.users into batches of 50
-IrcChannel.prototype.onNicklist = function (event) {
- this.clientEvent('userlist', {
+function onNicklist(event) {
+ this.irc_connection.clientEvent('userlist', {
users: event.users,
channel: this.name
});
-};
+ // TODO: uncomment when using an IrcUser per nick
+ //updateUsersList.call(this, event.users);
+}
-IrcChannel.prototype.onNicklistEnd = function (event) {
- this.clientEvent('userlist_end', {
+function onNicklistEnd(event) {
+ this.irc_connection.clientEvent('userlist_end', {
users: event.users,
channel: this.name
});
-};
+ // TODO: uncomment when using an IrcUser per nick
+ //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);
+ }
+ });
+ }
+}
-/*
-server:event
-server:*
-channel:#channel:event
-channel:*:event
-user:event
-user:*
-
-Server disconnected:
- server:disconnect
- server:*
-
-Joining channel #kiwiirc:
- channel:#kiwiirc:join
- channel:*:join
-
-Channel message:
- channel:#kiwiirc:privmsg
- channel:*:privmsg
-
-Private message:
- user:privmsg
- user:*
-*/
\ No newline at end of file
+
+function onTopic(event) {
+ this.irc_connection.clientEvent('topic', {
+ nick: event.nick,
+ channel: this.name,
+ topic: event.topic
+ });
+}
+
+
+function onBanList(event) {
+ this.ban_list_buffer.push(event);
+}
+
+function onBanListEnd(event) {
+ var that = this;
+ this.ban_list_buffer.forEach(function (ban) {
+ 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', {
+ nick: event.nick,
+ channel: event.channel,
+ when: event.when
+ });
+}
+
+function onMode(event) {
+ this.irc_connection.clientEvent('mode', {
+ target: event.target,
+ nick: event.nick,
+ modes: event.modes
+ });
+}