-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirc-forward.js
executable file
·68 lines (59 loc) · 1.63 KB
/
irc-forward.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
#!/usr/bin/env node
// Requires node >= 1.0
// IRC config options
const NICK = process.env["NICK"];
const HOST = process.env["HOST"];
const PORT = process.env["PORT"] || 6667;
const CHANNEL = process.env["CHANNEL"];
const PASSWORD = process.env["PASSWORD"];
// Port for incoming messages
const LISTEN_PORT = process.env["LISTEN_PORT"];
// irc client
const net = require("net");
const readline = require("readline");
const client = net.connect({
host: HOST,
port: PORT
});
function send(msg, log=true) {
if (log) {
console.log(msg);
}
client.write(msg + '\r\n');
}
send(`NICK ${NICK}`);
send(`USER ${NICK} 0 * :${NICK}`);
if (PASSWORD !== undefined) {
send(`PRIVMSG NickServ :IDENTIFY ${PASSWORD}`, false);
}
send(`JOIN ${CHANNEL}`);
const rl = readline.createInterface({
input: client,
});
rl.on('line', line => {
if (line.startsWith('PING :')) {
send(`PONG ${line.slice(5)}`, false);
} else {
console.log(line);
}
});
client.on('close', () => process.exit(1));
// Listening server
// IRC messages have a max length of 512 bytes
const MAX_LENGTH = 512 - `PRIVMSG ${CHANNEL} : \r\n`.length;
const LENGTH_REGEX = new RegExp(`.{1,${MAX_LENGTH}}`, 'g');
net.createServer(socket => {
const lines = readline.createInterface({
input: socket
});
let first = true;
lines.on('line', line => {
for (const part of line.trim().match(LENGTH_REGEX)) {
if (first)
send(`PRIVMSG ${CHANNEL} :${part}`);
else
send(`PRIVMSG ${CHANNEL} : ${part}`);
first = false;
}
});
}).listen(LISTEN_PORT, 'localhost');