Hi all,

this patch adds a body_fh() call to Qpsmtpd::Transaction. It returns a
file handle to the temporary file with the message (or undef if the file
is not open yet). 
The first diff is a minimum, i.e. just adds the sub and pod, the second
replaces (nearly) all $self->{_body_file} with $self->body_fh.

If you wonder why this is needed, see my next mail ;-)

        Hanno
--- 0.3x/lib/Qpsmtpd/Transaction.pm     2005-11-20 10:52:18.000000000 +0100
+++ chunk/lib/Qpsmtpd/Transaction.pm    2006-03-27 09:19:04.000000000 +0200
@@ -110,6 +110,10 @@
   $self->{_body_array} = undef;
 }
 
+sub body_fh {
+  $_[0]->{_body_file} || undef;
+}
+
 sub body_write {
   my $self = shift;
   my $data = shift;
@@ -294,6 +298,11 @@
 Returns the temporary filename used to store the message contents; useful for
 virus scanners so that an additional copy doesn't need to be made.
 
+=head2 body_fh ( )
+
+Returns a file handle to the temporary file with the message (or undef if the 
+file is not open yet).
+
 =head2 body_write( $data )
 
 Write data to the end of the email.
--- ../0.3x/lib/Qpsmtpd/Transaction.pm  2005-11-20 10:52:18.000000000 +0100
+++ lib/Qpsmtpd/Transaction.pm  2006-07-23 09:51:07.000000000 +0200
@@ -61,7 +61,7 @@
 sub set_body_start {
     my $self = shift;
     $self->{_body_start} = $self->body_current_pos;
-    if ($self->{_body_file}) {
+    if ($self->body_fh) {
                $self->{_header_size} = $self->{_body_start};
     }
     else {
@@ -82,8 +82,8 @@
 
 sub body_current_pos {
     my $self = shift;
-    if ($self->{_body_file}) {
-        return tell($self->{_body_file});
+    if ($self->body_fh) {
+        return tell($self->body_fh);
     }
     return $self->{_body_current_pos} || 0;
 }
@@ -91,7 +91,7 @@
 sub body_filename {
   my $self = shift;
   $self->body_spool() unless $self->{_filename};
-  $self->{_body_file}->flush(); # so contents won't be cached
+  $self->body_fh->flush(); # so contents won't be cached
   return $self->{_filename};
 }
 
@@ -103,23 +103,27 @@
     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_fh->print($line) or die "Cannot print to temp file: $!";
     }
     $self->{_body_start} = $self->{_header_size};
   }
   $self->{_body_array} = undef;
 }
 
+sub body_fh {
+  $_[0]->{_body_file} || undef;
+}
+
 sub body_write {
   my $self = shift;
   my $data = shift;
-  if ($self->{_body_file}) {
+  if ($self->body_fh) {
     #warn("body_write to file\n");
     # go to the end of the file
-    seek($self->{_body_file},0,2)
+    seek($self->body_fh,0,2)
       unless $self->{_body_file_writing};
     $self->{_body_file_writing} = 1;
-    $self->{_body_file}->print(ref $data eq "SCALAR" ? $$data : $data)
+    $self->body_fh->print(ref $data eq "SCALAR" ? $$data : $data)
       and $self->{_body_size} += length (ref $data eq "SCALAR" ? $$data : 
$data);
   }
   else {
@@ -148,9 +152,9 @@
 sub body_resetpos {
   my $self = shift;
   
-  if ($self->{_body_file}) {
+  if ($self->body_fh) {
     my $start = $self->{_body_start} || 0;
-    seek($self->{_body_file}, $start, 0);
+    seek($self->body_fh, $start, 0);
     $self->{_body_file_writing} = 0;
   }
   else {
@@ -162,12 +166,12 @@
 
 sub body_getline {
   my $self = shift;
-  if ($self->{_body_file}) {
+  if ($self->body_fh) {
     my $start = $self->{_body_start} || 0;
-    seek($self->{_body_file}, $start,0)
+    seek($self->body_fh, $start,0)
       if $self->{_body_file_writing};
     $self->{_body_file_writing} = 0;
-    my $line = $self->{_body_file}->getline;
+    my $line = $self->body_fh->getline;
     return $line;
   }
   else {
@@ -195,7 +199,7 @@
   # would we save some disk flushing if we unlinked the file before
   # closing it?
 
-  undef $self->{_body_file} if $self->{_body_file};
+  undef $self->{_body_file} if $self->body_fh;
   if ($self->{_filename} and -e $self->{_filename}) {
     unlink $self->{_filename} or $self->log(LOGERROR, "Could not unlink ", 
$self->{_filename}, ": $!");
   }
@@ -294,6 +298,11 @@
 Returns the temporary filename used to store the message contents; useful for
 virus scanners so that an additional copy doesn't need to be made.
 
+=head2 body_fh ( )
+
+Returns a file handle to the temporary file with the message (or undef if the 
+file is not open yet).
+
 =head2 body_write( $data )
 
 Write data to the end of the email.

Reply via email to