rework logic of vacation.pl

This commit is contained in:
mischa 2022-08-18 14:57:25 +00:00
parent 386bf42a35
commit 5180f6dcbc
1 changed files with 26 additions and 17 deletions

View File

@ -46,8 +46,8 @@ select(STDOUT);
$|++;
select($fh);
$|++;
print STDOUT "register|filter|smtp-in|rcpt-to\n";
print STDOUT "register|filter|smtp-in|mail-from\n";
print STDOUT "register|report|smtp-in|tx-mail\n";
print STDOUT "register|report|smtp-in|tx-rcpt\n";
print STDOUT "register|filter|smtp-in|data-line\n";
print STDOUT "register|ready\n";
@ -55,21 +55,30 @@ while (my $line = <>) {
next if ($line =~ m/^config/);
chomp $line;
print $fh "$line\n" if ($opt_v);
if ($line =~ m/filter/) {
my ($stream, $version, $timestamp, $subsystem, $event, $sid, $token, $data) = split /\|/, $line;
if ($line =~ m/mail-from/) {
$from = $data;
print STDOUT "filter-result|$sid|$token|proceed\n";
}
if ($line =~ m/rcpt-to/) {
$email = $data;
if ($line =~ m/^report/) {
print $fh "$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;
print $fh "Virtual Vacation: created session $sid\n";
print STDOUT "filter-result|$sid|$token|proceed\n";
$from = $address;
print $fh "Virtual Vacation: created session $sid\n" if ($opt_d);
if ($from =~ m/(postmaster|noreply|no-reply|bounce)@/i) { $ooo{$sid} = 0; }
} elsif ($event eq "tx-mail" && $code ne "ok") {
$ooo{$sid} = 0;
}
if ($event eq "tx-rcpt" && $code eq "ok") {
$email = $address;
} elsif ($event eq "tx-rcpt" && $code ne "ok") {
delete $ooo{$sid};
print $fh "Virtual Vacation: removed session $sid\n" if ($opt_d);
}
}
if ($line =~ m/^filter/) {
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; }
if ($data =~ m/^list-(help|id|owner|post|subscribe|unsubscribe):.*/i) { $ooo{$sid} = 0; }
if ($data =~ m/^x-loop:\s+opensmtpd\ admin\ virtual\ vacation/i) { $ooo{$sid} = 0; }
print STDOUT "filter-dataline|$sid|$token|$data\n";
}
@ -79,22 +88,22 @@ while (my $line = <>) {
my $sth = do_query($query);
my $rv = $sth->rows;
if ($rv == 1) {
my @row = $sth->fetchrow_array;
print $fh "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 = do_query($query);
$rv = $sth->rows;
if ($rv == 0) {
print $fh "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);
print $fh "Virtual Vacation: Sending OOO to $from\n" if ($opt_d);
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
print $fh_email "From: $email\n";
print $fh_email "To: $from\n";;
print $fh_email "Subject: $row[0]\n";
print $fh_email "Subject: $vacation_msg[0]\n";
print $fh_email "X-Loop: OpenSMTPD Admin Virtual Vacation\n";
print $fh_email "Content-Type: text/plain; charset=utf-8\n\n";
print $fh_email "$row[1]\n";
print $fh_email "$vacation_msg[1]\n";
close $fh_email;
}
delete $ooo{$sid};