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