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
;
8 // Lowercase the channel name so we don't run into case-sensitive issues
9 this.name
= name
.toLowerCase();
12 this.ban_list_buffer
= [];
14 // Listen for events on the IRC connection
22 ctcp_request
: onCtcpRequest
,
23 ctcp_response
: onCtcpResponse
,
26 userlist_end
: onNicklistEnd
,
28 banlist_end
: onBanListEnd
,
29 topicsetby
: onTopicSetBy
,
33 EventBinder
.bindIrcEvents('channel ' + this.name
, this.irc_events
, this, irc_connection
);
37 module
.exports
= IrcChannel
;
40 IrcChannel
.prototype.dispose = function (){
41 EventBinder
.unbindIrcEvents('channel ' + this.name
, this.irc_events
, this.irc_connection
);
42 this.irc_connection
= undefined;
47 function onJoin(event
) {
50 global
.modules
.emit('irc channel join', {
52 connection
: this.irc_connection
,
56 that
.irc_connection
.clientEvent('join', {
60 hostname
: event
.hostname
,
67 function onPart(event
) {
70 global
.modules
.emit('irc channel part', {
72 connection
: this.irc_connection
,
76 that
.irc_connection
.clientEvent('part', {
79 hostname
: event
.hostname
,
81 message
: event
.message
,
88 function onKick(event
) {
91 global
.modules
.emit('irc channel kick', {
93 connection
: this.irc_connection
,
97 that
.irc_connection
.clientEvent('kick', {
98 kicked
: event
.kicked
, // Nick of the kicked
99 nick
: event
.nick
, // Nick of the kicker
101 hostname
: event
.hostname
,
103 message
: event
.message
,
110 function onQuit(event
) {
113 global
.modules
.emit('irc channel quit', {
115 connection
: this.irc_connection
,
119 that
.irc_connection
.clientEvent('quit', {
122 hostname
: event
.hostname
,
123 message
: event
.message
,
130 function onMsg(event
) {
133 global
.modules
.emit('irc message', {
135 connection
: this.irc_connection
,
139 that
.irc_connection
.clientEvent('msg', {
142 hostname
: event
.hostname
,
151 function onNotice(event
) {
154 global
.modules
.emit('irc channel notice', {
156 connection
: this.irc_connection
,
160 that
.irc_connection
.clientEvent('notice', {
161 from_server
: event
.from_server
,
164 hostname
: event
.hostname
,
165 target
: event
.target
,
173 function onCtcpRequest(event
) {
174 this.irc_connection
.clientEvent('ctcp_request', {
177 hostname
: event
.hostname
,
178 target
: event
.target
,
186 function onCtcpResponse(event
) {
187 this.irc_connection
.clientEvent('ctcp_response', {
190 hostname
: event
.hostname
,
191 target
: event
.target
,
199 // TODO: Split event.users into batches of 50
200 function onNicklist(event
) {
201 this.irc_connection
.clientEvent('userlist', {
205 // TODO: uncomment when using an IrcUser per nick
206 //updateUsersList.call(this, event.users);
210 function onNicklistEnd(event
) {
211 this.irc_connection
.clientEvent('userlist_end', {
215 // TODO: uncomment when using an IrcUser per nick
216 //updateUsersList.call(this, event.users);
219 function updateUsersList(users
) {
222 users
.forEach(function (user
) {
223 if (!that
.irc_connection
.irc_users
[user
.nick
]) {
224 that
.irc_connection
.irc_users
[user
.nick
] = new IrcUser(that
.irc_connection
, user
.nick
);
231 function onTopic(event
) {
234 global
.modules
.emit('irc channel topic', {
236 connection
: this.irc_connection
,
240 that
.irc_connection
.clientEvent('topic', {
250 function onChannelInfo(event
) {
251 // Channel info event may contain 1 of several types of info,
252 // including creation time, modes. So just pipe the event
253 // right through to the client
254 this.irc_connection
.clientEvent('channel_info', event
);
258 function onBanList(event
) {
259 this.ban_list_buffer
.push(event
);
262 function onBanListEnd(event
) {
263 this.irc_connection
.clientEvent('banlist', {
265 bans
: this.ban_list_buffer
268 this.ban_list_buffer
= [];
271 function onTopicSetBy(event
) {
272 this.irc_connection
.clientEvent('topicsetby', {
274 channel
: event
.channel
,
279 function onMode(event
) {
282 global
.modules
.emit('irc channel mode', {
284 connection
: this.irc_connection
,
288 that
.irc_connection
.clientEvent('mode', {
289 target
: event
.target
,