$LOCKFILE = $DBFILE =~ s/([^\/]+)\z/.$1.lck/r;
}
-my ($DBH, $IMAP);
-
+my $DBH;
# Clean after us
-sub clean() {
- print STDERR "Cleaning...\n" if $CONFIG{debug};
+sub cleanup() {
+ print STDERR "Cleaning up...\n" if $CONFIG{debug};
unlink $LOCKFILE if defined $LOCKFILE and -f $LOCKFILE;
- undef $_ foreach grep defined, map {$IMAP->{$_}->{client}} keys %$IMAP;
$DBH->disconnect() if defined $DBH;
}
-$SIG{$_} = sub { clean(); die "$!\n"; } foreach qw/INT TERM/;
+$SIG{$_} = sub { cleanup(); print STDERR "$!\n"; exit 1; } foreach qw/INT TERM/;
+$SIG{$_} = sub { cleanup(); print STDERR "$!\n"; exit 0; } foreach qw/HUP/;
#############################################################################
#############################################################################
# Connect to the local and remote IMAP servers
+my $IMAP;
foreach my $name (qw/local remote/) {
my %config = %{$CONF->{$name}};
$config{$_} = $CONFIG{$_} foreach keys %CONFIG;
}
}
# clean state!
- exit 0 if $CONFIG{oneshot} or $CONFIG{check};
+ if ($CONFIG{oneshot} or $CONFIG{check}) {
+ cleanup();
+ exit 0;
+ }
wait_notifications(900);
}
-END { clean (); }
+END { cleanup(); }
# Close handles when the Net::IMAP::Sync object is destroyed.
sub DESTROY($) {
my $self = shift;
- foreach (qw/STDIN STDOUT/) {
- $self->{$_}->close() if defined $self->{$_} and $self->{$_}->opened();
+ if (defined $self->{STDIN} and $self->{STDIN}->opened() and
+ defined $self->{STDOUT} and $self->{STDOUT}->opened()) {
+ $self->logout();
}
$self->{STDERR}->close() if defined $self->{STDERR} and $self->{STDERR}->opened()
and $self->{STDERR} ne \*STDERR;
if (s/\A\* //) {
if (s/\ABYE //) {
+ foreach (qw/STDIN STDOUT/) {
+ $self->{$_}->close() if defined $self->{$_} and $self->{$_}->opened();
+ }
exit 0;
}
elsif (s/\A(?:OK|NO|BAD) //) {