Server module hooks for IRC events
[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 global.modules.emit('irc channel kick', {
88 channel: this,
89 connection: this.irc_connection,
90 irc_event: event
91 })
92 .done(function() {
93 this.irc_connection.clientEvent('kick', {
94 kicked: event.kicked, // Nick of the kicked
95 nick: event.nick, // Nick of the kicker
96 ident: event.ident,
97 hostname: event.hostname,
98 channel: this.name,
99 message: event.message,
100 time: event.time
101 });
102 });
103 }
104
105
106 function onQuit(event) {
107 var that = this;
108
109 global.modules.emit('irc channel quit', {
110 channel: this,
111 connection: this.irc_connection,
112 irc_event: event
113 })
114 .done(function() {
115 that.irc_connection.clientEvent('quit', {
116 nick: event.nick,
117 ident: event.ident,
118 hostname: event.hostname,
119 message: event.message,
120 time: event.time
121 });
122 });
123 }
124
125
126 function onMsg(event) {
127 var that = this;
128
129 global.modules.emit('irc message', {
130 channel: this,
131 connection: this.irc_connection,
132 irc_event: event
133 })
134 .done(function() {
135 that.irc_connection.clientEvent('msg', {
136 nick: event.nick,
137 ident: event.ident,
138 hostname: event.hostname,
139 channel: that.name,
140 msg: event.msg,
141 time: event.time
142 });
143 });
144 }
145
146
147 function onNotice(event) {
148 var that = this;
149
150 global.modules.emit('irc channel notice', {
151 channel: this,
152 connection: this.irc_connection,
153 irc_event: event
154 })
155 .done(function() {
156 that.irc_connection.clientEvent('notice', {
157 from_server: event.from_server,
158 nick: event.nick,
159 ident: event.ident,
160 hostname: event.hostname,
161 target: event.target,
162 msg: event.msg,
163 time: event.time
164 });
165 });
166 }
167
168
169 function onCtcpRequest(event) {
170 this.irc_connection.clientEvent('ctcp_request', {
171 nick: event.nick,
172 ident: event.ident,
173 hostname: event.hostname,
174 target: event.target,
175 type: event.type,
176 msg: event.msg,
177 time: event.time
178 });
179 }
180
181
182 function onCtcpResponse(event) {
183 this.irc_connection.clientEvent('ctcp_response', {
184 nick: event.nick,
185 ident: event.ident,
186 hostname: event.hostname,
187 target: event.target,
188 type: event.type,
189 msg: event.msg,
190 time: event.time
191 });
192 }
193
194
195 // TODO: Split event.users into batches of 50
196 function onNicklist(event) {
197 this.irc_connection.clientEvent('userlist', {
198 users: event.users,
199 channel: this.name
200 });
201 // TODO: uncomment when using an IrcUser per nick
202 //updateUsersList.call(this, event.users);
203 }
204
205
206 function onNicklistEnd(event) {
207 this.irc_connection.clientEvent('userlist_end', {
208 users: event.users,
209 channel: this.name
210 });
211 // TODO: uncomment when using an IrcUser per nick
212 //updateUsersList.call(this, event.users);
213 }
214
215 function updateUsersList(users) {
216 var that = this;
217 if (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);
221 }
222 });
223 }
224 }
225
226
227 function onTopic(event) {
228 var that = this;
229
230 global.modules.emit('irc channel topic', {
231 channel: this,
232 connection: this.irc_connection,
233 irc_event: event
234 })
235 .done(function() {
236 that.irc_connection.clientEvent('topic', {
237 nick: event.nick,
238 channel: that.name,
239 topic: event.topic,
240 time: event.time
241 });
242 });
243 }
244
245
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);
251 }
252
253
254 function onBanList(event) {
255 this.ban_list_buffer.push(event);
256 }
257
258 function onBanListEnd(event) {
259 this.irc_connection.clientEvent('banlist', {
260 channel: this.name,
261 bans: this.ban_list_buffer
262 });
263
264 this.ban_list_buffer = [];
265 }
266
267 function onTopicSetBy(event) {
268 this.irc_connection.clientEvent('topicsetby', {
269 nick: event.nick,
270 channel: event.channel,
271 when: event.when
272 });
273 }
274
275 function onMode(event) {
276 var that = this;
277
278 global.modules.emit('irc channel mode', {
279 channel: this,
280 connection: this.irc_connection,
281 irc_event: event
282 })
283 .done(function() {
284 that.irc_connection.clientEvent('mode', {
285 target: event.target,
286 nick: event.nick,
287 modes: event.modes,
288 time: event.time
289 });
290 });
291 }