blib/lib/Mail/Postfix/Postdrop.pm

filter: Statementstmt 26.2% 31 / 118 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 40.0% 8 / 20 Podpod 85.7% 6 / 7 totaltotal 23.4% 45 / 192 CC 25 SCAR 27.9
CC
25
Cov
19%
new
42.0
write
30.0
inject
20.0
CRAP 16.3
↑ Summary
1 # $Id: Postdrop.pm 2376 2008-10-27 09:36:02Z makholm $
2 package Mail::Postfix::Postdrop;
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.3';
8  
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 our @EXPORT_OK = qw(inject);
12  
13 1 ▶use Email::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Email::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use File::Temp qw(tempfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use IO::Socket::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 # Can we deduce these?
21 1 our $MAILDROP_QUEUE_DIR = '/var/spool/postfix/maildrop/';
22 1 our $PICKUP_SERVICE_SOCKET = '/var/spool/postfix/public/pickup';
23  
24 1 my %rec_types;
25 1 %rec_types = (
26 REC_TYPE_SIZE => 'C', # first record, created by cleanup
27 REC_TYPE_TIME => 'T', # time stamp, required
28 REC_TYPE_FULL => 'F', # full name, optional
29 REC_TYPE_INSP => 'I', # inspector transport
30 REC_TYPE_FILT => 'L', # loop filter transport
31 REC_TYPE_FROM => 'S', # sender, required
32 REC_TYPE_DONE => 'D', # delivered recipient, optional
33 REC_TYPE_RCPT => 'R', # todo recipient, optional
34 REC_TYPE_ORCP => 'O', # original recipient, optional
35 REC_TYPE_WARN => 'W', # warning message time
36 REC_TYPE_ATTR => 'A', # named attribute for extensions
37  
38 REC_TYPE_MESG => 'M', # start message records
39  
40 REC_TYPE_CONT => 'L', # long data record
41 REC_TYPE_NORM => 'N', # normal data record
42  
43 REC_TYPE_XTRA => 'X', # start extracted records
44  
45 REC_TYPE_RRTO => 'r', # return-receipt, from headers
46 REC_TYPE_ERTO => 'e', # errors-to, from headers
47 REC_TYPE_PRIO => 'P', # priority
48 REC_TYPE_VERP => 'V', # VERP delimiters
49  
50 REC_TYPE_END => 'E', # terminator, required
51  
52 );
53  
54 0 ▶sub inject {
Subroutine inject: not called CC=4 SCAR=30.0
Pod documented
55 0 my $postdrop = __PACKAGE__->new(@_);
56  
57 0 ▶ $postdrop->build or return;
Branch: 1 branch
BranchTrueFalseTotal
$postdrop->build or return 0 0 0
58 0 ▶ $postdrop->write or return;
Branch: 1 branch
BranchTrueFalseTotal
$postdrop->write or return 0 0 0
59 0 ▶ $postdrop->release or return;
Branch: 1 branch
BranchTrueFalseTotal
$postdrop->release or return 0 0 0
60  
61 0 $postdrop->notify;
62  
63 0 return 1;
64 }
65  
66 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
67 0 my ( $class, $message, %overrides ) = @_;
68  
69 # Email::Abstract->new() is just a no-op when called with
70 # an object taht allready is an Email::Abstract. So this is ok:
71 0 $message = Email::Abstract->new($message);
72  
73 0 ▶ $overrides{Attr} ||= { rewrite_context => 'local' };
Condition0%0/2
$overrides{"Attr"} ||= {"rewrite_context", "local"}
l!l
00
MC/DC0%0/1
$overrides{"Attr"} ||= {"rewrite_context", "local"}
$overrides{"Attr"}
Truth table0%0/2
A
A= $overrides{"Attr"}
Aresult
11
00
74 0 ▶ $overrides{Timestamp} = time() unless exists $overrides{Timestamp};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $overrides{"Timestamp"} 0 0 0
75  
76 0 ▶ unless ( exists $overrides{Sender} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $overrides{"Sender"}) 0 0 0
77 0 my $sender = $message->get_header("Sender");
78 0 ▶ $sender ||= $message->get_header("From");
Condition0%0/3
$sender ||= $message->get_header("From")
l!l&&r!l&&!r
000
MC/DC0%0/2
$sender ||= $message->get_header("From")
$sender $message->get_header("From")
Truth table0%0/3
A ||= B
A= $senderB= $message->get_header("From")
ABresult
1X1
011
000
79  
80 0 $overrides{Sender} = ( Email::Address->parse($sender) )[0]->address;
81 }
82  
83 0 ▶ unless ( exists $overrides{Recipients} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $overrides{"Recipients"}) 0 0 0
84 $overrides{Recipients} = [
85 0 map { $_->address }
86 0 map { Email::Address->parse($_) }
87 0 map { $message->get_header($_) } qw(To Cc Bcc)
88 ];
89 }
90  
91 0 return bless {
92 message => $message,
93 args => \%overrides,
94 }, $class;
95 }
96  
97 0 ▶sub build {
Subroutine build: not called CC=4 SCAR=30.0
Pod documented
98 0 my ($self) = @_;
99  
100 0 $self->_build_rec_time();
101 0 $self->_build_attr( %{ $self->{args}->{Attr} } );
102 0 ▶ $self->_build_rec( 'REC_TYPE_FROM', $self->{args}->{Sender} || "" );
Condition0%0/2
$self->{'args'}{'Sender'} || ''
l!l
00
MC/DC0%0/1
$self->{'args'}{'Sender'} || ''
$self->{'args'}{'Sender'}
Truth table0%0/2
A
A= $self->{'args'}{'Sender'}
Aresult
11
00
103 0 for ( @{ $self->{args}->{Recipients} } ) {
104 0 $self->_build_rec( 'REC_TYPE_RCPT', $_ );
105 }
106  
107 # add an empty message length record.
108 # cleanup is supposed to understand that.
109 # see src/pickup/pickup.c
110 0 $self->_build_rec( 'REC_TYPE_MESG', "" );
111  
112 # a received header has already been added in SMTP.pm
113 # so we can just copy the message:
114  
115 0 for ( split( /\r?\n/, $self->{message}->as_string() ) ) {
116 0 $self->_build_msg_line($_);
117 }
118  
119 # finish it.
120 0 $self->_build_rec( 'REC_TYPE_XTRA', "" );
121 0 $self->_build_rec( 'REC_TYPE_END', "" );
122  
123 0 return $self->{content};
124 }
125  
126 # We're a method so it ok
127 0 ▶sub write { ## no critic (ProhibitBuiltinHomonyms)
Subroutine write: not called CC=5 SCAR=34.0
Pod documented
128 0 my $self = shift;
129  
130 0 ▶ my $oldumask = umask oct(177)
Branch: 1 branch
BranchTrueFalseTotal
my $oldumask = umask 0177 or return 0 0 0
131 or return;
132  
133 0 ▶ my ( $fh, $filename ) = tempfile( DIR => $MAILDROP_QUEUE_DIR )
Branch: 1 branch
BranchTrueFalseTotal
() = tempfile("DIR", $Mail::Postfix::Postdrop::MAILDROP_QUEUE_DIR) or return 0 0 0
134 or return;
135  
136 0 $self->{filename} = $filename;
137  
138 print $fh $self->{content}
139 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
print $fh $self->{"content"} or return 0 0 0
140  
141 0 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return 0 0 0
142 or return;
143  
144 0 umask $oldumask;
145  
146 0 return 1;
147 }
148  
149 0 ▶sub release {
Subroutine release: not called CC=2 SCAR=17.9
Pod documented
150 0 my $self = shift;
151  
152 chmod oct(744), $self->{filename}
153 0 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
chmod 0744, $self->{"filename"} or return 0 0 0
154  
155 0 return 1;
156 }
157  
158 0 ▶sub drop {
Subroutine drop: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my $self = shift;
160  
161 0 unlink $self->{filename};
162  
163 0 return 1;
164 }
165  
166 0 ▶sub notify {
Subroutine notify: not called CC=3 SCAR=24.8
Pod documented
167 0 my $fh;
168  
169 0 ▶ open $fh, ">", $PICKUP_SERVICE_SOCKET
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", $Mail::Postfix::Postdrop::PICKUP_SERVICE_SOCKET or return 0 0 0
170 or return;
171  
172 0 ▶ print $fh "W"
Branch: 1 branch
BranchTrueFalseTotal
print $fh "W" or return 0 0 0
173 or return;
174  
175 0 close $fh;
176  
177 0 return 1;
178 }
179  
180 ############################################################
181 #
182 # Auxillary functions for building the queue file
183 #
184 ############################################################
185  
186 0 ▶sub _build_rec {
Subroutine _build_rec: not called CC=3 SCAR=24.8
187 0 my ( $self, $type, @list ) = @_;
188  
189 0 ▶ croak "unknown record type" unless ( $rec_types{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $rec_types{$type} 0 0 0
190 0 $self->{content} .= ( $rec_types{$type} );
191  
192 # the length is a little endian base-128 number where each
193 # byte except the last has the high bit set:
194 0 my $s = "@list";
195 0 my $ln = length($s);
196 0 ▶ while ( $ln >= 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln >= 128) 0 0 0
197 0 my $lnl = $ln & 0x7F;
198 0 $ln >>= 7;
199 0 $self->{content} .= ( chr( $lnl | 0x80 ) );
200 }
201 0 $self->{content} .= ( chr($ln) );
202  
203 0 $self->{content} .= ($s);
204  
205 0 return;
206 }
207  
208 0 ▶sub _build_rec_size {
Subroutine _build_rec_size: not called CC=1 SCAR=6.9
209 0 my ( $self, $content_size, $data_offset, $rcpt_count ) = @_;
210  
211 0 my $s = sprintf( "%15ld %15ld %15ld", $content_size, $data_offset, $rcpt_count );
212 0 $self->_build_rec( 'REC_TYPE_SIZE', $s );
213 0 return;
214 }
215  
216 0 ▶sub _build_rec_time {
Subroutine _build_rec_time: not called CC=3 SCAR=24.8
217 0 my ( $self, $time ) = @_;
218  
219 0 ▶ $time = $self->{args}->{Timestamp} unless ( defined($time) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
220  
221 0 ▶ return unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $time 0 0 0
222  
223 0 my $s = sprintf( "%d", $time );
224 0 $self->_build_rec( 'REC_TYPE_TIME', $s );
225 0 return;
226 }
227  
228 0 ▶sub _build_attr {
Subroutine _build_attr: not called CC=2 SCAR=17.9
229 0 my ( $self, %kv ) = @_;
230 0 for ( keys %kv ) {
231 0 $self->_build_rec( 'REC_TYPE_ATTR', "$_=$kv{$_}" );
232 }
233 0 return;
234 }
235  
236 0 ▶sub _build_msg_line {
Subroutine _build_msg_line: not called CC=2 SCAR=17.9
237 0 my ( $self, $line ) = @_;
238  
239 0 $line =~ s/\r?\n$//s;
240  
241 # split into 1k chunks.
242 0 ▶ while ( length($line) > 1024 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > 1024) 0 0 0
243 0 my $s = substr( $line, 0, 1024 );
244 0 $line = substr( $line, 1024 );
245 0 $self->_build_rec( 'REC_TYPE_CONT', $s );
246 }
247 0 $self->_build_rec( 'REC_TYPE_NORM', $line );
248 0 return;
249 }
250  
251 1 1;
252  
253 __END__
↑ Summary