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