1 var util
= require('util'),
2 events
= require('events'),
4 IrcConnection
= require('./connection.js').IrcConnection
;
6 var State = function (client
, save_state
) {
9 events
.EventEmitter
.call(this);
11 this.save_state
= save_state
|| false;
13 this.irc_connections
= [];
14 this.next_connection
= 0;
16 this.client
.on('dispose', function () {
17 if (!that
.save_state
) {
18 _
.each(that
.irc_connections
, function (irc_connection
, i
, cons
) {
20 irc_connection
.end('QUIT :' + (global
.config
.quit_message
|| ''));
21 irc_connection
.dispose();
31 util
.inherits(State
, events
.EventEmitter
);
33 module
.exports
= State
;
35 State
.prototype.connect = function (hostname
, port
, ssl
, nick
, user
, pass
, callback
) {
38 if (global
.config
.restrict_server
) {
39 con
= new IrcConnection(
40 global
.config
.restrict_server
,
41 global
.config
.restrict_server_port
,
42 global
.config
.restrict_server_ssl
,
45 global
.config
.restrict_server_password
,
49 con
= new IrcConnection(
59 con_num
= this.next_connection
++;
60 this.irc_connections
[con_num
] = con
;
61 con
.con_num
= con_num
;
63 new IrcCommands(con
, con_num
, this).bindEvents();
65 con
.on('connected', function () {
66 return callback(null, con_num
);
69 con
.on('error', function (err
) {
70 console
.log('irc_connection error (' + hostname
+ '):', err
);
71 return callback(err
.code
, {server
: con_num
, error
: err
});
74 con
.on('close', function () {
75 that
.irc_connections
[con_num
] = null;
79 State
.prototype.sendIrcCommand = function () {
80 this.client
.sendIrcCommand
.apply(this.client
, arguments
);
83 State
.prototype.sendKiwiCommand = function () {
84 this.client
.sendKiwicommand
.apply(this.client
, arguments
);
87 State
.prototype.dispose = function () {
89 this.removeAllListeners();