Debian Codegraph @

lib/Qpsmtpd/Transaction.pm

raw
1 package Qpsmtpd::Transaction;
2 use Qpsmtpd;
3 @ISA = qw(Qpsmtpd);
4 use strict;
5 use warnings;
6
7 use Qpsmtpd::Utils;
8 use Qpsmtpd::Constants;
9
10 use IO::File qw(O_RDWR O_CREAT);
11 use Socket qw(inet_aton);
12 use Sys::Hostname;
13 use Time::HiRes qw(gettimeofday);
14
15 sub new { start(@_) }
16
17 sub start {
18 my $proto = shift;
19 my $class = ref($proto) || $proto;
20 my %args = @_;
21
22 my $self = {_rcpt => [], started => time,};
23 bless($self, $class);
24 return $self;
25 }
26
27 sub add_recipient {
28 my ($self, $rcpt) = @_;
29 push @{$self->{_recipients}}, $rcpt if $rcpt;
30 }
31
32 sub remove_recipient {
33 my ($self, $rcpt) = @_;
34 $self->{_recipients} =
35 [grep { $_->address ne $rcpt->address } @{$self->{_recipients} || []}]
36 if $rcpt;
37 }
38
39 sub recipients {
40 my $self = shift;
41 @_ and $self->{_recipients} = [@_];
42 ($self->{_recipients} ? @{$self->{_recipients}} : ());
43 }
44
45 sub sender {
46 my $self = shift;
47 @_ and $self->{_sender} = shift;
48 $self->{_sender};
49 }
50
51 sub header {
52 my $self = shift;
53 @_ and $self->{_header} = shift;
54 $self->{_header};
55 }
56
57 # blocked() will return when we actually can do something useful with it...
58 #sub blocked {
59 # my $self = shift;
60 # carp 'Use of transaction->blocked is deprecated;'
61 # . 'tell [email protected] if you have a reason to use it';
62 # @_ and $self->{_blocked} = shift;
63 # $self->{_blocked};
64 #}
65
66 sub notes {
67 my ($self, $key) = (shift, shift);
68
69 # Check for any additional arguments passed by the caller -- including undef
70 return $self->{_notes}->{$key} unless @_;
71 return $self->{_notes}->{$key} = shift;
72 }
73
74 sub set_body_start {
75 my $self = shift;
76 $self->{_body_start} = $self->body_current_pos;
77 if ($self->{_body_file}) {
78 $self->{_header_size} = $self->{_body_start};
79 }
80 else {
81 $self->{_header_size} = 0;
82 if ($self->{_body_array}) {
83 foreach my $line (@{$self->{_body_array}}) {
84 $self->{_header_size} += length($line);
85 }
86 }
87 }
88 }
89
90 sub body_start {
91 my $self = shift;
92 @_ and die "body_start now read only";
93 $self->{_body_start};
94 }
95
96 sub body_current_pos {
97 my $self = shift;
98 if ($self->{_body_file}) {
99 return tell($self->{_body_file});
100 }
101 return $self->{_body_current_pos} || 0;
102 }
103
104 sub body_filename {
105 my $self = shift;
106 $self->body_spool() unless $self->{_filename};
107 $self->{_body_file}->flush(); # so contents won't be cached
108 return $self->{_filename};
109 }
110
111 sub body_spool {
112 my $self = shift;
113 $self->log(LOGINFO, "spooling message to disk");
114 $self->{_filename} = $self->temp_file();
115 $self->{_body_file} =
116 IO::File->new($self->{_filename}, O_RDWR | O_CREAT, 0600)
117 or die "Could not open file $self->{_filename} - $! "
118 ; # . $self->{_body_file}->error;
119 if ($self->{_body_array}) {
120 foreach my $line (@{$self->{_body_array}}) {
121 $self->{_body_file}->print($line)
122 or die "Cannot print to temp file: $!";
123 }
124 $self->{_body_start} = $self->{_header_size};
125 }
126 else {
127 $self->log(LOGERROR, "no message body");
128 }
129 $self->{_body_array} = undef;
130 }
131
132 sub body_write {
133 my $self = shift;
134 my $data = shift;
135 if ($self->{_body_file}) {
136
137 #warn("body_write to file\n");
138 # go to the end of the file
139 seek($self->{_body_file}, 0, 2)
140 unless $self->{_body_file_writing};
141 $self->{_body_file_writing} = 1;
142 $self->{_body_file}->print(ref $data eq "SCALAR" ? $$data : $data)
143 and $self->{_body_size} +=
144 length(ref $data eq "SCALAR" ? $$data : $data);
145 }
146 else {
147 #warn("body_write to array\n");
148 $self->{_body_array} ||= [];
149 my $ref = ref($data) eq "SCALAR" ? $data : \$data;
150 pos($$ref) = 0;
151 while ($$ref =~ m/\G(.*?\n)/gc) {
152 push @{$self->{_body_array}}, $1;
153 $self->{_body_size} += length($1);
154 ++$self->{_body_current_pos};
155 }
156 if ($$ref =~ m/\G(.+)\z/gc) {
157 push @{$self->{_body_array}}, $1;
158 $self->{_body_size} += length($1);
159 ++$self->{_body_current_pos};
160 }
161 $self->body_spool if ($self->{_body_size} >= $self->size_threshold());
162 }
163 }
164
165 sub body_size { # depreceated, use data_size() instead
166 my $self = shift;
167 $self->log(LOGWARN,
168 "WARNING: body_size() is depreceated, use data_size() instead");
169 $self->{_body_size} || 0;
170 }
171
172 sub data_size {
173 shift->{_body_size} || 0;
174 }
175
176 sub body_length {
177 my $self = shift;
178 $self->{_body_size} or return 0;
179 $self->{_header_size} or return 0;
180 return $self->{_body_size} - $self->{_header_size};
181 }
182
183 sub body_resetpos {
184 my $self = shift;
185
186 if ($self->{_body_file}) {
187 my $start = $self->{_body_start} || 0;
188 seek($self->{_body_file}, $start, 0);
189 $self->{_body_file_writing} = 0;
190 }
191 else {
192 $self->{_body_current_pos} = $self->{_body_start};
193 }
194
195 1;
196 }
197
198 sub body_getline {
199 my $self = shift;
200 if ($self->{_body_file}) {
201 my $start = $self->{_body_start} || 0;
202 seek($self->{_body_file}, $start, 0)
203 if $self->{_body_file_writing};
204 $self->{_body_file_writing} = 0;
205 my $line = $self->{_body_file}->getline;
206 return $line;
207 }
208 else {
209 return unless $self->{_body_array};
210 $self->{_body_current_pos} ||= 0;
211 my $line = $self->{_body_array}->[$self->{_body_current_pos}];
212 $self->{_body_current_pos}++;
213 return $line;
214 }
215 }
216
217 sub body_as_string {
218 my $self = shift;
219 $self->body_resetpos;
220 local $/;
221 my $str = '';
222 while (defined(my $line = $self->body_getline)) {
223 $str .= $line;
224 }
225 return $str;
226 }
227
228 sub body_fh {
229 return shift->{_body_file};
230 }
231
232 sub dup_body_fh {
233 my ($self) = @_;
234 open(my $fh, '<&=', $self->body_fh);
235 return $fh;
236 }
237
238 sub DESTROY {
239 my $self = shift;
240
241 # would we save some disk flushing if we unlinked the file before
242 # closing it?
243
244 $self->log(LOGDEBUG, sprintf("DESTROY called by %s, %s, %s", (caller)));
245
246 if ($self->{_body_file}) {
247 undef $self->{_body_file};
248 }
249
250 if ($self->{_filename} and -e $self->{_filename}) {
251 if (unlink $self->{_filename}) {
252 $self->log(LOGDEBUG, "unlinked ", $self->{_filename});
253 }
254 else {
255 $self->log(LOGERROR, "Could not unlink ",
256 $self->{_filename}, ": $!");
257 }
258 }
259
260 # These may not exist
261 if ($self->{_temp_files}) {
262 $self->log(LOGDEBUG, "Cleaning up temporary transaction files");
263 foreach my $file (@{$self->{_temp_files}}) {
264 next unless -e $file;
265 unlink $file
266 or $self->log(LOGERROR, "Could not unlink temporary file",
267 $file, ": $!");
268 }
269 }
270
271 # Ditto
272 if ($self->{_temp_dirs}) {
273 eval { use File::Path };
274 $self->log(LOGDEBUG, "Cleaning up temporary directories");
275 foreach my $dir (@{$self->{_temp_dirs}}) {
276 rmtree($dir)
277 or $self->log(LOGERROR, "Could not unlink temporary dir",
278 $dir, ": $!");
279 }
280 }
281 }
282
283 1;
284 __END__
285
286 =head1 NAME
287
288 Qpsmtpd::Transaction - single SMTP session transaction data
289
290 =head1 SYNOPSIS
291
292 foreach my $recip ($transaction->recipients) {
293 print "T", $recip->address, "\0";
294 }
295
296 =head1 DESCRIPTION
297
298 Qpsmtpd::Transaction maintains a single SMTP session's data, including
299 the envelope details and the mail header and body.
300
301 The docs below cover using the C<$transaction> object from within plugins
302 rather than constructing a C<Qpsmtpd::Transaction> object, because the
303 latter is done for you by qpsmtpd.
304
305 =head1 API
306
307 =head2 add_recipient($recipient)
308
309 This adds a new recipient (as in RCPT TO) to the envelope of the mail.
310
311 The C<$recipient> is a C<Qpsmtpd::Address> object. See L<Qpsmtpd::Address>
312 for more details.
313
314 =head2 remove_recipient($recipient)
315
316 This removes a recipient (as in RCPT TO) from the envelope of the mail.
317
318 The C<$recipient> is a C<Qpsmtpd::Address> object. See L<Qpsmtpd::Address>
319 for more details.
320
321 =head2 recipients( )
322
323 This returns a list of the current recipients in the envelope.
324
325 Each recipient returned is a C<Qpsmtpd::Address> object.
326
327 This method is also a setter. Pass in a list of recipients to change
328 the recipient list to an entirely new list. Note that the recipients
329 you pass in B<MUST> be C<Qpsmtpd::Address> objects.
330
331 =head2 sender( [ ADDRESS ] )
332
333 Get or set the sender (MAIL FROM) address in the envelope.
334
335 The sender is a C<Qpsmtpd::Address> object.
336
337 =head2 header( [ HEADER ] )
338
339 Get or set the header of the email.
340
341 The header is a <Mail::Header> object, which gives you access to all
342 the individual headers using a simple API. e.g.:
343
344 my $headers = $transaction->header();
345 my $msgid = $headers->get('Message-Id');
346 my $subject = $headers->get('Subject');
347
348 =head2 notes( $key [, $value ] )
349
350 Get or set a note on the transaction. This is a piece of data that you wish
351 to attach to the transaction and read somewhere else. For example you can
352 use this to pass data between plugins.
353
354 Note though that these notes will be lost when a transaction ends, for
355 example on a C<RSET> or after C<DATA> completes, so you might want to
356 use the notes field in the C<Qpsmtpd::Connection> object instead.
357
358 =head2 body_filename ( )
359
360 Returns the temporary filename used to store the message contents; useful for
361 virus scanners so that an additional copy doesn't need to be made.
362
363 Calling C<body_filename()> also forces spooling to disk. A message is not
364 spooled to disk if it's size is smaller than
365 I<$self-E<gt>config("size_threshold")>, default threshold is 0, the sample
366 config file sets this to 10000.
367
368 =head2 body_write( $data )
369
370 Write data to the end of the email.
371
372 C<$data> can be either a plain scalar, or a reference to a scalar.
373
374 =head2 body_size( )
375
376 B<Deprecated>, Use I<data_size()> instead.
377
378 =head2 data_size( )
379
380 Get the current size of the email. Note that this is not the size of the
381 message that will be queued, it is the size of what the client sent after
382 the C<DATA> command. If you need the size that will be queued, use
383
384 my $msg_len = length($transaction->header->as_string)
385 + $transaction->body_length;
386
387 The line above is of course only valid in I<hook_queue( )>, as other plugins
388 may add headers and qpsmtpd will add it's I<Received:> header.
389
390 =head2 body_length( )
391
392 Get the current length of the body of the email. This length includes the
393 empty line between the headers and the body. Until the client has sent
394 some data of the body of the message (i.e. headers are finished and client
395 sent the empty line) this will return 0.
396
397 =head2 body_resetpos( )
398
399 Resets the body filehandle to the start of the file (via C<seek()>).
400
401 Use this function before every time you wish to process the entire
402 body of the email to ensure that some other plugin has not moved the
403 file pointer.
404
405 =head2 body_getline( )
406
407 Returns a single line of data from the body of the email.
408
409 =head2 body_fh( )
410
411 Returns the file handle to the temporary file of the email. This will return
412 undef if the file is not opened (yet). In I<hook_data( )> or later you can
413 force spooling to disk by calling I<$transaction-E<gt>body_filename>.
414
415 =head2 dup_body_fh( )
416
417 Returns a dup()'d file handle to the temporary file of the email. This can be
418 useful if an external module may call close() on the filehandle that is passed
419 to it. This should only be used for reads, as writing to a dup'd filehandle
420 may have unintended consequences.
421
422 =head1 SEE ALSO
423
424 L<Mail::Header>, L<Qpsmtpd::Address>, L<Qpsmtpd::Connection>
425
426 =cut