-
Notifications
You must be signed in to change notification settings - Fork 5
/
irc.js
77 lines (61 loc) · 1.86 KB
/
irc.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
73
74
75
76
var archiver = require("./archive.js"),
irc = require("irc"),
config = require("./config.js");
var servers = config.irc.servers, nick=config.irc.nick;
var chanIndex = {}, i, l, clients = {}, serv, chan;
//--- build an index for fast lookups.
for(serv in servers) {
clients[serv] = connect(serv, nick, function(message) {
archiver.add(message);
});
for(i=0, l=servers[serv].length; i<l; i++) {
chan = servers[serv][i];
chanIndex[chan.toLowerCase()] = serv;
if(chan != '*') clients[serv].addListener('registered', (function(serv, chan) {
return function() {
clients[serv].join('#'+chan);
};
}(serv, chan)));
}
}
function connect(server, nick, callback) {
client = new irc.Client(server, nick, {
userName: 'scrollback',
realName: 'via scrollback.io'
// , debug: true
});
function uh(s) { return s.replace(/^\#*/,'').toLowerCase(); }
function message(type, from, to, text) {
return {
type: type, from: from, to: to, text: text,
time: new Date().getTime()
};
}
client.addListener('error', function(message) {
console.log("Error from " + message.server, message.args);
});
client.addListener('message', function(nick, channel, text) {
callback(message('text', nick, uh(channel), text));
});
client.addListener('join', function(channel, from) {
callback(message('join', from, uh(channel), ''));
});
client.addListener('part', function(channel, from, reason) {
callback(message('part', from, uh(channel), reason));
});
client.addListener('quit', function(from, reason, channels) {
for(i=0, l=channels.length; i<l; i++) {
callback(message('part', from, uh(channels[i]), reason));
}
});
return client;
}
exports.getServer = function(id) {
var idx = id.toLowerCase();
if(!chanIndex[idx]) {
clients[chanIndex['*']].join('#'+id);
chanIndex[idx] = chanIndex['*'];
}
return chanIndex[idx];
};
exports.connect = connect;