d7498460d2c8adfb2a81ffb0ca81b6cf6ea744b2
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 password_mismatch
: onPasswordMismatch
,
24 channel_redirect
: onChannelRedirect
,
25 no_such_nick
: onNoSuchNick
,
26 cannot_send_to_channel
: onCannotSendToChan
,
27 too_many_channels
: onTooManyChannels
,
28 user_not_in_channel
: onUserNotInChannel
,
29 not_on_channel
: onNotOnChannel
,
30 channel_is_full
: onChannelIsFull
,
31 invite_only_channel
: onInviteOnlyChannel
,
32 banned_from_channel
: onBannedFromChannel
,
33 bad_channel_key
: onBadChannelKey
,
34 chanop_privs_needed
: onChanopPrivsNeeded
,
35 nickname_in_use
: onNicknameInUse
37 EventBinder
.bindIrcEvents('server:' + this.host
, this.irc_events
, this, this.irc_connection
);
43 module
.exports
= IrcServer
;
46 IrcServer
.prototype.dispose = function (){
47 EventBinder
.unbindIrcEvents('server:' + this.host
, this.irc_events
, this.irc_connection
);
48 this.irc_connection
= undefined;
53 function onConnect(event
) {
54 this.irc_connection
.clientEvent('connect', {
59 function onOptions(event
) {
60 this.irc_connection
.clientEvent('options', {
61 options
: event
.options
,
66 function onListStart(event
) {
67 this.irc_connection
.clientEvent('list_start', {});
68 this.list_buffer
= [];
71 function onListChannel(event
) {
73 this.list_buffer
.push({
74 channel
: event
.channel
,
75 num_users
: event
.num_users
,
79 if (this.list_buffer
.length
> 200) {
80 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
81 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
82 return 0 - channel
.num_users
;
84 this.irc_connection
.clientEvent('list_channel', {
87 this.list_buffer
= [];
91 function onListEnd(event
) {
94 buf
= _
.sortBy(this.list_buffer
, function (channel
) {
95 // sortBy sorts in ascending order, we want to sort by descending, hence using 0 - num_users.
96 return 0 - channel
.num_users
;
98 this.irc_connection
.clientEvent('list_channel', {
101 this.list_buffer
= [];
104 this.irc_connection
.clientEvent('list_end', {});
107 function onMotdStart(event
) {
108 this.motd_buffer
= '';
111 function onMotd(event
) {
112 this.motd_buffer
+= event
.motd
;
115 function onMotdEnd(event
) {
116 this.irc_connection
.clientEvent('motd', {
117 msg
: this.motd_buffer
121 function onError(event
) {
122 this.irc_connection
.clientEvent('irc_error', {
128 function onPasswordMismatch(event
) {
129 this.irc_connection
.clientEvent('irc_error', {
130 error
: 'password_mismatch'
134 function onChannelRedirect(event
) {
135 this.irc_connection
.clientEvent('channel_redirect', {
141 function onNoSuchNick(event
) {
142 this.irc_connection
.clientEvent('irc_error', {
143 error
: 'no_such_nick',
149 function onCannotSendToChan(event
) {
150 this.irc_connection
.clientEvent('irc_error', {
151 error
: 'cannot_send_to_chan',
152 channel
: event
.channel
,
157 function onTooManyChannels(event
) {
158 this.irc_connection
.clientEvent('irc_error', {
159 error
: 'too_many_channels',
160 channel
: event
.channel
,
165 function onUserNotInChannel(event
) {
166 this.irc_connection
.clientEvent('irc_error', {
167 error
: 'user_not_in_channel',
169 channel
: event
.channel
,
174 function onNotOnChannel(event
) {
175 this.irc_connection
.clientEvent('irc_error', {
176 error
: 'not_on_channel',
177 channel
: event
.channel
,
182 function onChannelIsFull(event
) {
183 this.irc_connection
.clientEvent('irc_error', {
184 error
: 'channel_is_full',
185 channel
: event
.channel
,
190 function onInviteOnlyChannel(event
) {
191 this.irc_connection
.clientEvent('irc_error', {
192 error
: 'invite_only_channel',
193 channel
: event
.channel
,
198 function onBannedFromChannel(event
) {
199 this.irc_connection
.clientEvent('irc_error', {
200 error
: 'banned_from_channel',
201 channel
: event
.channel
,
206 function onBadChannelKey(event
) {
207 this.irc_connection
.clientEvent('irc_error', {
208 error
: 'bad_channel_key',
209 channel
: event
.channel
,
214 function onChanopPrivsNeeded(event
) {
215 this.irc_connection
.clientEvent('irc_error', {
216 error
: 'chanop_privs_needed',
217 channel
: event
.channel
,
222 function onNicknameInUse(event
) {
223 this.irc_connection
.clientEvent('irc_error', {
224 error
: 'nickname_in_use',