-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelayirc.pl
executable file
·311 lines (247 loc) · 7.67 KB
/
relayirc.pl
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#!/usr/bin/perl -w
require Fcntl;
use Net::IRC;
use IO::Socket;
use POSIX;
use strict;
my @efnetlist = ( 'irc.mcs.net', 'irc.lightning.net', 'irc.prison.net',
'irc.colorado.edu', 'irc.best.net', 'irc.plur.net',
'ircd.east.gblx.net', 'ircd.west.gblx.net',
'irc.emory.edu', 'irc.ins.net.uk', 'irc.blackened.com',
'irc.core.com', 'irc.mindspring.com', 'irc.stanford.edu',
'irc.sprynet.com' );
my @cuckoonetlist = ( 'phobia.gildea.com', 'dazed.slacker.com', 'irc.kooks.net',
'irc.followell.net', 'irc.ivo.nu' );
my %config = ( server1addr => \@efnetlist,
server1port => 6667,
server2addr => \@cuckoonetlist,
server2port => 6667,
nick => 'dctirelay',
ircname => "distributed.net channel relayer",
username => 'relayirc',
consoledebug => 1,
servertimeout => 18*60,
bindaddr => 'nodezero.distributed.net',
pidfile => '/var/run/relayirc.pid'
);
my @channels = ( '#distributed', '#dcti', '#dcti-tunes', '#feline', '#ktremain' );
my %passwords = ( '#dcti' => 'itshotinatl',
'#dcti-logs' => 'itshotinatl' );
#
# Update the PID file with our current PID.
#
if ( defined $config{pidfile} ) {
my $oldpid;
if (open(PIDFILE, $config{pidfile})) {
local $/ = undef;
if (<PIDFILE> =~ m/^(\d+)/) {
$oldpid = $1;
}
close(PIDFILE);
}
if ($oldpid && kill(0, $oldpid) > 0) {
print STDERR "Another instance already running. Aborting.\n";
exit 0;
}
if (open(PIDFILE, ">" . $config{pidfile})) {
print PIDFILE $$;
close(PIDFILE);
} else {
print STDERR "Unable to write to pidfile " . $config{pidfile} . "\n"
if $config{consoledebug};
}
}
#
# Establish server connections and begin relaying traffic
#
my $irc = new Net::IRC;
my ($conn1, $conn1ping) = (undef, time());
my ($conn2, $conn2ping) = (undef, time());
for (;;) {
sleep 2;
# Create the IRC Connection objects to the first server.
if (!defined $conn1 || !$conn1->connected()) {
$conn1 = &connectserver( &selectrandom(@{$config{server1addr}}),
$config{server1port} )
or next;
$conn1ping = time();
}
# Create the IRC Connection objects to the second server.
if (!defined $conn2 || !$conn2->connected()) {
$conn2 = &connectserver( &selectrandom(@{$config{server2addr}}),
$config{server2port} )
or next;
$conn2ping = time();
}
# Enter main relaying loop.
print STDERR "Starting main loop...\n"
if $config{consoledebug};
eval {
while (defined $conn1 && $conn1->connected() &&
defined $conn2 && $conn2->connected())
{
$irc->do_one_loop;
if (time() - $conn1ping > $config{servertimeout}) {
warn "primary connection timed out";
$conn1->quit();
undef $conn1;
last;
}
if (time() - $conn2ping > $config{servertimeout}) {
warn "secondary connection timed out";
$conn2->quit();
undef $conn2;
last;
}
}
};
print STDERR "Recycling main loop...\n"
if $config{consoledebug};
}
exit 0;
sub selectrandom
{
my @listitems = @_;
return $listitems[rand scalar(@listitems)];
}
sub connectserver
{
my $oneserver = shift;
my $oneport = shift;
print STDERR "Creating connection to IRC server $oneserver...\n"
if $config{consoledebug};
my $oneconn = $irc->newconn(Server => $oneserver,
Port => $oneport,
Nick => $config{nick},
Ircname => $config{ircname},
Username => $config{username},
LocalAddr => $config{bindaddr});
if (!$oneconn || !$oneconn->connected()) {
warn "Can't connect to IRC server.\n";
return undef;
}
print STDERR "Connected to IRC server.\n"
if $config{consoledebug};
# Install all of the handlers to catch events.
print STDERR "Installing handler routines...\n"
if $config{consoledebug};
$oneconn->add_handler('cping', \&on_ping);
$oneconn->add_handler('cversion', \&on_version);
$oneconn->add_handler('crping', \&on_ping_reply);
$oneconn->add_handler('invite', \&on_invite);
$oneconn->add_handler('ping', \&on_activity_ping_hook, 2);
$oneconn->add_handler('msg', \&on_activity_ping_hook, 2);
#$oneconn->add_handler('public', \&on_activity_ping_hook, 2);
#$oneconn->add_handler('caction', \&on_activity_ping_hook, 2);
$oneconn->add_handler('public', \&on_relay_public_hook, 2);
$oneconn->add_handler('caction', \&on_relay_caction_hook, 2);
$oneconn->add_global_handler(376, \&on_connect);
$oneconn->add_global_handler(433, \&on_nick_taken);
$oneconn->add_global_handler([ 251,252,253,254,302,255 ], \&on_init);
return $oneconn;
}
sub on_init {
my ($self, $event) = @_;
my (@args) = ($event->args);
shift (@args);
print STDERR "*** @args\n"
if $config{consoledebug};
}
# What to do when the bot successfully connects.
sub on_connect {
my $self = shift;
foreach my $chan (@channels) {
$self->join( $chan, $passwords{$chan} );
}
}
# What to do when somebody invites me someplace.
sub on_invite {
my ($self, $event) = @_;
my $channel = ($event->args)[0];
foreach my $chan (@channels) {
if ( $chan eq $channel ) {
$self->join( $chan, $passwords{$chan} );
return;
}
}
my $nick = $event->nick;
print STDERR "*** ignoring invite request for $channel from $nick\n"
if $config{consoledebug};
}
# Yells about incoming CTCP PINGs.
sub on_ping {
my ($self, $event) = @_;
my $nick = $event->nick;
$self->ctcp_reply($nick, 'PING ' . join (' ', ($event->args)));
print STDERR "*** CTCP PING request from $nick received\n"
if $config{consoledebug};
}
# Handles messages and actions
sub on_relay_public_hook {
my ($self, $event) = @_;
my $nick = $event->nick;
my $channel = $event->to;
my $text = join(' ', $event->args);
if ($self eq $conn1) {
$conn1ping = time();
$conn2->privmsg($channel, '<' . $nick . '> ' . $text);
} elsif ($self eq $conn2) {
$conn2ping = time();
$conn1->privmsg($channel, '<' . $nick . '> ' . $text);
} else {
warn "ignoring public relay";
}
}
sub on_relay_caction_hook {
my ($self, $event) = @_;
my $nick = $event->nick;
my $channel = join(' ', $event->to); # BUGBUG: $event->to
my $text = join(' ', $event->args);
#warn "got action for $channel and $nick and $text";
if ($self eq $conn1) {
$conn1ping = time();
$conn2->me($channel, 'indicates that ' . $nick . ' ' . $text);
} elsif ($self eq $conn2) {
$conn2ping = time();
$conn1->me($channel, 'indicates that ' . $nick . ' ' . $text);
} else {
warn "ignoring caction relay";
}
}
# Minimal post-event hook to catch activity and reset timeouts.
sub on_activity_ping_hook {
my ($self, $event) = @_;
if ($self eq $conn1) {
print STDERR "*** Activity for primary server received\n"
if $config{consoledebug};
$conn1ping = time();
} elsif ($self eq $conn2) {
print STDERR "*** Activity for secondary server received\n"
if $config{consoledebug};
$conn2ping = time();
} else {
warn "unknown self reference in activity ping hook";
}
}
# Yells about incoming CTCP PINGs.
sub on_version {
my ($self, $event) = @_;
my $nick = $event->nick;
$self->ctcp_reply($nick, 'Bovine Super Duper IRC Client');
print STDERR "*** CTCP VERSION request from $nick received\n"
if $config{consoledebug};
}
# Gives lag results for outgoing PINGs.
sub on_ping_reply {
my ($self, $event) = @_;
my $args = ($event->args)[1];
my $nick = $event->nick;
$args = time - $args;
print STDERR "*** CTCP PING reply from $nick: $args sec.\n"
if $config{consoledebug};
}
# Change our nick if someone stole it.
sub on_nick_taken {
my ($self) = shift;
$self->nick(substr($self->nick, -1) . substr($self->nick, 0, 8));
}