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