Commit | Line | Data |
---|---|---|
2a8d2d5f | 1 | var util = require('util'), |
25edd441 D |
2 | EventBinder = require('./eventbinder.js'), |
3 | _ = require('lodash'); | |
2a8d2d5f | 4 | |
dfb8437c | 5 | var IrcServer = function (irc_connection) { |
1cc056b8 | 6 | this.irc_connection = irc_connection; |
635e02c3 | 7 | |
1cc056b8 JA |
8 | this.list_buffer = []; |
9 | this.motd_buffer = ''; | |
32a09dc1 | 10 | |
635e02c3 D |
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, | |
ac24e0a7 | 20 | error: onError, |
ebe178d6 | 21 | password_mismatch: onPasswordMismatch, |
635e02c3 D |
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, | |
3d753975 | 30 | user_on_channel: onUserAlreadyInChannel, |
635e02c3 D |
31 | banned_from_channel: onBannedFromChannel, |
32 | bad_channel_key: onBadChannelKey, | |
33 | chanop_privs_needed: onChanopPrivsNeeded, | |
66c980d3 | 34 | nickname_in_use: onNicknameInUse, |
3d753975 D |
35 | erroneus_nickname: onErroneusNickname, |
36 | unknown_command: onUnknownCommand | |
635e02c3 | 37 | }; |
d9285da9 | 38 | EventBinder.bindIrcEvents('server *', this.irc_events, this, this.irc_connection); |
635e02c3 | 39 | |
1cc056b8 JA |
40 | }; |
41 | ||
1cc056b8 | 42 | |
2a8d2d5f | 43 | module.exports = IrcServer; |
1cc056b8 | 44 | |
635e02c3 D |
45 | |
46 | IrcServer.prototype.dispose = function (){ | |
d9285da9 | 47 | EventBinder.unbindIrcEvents('server *', this.irc_events, this.irc_connection); |
635e02c3 | 48 | this.irc_connection = undefined; |
1cc056b8 JA |
49 | }; |
50 | ||
635e02c3 D |
51 | |
52 | ||
1cc056b8 JA |
53 | function onConnect(event) { |
54 | this.irc_connection.clientEvent('connect', { | |
55 | nick: event.nick | |
56 | }); | |
32a09dc1 | 57 | } |
1cc056b8 JA |
58 | |
59 | function onOptions(event) { | |
60 | this.irc_connection.clientEvent('options', { | |
61 | options: event.options, | |
62 | cap: event.cap | |
63 | }); | |
32a09dc1 | 64 | } |
1cc056b8 JA |
65 | |
66 | function onListStart(event) { | |
67 | this.irc_connection.clientEvent('list_start', {}); | |
25edd441 | 68 | this.list_buffer = []; |
a1c04b98 | 69 | this.busy_listing = true; |
32a09dc1 | 70 | } |
1cc056b8 JA |
71 | |
72 | function onListChannel(event) { | |
d60cd97a | 73 | var buf; |
a1c04b98 | 74 | if (!this.busy_listing) { |
d60cd97a | 75 | onListStart.call(this); |
a1c04b98 | 76 | } |
1cc056b8 JA |
77 | this.list_buffer.push({ |
78 | channel: event.channel, | |
79 | num_users: event.num_users, | |
80 | topic: event.topic | |
81 | }); | |
32a09dc1 | 82 | |
1cc056b8 JA |
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 = []; | |
25edd441 | 92 | } |
32a09dc1 | 93 | } |
1cc056b8 JA |
94 | |
95 | function onListEnd(event) { | |
25edd441 | 96 | var buf; |
32a09dc1 | 97 | |
25edd441 D |
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 = []; | |
a1c04b98 | 106 | this.busy_listing = false; |
32a09dc1 | 107 | |
1cc056b8 | 108 | this.irc_connection.clientEvent('list_end', {}); |
32a09dc1 | 109 | } |
1cc056b8 JA |
110 | |
111 | function onMotdStart(event) { | |
112 | this.motd_buffer = ''; | |
32a09dc1 | 113 | } |
1cc056b8 JA |
114 | |
115 | function onMotd(event) { | |
116 | this.motd_buffer += event.motd; | |
32a09dc1 | 117 | } |
1cc056b8 JA |
118 | |
119 | function onMotdEnd(event) { | |
120 | this.irc_connection.clientEvent('motd', { | |
121 | msg: this.motd_buffer | |
122 | }); | |
32a09dc1 | 123 | } |
1cc056b8 | 124 | |
ac24e0a7 | 125 | function onError(event) { |
1cc056b8 JA |
126 | this.irc_connection.clientEvent('irc_error', { |
127 | error: 'error', | |
128 | reason: event.reason | |
129 | }); | |
32a09dc1 | 130 | } |
1cc056b8 | 131 | |
ebe178d6 D |
132 | function onPasswordMismatch(event) { |
133 | this.irc_connection.clientEvent('irc_error', { | |
134 | error: 'password_mismatch' | |
135 | }); | |
136 | } | |
137 | ||
1cc056b8 JA |
138 | function onChannelRedirect(event) { |
139 | this.irc_connection.clientEvent('channel_redirect', { | |
140 | from: event.from, | |
141 | to: event.to | |
142 | }); | |
32a09dc1 | 143 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 151 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 159 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 167 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 176 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 184 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 192 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 200 | } |
1cc056b8 | 201 | |
3d753975 D |
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 | ||
1cc056b8 JA |
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 | }); | |
32a09dc1 | 216 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 224 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 232 | } |
1cc056b8 JA |
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 | }); | |
32a09dc1 | 240 | } |
66c980d3 JA |
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 | } | |
3d753975 D |
249 | |
250 | function onUnknownCommand(event) { | |
251 | this.irc_connection.clientEvent('unknown_command', { | |
252 | error: 'unknown_command', | |
253 | command: event.command, | |
17dfa698 | 254 | params: event.params |
3d753975 D |
255 | }); |
256 | } |