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