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