1 var util
= require('util'),
2 EventBinder
= require('./eventbinder.js'),
5 var IrcServer = function (irc_connection
) {
6 this.irc_connection
= irc_connection
;
14 list_start
: onListStart
,
15 list_channel
: onListChannel
,
17 motd_start
: onMotdStart
,
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 erroneus_nickname
: onErroneusNickname
36 EventBinder
.bindIrcEvents('server *', this.irc_events
, this, this.irc_connection
);
41 module
.exports
= IrcServer
;
44 IrcServer
.prototype.dispose = function (){
45 EventBinder
.unbindIrcEvents('server *', this.irc_events
, this.irc_connection
);
46 this.irc_connection
= undefined;
51 function onConnect(event
) {
52 this.irc_connection
.clientEvent('connect', {
57 function onOptions(event
) {
58 this.irc_connection
.clientEvent('options', {
59 options
: event
.options
,
64 function onListStart(event
) {
65 this.irc_connection
.clientEvent('list_start', {});
66 this.list_buffer
= [];
67 this.busy_listing
= true;
70 function onListChannel(event
) {
72 if (!this.busy_listing
) {
73 onListStart
.call(this);
75 this.list_buffer
.push({
76 channel
: event
.channel
,
77 num_users
: event
.num_users
,
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
;
86 this.irc_connection
.clientEvent('list_channel', {
89 this.list_buffer
= [];
93 function onListEnd(event
) {
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
;
100 this.irc_connection
.clientEvent('list_channel', {
103 this.list_buffer
= [];
104 this.busy_listing
= false;
106 this.irc_connection
.clientEvent('list_end', {});
109 function onMotdStart(event
) {
110 this.motd_buffer
= '';
113 function onMotd(event
) {
114 this.motd_buffer
+= event
.motd
;
117 function onMotdEnd(event
) {
118 this.irc_connection
.clientEvent('motd', {
119 msg
: this.motd_buffer
123 function onError(event
) {
124 this.irc_connection
.clientEvent('irc_error', {
130 function onPasswordMismatch(event
) {
131 this.irc_connection
.clientEvent('irc_error', {
132 error
: 'password_mismatch'
136 function onChannelRedirect(event
) {
137 this.irc_connection
.clientEvent('channel_redirect', {
143 function onNoSuchNick(event
) {
144 this.irc_connection
.clientEvent('irc_error', {
145 error
: 'no_such_nick',
151 function onCannotSendToChan(event
) {
152 this.irc_connection
.clientEvent('irc_error', {
153 error
: 'cannot_send_to_chan',
154 channel
: event
.channel
,
159 function onTooManyChannels(event
) {
160 this.irc_connection
.clientEvent('irc_error', {
161 error
: 'too_many_channels',
162 channel
: event
.channel
,
167 function onUserNotInChannel(event
) {
168 this.irc_connection
.clientEvent('irc_error', {
169 error
: 'user_not_in_channel',
171 channel
: event
.channel
,
176 function onNotOnChannel(event
) {
177 this.irc_connection
.clientEvent('irc_error', {
178 error
: 'not_on_channel',
179 channel
: event
.channel
,
184 function onChannelIsFull(event
) {
185 this.irc_connection
.clientEvent('irc_error', {
186 error
: 'channel_is_full',
187 channel
: event
.channel
,
192 function onInviteOnlyChannel(event
) {
193 this.irc_connection
.clientEvent('irc_error', {
194 error
: 'invite_only_channel',
195 channel
: event
.channel
,
200 function onBannedFromChannel(event
) {
201 this.irc_connection
.clientEvent('irc_error', {
202 error
: 'banned_from_channel',
203 channel
: event
.channel
,
208 function onBadChannelKey(event
) {
209 this.irc_connection
.clientEvent('irc_error', {
210 error
: 'bad_channel_key',
211 channel
: event
.channel
,
216 function onChanopPrivsNeeded(event
) {
217 this.irc_connection
.clientEvent('irc_error', {
218 error
: 'chanop_privs_needed',
219 channel
: event
.channel
,
224 function onNicknameInUse(event
) {
225 this.irc_connection
.clientEvent('irc_error', {
226 error
: 'nickname_in_use',
232 function onErroneusNickname(event
) {
233 this.irc_connection
.clientEvent('irc_error', {
234 error
: 'erroneus_nickname',