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
) {
89 global
.modules
.emit('irc channel kick', {
91 connection
: this.irc_connection
,
95 that
.irc_connection
.clientEvent('kick', {
96 kicked
: event
.kicked
, // Nick of the kicked
97 nick
: event
.nick
, // Nick of the kicker
99 hostname
: event
.hostname
,
101 message
: event
.message
,
108 function onQuit(event
) {
111 global
.modules
.emit('irc channel quit', {
113 connection
: this.irc_connection
,
117 that
.irc_connection
.clientEvent('quit', {
120 hostname
: event
.hostname
,
121 message
: event
.message
,
128 function onMsg(event
) {
131 global
.modules
.emit('irc message', {
133 connection
: this.irc_connection
,
137 that
.irc_connection
.clientEvent('msg', {
140 hostname
: event
.hostname
,
149 function onNotice(event
) {
152 global
.modules
.emit('irc channel notice', {
154 connection
: this.irc_connection
,
158 that
.irc_connection
.clientEvent('notice', {
159 from_server
: event
.from_server
,
162 hostname
: event
.hostname
,
163 target
: event
.target
,
171 function onCtcpRequest(event
) {
172 this.irc_connection
.clientEvent('ctcp_request', {
175 hostname
: event
.hostname
,
176 target
: event
.target
,
184 function onCtcpResponse(event
) {
185 this.irc_connection
.clientEvent('ctcp_response', {
188 hostname
: event
.hostname
,
189 target
: event
.target
,
197 // TODO: Split event.users into batches of 50
198 function onNicklist(event
) {
199 this.irc_connection
.clientEvent('userlist', {
203 // TODO: uncomment when using an IrcUser per nick
204 //updateUsersList.call(this, event.users);
208 function onNicklistEnd(event
) {
209 this.irc_connection
.clientEvent('userlist_end', {
213 // TODO: uncomment when using an IrcUser per nick
214 //updateUsersList.call(this, event.users);
217 function updateUsersList(users
) {
220 users
.forEach(function (user
) {
221 if (!that
.irc_connection
.irc_users
[user
.nick
]) {
222 that
.irc_connection
.irc_users
[user
.nick
] = new IrcUser(that
.irc_connection
, user
.nick
);
229 function onTopic(event
) {
232 global
.modules
.emit('irc channel topic', {
234 connection
: this.irc_connection
,
238 that
.irc_connection
.clientEvent('topic', {
248 function onChannelInfo(event
) {
249 // Channel info event may contain 1 of several types of info,
250 // including creation time, modes. So just pipe the event
251 // right through to the client
252 this.irc_connection
.clientEvent('channel_info', event
);
256 function onBanList(event
) {
257 this.ban_list_buffer
.push(event
);
260 function onBanListEnd(event
) {
261 this.irc_connection
.clientEvent('banlist', {
263 bans
: this.ban_list_buffer
266 this.ban_list_buffer
= [];
269 function onTopicSetBy(event
) {
270 this.irc_connection
.clientEvent('topicsetby', {
272 channel
: event
.channel
,
277 function onMode(event
) {
280 global
.modules
.emit('irc channel mode', {
282 connection
: this.irc_connection
,
286 that
.irc_connection
.clientEvent('mode', {
287 target
: event
.target
,