1 var util
= require('util'),
2 Binder
= require('./binder.js');
4 var IrcServer = function (irc_connection
, host
, port
) {
5 this.irc_connection
= irc_connection
;
9 this.scope
= 'server:' + host
;
13 this.list_buffer
= [];
14 this.motd_buffer
= '';
17 util
.inherits(IrcServer
, Binder
);
19 module
.exports
= IrcServer
;
21 IrcServer
.prototype.irc_events
= {
24 list_start
: onListStart
,
25 list_channel
: onListChannel
,
27 motd_start
: onMotdStart
,
31 channel_redirect
: onChannelRedirect
,
32 no_such_nick
: onNoSuchNick
,
33 cannot_send_to_channel
: onChannotSendToChan
,
34 too_many_channels
: onTooManyChannels
,
35 user_not_in_channel
: onUserNotInChannel
,
36 not_on_channel
: onNotOnChannel
,
37 channel_is_full
: onChannelisFull
,
38 invite_only_channel
: onInviteOnlyChannel
,
39 banned_from_channel
: onBannedFromChannel
,
40 bad_channel_key
: onBadChannelKey
,
41 chanop_privs_needed
: onChanopPrivsNeeded
,
42 nickname_in_use
: onNicknameInUse
45 function onConnect(event
) {
46 this.irc_connection
.clientEvent('connect', {
51 function onOptions(event
) {
52 this.irc_connection
.clientEvent('options', {
53 options
: event
.options
,
58 function onListStart(event
) {
59 this.irc_connection
.clientEvent('list_start', {});
62 function onListChannel(event
) {
64 this.list_buffer
.push({
65 channel
: event
.channel
,
66 num_users
: event
.num_users
,
70 if (this.list_buffer
.length
> 200) {
71 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
72 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
73 return 0 - channel
.num_users
;
75 this.irc_connection
.clientEvent('list_channel', {
78 this.list_buffer
= [];
82 function onListEnd(event
) {
83 if (this.list_buffer
.length
> 200) {
84 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
85 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
86 return 0 - channel
.num_users
;
88 this.irc_connection
.clientEvent('list_channel', {
91 this.list_buffer
= [];
94 this.irc_connection
.clientEvent('list_end', {});
97 function onMotdStart(event
) {
98 this.motd_buffer
= '';
101 function onMotd(event
) {
102 this.motd_buffer
+= event
.motd
;
105 function onMotdEnd(event
) {
106 this.irc_connection
.clientEvent('motd', {
107 msg
: this.motd_buffer
111 function onError(event
) {
112 this.irc_connection
.clientEvent('irc_error', {
118 function onChannelRedirect(event
) {
119 this.irc_connection
.clientEvent('channel_redirect', {
125 function onNoSuchNick(event
) {
126 this.irc_connection
.clientEvent('irc_error', {
127 error
: 'no_such_nick',
133 function onCannotSendToChan(event
) {
134 this.irc_connection
.clientEvent('irc_error', {
135 error
: 'cannot_send_to_chan',
136 channel
: event
.channel
,
141 function onTooManyChannels(event
) {
142 this.irc_connection
.clientEvent('irc_error', {
143 error
: 'too_many_channels',
144 channel
: event
.channel
,
149 function onUserNotInChannel(event
) {
150 this.irc_connection
.clientEvent('irc_error', {
151 error
: 'user_not_in_channel',
153 channel
: event
.channel
,
158 function onNotOnChannel(event
) {
159 this.irc_connection
.clientEvent('irc_error', {
160 error
: 'not_on_channel',
161 channel
: event
.channel
,
166 function onChannelIsFull(event
) {
167 this.irc_connection
.clientEvent('irc_error', {
168 error
: 'channel_is_full',
169 channel
: event
.channel
,
174 function onInviteOnlyChannel(event
) {
175 this.irc_connection
.clientEvent('irc_error', {
176 error
: 'invite_only_channel',
177 channel
: event
.channel
,
182 function onBannedFromChannel(event
) {
183 this.irc_connection
.clientEvent('irc_error', {
184 error
: 'banned_from_channel',
185 channel
: event
.channel
,
190 function onBadChannelKey(event
) {
191 this.irc_connection
.clientEvent('irc_error', {
192 error
: 'bad_channel_key',
193 channel
: event
.channel
,
198 function onChanopPrivsNeeded(event
) {
199 this.irc_connection
.clientEvent('irc_error', {
200 error
: 'chanop_privs_needed',
201 channel
: event
.channel
,
206 function onNicknameInUse(event
) {
207 this.irc_connection
.clientEvent('irc_error', {
208 error
: 'nickname_in_use',