Channel case-sensitivity issues. Main cause of "Cannot call method 'clientEvent'...
[KiwiIRC.git] / server / irc / channel.js
CommitLineData
cefa0900
JA
1var util = require('util'),
2 EventBinder = require('./eventbinder.js'),
3 IrcUser = require('./user.js');
b1fa8101 4
1f6caa9c 5var IrcChannel = function(irc_connection, name) {
a9833d9c 6 this.irc_connection = irc_connection;
36290f9f
D
7
8 // Lowercase the channel name so we don't run into case-sensitive issues
9 this.name = name.toLowerCase();
ffbbc70f 10
a9833d9c 11 this.members = [];
059f4918 12 this.ban_list_buffer = [];
635e02c3
D
13
14 // Listen for events on the IRC connection
15 this.irc_events = {
16 join: onJoin,
17 part: onPart,
18 kick: onKick,
19 quit: onQuit,
20 privmsg: onMsg,
21 notice: onNotice,
22 ctcp_request: onCtcpRequest,
23 ctcp_response: onCtcpResponse,
24 topic: onTopic,
cefa0900
JA
25 userlist: onNicklist,
26 userlist_end: onNicklistEnd,
635e02c3
D
27 banlist: onBanList,
28 banlist_end: onBanListEnd,
cefa0900 29 topicsetby: onTopicSetBy,
72db27e4
D
30 mode: onMode,
31 info: onChannelInfo
635e02c3 32 };
d9285da9 33 EventBinder.bindIrcEvents('channel ' + this.name, this.irc_events, this, irc_connection);
32a09dc1 34};
b1fa8101 35
2a8d2d5f
JA
36
37module.exports = IrcChannel;
38
b1fa8101 39
a9833d9c 40IrcChannel.prototype.dispose = function (){
d9285da9 41 EventBinder.unbindIrcEvents('channel ' + this.name, this.irc_events, this.irc_connection);
a9833d9c
D
42 this.irc_connection = undefined;
43};
b1fa8101 44
ffbbc70f 45
b1fa8101 46
a9833d9c 47function onJoin(event) {
6c7b2006
D
48 var that = this;
49
50 global.modules.emit('irc channel join', {
51 channel: this,
52 connection: this.irc_connection,
53 irc_event: event
54 })
55 .done(function() {
56 that.irc_connection.clientEvent('join', {
57 channel: that.name,
58 nick: event.nick,
59 ident: event.ident,
60 hostname: event.hostname,
61 time: event.time
62 });
a9833d9c 63 });
32a09dc1 64}
b1fa8101
D
65
66
a9833d9c 67function onPart(event) {
6c7b2006
D
68 var that = this;
69
70 global.modules.emit('irc channel part', {
71 channel: this,
72 connection: this.irc_connection,
73 irc_event: event
74 })
75 .done(function() {
76 that.irc_connection.clientEvent('part', {
77 nick: event.nick,
78 ident: event.ident,
79 hostname: event.hostname,
80 channel: that.name,
81 message: event.message,
82 time: event.time
83 });
ffbbc70f 84 });
32a09dc1 85}
b1fa8101 86
ffbbc70f 87
a9833d9c 88function onKick(event) {
5be54b3d
CC
89 var that = this;
90
6c7b2006
D
91 global.modules.emit('irc channel kick', {
92 channel: this,
93 connection: this.irc_connection,
94 irc_event: event
95 })
96 .done(function() {
5be54b3d 97 that.irc_connection.clientEvent('kick', {
6c7b2006
D
98 kicked: event.kicked, // Nick of the kicked
99 nick: event.nick, // Nick of the kicker
100 ident: event.ident,
101 hostname: event.hostname,
5be54b3d 102 channel: that.name,
6c7b2006
D
103 message: event.message,
104 time: event.time
105 });
ffbbc70f 106 });
32a09dc1 107}
b1fa8101
D
108
109
a9833d9c 110function onQuit(event) {
6c7b2006
D
111 var that = this;
112
113 global.modules.emit('irc channel quit', {
114 channel: this,
115 connection: this.irc_connection,
116 irc_event: event
117 })
118 .done(function() {
119 that.irc_connection.clientEvent('quit', {
120 nick: event.nick,
121 ident: event.ident,
122 hostname: event.hostname,
123 message: event.message,
124 time: event.time
125 });
ffbbc70f 126 });
32a09dc1 127}
ffbbc70f
D
128
129
a9833d9c 130function onMsg(event) {
6c7b2006
D
131 var that = this;
132
133 global.modules.emit('irc message', {
134 channel: this,
135 connection: this.irc_connection,
136 irc_event: event
137 })
138 .done(function() {
139 that.irc_connection.clientEvent('msg', {
140 nick: event.nick,
141 ident: event.ident,
142 hostname: event.hostname,
143 channel: that.name,
144 msg: event.msg,
145 time: event.time
146 });
ffbbc70f 147 });
32a09dc1 148}
ffbbc70f
D
149
150
a9833d9c 151function onNotice(event) {
6c7b2006
D
152 var that = this;
153
154 global.modules.emit('irc channel notice', {
155 channel: this,
156 connection: this.irc_connection,
157 irc_event: event
158 })
159 .done(function() {
160 that.irc_connection.clientEvent('notice', {
161 from_server: event.from_server,
162 nick: event.nick,
163 ident: event.ident,
164 hostname: event.hostname,
165 target: event.target,
166 msg: event.msg,
167 time: event.time
168 });
ffbbc70f 169 });
32a09dc1 170}
ffbbc70f
D
171
172
a9833d9c 173function onCtcpRequest(event) {
3c91bff8 174 this.irc_connection.clientEvent('ctcp_request', {
ffbbc70f
D
175 nick: event.nick,
176 ident: event.ident,
177 hostname: event.hostname,
178 target: event.target,
179 type: event.type,
d10c74a6
JA
180 msg: event.msg,
181 time: event.time
ffbbc70f 182 });
32a09dc1 183}
ffbbc70f
D
184
185
a9833d9c 186function onCtcpResponse(event) {
3c91bff8 187 this.irc_connection.clientEvent('ctcp_response', {
e2752476
D
188 nick: event.nick,
189 ident: event.ident,
190 hostname: event.hostname,
191 target: event.target,
192 type: event.type,
d10c74a6
JA
193 msg: event.msg,
194 time: event.time
e2752476 195 });
32a09dc1 196}
e2752476
D
197
198
ffbbc70f 199// TODO: Split event.users into batches of 50
a9833d9c 200function onNicklist(event) {
3c91bff8 201 this.irc_connection.clientEvent('userlist', {
ffbbc70f
D
202 users: event.users,
203 channel: this.name
204 });
df6d68a5
D
205 // TODO: uncomment when using an IrcUser per nick
206 //updateUsersList.call(this, event.users);
32a09dc1 207}
ffbbc70f
D
208
209
a9833d9c 210function onNicklistEnd(event) {
3c91bff8 211 this.irc_connection.clientEvent('userlist_end', {
ffbbc70f
D
212 users: event.users,
213 channel: this.name
214 });
df6d68a5
D
215 // TODO: uncomment when using an IrcUser per nick
216 //updateUsersList.call(this, event.users);
32a09dc1 217}
ffbbc70f 218
cefa0900
JA
219function updateUsersList(users) {
220 var that = this;
221 if (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);
225 }
226 });
227 }
228}
229
f4ec5774 230
a9833d9c 231function onTopic(event) {
6c7b2006
D
232 var that = this;
233
234 global.modules.emit('irc channel topic', {
235 channel: this,
236 connection: this.irc_connection,
237 irc_event: event
238 })
239 .done(function() {
240 that.irc_connection.clientEvent('topic', {
241 nick: event.nick,
242 channel: that.name,
243 topic: event.topic,
244 time: event.time
245 });
f4ec5774 246 });
32a09dc1 247}
f4ec5774 248
059f4918 249
72db27e4
D
250function 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);
255}
256
257
059f4918
JA
258function onBanList(event) {
259 this.ban_list_buffer.push(event);
32a09dc1 260}
059f4918
JA
261
262function onBanListEnd(event) {
72db27e4
D
263 this.irc_connection.clientEvent('banlist', {
264 channel: this.name,
265 bans: this.ban_list_buffer
059f4918 266 });
72db27e4 267
059f4918 268 this.ban_list_buffer = [];
32a09dc1 269}
059f4918 270
059f4918
JA
271function onTopicSetBy(event) {
272 this.irc_connection.clientEvent('topicsetby', {
273 nick: event.nick,
274 channel: event.channel,
275 when: event.when
276 });
32a09dc1 277}
059f4918 278
a7973dfb 279function onMode(event) {
6c7b2006
D
280 var that = this;
281
282 global.modules.emit('irc channel mode', {
283 channel: this,
284 connection: this.irc_connection,
285 irc_event: event
286 })
287 .done(function() {
288 that.irc_connection.clientEvent('mode', {
289 target: event.target,
290 nick: event.nick,
291 modes: event.modes,
292 time: event.time
293 });
a7973dfb 294 });
32a09dc1 295}