1 var util
= require('util'),
2 EventBinder
= require('./eventbinder.js'),
3 IrcUser
= require('./user.js');
5 var IrcChannel = function(irc_connection
, name
) {
6 this.irc_connection
= irc_connection
;
10 this.ban_list_buffer
= [];
12 // Listen for events on the IRC connection
20 ctcp_request
: onCtcpRequest
,
21 ctcp_response
: onCtcpResponse
,
24 userlist_end
: onNicklistEnd
,
26 banlist_end
: onBanListEnd
,
27 topicsetby
: onTopicSetBy
,
31 EventBinder
.bindIrcEvents('channel ' + this.name
, this.irc_events
, this, irc_connection
);
35 module
.exports
= IrcChannel
;
38 IrcChannel
.prototype.dispose = function (){
39 EventBinder
.unbindIrcEvents('channel ' + this.name
, this.irc_events
, this.irc_connection
);
40 this.irc_connection
= undefined;
45 function onJoin(event
) {
48 global
.modules
.emit('irc channel join', {
50 connection
: this.irc_connection
,
54 that
.irc_connection
.clientEvent('join', {
58 hostname
: event
.hostname
,
65 function onPart(event
) {
68 global
.modules
.emit('irc channel part', {
70 connection
: this.irc_connection
,
74 that
.irc_connection
.clientEvent('part', {
77 hostname
: event
.hostname
,
79 message
: event
.message
,
86 function onKick(event
) {
87 global
.modules
.emit('irc channel kick', {
89 connection
: this.irc_connection
,
93 this.irc_connection
.clientEvent('kick', {
94 kicked
: event
.kicked
, // Nick of the kicked
95 nick
: event
.nick
, // Nick of the kicker
97 hostname
: event
.hostname
,
99 message
: event
.message
,
106 function onQuit(event
) {
109 global
.modules
.emit('irc channel quit', {
111 connection
: this.irc_connection
,
115 that
.irc_connection
.clientEvent('quit', {
118 hostname
: event
.hostname
,
119 message
: event
.message
,
126 function onMsg(event
) {
129 global
.modules
.emit('irc message', {
131 connection
: this.irc_connection
,
135 that
.irc_connection
.clientEvent('msg', {
138 hostname
: event
.hostname
,
147 function onNotice(event
) {
150 global
.modules
.emit('irc channel notice', {
152 connection
: this.irc_connection
,
156 that
.irc_connection
.clientEvent('notice', {
157 from_server
: event
.from_server
,
160 hostname
: event
.hostname
,
161 target
: event
.target
,
169 function onCtcpRequest(event
) {
170 this.irc_connection
.clientEvent('ctcp_request', {
173 hostname
: event
.hostname
,
174 target
: event
.target
,
182 function onCtcpResponse(event
) {
183 this.irc_connection
.clientEvent('ctcp_response', {
186 hostname
: event
.hostname
,
187 target
: event
.target
,
195 // TODO: Split event.users into batches of 50
196 function onNicklist(event
) {
197 this.irc_connection
.clientEvent('userlist', {
201 // TODO: uncomment when using an IrcUser per nick
202 //updateUsersList.call(this, event.users);
206 function onNicklistEnd(event
) {
207 this.irc_connection
.clientEvent('userlist_end', {
211 // TODO: uncomment when using an IrcUser per nick
212 //updateUsersList.call(this, event.users);
215 function updateUsersList(users
) {
218 users
.forEach(function (user
) {
219 if (!that
.irc_connection
.irc_users
[user
.nick
]) {
220 that
.irc_connection
.irc_users
[user
.nick
] = new IrcUser(that
.irc_connection
, user
.nick
);
227 function onTopic(event
) {
230 global
.modules
.emit('irc channel topic', {
232 connection
: this.irc_connection
,
236 that
.irc_connection
.clientEvent('topic', {
246 function onChannelInfo(event
) {
247 // Channel info event may contain 1 of several types of info,
248 // including creation time, modes. So just pipe the event
249 // right through to the client
250 this.irc_connection
.clientEvent('channel_info', event
);
254 function onBanList(event
) {
255 this.ban_list_buffer
.push(event
);
258 function onBanListEnd(event
) {
259 this.irc_connection
.clientEvent('banlist', {
261 bans
: this.ban_list_buffer
264 this.ban_list_buffer
= [];
267 function onTopicSetBy(event
) {
268 this.irc_connection
.clientEvent('topicsetby', {
270 channel
: event
.channel
,
275 function onMode(event
) {
278 global
.modules
.emit('irc channel mode', {
280 connection
: this.irc_connection
,
284 that
.irc_connection
.clientEvent('mode', {
285 target
: event
.target
,