From 35ae210acefcacd6cfd5f30aac1f228db1428f60 Mon Sep 17 00:00:00 2001 From: jeroen Date: Mon, 22 Aug 2022 18:19:47 +0200 Subject: [PATCH] Update vacation.pl: rework SQL, ht martijn@, added parsing of the sql config file --- addons/virtual_vacation/vacation.pl | 69 +++++++++++++++++++---------- 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/addons/virtual_vacation/vacation.pl b/addons/virtual_vacation/vacation.pl index a2edc97..eb37755 100755 --- a/addons/virtual_vacation/vacation.pl +++ b/addons/virtual_vacation/vacation.pl @@ -22,28 +22,42 @@ use Getopt::Std; use DBI; use POSIX qw(strftime); +# -c = location of the SQL config file for OpenSMTPD +# -l = logging of virtual vacation parsed report/filter streams and decisions +# -v = verbose (extra flag) logging of report stream +# -d = debug (extra flag) logging of filter stream +getopts('c:lvd'); +our($opt_c, $opt_l, $opt_v, $opt_d); + my $db_type = 'MariaDB'; my $db_host = ''; my $db_user = ''; my $db_pass = ''; my $db_name = ''; -# -l = logging of virtual vacation parsed report/filter streams and decisions -# -v = verbose (extra flag) logging of report stream -# -d = debug (extra flag) logging of filter stream -getopts('lvd'); -our($opt_l, $opt_v, $opt_d); - -my $email = ""; -my $from = ""; -my %ooo; -my $dbh = DBI->connect("DBI:$db_type:$db_name:$db_host", "$db_user", "$db_pass", {RaiseError => 1}); -sub doquery { - my ($query) = @_; - my $sth = $dbh->prepare($query); - $sth->execute; - return $sth; +if ($opt_c && -e $opt_c) { + open (my $fh_config, '<', $opt_c); + while (my $line = <$fh_config>) { + chomp $line; + if ($line =~ /^host\s+(.*)$/) { $db_host = $1; } + if ($line =~ m/^username\s+(.*)$/) { $db_user = $1; } + if ($line =~ m/^password\s+(.*)$/) { $db_pass = $1; } + if ($line =~ m/^database\s+(.*)$/) { $db_name = $1; } + } + close $fh_config; +} else { + print "ERROR: UNABLE TO LOCATE CONFIG FILE!\n"; + exit 1; } + +my %ooo; +my $email = ''; +my $from = ''; +my $dbh = DBI->connect("DBI:$db_type:$db_name:$db_host", "$db_user", "$db_pass", {RaiseError => 1}); +my $selvacation = $dbh->prepare("SELECT subject,body FROM vacation WHERE email=? and active='1'"); +my $selcache = $dbh->prepare("SELECT cache FROM vacation WHERE email=? AND FIND_IN_SET(?, cache)"); +my $upcache = $dbh->prepare("UPDATE vacation SET cache=CONCAT(cache,',',?) WHERE email=?"); + sub dolog { my ($fh, $msg, $opt) = @_; print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: $msg\n") if ($opt); @@ -96,16 +110,24 @@ while (my $line = <>) { } if ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 1) { dolog($fh, "$sid to: $email, from: $from", $opt_l); - my $sth = doquery(qq{SELECT subject,body FROM vacation WHERE email='$email' and active=1}); - my $rv = $sth->rows; - if ($rv == 1) { + $selvacation->bind_param(1, $email); + $selvacation->execute; + + if ($selvacation->rows == 1) { dolog($fh, "$sid found OOO for $email", $opt_l); - my @vacation_msg = $sth->fetchrow_array; - $sth = doquery(qq{SELECT cache FROM vacation WHERE email='$email' AND FIND_IN_SET('$from',cache)}); - $rv = $sth->rows; - if ($rv == 0) { + my @vacation_msg = $selvacation->fetchrow_array; + + $selcache->bind_param(1, $email); + $selcache->bind_param(2, $from); + $selcache->execute; + + if ($selcache->rows == 0) { dolog($fh, "$sid sending OOO to $from", $opt_l); - $sth = doquery(qq{UPDATE vacation SET cache=CONCAT(cache,',','$from') WHERE email='$email'}); + + $upcache->bind_param(1, $from); + $upcache->bind_param(2, $email); + $upcache->execute; + open my $fh_email, "|-", "/usr/sbin/sendmail -t"; print $fh_email "From: $email\n"; print $fh_email "To: $from\n";; @@ -131,4 +153,5 @@ while (my $line = <>) { } } close $fh; +$dbh->disconnect(); 0;