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