1 var util
= require('util'),
2 events
= require('events'),
4 IrcConnection
= require('./connection.js').IrcConnection
;
6 var State = function (client
, save_state
) {
7 events
.EventEmitter
.call(this);
9 this.save_state
= save_state
|| false;
11 this.irc_connections
= [];
12 this.next_connection
= 0;
14 this.client
.on('disconnect', function () {
15 if (!this.save_state
) {
16 _
.each(this.irc_connections
, function (irc_connection
, i
, cons
) {
18 irc_connection
.end('QUIT :' + (global
.config
.quit_message
|| ''));
19 irc_connection
.dispose();
29 util
.inherits(State
, events
.EventEmitter
);
31 module
.exports
= State
;
33 State
.prototype.connect = function (hostname
, port
, ssl
, nick
, user
, pass
, callback
) {
36 if (global
.config
.restrict_server
) {
37 con
= new IrcConnection(
38 global
.config
.restrict_server
,
39 global
.config
.restrict_server_port
,
40 global
.config
.restrict_server_ssl
,
43 global
.config
.restrict_server_password
,
47 con
= new IrcConnection(
57 con_num
= this.next_connection
++;
58 this.irc_connections
[con_num
] = con
;
59 con
.con_num
= con_num
;
61 new IrcCommands(con
, con_num
, this).bindEvents();
63 con
.on('connected', function () {
64 return callback(null, con_num
);
67 con
.on('error', function (err
) {
68 console
.log('irc_connection error (' + hostname
+ '):', err
);
69 return callback(err
.code
, {server
: con_num
, error
: err
});
72 con
.on('close', function () {
73 that
.irc_connections
[con_num
] = null;
77 State
.prototype.sendIrcCommand = function () {
78 this.client
.sendIrcCommand
.apply(this.client
, arguments
);
81 State
.prototype.sendKiwiCommand = function () {
82 this.client
.sendKiwicommand
.apply(this.client
, arguments
);
85 State
.prototype.dispose = function () {
87 this.removeAllListeners();