diff --git a/lib/Qpsmtpd.pm b/lib/Qpsmtpd.pm index 058a487..4e86f6d 100644 --- a/lib/Qpsmtpd.pm +++ b/lib/Qpsmtpd.pm @@ -1,6 +1,6 @@ package Qpsmtpd; use strict; -use vars qw($VERSION $Logger $TraceLevel $Spool_dir); +use vars qw($VERSION $Logger $TraceLevel $Spool_dir $Size_threshold); use Sys::Hostname; use Qpsmtpd::Constants; @@ -415,6 +415,15 @@ sub temp_dir { return $dirname; } +sub size_threshold { + my $self = shift; + unless ( defined $Size_threshold ) { + $Size_threshold = $self->config('memory_threshold') || 10_000; + $self->log(LOGNOTICE, "size_threshold set to $Size_threshold"); + } + return $Size_threshold; +} + 1; __END__ diff --git a/lib/Qpsmtpd/TcpServer.pm b/lib/Qpsmtpd/TcpServer.pm index dcac57d..46022d7 100644 --- a/lib/Qpsmtpd/TcpServer.pm +++ b/lib/Qpsmtpd/TcpServer.pm @@ -39,7 +39,7 @@ sub run { my $self = shift; # should be somewhere in Qpsmtpd.pm and not here... - $self->load_plugins; + $self->load_plugins unless $self->{hooks}; my $rc = $self->start_conversation; return if $rc != DONE; diff --git a/lib/Qpsmtpd/Transaction.pm b/lib/Qpsmtpd/Transaction.pm index a6dc3be..7221ecd 100644 --- a/lib/Qpsmtpd/Transaction.pm +++ b/lib/Qpsmtpd/Transaction.pm @@ -15,9 +15,6 @@ sub start { my %args = @_; my $self = { _rcpt => [], started => time }; bless ($self, $class); - my $sz = $self->config('memory_threshold'); - $sz = 10_000 unless defined($sz); - $self->{_size_threshold} = $sz; return $self; } @@ -91,13 +88,27 @@ sub body_current_pos { return $self->{_body_current_pos} || 0; } -# TODO - should we create the file here if we're storing as an array? sub body_filename { my $self = shift; - return unless $self->{_body_file}; + $self->body_spool() unless $self->{_body_file}; return $self->{_filename}; } +sub body_spool { + my $self = shift; + $self->log(LOGWARN, "spooling to disk"); + $self->{_filename} = $self->temp_file(); + $self->{_body_file} = IO::File->new($self->{_filename}, O_RDWR|O_CREAT, 0600) + or die "Could not open file $self->{_filename} - $! "; # . $self->{_body_file}->error; + if ($self->{_body_array}) { + foreach my $line (@{ $self->{_body_array} }) { + $self->{_body_file}->print($line) or die "Cannot print to temp file: $!"; + } + $self->{_body_start} = $self->{_header_size}; + } + $self->{_body_array} = undef; +} + sub body_write { my $self = shift; my $data = shift; @@ -125,19 +136,7 @@ sub body_write { $self->{_body_size} += length($1); ++$self->{_body_current_pos}; } - if ($self->{_body_size} >= $self->{_size_threshold}) { - #warn("spooling to disk\n"); - $self->{_filename} = $self->temp_file(); - $self->{_body_file} = IO::File->new($self->{_filename}, O_RDWR|O_CREAT, 0600) - or die "Could not open file $self->{_filename} - $! "; # . $self->{_body_file}->error; - if ($self->{_body_array}) { - foreach my $line (@{ $self->{_body_array} }) { - $self->{_body_file}->print($line) or die "Cannot print to temp file: $!"; - } - $self->{_body_start} = $self->{_header_size}; - } - $self->{_body_array} = undef; - } + $self->body_spool if ( $self->{_body_size} >= $self->size_threshold() ); } } diff --git a/qpsmtpd-forkserver b/qpsmtpd-forkserver index 64f38d7..2d9d35b 100755 --- a/qpsmtpd-forkserver +++ b/qpsmtpd-forkserver @@ -169,6 +169,10 @@ if ($PID_FILE) { close PID; } +# Populate class cached variables +$qpsmtpd->spool_dir; +$qpsmtpd->size_threshold; + while (1) { REAPER(); my $running = scalar keys %childstatus;