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
35 EventBinder
.bindIrcEvents('server *', this.irc_events
, this, this.irc_connection
);
40 module
.exports
= IrcServer
;
43 IrcServer
.prototype.dispose = function (){
44 EventBinder
.unbindIrcEvents('server *', this.irc_events
, this.irc_connection
);
45 this.irc_connection
= undefined;
50 function onConnect(event
) {
51 this.irc_connection
.clientEvent('connect', {
56 function onOptions(event
) {
57 this.irc_connection
.clientEvent('options', {
58 options
: event
.options
,
63 function onListStart(event
) {
64 this.irc_connection
.clientEvent('list_start', {});
65 this.list_buffer
= [];
66 this.busy_listing
= true;
69 function onListChannel(event
) {
71 if (!this.busy_listing
) {
72 onListStart
.call(this);
74 this.list_buffer
.push({
75 channel
: event
.channel
,
76 num_users
: event
.num_users
,
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
;
85 this.irc_connection
.clientEvent('list_channel', {
88 this.list_buffer
= [];
92 function onListEnd(event
) {
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
;
99 this.irc_connection
.clientEvent('list_channel', {
102 this.list_buffer
= [];
103 this.busy_listing
= false;
105 this.irc_connection
.clientEvent('list_end', {});
108 function onMotdStart(event
) {
109 this.motd_buffer
= '';
112 function onMotd(event
) {
113 this.motd_buffer
+= event
.motd
;
116 function onMotdEnd(event
) {
117 this.irc_connection
.clientEvent('motd', {
118 msg
: this.motd_buffer
122 function onError(event
) {
123 this.irc_connection
.clientEvent('irc_error', {
129 function onPasswordMismatch(event
) {
130 this.irc_connection
.clientEvent('irc_error', {
131 error
: 'password_mismatch'
135 function onChannelRedirect(event
) {
136 this.irc_connection
.clientEvent('channel_redirect', {
142 function onNoSuchNick(event
) {
143 this.irc_connection
.clientEvent('irc_error', {
144 error
: 'no_such_nick',
150 function onCannotSendToChan(event
) {
151 this.irc_connection
.clientEvent('irc_error', {
152 error
: 'cannot_send_to_chan',
153 channel
: event
.channel
,
158 function onTooManyChannels(event
) {
159 this.irc_connection
.clientEvent('irc_error', {
160 error
: 'too_many_channels',
161 channel
: event
.channel
,
166 function onUserNotInChannel(event
) {
167 this.irc_connection
.clientEvent('irc_error', {
168 error
: 'user_not_in_channel',
170 channel
: event
.channel
,
175 function onNotOnChannel(event
) {
176 this.irc_connection
.clientEvent('irc_error', {
177 error
: 'not_on_channel',
178 channel
: event
.channel
,
183 function onChannelIsFull(event
) {
184 this.irc_connection
.clientEvent('irc_error', {
185 error
: 'channel_is_full',
186 channel
: event
.channel
,
191 function onInviteOnlyChannel(event
) {
192 this.irc_connection
.clientEvent('irc_error', {
193 error
: 'invite_only_channel',
194 channel
: event
.channel
,
199 function onBannedFromChannel(event
) {
200 this.irc_connection
.clientEvent('irc_error', {
201 error
: 'banned_from_channel',
202 channel
: event
.channel
,
207 function onBadChannelKey(event
) {
208 this.irc_connection
.clientEvent('irc_error', {
209 error
: 'bad_channel_key',
210 channel
: event
.channel
,
215 function onChanopPrivsNeeded(event
) {
216 this.irc_connection
.clientEvent('irc_error', {
217 error
: 'chanop_privs_needed',
218 channel
: event
.channel
,
223 function onNicknameInUse(event
) {
224 this.irc_connection
.clientEvent('irc_error', {
225 error
: 'nickname_in_use',