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