This repository has been archived by the owner on Dec 26, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ircbridged
executable file
·122 lines (101 loc) · 3.13 KB
/
ircbridged
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
121
122
#!/usr/local/bin/node
/*
* Bridges UDP to IRC.
*
* Receives JSON-RPC UDP datagrams on port 41234 containing:
*
* {"method": "join": "params": ["#channel aChannelPassword"]}
* {"method": "notice": "params": ["#channel", "Hello World as Notice"]}
* {"method": "message": "params": ["#channel", "Hello World as Message"]}
*/
var irc = require('irc');
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
var argv = require('optimist')
.demand(['server'])
.describe('server', 'IRC server address')
.describe('port', 'IRC server port')
.describe('nick', 'IRC nick')
.describe('realname', 'IRC realname')
.describe('ssl', 'Use SSL')
.describe('listenport', 'UDP port for JSON-RPC')
.default({port: 6667, ssl: false, nick: 'ircbridge', realname: 'IRC Bridge', 'listenport': 41234})
.argv;
var subscriptions = {}
var LIFETIME = 1800 * 1000; // 30 min
var client = new irc.Client(argv.server, argv.nick, {
userName: argv.nick,
realName: argv.realname,
port: argv.port,
secure: argv.ssl,
selfSigned: true,
certExpired: true,
floodProtection: true,
stripColors: true
});
function handle(data, rinfo) {
if (data.method === 'notice') {
client.notice(data.params[0], data.params[1]);
} else if (data.method === 'message') {
client.say(data.params[0], data.params[1]);
} else if (data.method === 'join') {
client.join(data.params[0]);
} else if (data.method === 'part') {
client.part(data.params[0]);
} else if (data.method === 'subscribe') {
var to = data.params[0];
if (!subscriptions[to])
subscriptions[to] = {};
var target = rinfo.address + ':' + rinfo.port;
subscriptions[to][target] = Date.now();
sendto(target, {method: 'subscribed', 'params': [to, LIFETIME]});
}
}
// Client subscriptions {{
function sendto(target, message) {
if (!(message instanceof Buffer))
message = new Buffer(JSON.stringify(message) + '\n');
var parts = target.split(':');
var host = parts[0];
var port = parseInt(parts[1], 10);
server.send(message, 0, message.length, port, host);
}
function broadcast(to, message) {
var subs = subscriptions[to];
if (!subs)
return;
var subCutoff = Date.now() - LIFETIME;
var data = new Buffer(JSON.stringify(message) + '\n');
for (target in subs) {
if (subs[target] < subCutoff) {
delete subs[target];
console.log('- ' + to + ' ' + target);
} else {
sendto(target, data);
}
}
}
// }}
// Errors / disconnects {{
client.addListener('message', function (nick, to, text, message) {
broadcast(to, {method:'message', params:[nick, to, text]});
});
client.on('netError', fail);
function fail(message) {
console.log(message);
console.log('Exiting');
process.exit(0)
}
// }}
server.on('message', function (msg, rinfo) {
var data = null;
try {
data = JSON.parse(msg);
} catch (e) {
console.log(e);
}
if (data)
handle(data, rinfo);
});
server.bind(parseInt(argv.listenport, 10));
// vim: set ft=javascript: