IE8 support - indexOf
[KiwiIRC.git] / server / irc / server.js
1 var util = require('util'),
2 EventBinder = require('./eventbinder.js'),
3 _ = require('lodash');
4
5 var IrcServer = function (irc_connection) {
6 this.irc_connection = irc_connection;
7
8 this.list_buffer = [];
9 this.motd_buffer = '';
10
11 this.irc_events = {
12 connect: onConnect,
13 options: onOptions,
14 list_start: onListStart,
15 list_channel: onListChannel,
16 list_end: onListEnd,
17 motd_start: onMotdStart,
18 motd: onMotd,
19 motd_end: onMotdEnd,
20 error: onError,
21 password_mismatch: onPasswordMismatch,
22 channel_redirect: onChannelRedirect,
23 no_such_nick: onNoSuchNick,
24 cannot_send_to_channel: onCannotSendToChan,
25 too_many_channels: onTooManyChannels,
26 user_not_in_channel: onUserNotInChannel,
27 not_on_channel: onNotOnChannel,
28 channel_is_full: onChannelIsFull,
29 invite_only_channel: onInviteOnlyChannel,
30 user_on_channel: onUserAlreadyInChannel,
31 banned_from_channel: onBannedFromChannel,
32 bad_channel_key: onBadChannelKey,
33 chanop_privs_needed: onChanopPrivsNeeded,
34 nickname_in_use: onNicknameInUse,
35 erroneus_nickname: onErroneusNickname,
36 unknown_command: onUnknownCommand
37 };
38 EventBinder.bindIrcEvents('server *', this.irc_events, this, this.irc_connection);
39
40 };
41
42
43 module.exports = IrcServer;
44
45
46 IrcServer.prototype.dispose = function (){
47 EventBinder.unbindIrcEvents('server *', this.irc_events, this.irc_connection);
48 this.irc_connection = undefined;
49 };
50
51
52
53 function onConnect(event) {
54 this.irc_connection.clientEvent('connect', {
55 nick: event.nick
56 });
57 }
58
59 function onOptions(event) {
60 this.irc_connection.clientEvent('options', {
61 options: event.options,
62 cap: event.cap
63 });
64 }
65
66 function onListStart(event) {
67 this.irc_connection.clientEvent('list_start', {});
68 this.list_buffer = [];
69 this.busy_listing = true;
70 }
71
72 function onListChannel(event) {
73 var buf;
74 if (!this.busy_listing) {
75 onListStart.call(this);
76 }
77 this.list_buffer.push({
78 channel: event.channel,
79 num_users: event.num_users,
80 topic: event.topic
81 });
82
83 if (this.list_buffer.length > 200) {
84 buf = _.sortBy(this.list_buffer, function (channel) {
85 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
86 return 0 - channel.num_users;
87 });
88 this.irc_connection.clientEvent('list_channel', {
89 chans: buf
90 });
91 this.list_buffer = [];
92 }
93 }
94
95 function onListEnd(event) {
96 var buf;
97
98 buf = _.sortBy(this.list_buffer, function (channel) {
99 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
100 return 0 - channel.num_users;
101 });
102 this.irc_connection.clientEvent('list_channel', {
103 chans: buf
104 });
105 this.list_buffer = [];
106 this.busy_listing = false;
107
108 this.irc_connection.clientEvent('list_end', {});
109 }
110
111 function onMotdStart(event) {
112 this.motd_buffer = '';
113 }
114
115 function onMotd(event) {
116 this.motd_buffer += event.motd;
117 }
118
119 function onMotdEnd(event) {
120 this.irc_connection.clientEvent('motd', {
121 msg: this.motd_buffer
122 });
123 }
124
125 function onError(event) {
126 this.irc_connection.clientEvent('irc_error', {
127 error: 'error',
128 reason: event.reason
129 });
130 }
131
132 function onPasswordMismatch(event) {
133 this.irc_connection.clientEvent('irc_error', {
134 error: 'password_mismatch'
135 });
136 }
137
138 function onChannelRedirect(event) {
139 this.irc_connection.clientEvent('channel_redirect', {
140 from: event.from,
141 to: event.to
142 });
143 }
144
145 function onNoSuchNick(event) {
146 this.irc_connection.clientEvent('irc_error', {
147 error: 'no_such_nick',
148 nick: event.nick,
149 reason: event.reason
150 });
151 }
152
153 function onCannotSendToChan(event) {
154 this.irc_connection.clientEvent('irc_error', {
155 error: 'cannot_send_to_chan',
156 channel: event.channel,
157 reason: event.reason
158 });
159 }
160
161 function onTooManyChannels(event) {
162 this.irc_connection.clientEvent('irc_error', {
163 error: 'too_many_channels',
164 channel: event.channel,
165 reason: event.reason
166 });
167 }
168
169 function onUserNotInChannel(event) {
170 this.irc_connection.clientEvent('irc_error', {
171 error: 'user_not_in_channel',
172 nick: event.nick,
173 channel: event.channel,
174 reason: event.reason
175 });
176 }
177
178 function onNotOnChannel(event) {
179 this.irc_connection.clientEvent('irc_error', {
180 error: 'not_on_channel',
181 channel: event.channel,
182 reason: event.reason
183 });
184 }
185
186 function onChannelIsFull(event) {
187 this.irc_connection.clientEvent('irc_error', {
188 error: 'channel_is_full',
189 channel: event.channel,
190 reason: event.reason
191 });
192 }
193
194 function onInviteOnlyChannel(event) {
195 this.irc_connection.clientEvent('irc_error', {
196 error: 'invite_only_channel',
197 channel: event.channel,
198 reason: event.reason
199 });
200 }
201
202 function onUserAlreadyInChannel(event) {
203 this.irc_connection.clientEvent('irc_error', {
204 error: 'user_on_channel',
205 channel: event.channel,
206 nick: event.nick
207 });
208 }
209
210 function onBannedFromChannel(event) {
211 this.irc_connection.clientEvent('irc_error', {
212 error: 'banned_from_channel',
213 channel: event.channel,
214 reason: event.reason
215 });
216 }
217
218 function onBadChannelKey(event) {
219 this.irc_connection.clientEvent('irc_error', {
220 error: 'bad_channel_key',
221 channel: event.channel,
222 reason: event.reason
223 });
224 }
225
226 function onChanopPrivsNeeded(event) {
227 this.irc_connection.clientEvent('irc_error', {
228 error: 'chanop_privs_needed',
229 channel: event.channel,
230 reason: event.reason
231 });
232 }
233
234 function onNicknameInUse(event) {
235 this.irc_connection.clientEvent('irc_error', {
236 error: 'nickname_in_use',
237 nick: event.nick,
238 reason: event.reason
239 });
240 }
241
242 function onErroneusNickname(event) {
243 this.irc_connection.clientEvent('irc_error', {
244 error: 'erroneus_nickname',
245 nick: event.nick,
246 reason: event.reason
247 });
248 }
249
250 function onUnknownCommand(event) {
251 this.irc_connection.clientEvent('unknown_command', {
252 error: 'unknown_command',
253 command: event.command,
254 params: event.params
255 });
256 }