rework SQL, ht martijn@, added parsing of the sql config file
This commit is contained in:
parent
1bbcee1fdf
commit
9494dd352a
|
@ -22,28 +22,42 @@ use Getopt::Std;
|
||||||
use DBI;
|
use DBI;
|
||||||
use POSIX qw(strftime);
|
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_type = 'MariaDB';
|
||||||
my $db_host = '';
|
my $db_host = '';
|
||||||
my $db_user = '';
|
my $db_user = '';
|
||||||
my $db_pass = '';
|
my $db_pass = '';
|
||||||
my $db_name = '';
|
my $db_name = '';
|
||||||
|
|
||||||
# -l = logging of virtual vacation parsed report/filter streams and decisions
|
if ($opt_c && -e $opt_c) {
|
||||||
# -v = verbose (extra flag) logging of report stream
|
open (my $fh_config, '<', $opt_c);
|
||||||
# -d = debug (extra flag) logging of filter stream
|
while (my $line = <$fh_config>) {
|
||||||
getopts('lvd');
|
chomp $line;
|
||||||
our($opt_l, $opt_v, $opt_d);
|
if ($line =~ /^host\s+(.*)$/) { $db_host = $1; }
|
||||||
|
if ($line =~ m/^username\s+(.*)$/) { $db_user = $1; }
|
||||||
my $email = "";
|
if ($line =~ m/^password\s+(.*)$/) { $db_pass = $1; }
|
||||||
my $from = "";
|
if ($line =~ m/^database\s+(.*)$/) { $db_name = $1; }
|
||||||
my %ooo;
|
}
|
||||||
my $dbh = DBI->connect("DBI:$db_type:$db_name:$db_host", "$db_user", "$db_pass", {RaiseError => 1});
|
close $fh_config;
|
||||||
sub doquery {
|
} else {
|
||||||
my ($query) = @_;
|
print "ERROR: UNABLE TO LOCATE CONFIG FILE!\n";
|
||||||
my $sth = $dbh->prepare($query);
|
exit 1;
|
||||||
$sth->execute;
|
|
||||||
return $sth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
sub dolog {
|
||||||
my ($fh, $msg, $opt) = @_;
|
my ($fh, $msg, $opt) = @_;
|
||||||
print $fh (POSIX::strftime("%h %d %H:%M:%S ", localtime) . "Virtual Vacation: $msg\n") if ($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) {
|
if ($line =~ m/data-line/ && $data eq '.' && $ooo{$sid} == 1) {
|
||||||
dolog($fh, "$sid to: $email, from: $from", $opt_l);
|
dolog($fh, "$sid to: $email, from: $from", $opt_l);
|
||||||
my $sth = doquery(qq{SELECT subject,body FROM vacation WHERE email=} . $dbh->quote($email) . qq{ and active=1});
|
$selvacation->bind_param(1, $email);
|
||||||
my $rv = $sth->rows;
|
$selvacation->execute;
|
||||||
if ($rv == 1) {
|
|
||||||
|
if ($selvacation->rows == 1) {
|
||||||
dolog($fh, "$sid found OOO for $email", $opt_l);
|
dolog($fh, "$sid found OOO for $email", $opt_l);
|
||||||
my @vacation_msg = $sth->fetchrow_array;
|
my @vacation_msg = $selvacation->fetchrow_array;
|
||||||
$sth = doquery(qq{SELECT cache FROM vacation WHERE email=} . $dbh->quote($email) . qq{AND FIND_IN_SET(} . $dbh->quote($from) . qq{,cache)});
|
|
||||||
$rv = $sth->rows;
|
$selcache->bind_param(1, $email);
|
||||||
if ($rv == 0) {
|
$selcache->bind_param(2, $from);
|
||||||
|
$selcache->execute;
|
||||||
|
|
||||||
|
if ($selcache->rows == 0) {
|
||||||
dolog($fh, "$sid sending OOO to $from", $opt_l);
|
dolog($fh, "$sid sending OOO to $from", $opt_l);
|
||||||
$sth = doquery(qq{UPDATE vacation SET cache=CONCAT(cache,',',} . $dbh->quote($from) . qq{) WHERE email=} . $dbh->quote($email));
|
|
||||||
|
$upcache->bind_param(1, $from);
|
||||||
|
$upcache->bind_param(2, $email);
|
||||||
|
$upcache->execute;
|
||||||
|
|
||||||
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
|
open my $fh_email, "|-", "/usr/sbin/sendmail -t";
|
||||||
print $fh_email "From: $email\n";
|
print $fh_email "From: $email\n";
|
||||||
print $fh_email "To: $from\n";;
|
print $fh_email "To: $from\n";;
|
||||||
|
|
Loading…
Reference in New Issue