move logging to function, remove gettime()

This commit is contained in:
mischa 2022-08-20 09:41:02 +00:00
parent 4792bd5b3f
commit 5bdc8b4742
1 changed files with 16 additions and 14 deletions

View File

@ -44,8 +44,9 @@ sub doquery {
$sth->execute;
return $sth;
}
sub gettime {
return POSIX::strftime("%h %d %H:%M:%S ", localtime);
sub dolog {
my ($fh, $msg, $opt) = @_;
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: $msg\n") if ($opt);
}
open (my $fh, '>', "/tmp/virtualvacation.log") if ($opt_l || $opt_v || $opt_d);
@ -62,12 +63,12 @@ while (my $line = <>) {
next if ($line =~ m/^config/);
chomp $line;
if ($line =~ m/^report/) {
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_v);
dolog($fh, "$line", $opt_v);
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 (gettime() . "Virtual Vacation: $sid created session\n") if ($opt_l);
dolog($fh, "$sid created session", $opt_l);
if ($from =~ m/^(postmaster|hostmaster|noreply|no-reply|bounce.*)@/i) { $ooo{$sid} = 0; }
} elsif ($event eq "tx-mail" && $code ne "ok") {
$ooo{$sid} = 0;
@ -76,38 +77,38 @@ while (my $line = <>) {
$email = $address;
} elsif ($event eq "tx-rcpt" && $code ne "ok") {
delete $ooo{$sid};
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_l);
dolog($fh, "$sid removed session", $opt_l);
}
}
if ($line =~ m/^filter/) {
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_d);
dolog($fh, "$line", $opt_d);
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 $fh (gettime() . "Virtual Vacation: $sid header found $data\n") if ($opt_l);
dolog($fh, "$sid header found $data", $opt_l);
}
if ($data =~ m/^list-(help|id|owner|post|subscribe|unsubscribe):.*/i) {
$ooo{$sid} = 0;
print $fh (gettime() . "Virtual Vacation: $sid header found $data\n") if ($opt_l);
dolog($fh, "$sid header found $data", $opt_l);
}
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 (gettime() . "Virtual Vacation: $sid to: $email, from: $from\n") if ($opt_l);
dolog($fh, "$sid to: $email, from: $from", $opt_l);
my $query = qq{SELECT subject,body FROM vacation WHERE email='$email' and active=1};
my $sth = doquery($query);
my $rv = $sth->rows;
if ($rv == 1) {
print $fh (gettime() . "Virtual Vacation: $sid found OOO for $email\n") if ($opt_l);
dolog($fh, "$sid found OOO for $email", $opt_l);
my @vacation_msg = $sth->fetchrow_array;
$query = qq{SELECT cache FROM vacation WHERE email='$email' AND FIND_IN_SET('$from',cache)};
$sth = doquery($query);
$rv = $sth->rows;
if ($rv == 0) {
print $fh (gettime() . "Virtual Vacation: $sid sending OOO to $from\n") if ($opt_l);
dolog($fh, "$sid sending OOO to $from", $opt_l);
$query = qq{UPDATE vacation SET cache=CONCAT(cache,',','$from') WHERE email='$email'};
$sth = doquery($query);
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
@ -120,14 +121,15 @@ while (my $line = <>) {
close $fh_email;
}
delete $ooo{$sid};
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_l);
dolog($fh, "$sid removed session", $opt_l);
} else {
delete $ooo{$sid};
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_l);
dolog($fh, "$sid removed session", $opt_l);
}
} elsif ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 0) {
delete $ooo{$sid};
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_l);
dolog($fh, "$sid removed session", $opt_l);
}
}
}