Code cleanup and experimental IPv6.

This commit is contained in:
Elijah Perrault committed 2011-02-06 20:42:03 -07:00
1 parent 4c6bacf177
commit 8ba8bf4d43
1 file changed
+70 -50
+70 -50
View File
@@ -39,57 +39,57 @@ use Core::IRC;
our $VERSION = 3.0.0; our $VERSION = 3.0.0;
$0 = "auto"; local $0 = 'auto';
# Check for build files. # Check for build files.
unless (-e "$Bin/../build/os" and -e "$Bin/../build/perl" and -e "$Bin/../build/time" and -e "$Bin/../build/ver") { unless (-e "$Bin/../build/os" and -e "$Bin/../build/perl" and -e "$Bin/../build/time" and -e "$Bin/../build/ver") {
println "Missing build file(s). Please build Auto before running it." and exit; println 'Missing build file(s). Please build Auto before running it.' and exit;
} }
# Check build OS. # Check build OS.
open(my $BFOS, q{<}, "$Bin/../build/os") or println "Cannot start: Broken build." and exit; open(my $BFOS, '<', "$Bin/../build/os") or println 'Cannot start: Broken build.' and exit;
my @BFOS = <$BFOS>; my @BFOS = <$BFOS>;
close $BFOS; close $BFOS;
if ($BFOS[0] ne $^O."\n") { if ($BFOS[0] ne $^O."\n") {
println "Cannot start: Broken build." and exit; println 'Cannot start: Broken build.' and exit;
} }
undef @BFOS; undef @BFOS;
# Check build features. # Check build features.
our $ENFEAT; our $ENFEAT;
open(my $BFFEAT, q{<}, "$Bin/../build/feat") or println "Cannot start: Broken build." and exit; open(my $BFFEAT, '<', "$Bin/../build/feat") or println 'Cannot start: Broken build.' and exit;
my @BFFEAT = <$BFFEAT>; my @BFFEAT = <$BFFEAT>;
close $BFFEAT; close $BFFEAT;
$ENFEAT = substr($BFFEAT[0], 0, length($BFFEAT[0]) - 1); $ENFEAT = substr($BFFEAT[0], 0, length($BFFEAT[0]) - 1);
undef @BFFEAT; undef @BFFEAT;
# Check build Perl version. # Check build Perl version.
open(my $BFPERL, q{<}, "$Bin/../build/perl") or println "Cannot start: Broken build." and exit; open(my $BFPERL, '<', "$Bin/../build/perl") or println 'Cannot start: Broken build.' and exit;
my @BFPERL = <$BFPERL>; my @BFPERL = <$BFPERL>;
close $BFPERL; close $BFPERL;
if ($BFPERL[0] ne $]."\n") { if ($BFPERL[0] ne $]."\n") {
println "Cannot start: Broken build." and exit; println 'Cannot start: Broken build.' and exit;
} }
undef @BFPERL; undef @BFPERL;
# Check build Auto version. # Check build Auto version.
open(my $BFVER, q{<}, "$Bin/../build/ver") or println "Cannot start: Broken build." and exit; open(my $BFVER, '<', "$Bin/../build/ver") or println 'Cannot start: Broken build.' and exit;
my @BFVER = <$BFVER>; my @BFVER = <$BFVER>;
close $BFVER; close $BFVER;
if ($BFVER[0] ne VER.".".SVER.".".REV.RSTAGE."\n") { if ($BFVER[0] ne VER.".".SVER.".".REV.RSTAGE."\n") {
println "Cannot start: Broken build." and exit; println 'Cannot start: Broken build.' and exit;
} }
undef @BFVER; undef @BFVER;
# Set signal handlers. # Set signal handlers.
$SIG{TERM} = \&signal_TERM; local $SIG{TERM} = \&signal_term;
$SIG{INT} = \&signal_INT; local $SIG{INT} = \&signal_int;
$SIG{HUP} = \&signal_HUP; local $SIG{HUP} = \&signal_hup;
$SIG{'__WARN__'} = \&signal_PerlWARN; local $SIG{'__WARN__'} = \&signal_perlwarn;
$SIG{'__DIE__'} = \&signal_PerlDIE; local $SIG{'__DIE__'} = \&signal_perldie;
API::Std::event_add("on_sigterm"); API::Std::event_add('on_sigterm');
API::Std::event_add("on_sigint"); API::Std::event_add('on_sigint');
API::Std::event_add("on_sighup"); API::Std::event_add('on_sighup');
# Print startup message. # Print startup message.
@@ -119,23 +119,23 @@ if (defined $ARGV[0]) {
# Check for updates. # Check for updates.
if (!$NUC and RSTAGE ne 'd') { if (!$NUC and RSTAGE ne 'd') {
println "* Connecting to update server..."; println '* Connecting to update server...';
my $uss = IO::Socket::INET->new( my $uss = IO::Socket::INET->new(
'Proto' => 'tcp', 'Proto' => 'tcp',
'PeerAddr' => 'dist.xelhua.org', 'PeerAddr' => 'dist.xelhua.org',
'PeerPort' => 80, 'PeerPort' => 80,
'Timeout' => 30 'Timeout' => 30
) or err(1, "Cannot connect to update server! Aborting update check."); ) or err(1, 'Cannot connect to update server! Aborting update check.');
send($uss, "GET http://dist.xelhua.org/auto/version.txt\n", 0); send($uss, "GET http://dist.xelhua.org/auto/version.txt\n", 0);
my $dll = ''; my $dll = '';
while (my $data = readline($uss)) { while (my $data = readline($uss)) {
$data =~ s/(\n|\r)//g; $data =~ s/(\n|\r)//g;
my ($v, $c) = split('=', $data); my ($v, $c) = split('=', $data);
if ($v eq "url") { if ($v eq 'url') {
$dll = $c; $dll = $c;
} }
elsif ($v eq "version") { elsif ($v eq 'version') {
if (VER.".".SVER.".".REV.RSTAGE ne $c) { if (VER.".".SVER.".".REV.RSTAGE ne $c) {
println("!!! NOTICE !!! Your copy of Auto is outdated. Current version: ".VER.".".SVER.".".REV.RSTAGE." - Latest version: ".$c); println("!!! NOTICE !!! Your copy of Auto is outdated. Current version: ".VER.".".SVER.".".REV.RSTAGE." - Latest version: ".$c);
println("!!! NOTICE !!! You can get the latest Auto by downloading ".$dll); println("!!! NOTICE !!! You can get the latest Auto by downloading ".$dll);
@@ -147,6 +147,11 @@ if (!$NUC and RSTAGE ne 'd') {
} }
} }
# Include IPv6 if Auto was built for it.
if ($ENFEAT =~ /ipv6/) {
require IO::Socket::INET6;
}
# Parse configuration file. # Parse configuration file.
println "* Parsing configuration file auto.conf..."; println "* Parsing configuration file auto.conf...";
our $CONF = Parser::Config->new("auto.conf") or err(1, "Failed to parse configuration file!", 1); our $CONF = Parser::Config->new("auto.conf") or err(1, "Failed to parse configuration file!", 1);
@@ -288,14 +293,29 @@ $SELECT = IO::Select->new();
my $it = 0; my $it = 0;
# Iterate through each configured server. # Iterate through each configured server.
foreach my $cskey (keys %cservers) { foreach my $cskey (keys %cservers) {
# Create the socket. # Prepare socket data.
$SOCKET{$cskey} = IO::Socket::INET->new( my %conndata = (
Proto => "tcp", Proto => "tcp",
LocalAddr => $cservers{$cskey}{'bind'}[0], LocalAddr => $cservers{$cskey}{'bind'}[0],
PeerAddr => $cservers{$cskey}{'host'}[0], PeerAddr => $cservers{$cskey}{'host'}[0],
PeerPort => $cservers{$cskey}{'port'}[0] PeerPort => $cservers{$cskey}{'port'}[0],
) or err(2, "Failed to connect to server: ".$cskey." [".$cservers{$cskey}{'host'}[0].":".$cservers{$cskey}{'port'}[0]."]", 0) and next; # Or error. Timeout => 20,
# Send PASS if we have one. );
# Set IPv6 data.
my $use6 = 0;
$use6 = $SOCKET{$cskey}{'ipv6'} if defined $SOCKET{$cskey}{'ipv6'}[0];
# Create the socket.
if ($use6) {
$SOCKET{$cskey} = IO::Socket::INET6->new(%conndata) or
err(2, 'Failed to connect to server: '.$cskey.' ['.$cservers{$cskey}{'host'}[0].':'.$cservers{$cskey}{'port'}[0].']', 0) and next; # Or error.
}
else {
$SOCKET{$cskey} = IO::Socket::INET->new(%conndata) or
err(2, 'Failed to connect to server: '.$cskey.' ['.$cservers{$cskey}{'host'}[0].':'.$cservers{$cskey}{'port'}[0].']', 0) and next; # Or error.
}
# Send PASS if we have one.
if (defined $cservers{$cskey}{'pass'}[0]) { if (defined $cservers{$cskey}{'pass'}[0]) {
socksnd($cskey, "PASS :".$cservers{$cskey}{'pass'}[0]) or socksnd($cskey, "PASS :".$cservers{$cskey}{'pass'}[0]) or
err(2, "Failed to connect to server: ".$cskey." [".$cservers{$cskey}{'host'}[0].":".$cservers{$cskey}{'port'}[0]."]", 0) err(2, "Failed to connect to server: ".$cskey." [".$cservers{$cskey}{'host'}[0].":".$cservers{$cskey}{'port'}[0]."]", 0)
@@ -425,62 +445,62 @@ sub mod_load {
## Signal handlers ## Signal handlers
# SIGTERM # SIGTERM
sub signal_TERM sub signal_term
{ {
API::Std::event_run("on_sigterm"); API::Std::event_run('on_sigterm');
API::IRC::quit($_, "Caught SIGTERM") foreach (keys %SOCKET); API::IRC::quit($_, 'Caught SIGTERM') foreach (keys %SOCKET);
DB::flush(); DB::flush();
dbug "!!! Caught SIGTERM; terminating..."; dbug '!!! Caught SIGTERM; terminating...';
alog "!!! Caught SIGTERM; terminating..."; alog '!!! Caught SIGTERM; terminating...';
if (-e "$Bin/auto.pid") { if (-e "$Bin/auto.pid") {
system("rm $Bin/auto.pid"); unlink "$Bin/auto.pid";
} }
sleep 1; sleep 1;
exit; exit;
} }
# SIGINT # SIGINT
sub signal_INT sub signal_int
{ {
API::Std::event_run("on_sigint"); API::Std::event_run('on_sigint');
API::IRC::quit($_, "Caught SIGINT") foreach (keys %SOCKET); API::IRC::quit($_, 'Caught SIGINT') foreach (keys %SOCKET);
DB::flush(); DB::flush();
dbug "!!! Caught SIGINT; terminating..."; dbug '!!! Caught SIGINT; terminating...';
alog "!!! Caught SIGINT; terminating..."; alog '!!! Caught SIGINT; terminating...';
if (-e "$Bin/auto.pid") { if (-e "$Bin/auto.pid") {
system("rm $Bin/auto.pid"); unlink "$Bin/auto.pid";
} }
sleep 1; sleep 1;
exit; exit;
} }
# SIGHUP # SIGHUP
sub signal_HUP sub signal_hup
{ {
API::Std::event_run("on_sighup"); API::Std::event_run('on_sighup');
dbug "!!! Caught SIGHUP but rehash is unavailable; ignoring"; dbug '!!! Caught SIGHUP but rehash is unavailable; ignoring';
alog "!!! Caught SIGHUP but rehash is unavailable; ignoring"; alog '!!! Caught SIGHUP but rehash is unavailable; ignoring';
return 1; return 1;
} }
# __WARN__ # __WARN__
sub signal_PerlWARN sub signal_perlwarn
{ {
alog "Perl Warning: ".$_[0]; alog 'Perl Warning: '.$_[0];
warn $_[0] if $DEBUG; warn $_[0] if $DEBUG;
return 1; return 1;
} }
# __DIE__ # __DIE__
sub signal_PerlDIE sub signal_perldie
{ {
return if $^S; return if $^S;
alog "Perl Fatal: ".$_[0]." -- Terminating program!"; alog 'Perl Fatal: '.$_[0].' -- Terminating program!';
API::IRC::quit($_, "A fatal error occurred!") foreach (keys %SOCKET); API::IRC::quit($_, 'A fatal error occurred!') foreach (keys %SOCKET);
DB::flush(); DB::flush();
if (-e "$Bin/auto.pid") { if (-e "$Bin/auto.pid") {
system("rm $Bin/auto.pid"); unlink "$Bin/auto.pid";
} }
sleep 1; sleep 1;
die "FATAL: ".$_[0]; die 'FATAL: '.$_[0];
} }