2022-08-18 14:01:52 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
#
|
|
|
|
# Copyright (c) 2022 Mischa Peters <mischa @ high5.nl>
|
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software for any
|
|
|
|
# purpose with or without fee is hereby granted, provided that the above
|
|
|
|
# copyright notice and this permission notice appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
#
|
|
|
|
use 5.024;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use autodie;
|
|
|
|
use Getopt::Std;
|
|
|
|
use DBI;
|
2022-08-20 07:59:10 +02:00
|
|
|
use POSIX qw(strftime);
|
2022-08-18 14:01:52 +02:00
|
|
|
|
|
|
|
my $db_type = 'MariaDB';
|
|
|
|
my $db_host = '';
|
|
|
|
my $db_user = '';
|
|
|
|
my $db_pass = '';
|
|
|
|
my $db_name = '';
|
|
|
|
|
|
|
|
getopts('dv');
|
|
|
|
our($opt_d, $opt_v);
|
|
|
|
|
|
|
|
my $email = "";
|
|
|
|
my $from = "";
|
|
|
|
my %ooo;
|
|
|
|
my $dbh = DBI->connect("DBI:$db_type:$db_name:$db_host", "$db_user", "$db_pass", {RaiseError => 1});
|
2022-08-20 08:13:12 +02:00
|
|
|
sub doquery {
|
2022-08-18 14:01:52 +02:00
|
|
|
my ($query) = @_;
|
|
|
|
my $sth = $dbh->prepare($query);
|
|
|
|
$sth->execute;
|
|
|
|
return $sth;
|
|
|
|
}
|
2022-08-20 08:13:12 +02:00
|
|
|
sub gettime {
|
|
|
|
return POSIX::strftime("%h %d %H:%M:%S ", localtime);
|
|
|
|
}
|
2022-08-18 14:01:52 +02:00
|
|
|
|
|
|
|
open (my $fh, '>', "/tmp/virtualvacation.log") if ($opt_d);
|
|
|
|
select(STDOUT);
|
|
|
|
$|++;
|
|
|
|
select($fh);
|
|
|
|
$|++;
|
2022-08-18 16:57:25 +02:00
|
|
|
print STDOUT "register|report|smtp-in|tx-mail\n";
|
|
|
|
print STDOUT "register|report|smtp-in|tx-rcpt\n";
|
2022-08-18 14:01:52 +02:00
|
|
|
print STDOUT "register|filter|smtp-in|data-line\n";
|
|
|
|
print STDOUT "register|ready\n";
|
|
|
|
|
|
|
|
while (my $line = <>) {
|
|
|
|
next if ($line =~ m/^config/);
|
|
|
|
chomp $line;
|
2022-08-18 16:57:25 +02:00
|
|
|
if ($line =~ m/^report/) {
|
2022-08-20 08:13:12 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_d);
|
2022-08-18 16:57:25 +02:00
|
|
|
my ($stream, $version, $timestamp, $subsystem, $event, $sid, $token, $code, $address) = split /\|/, $line;
|
|
|
|
if ($event eq "tx-mail" && $code eq "ok") {
|
2022-08-18 14:01:52 +02:00
|
|
|
$ooo{$sid} = 1;
|
2022-08-18 16:57:25 +02:00
|
|
|
$from = $address;
|
2022-08-20 10:21:51 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid created session\n") if ($opt_d);
|
2022-08-19 14:38:50 +02:00
|
|
|
if ($from =~ m/^(postmaster|hostmaster|noreply|no-reply|bounce.*)@/i) { $ooo{$sid} = 0; }
|
2022-08-18 16:57:25 +02:00
|
|
|
} 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};
|
2022-08-20 10:21:51 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_d);
|
2022-08-18 14:01:52 +02:00
|
|
|
}
|
2022-08-18 16:57:25 +02:00
|
|
|
}
|
|
|
|
if ($line =~ m/^filter/) {
|
2022-08-20 08:13:12 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $line\n") if ($opt_v);
|
2022-08-18 16:57:25 +02:00
|
|
|
my ($stream, $version, $timestamp, $subsystem, $event, $sid, $token, $data) = split /\|/, $line;
|
2022-08-18 14:01:52 +02:00
|
|
|
if ($line =~ m/data-line/) {
|
|
|
|
if (!$data) { $data = ""; }
|
2022-08-20 08:13:12 +02:00
|
|
|
if ($data =~ m/^precedence:\s+(bulk|list|junk)/i) {
|
|
|
|
$ooo{$sid} = 0;
|
2022-08-20 10:18:32 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid header found $data\n") if ($opt_d);
|
2022-08-20 08:13:12 +02:00
|
|
|
}
|
|
|
|
if ($data =~ m/^list-(help|id|owner|post|subscribe|unsubscribe):.*/i) {
|
|
|
|
$ooo{$sid} = 0;
|
2022-08-20 10:18:32 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid header found $data\n") if ($opt_d);
|
2022-08-20 08:13:12 +02:00
|
|
|
}
|
2022-08-18 14:01:52 +02:00
|
|
|
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) {
|
2022-08-20 10:18:32 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid to: $email, from: $from\n") if ($opt_d);
|
2022-08-18 14:01:52 +02:00
|
|
|
my $query = qq{SELECT subject,body FROM vacation WHERE email='$email' and active=1};
|
2022-08-20 08:13:12 +02:00
|
|
|
my $sth = doquery($query);
|
2022-08-18 14:01:52 +02:00
|
|
|
my $rv = $sth->rows;
|
|
|
|
if ($rv == 1) {
|
2022-08-20 10:18:32 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid found OOO for $email\n") if ($opt_d);
|
2022-08-18 16:57:25 +02:00
|
|
|
my @vacation_msg = $sth->fetchrow_array;
|
2022-08-18 14:01:52 +02:00
|
|
|
$query = qq{SELECT cache FROM vacation WHERE email='$email' AND FIND_IN_SET('$from',cache)};
|
2022-08-20 08:13:12 +02:00
|
|
|
$sth = doquery($query);
|
2022-08-18 14:01:52 +02:00
|
|
|
$rv = $sth->rows;
|
|
|
|
if ($rv == 0) {
|
2022-08-20 10:18:32 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid sending OOO to $from\n") if ($opt_d);
|
2022-08-18 14:01:52 +02:00
|
|
|
$query = qq{UPDATE vacation SET cache=CONCAT(cache,',','$from') WHERE email='$email'};
|
2022-08-20 08:13:12 +02:00
|
|
|
$sth = doquery($query);
|
2022-08-18 14:01:52 +02:00
|
|
|
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
|
|
|
|
print $fh_email "From: $email\n";
|
|
|
|
print $fh_email "To: $from\n";;
|
2022-08-18 16:57:25 +02:00
|
|
|
print $fh_email "Subject: $vacation_msg[0]\n";
|
2022-08-18 14:01:52 +02:00
|
|
|
print $fh_email "X-Loop: OpenSMTPD Admin Virtual Vacation\n";
|
|
|
|
print $fh_email "Content-Type: text/plain; charset=utf-8\n\n";
|
2022-08-18 16:57:25 +02:00
|
|
|
print $fh_email "$vacation_msg[1]\n";
|
2022-08-18 14:01:52 +02:00
|
|
|
close $fh_email;
|
|
|
|
}
|
|
|
|
delete $ooo{$sid};
|
2022-08-20 10:21:51 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_d);
|
2022-08-18 14:01:52 +02:00
|
|
|
}
|
|
|
|
} elsif ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 0) {
|
|
|
|
delete $ooo{$sid};
|
2022-08-20 10:21:51 +02:00
|
|
|
print $fh (gettime() . "Virtual Vacation: $sid removed session\n") if ($opt_d);
|
2022-08-18 14:01:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close $fh;
|
|
|
|
0;
|