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