Greetings,

I've hacked a method I needed into Qpsmtpd::Transaction called 
body_front_write(), which writes to the front of the message body.  I drew 
heavily from body_write(), but couldn't find a way to avoid making a copy of 
the body in order to write to the front of it.  I was wondering if anyone knew 
of a cleaner way to do this, or if a something like this would have a chance of 
making it into the core.

Here's the diff, thanks in advance for any advice.

@@ -110,6 +110,43 @@
   $self->{_body_array} = undef;
 }
 
+sub body_front_write {
+   my $self = shift;
+  my $data = shift;
+  if ($self->{_body_file}) {
+    #warn("body_front_write to file\n");
+    
+    # go to the beginning of the file
+    $self->body_resetpos
+      unless $self->{body_file_writing};
+    $self->{_body_file_writing} = 1;
+    
+    my $new_body;
+    if (ref $data eq "SCALAR") {
+        $new_body = $$data . $self->body_as_string;
+    } else {
+        $new_body = $data . $self->body_as_string;
+    }
+    
+    $self->body_resetpos;
+    
+    $self->{_body_file}->print($new_body)
+      and $self->{_body_size} = length ($new_body);
+    $self->{_body_file_writing} = 0;
+  }
+  else {
+    #warn("body_front_write to array\n");
+    $self->{_body_array} ||= [];
+    
+    my $ref = ref($data) eq "SCALAR" ? $data : \$data;
+    my @data_ary = split('', $$ref);
+    unshift @{ $self->{_body_array} }, @data_ary;
+    $self->{_body_size} += scalar(@{ $self->{_body_array} });
+    $self->{_body_current_pos} = ( scalar(@{ $self->{_body_array} }) - 1 );
+    $self->body_spool if ( $self->{_body_size} >= $self->size_threshold() );
+  }
+}   
+

Reply via email to