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