-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (110 loc) · 3.8 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var libircd = require("libircd.js");
var libhack = require("libhack.js");
var util = require("util");
var fs = require("fs");
var hackHostname = 'wss://hack.chat/chat-ws';
var hackUsername = null; // set by irc client
var ircUserguid = null; // set by irc client
var hackByChannelName = {};
var hackChannelValidator = new RegExp("^[a-z0-9]+$", "i");
var config = JSON.parse(fs.readFileSync("./config.json"));
function FakeIrcHackUser(nick) {
libircd.Client.call(this);
this.nick = nick;
}
util.inherits(FakeIrcHackUser, libircd.Client);
FakeIrcHackUser.prototype.getRemoteAddress = function() {
return 'hack.chat';
}
FakeIrcHackUser.prototype.write = function(data) {
// Nothing will happen here
}
var server = new libircd.IrcServer(config.serverPassword);
server.on('message', function(json) {
var hackClient = hackByChannelName[json.channel];
if (!hackClient) return;
hackClient.sendMessage(json.message);
});
server.on('connect', function(json) {
hackUsername = json.nick;
ircUserguid = json.guid;
});
server.on('roomchange', function(roomJson) {
var hackChannel = roomJson.channel;
if (!hackChannel || (typeof hackChannel) != 'string') return;
hackChannel = hackChannel.substr(1);
if (!hackChannelValidator.test(hackChannel)) return;
var fakeClientsByChannel = {};
function getOrCreateFakeClient(channelName, nick) {
var channel = fakeClientsByChannel[channelName];
if (!channel) fakeClientsByChannel[channelName] = channel = {};
var client = channel[nick];
if (client)
return client;
else
channel[nick] = client = new FakeIrcHackUser(nick);
if (!channel['']) channel[''] = 0;
channel[''] += 1;
server.addClient(client);
server.clientJoinsChannel(client, roomJson.channel);
return client;
}
function removeFakeClient(channelName, nick) {
var channel = fakeClientsByChannel[channelName];
if (!channel) fakeClientsByChannel[channelName] = channel = {};
var client = channel[nick];
if (!client) return;
channel[''] -= 1;
server.kick(nick, channelName);
if (client.channelCount == 0)
server.killClient(client, libircd.KILLREASON.connectionClosed);
delete channel[nick];
}
var hackClient;
if (roomJson.bJoin) {
hackClient = hackByChannelName[roomJson.channel] = new libhack.Client(hackHostname, hackChannel, hackUsername, config.userPassword);
hackClient.on('message', function(json) {
var channel = server.channels[roomJson.channel];
if (!channel) return;
if (json.nick == hackUsername) return;
json.message.split("\n").forEach(function(line) {
channel.deliverMessage(getOrCreateFakeClient(roomJson.channel, json.nick), line);
});
});
hackClient.on('begin', function() {
roomJson.joinFunction();
});
hackClient.on('logged', function(json) {
// automatically connects fake users if they were not present before
if (json.bLogin)
getOrCreateFakeClient(roomJson.channel, json.nick);
else
removeFakeClient(roomJson.channel, json.nick);
});
hackClient.on('userlist', function(json) {
console.log(JSON.stringify(json));
for (var i = 0; i < json.nicks.length; ++i) {
if (json.nicks[i] == hackUsername) continue;
getOrCreateFakeClient(roomJson.channel, json.nicks[i]);
}
});
hackClient.on('end', function() {
server.kick(server.clientsByGuid[ircUserguid], roomJson.channel);
});
hackClient.connect();
} else {
// Cleanup room?
var channel = server.channels[roomJson.channel];
var clients = fakeClientsByChannel[roomJson.channel];
if (!channel || !clients || channel.clientCount - clients[''] == 0) { // empty, not counting fake users
server.removeChannel(channel);
delete fakeClientsByChannel[roomJson.channel];
hackClient = hackByChannelName[roomJson.channel];
if (hackClient) {
hackClient.disconnect();
delete hackByChannelName[roomJson.channel];
}
}
}
});
server.start(config.listenIp, config.listenPort);