Commit | Line | Data |
---|---|---|
b09157de JA |
1 | var util = require('util'), |
2 | events = require('events'), | |
3 | _ = require('lodash'), | |
3ec786bc JA |
4 | IrcConnection = require('./connection.js').IrcConnection, |
5 | IrcCommands = require('./commands.js'); | |
76ff1063 JA |
6 | |
7 | var State = function (client, save_state) { | |
6b8fa7a6 D |
8 | var that = this; |
9 | ||
76ff1063 JA |
10 | events.EventEmitter.call(this); |
11 | this.client = client; | |
12 | this.save_state = save_state || false; | |
13 | ||
14 | this.irc_connections = []; | |
15 | this.next_connection = 0; | |
16 | ||
5befab98 | 17 | this.client.on('dispose', function () { |
6b8fa7a6 D |
18 | if (!that.save_state) { |
19 | _.each(that.irc_connections, function (irc_connection, i, cons) { | |
76ff1063 JA |
20 | if (irc_connection) { |
21 | irc_connection.end('QUIT :' + (global.config.quit_message || '')); | |
22 | irc_connection.dispose(); | |
8838bdd6 | 23 | global.servers.removeConnection(irc_connection); |
76ff1063 JA |
24 | cons[i] = null; |
25 | } | |
26 | }); | |
27 | ||
6b8fa7a6 | 28 | that.dispose(); |
76ff1063 JA |
29 | } |
30 | }); | |
31 | }; | |
32 | ||
33 | util.inherits(State, events.EventEmitter); | |
34 | ||
35 | module.exports = State; | |
36 | ||
37 | State.prototype.connect = function (hostname, port, ssl, nick, user, pass, callback) { | |
38 | var that = this; | |
39 | var con, con_num; | |
83c25bfa | 40 | |
2087f5d1 JA |
41 | // Check the per-server limit on the number of connections |
42 | if ((global.config.max_server_conns > 0) && | |
43 | (!global.config.restrict_server) && | |
44 | (!(global.config.webirc_pass && global.config.webirc_pass[hostname])) && | |
8ae445ce | 45 | (!(global.config.ip_as_username && _.contains(global.config.ip_as_username, hostname))) && |
2087f5d1 JA |
46 | (global.servers.numOnHost(hostname) >= global.config.max_server_conns)) |
47 | { | |
48 | return callback('Too many connections to host', {host: hostname, limit: global.config.max_server_conns}); | |
76ff1063 | 49 | } |
2087f5d1 | 50 | |
c3204224 PV |
51 | con = new IrcConnection( |
52 | hostname, | |
53 | port, | |
54 | ssl, | |
55 | nick, | |
56 | user, | |
57 | pass, | |
58 | this); | |
3ec786bc | 59 | |
76ff1063 JA |
60 | con_num = this.next_connection++; |
61 | this.irc_connections[con_num] = con; | |
62 | con.con_num = con_num; | |
3ec786bc JA |
63 | |
64 | con.irc_commands = new IrcCommands(con, con_num, this); | |
65 | ||
76ff1063 | 66 | con.on('connected', function () { |
8838bdd6 | 67 | global.servers.addConnection(this); |
76ff1063 JA |
68 | return callback(null, con_num); |
69 | }); | |
3ec786bc | 70 | |
76ff1063 | 71 | con.on('error', function (err) { |
b09157de | 72 | console.log('irc_connection error (' + hostname + '):', err); |
76ff1063 JA |
73 | return callback(err.code, {server: con_num, error: err}); |
74 | }); | |
75 | ||
76 | con.on('close', function () { | |
a36b7eb6 D |
77 | // TODO: Can we get a better reason for the disconnection? Was it planned? |
78 | that.sendIrcCommand('disconnect', {server: con.con_num, reason: 'disconnected'}); | |
79 | ||
76ff1063 | 80 | that.irc_connections[con_num] = null; |
8838bdd6 | 81 | global.servers.removeConnection(this); |
76ff1063 JA |
82 | }); |
83 | }; | |
84 | ||
85 | State.prototype.sendIrcCommand = function () { | |
86 | this.client.sendIrcCommand.apply(this.client, arguments); | |
87 | }; | |
88 | ||
89 | State.prototype.sendKiwiCommand = function () { | |
90 | this.client.sendKiwicommand.apply(this.client, arguments); | |
91 | }; | |
92 | ||
93 | State.prototype.dispose = function () { | |
5befab98 | 94 | this.emit('dispose'); |
76ff1063 JA |
95 | this.removeAllListeners(); |
96 | }; |