1 var util
= require('util'),
2 EventBinder
= require('./eventbinder.js'),
5 var IrcServer = function (irc_connection
, host
, port
) {
6 this.irc_connection
= irc_connection
;
10 this.list_buffer
= [];
11 this.motd_buffer
= '';
16 list_start
: onListStart
,
17 list_channel
: onListChannel
,
19 motd_start
: onMotdStart
,
23 channel_redirect
: onChannelRedirect
,
24 no_such_nick
: onNoSuchNick
,
25 cannot_send_to_channel
: onCannotSendToChan
,
26 too_many_channels
: onTooManyChannels
,
27 user_not_in_channel
: onUserNotInChannel
,
28 not_on_channel
: onNotOnChannel
,
29 channel_is_full
: onChannelIsFull
,
30 invite_only_channel
: onInviteOnlyChannel
,
31 banned_from_channel
: onBannedFromChannel
,
32 bad_channel_key
: onBadChannelKey
,
33 chanop_privs_needed
: onChanopPrivsNeeded
,
34 nickname_in_use
: onNicknameInUse
36 EventBinder
.bindIrcEvents('server:' + this.host
, this.irc_events
, this, irc_connection
);
42 module
.exports
= IrcServer
;
45 IrcServer
.prototype.dispose = function (){
46 EventBinder
.unbindIrcEvents('server:' + this.host
, this.irc_events
);
47 this.irc_connection
= undefined;
52 function onConnect(event
) {
53 this.irc_connection
.clientEvent('connect', {
58 function onOptions(event
) {
59 this.irc_connection
.clientEvent('options', {
60 options
: event
.options
,
65 function onListStart(event
) {
66 this.irc_connection
.clientEvent('list_start', {});
67 this.list_buffer
= [];
70 function onListChannel(event
) {
72 this.list_buffer
.push({
73 channel
: event
.channel
,
74 num_users
: event
.num_users
,
78 if (this.list_buffer
.length
> 200) {
79 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
80 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
81 return 0 - channel
.num_users
;
83 this.irc_connection
.clientEvent('list_channel', {
86 this.list_buffer
= [];
90 function onListEnd(event
) {
93 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
94 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
95 return 0 - channel
.num_users
;
97 this.irc_connection
.clientEvent('list_channel', {
100 this.list_buffer
= [];
103 this.irc_connection
.clientEvent('list_end', {});
106 function onMotdStart(event
) {
107 this.motd_buffer
= '';
110 function onMotd(event
) {
111 this.motd_buffer
+= event
.motd
;
114 function onMotdEnd(event
) {
115 this.irc_connection
.clientEvent('motd', {
116 msg
: this.motd_buffer
120 function onError(event
) {
121 this.irc_connection
.clientEvent('irc_error', {
127 function onChannelRedirect(event
) {
128 this.irc_connection
.clientEvent('channel_redirect', {
134 function onNoSuchNick(event
) {
135 this.irc_connection
.clientEvent('irc_error', {
136 error
: 'no_such_nick',
142 function onCannotSendToChan(event
) {
143 this.irc_connection
.clientEvent('irc_error', {
144 error
: 'cannot_send_to_chan',
145 channel
: event
.channel
,
150 function onTooManyChannels(event
) {
151 this.irc_connection
.clientEvent('irc_error', {
152 error
: 'too_many_channels',
153 channel
: event
.channel
,
158 function onUserNotInChannel(event
) {
159 this.irc_connection
.clientEvent('irc_error', {
160 error
: 'user_not_in_channel',
162 channel
: event
.channel
,
167 function onNotOnChannel(event
) {
168 this.irc_connection
.clientEvent('irc_error', {
169 error
: 'not_on_channel',
170 channel
: event
.channel
,
175 function onChannelIsFull(event
) {
176 this.irc_connection
.clientEvent('irc_error', {
177 error
: 'channel_is_full',
178 channel
: event
.channel
,
183 function onInviteOnlyChannel(event
) {
184 this.irc_connection
.clientEvent('irc_error', {
185 error
: 'invite_only_channel',
186 channel
: event
.channel
,
191 function onBannedFromChannel(event
) {
192 this.irc_connection
.clientEvent('irc_error', {
193 error
: 'banned_from_channel',
194 channel
: event
.channel
,
199 function onBadChannelKey(event
) {
200 this.irc_connection
.clientEvent('irc_error', {
201 error
: 'bad_channel_key',
202 channel
: event
.channel
,
207 function onChanopPrivsNeeded(event
) {
208 this.irc_connection
.clientEvent('irc_error', {
209 error
: 'chanop_privs_needed',
210 channel
: event
.channel
,
215 function onNicknameInUse(event
) {
216 this.irc_connection
.clientEvent('irc_error', {
217 error
: 'nickname_in_use',