Use a space instead of colon as event delimiter
[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
41 module.exports = IrcServer;
42
43
44 IrcServer.prototype.dispose = function (){
45 EventBinder.unbindIrcEvents('server *', this.irc_events, this.irc_connection);
46 this.irc_connection = undefined;
47 };
48
49
50
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 this.list_buffer = [];
67 this.busy_listing = true;
68 };
69
70 function onListChannel(event) {
71 var buf;
72 if (!this.busy_listing) {
73 onListStart.call(this);
74 }
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 = [];
90 }
91 };
92
93 function onListEnd(event) {
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 = [];
104 this.busy_listing = false;
105
106 this.irc_connection.clientEvent('list_end', {});
107 };
108
109 function onMotdStart(event) {
110 this.motd_buffer = '';
111 };
112
113 function onMotd(event) {
114 this.motd_buffer += event.motd;
115 };
116
117 function onMotdEnd(event) {
118 this.irc_connection.clientEvent('motd', {
119 msg: this.motd_buffer
120 });
121 };
122
123 function onError(event) {
124 this.irc_connection.clientEvent('irc_error', {
125 error: 'error',
126 reason: event.reason
127 });
128 };
129
130 function onPasswordMismatch(event) {
131 this.irc_connection.clientEvent('irc_error', {
132 error: 'password_mismatch'
133 });
134 }
135
136 function onChannelRedirect(event) {
137 this.irc_connection.clientEvent('channel_redirect', {
138 from: event.from,
139 to: event.to
140 });
141 };
142
143 function 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
151 function 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
159 function 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
167 function 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
176 function 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
184 function 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
192 function 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
200 function 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
208 function 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
216 function 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
224 function onNicknameInUse(event) {
225 this.irc_connection.clientEvent('irc_error', {
226 error: 'nickname_in_use',
227 nick: event.nick,
228 reason: event.reason
229 });
230 };