/
Manager.js
70 lines (57 loc) · 1.9 KB
/
Manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var Connection = require ("./Connection.js");
var IRCManager = function (config, event_manager) {
this.config = config;
this.event_manager = event_manager;
this.connections = [];
};
/* This function takes the server profile from the config and extends
* it with the defaults from the config so that when the defaults
* change or the server profile changes, it will updated as such
*/
IRCManager.prototype.create_profile = function (profile, defaults) {
profile.__proto__ = defaults;
return profile;
};
IRCManager.prototype.connect = function () {
var connection, profile, servers, defaults;
servers = this.config.data.IRC.servers;
defaults = this.config.data.IRC.default;
for (var i = 0, len = servers.length; i < len; i++) {
profile = this.create_profile (servers[i], defaults);
connection = new Connection (profile, this.event_manager);
///* The following section is a massive hack, used temporarily as a testing interface.
connection.on("001", function (message) {
this.raw ("JOIN #oftn");
var rl = require("readline");
var i = rl.createInterface(process.stdin, process.stdout, null);
i.on("line", function(line) {
connection.send ("PRIVMSG #oftn :" + line.trim());
doprmpt();
});
doprmpt();
function doprmpt() {
i.setPrompt ("<"+connection.nickname+"> ", connection.nickname.length + 3);
i.prompt ();
}
});
//*/
console.log ("Connecting to IRC server \"%s\"", connection.name);
connection.connect ();
this.connections.push (connection);
}
};
IRCManager.prototype.disconnect = function(callback) {
var connections, self = this;
// First we clone the connections array to use as a queue
connections = this.connections.slice ();
console.log ("Waiting for servers to close connections...");
(function next () {
var c = connections.shift ();
if (!c) {
callback.call (self);
return;
}
c.quit (next);
}) ();
};
module.exports = IRCManager;