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