-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathigelkott.js
72 lines (58 loc) · 2.15 KB
/
igelkott.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
71
72
var Stream = require('stream'),
Erk = require('erk'),
Net = require('net');
Queue = require('./lib/queue').Queue,
PluginHandler = require('./lib/pluginHandler.js').PluginHandler;
var Igelkott = module.exports = function Igelkott(config) {
Stream.Duplex.call(this, {
objectMode: true
});
if ( ! config.plugins) throw new Error("config.plugins is not defined, igelkott is pointless without any plugins");
this.config = config;
this.parser = new Erk.Parser();
this.composer = new Erk.Composer();
this.queue = new Queue(this);
this.plugin = new PluginHandler(this);
this._read = function _read(size) {};
this._write = function _write(message, enc, callback) {
this.emit(message.command, message);
this.queue.handleQueue(message);
callback();
};
// Time to load some plugins.
for (var plugin in this.config.plugins) {
this.load(plugin, this.config.plugins[plugin]);
}
this.setUpServer(this.config.adapter || new Net.Socket());
this.doConnect(this.config.connect || function() {
this.server.connect(this.config.server.port, this.config.server.host);
});
};
Igelkott.prototype = Object.create(Stream.Duplex.prototype, {
constructor: {
value: Igelkott
}
});
Igelkott.prototype.load = function load(pluginName, config, plugin) {
var config = config || this.config.plugins[pluginName] || {};
var plugin = plugin || require(this.config.pluginPath + 'igelkott-'+pluginName).Plugin;
try {
this.plugin.load(pluginName, config, plugin);
} catch (err)
{
console.log(err);
this.error('No such plugin: '+pluginName);
}
};
Igelkott.prototype.setUpServer = function setUpServer(server) {
this.server = server;
this.server.on('connect', function() {
this.pipe(this.composer).pipe(this.server).pipe(this.parser).pipe(this);
this.emit('connect');
}.bind(this));
};
Igelkott.prototype.doConnect = function doConnect(doConnect) { this.connection = doConnect; };
Igelkott.prototype.log = function log() {};
Igelkott.prototype.error = function error() {};
Igelkott.prototype.connect = function connect() { this.connection(); };
Igelkott.prototype.end = function connect() { this.log('Time to sleep...'); };