move timestamp to function, move some if statements to multiline

This commit is contained in:
mischa 2022-08-20 06:13:12 +00:00
parent 07fdd571ca
commit 25591c1875
1 changed files with 24 additions and 15 deletions

View File

@ -35,12 +35,15 @@ my $email = "";
my $from = "";
my %ooo;
my $dbh = DBI->connect("DBI:$db_type:$db_name:$db_host", "$db_user", "$db_pass", {RaiseError => 1});
sub do_query {
sub doquery {
my ($query) = @_;
my $sth = $dbh->prepare($query);
$sth->execute;
return $sth;
}
sub gettime {
return POSIX::strftime("%h %d %H:%M:%S ", localtime);
}
open (my $fh, '>', "/tmp/virtualvacation.log") if ($opt_d);
select(STDOUT);
@ -55,14 +58,13 @@ print STDOUT "register|ready\n";
while (my $line = <>) {
next if ($line =~ m/^config/);
chomp $line;
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: $line\n") if ($opt_v);
if ($line =~ m/^report/) {
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: $line\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_d);
my ($stream, $version, $timestamp, $subsystem, $event, $sid, $token, $code, $address) = split /\|/, $line;
if ($event eq "tx-mail" && $code eq "ok") {
$ooo{$sid} = 1;
$from = $address;
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: created session $sid\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: created session $sid\n") if ($opt_d);
if ($from =~ m/^(postmaster|hostmaster|noreply|no-reply|bounce.*)@/i) { $ooo{$sid} = 0; }
} elsif ($event eq "tx-mail" && $code ne "ok") {
$ooo{$sid} = 0;
@ -71,33 +73,40 @@ while (my $line = <>) {
$email = $address;
} elsif ($event eq "tx-rcpt" && $code ne "ok") {
delete $ooo{$sid};
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: removed session $sid\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: removed session $sid\n") if ($opt_d);
}
}
if ($line =~ m/^filter/) {
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_v);
my ($stream, $version, $timestamp, $subsystem, $event, $sid, $token, $data) = split /\|/, $line;
if ($line =~ m/data-line/) {
if (!$data) { $data = ""; }
if ($data =~ m/^precedence:\s+(bulk|list|junk)/i) { $ooo{$sid} = 0; print "Virtual Vacation: header found $data\n" if ($opt_d); }
if ($data =~ m/^list-(help|id|owner|post|subscribe|unsubscribe):.*/i) { $ooo{$sid} = 0; print "Virtual Vacation: header found $data\n" if ($opt_d); }
if ($data =~ m/^precedence:\s+(bulk|list|junk)/i) {
$ooo{$sid} = 0;
print $fh (gettime() . "Virtual Vacation: header found $data\n") if ($opt_d);
}
if ($data =~ m/^list-(help|id|owner|post|subscribe|unsubscribe):.*/i) {
$ooo{$sid} = 0;
print $fh (gettime() . "Virtual Vacation: header found $data\n") if ($opt_d);
}
if ($data =~ m/^x-loop:\s+opensmtpd\ admin\ virtual\ vacation/i) { $ooo{$sid} = 0; }
print STDOUT "filter-dataline|$sid|$token|$data\n";
}
if ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 1) {
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: To: $email, From: $from\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: To: $email, From: $from\n") if ($opt_d);
my $query = qq{SELECT subject,body FROM vacation WHERE email='$email' and active=1};
my $sth = do_query($query);
my $sth = doquery($query);
my $rv = $sth->rows;
if ($rv == 1) {
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: Found OOO for $email\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: Found OOO for $email\n") if ($opt_d);
my @vacation_msg = $sth->fetchrow_array;
$query = qq{SELECT cache FROM vacation WHERE email='$email' AND FIND_IN_SET('$from',cache)};
$sth = do_query($query);
$sth = doquery($query);
$rv = $sth->rows;
if ($rv == 0) {
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: Sending OOO to $from\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: Sending OOO to $from\n") if ($opt_d);
$query = qq{UPDATE vacation SET cache=CONCAT(cache,',','$from') WHERE email='$email'};
$sth = do_query($query);
$sth = doquery($query);
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
print $fh_email "From: $email\n";
print $fh_email "To: $from\n";;
@ -108,11 +117,11 @@ while (my $line = <>) {
close $fh_email;
}
delete $ooo{$sid};
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: removed session $sid\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: removed session $sid\n") if ($opt_d);
}
} elsif ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 0) {
delete $ooo{$sid};
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: removed session $sid\n") if ($opt_d);
print $fh (gettime() . "Virtual Vacation: removed session $sid\n") if ($opt_d);
}
}
}