<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Qpsmtpd::Transaction;
use Qpsmtpd;
@ISA = qw(Qpsmtpd);
use strict;
use Qpsmtpd::Utils;
use Qpsmtpd::Constants;

use IO::File qw(O_RDWR O_CREAT);

# For unique filenames. We write to a local tmp dir so we don't need
# to make them unpredictable.
my $transaction_counter = 0; 

sub new { start(@_) }

sub start {
  my $proto = shift;
  my $class = ref($proto) || $proto;
  my %args = @_;
  my $self = { _rcpt =&gt; [], started =&gt; time };
  bless ($self, $class);
}

sub add_recipient {
  my $self = shift;
  @_ and push @{$self-&gt;{_recipients}}, shift;
}

sub recipients {
  my $self = shift;
  @_ and $self-&gt;{_recipients} = [@_];
  ($self-&gt;{_recipients} ? @{$self-&gt;{_recipients}} : ());
}

sub clear_recipients {
  my $self = shift;
  $self-&gt;{_recipients} = [];
}

sub relaying {
  my $self = shift;
  @_ and $self-&gt;{_relaying} = shift;
  $self-&gt;{_relaying};
}

sub sender {
  my $self = shift;
  @_ and $self-&gt;{_sender} = shift;
  $self-&gt;{_sender};
}

sub header {
  my $self = shift;
  @_ and $self-&gt;{_header} = shift;
  $self-&gt;{_header};
}

# blocked() will return when we actually can do something useful with it...
#sub blocked {
#  my $self = shift;
#  carp 'Use of transaction-&gt;blocked is deprecated;'
#       . 'tell ask@develooper.com if you have a reason to use it';
#  @_ and $self-&gt;{_blocked} = shift;
#  $self-&gt;{_blocked};
#}

sub notes {
  my $self = shift;
  my $key  = shift;
  @_ and $self-&gt;{_notes}-&gt;{$key} = shift;
  #warn Data::Dumper-&gt;Dump([\$self-&gt;{_notes}], [qw(notes)]);
  $self-&gt;{_notes}-&gt;{$key};
}

sub body_filename {
  my $self = shift;
  return unless $self-&gt;{_body_file};
  return $self-&gt;{_filename};
}

sub body_write {
  my $self = shift;
  my $data = shift;
  unless ($self-&gt;{_body_file}) {
     my $spool_dir = $self-&gt;config('spool_dir') ? $self-&gt;config('spool_dir') 
                                                : Qpsmtpd::Utils::tildeexp('~/tmp/');

     $spool_dir .= "/" unless ($spool_dir =~ m!/$!);
     
     $spool_dir =~ /^(.+)$/ or die "spool_dir not configured properly";
     $spool_dir = $1;

     if (-e $spool_dir) {
       my $mode = (stat($spool_dir))[2];
       die "Permissions on spool_dir $spool_dir are not 0700" if $mode &amp; 07077;
     }

     -d $spool_dir or mkdir($spool_dir, 0700) or die "Could not create spool_dir $spool_dir: $!";
     $self-&gt;{_filename} = $spool_dir . join(":", time, $$, $transaction_counter++);
     $self-&gt;{_filename} =~ tr!A-Za-z0-9:/_-!!cd;
    $self-&gt;{_body_file} = IO::File-&gt;new($self-&gt;{_filename}, O_RDWR|O_CREAT, 0600)
      or die "Could not open file $self-&gt;{_filename} - $! "; # . $self-&gt;{_body_file}-&gt;error;
  }
  # go to the end of the file
  seek($self-&gt;{_body_file},0,2)
    unless $self-&gt;{_body_file_writing};
  $self-&gt;{_body_file_writing} = 1;
  $self-&gt;{_body_file}-&gt;print(ref $data eq "SCALAR" ? $$data : $data)
    and $self-&gt;{_body_size} += length (ref $data eq "SCALAR" ? $$data : $data); 
}

sub body_size {
  shift-&gt;{_body_size} || 0;
}

sub body_resetpos {
  my $self = shift;
  return unless $self-&gt;{_body_file};
  seek($self-&gt;{_body_file}, 0,0);
  $self-&gt;{_body_file_writing} = 0;
  1;
}

sub body_getline {
  my $self = shift;
  return unless $self-&gt;{_body_file};
  seek($self-&gt;{_body_file}, 0,0)
    if $self-&gt;{_body_file_writing};
  $self-&gt;{_body_file_writing} = 0;
  my $line = $self-&gt;{_body_file}-&gt;getline;
  return $line;
}

sub DESTROY {
  my $self = shift;
  # would we save some disk flushing if we unlinked the file before
  # closing it?

  undef $self-&gt;{_body_file} if $self-&gt;{_body_file};
  if ($self-&gt;{_filename} and -e $self-&gt;{_filename}) {
    unlink $self-&gt;{_filename} or $self-&gt;log(LOGERROR, "Could not unlink ", $self-&gt;{_filename}, ": $!");
  }
}


1;
__END__

=head1 NAME

Qpsmtpd::Transaction - single SMTP session transaction data

=head1 SYNOPSIS

  foreach my $recip ($transaction-&gt;recipients) {
    print "T", $recip-&gt;address, "\0";
  }

=head1 DESCRIPTION

Qpsmtpd::Transaction maintains a single SMTP session's data, including
the envelope details and the mail header and body.

The docs below cover using the C&lt;$transaction&gt; object from within plugins
rather than constructing a C&lt;Qpsmtpd::Transaction&gt; object, because the
latter is done for you by qpsmtpd.

=head1 API

=head2 add_recipient($recipient)

This adds a new recipient (as in RCPT TO) to the envelope of the mail.

The C&lt;$recipient&gt; is a C&lt;Qpsmtpd::Address&gt; object. See L&lt;Qpsmtpd::Address&gt;
for more details.

=head2 recipients()

This returns a list of the current recipients in the envelope.

Each recipient returned is a C&lt;Qpsmtpd::Address&gt; object.

&lt;&lt;&lt;&lt;&lt;&lt;&lt; .working
=head2 clear_recipients()

Clear the list of recipients in the envelope.

=head2 relaying( )
=======
This method is also a setter. Pass in a list of recipients to change
the recipient list to an entirely new list. Note that the recipients
you pass in B&lt;MUST&gt; be C&lt;Qpsmtpd::Address&gt; objects.
&gt;&gt;&gt;&gt;&gt;&gt;&gt; .merge-right.r111

=head2 sender( [ ADDRESS ] )

Get or set the sender (MAIL FROM) address in the envelope.

The sender is a C&lt;Qpsmtpd::Address&gt; object.

=head2 header( [ HEADER ] )

Get or set the header of the email.

The header is a &lt;Mail::Header&gt; object, which gives you access to all
the individual headers using a simple API. e.g.:

  my $headers = $transaction-&gt;header();
  my $msgid = $headers-&gt;get('Message-Id');
  my $subject = $headers-&gt;get('Subject');

=head2 notes( $key [, $value ] )

Get or set a note on the transaction. This is a piece of data that you wish
to attach to the transaction and read somewhere else. For example you can
use this to pass data between plugins.

Note though that these notes will be lost when a transaction ends, for
example on a C&lt;RSET&gt; or after C&lt;DATA&gt; completes, so you might want to
use the notes field in the C&lt;Qpsmtpd::Connection&gt; object instead.

=head2 body_filename ( )

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_write( $data )

Write data to the end of the email.

C&lt;$data&gt; can be either a plain scalar, or a reference to a scalar.

=head2 body_size( )

Get the current size of the email.

=head2 body_resetpos( )

Resets the body filehandle to the start of the file (via C&lt;seek()&gt;).

Use this function before every time you wish to process the entire
body of the email to ensure that some other plugin has not moved the
file pointer.

=head2 body_getline( )

Returns a single line of data from the body of the email.

=head1 SEE ALSO

L&lt;Mail::Header&gt;, L&lt;Qpsmtpd::Address&gt;, L&lt;Qpsmtpd::Connection&gt;

=cut
</pre></body></html>