MtPolicyd/Request.pm

filter: Statementstmt 50.0% 30 / 60 Branchbran 20.0% 4 / 20 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 75.0% 6 / 8 Podpod 100% 4 / 4 totaltotal 44.8% 48 / 107 CC 14 SCAR 34.0
CC
14
Cov
42%
new_from_fh
72.0
do_cached
7.5
is_already_done
4.1
CRAP 29.9
« MtPolicyd/Plugin/Greylist.pm ↑ Summary MtPolicyd/Client/Response.pm »
1 package Mail::MtPolicyd::Request;
2  
3 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Mail::MtPolicyd::Plugin::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '1.12'; # VERSION
9 # ABSTRACT: the request object
10  
11  
12 13 has 'attributes' => (
13 is => 'ro', isa => 'HashRef', required => 1,
14 traits => [ 'Hash' ],
15 handles => { 'attr' => 'get' },
16 );
17  
18  
19 # gets attached later
20 13 has 'session' => ( is => 'rw', isa => 'Maybe[HashRef]' );
21  
22  
23 13 has 'server' => (
24 is => 'ro', isa => 'Net::Server', required => 1,
25 handles => {
26 'log' => 'log',
27 }
28 );
29  
30  
31 has 'type' => (
32 is => 'ro',
33 isa => 'Str',
34 lazy => 1,
35 default => sub {
36 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
37 1 return( $self->attr('request') );
38 }
39 13 );
40  
41  
42 13 has 'use_caching' => ( is => 'rw', isa => 'Bool', default => 1 );
43  
44  
45 sub dump_attr {
46 0 ▶ my $self = shift;
Subroutine dump_attr: not called CC=1 SCAR=6.9
Pod documented
47 0 my $attr = $self->attributes;
48 0 return( join(', ', map { $_.'='.$attr->{$_} } keys %$attr ) );
49 }
50  
51  
52 sub new_from_fh {
53 0 ▶ my ( $class, $fh ) = ( shift, shift );
Subroutine new_from_fh: not called CC=8 SCAR=42.8
Pod documented
54 0 my $attr = {};
55 0 my $complete = 0;
56 0 my $line;
57 0 ▶ while( defined( $line = $fh->getline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $fh->getline)) 0 0 0
58 0 $line =~ s/\r?\n$//;
59 0 ▶ if( $line eq '') { $complete = 1 ; last; }
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
60 0 my ( $name, $value ) = split('=', $line);
61 0 ▶ if( ! defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
62 0 die('error parsing request');
63 }
64 0 $attr->{$name} = $value;
65 }
66 0 ▶ if( $fh->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->error) 0 0 0
67 0 die('while reading request: '.$fh->error);
68 }
69 0 ▶ if( ! defined $line && ! $complete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line or $complete) 0 0 0
Condition0%0/3
defined $line or $complete
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $line or $complete
defined $line $complete
Truth table0%0/3
A or B
A= defined $lineB= $complete
ABresult
1X1
011
000
70 0 die('connection closed by peer');
71 }
72 0 ▶ if( ! $complete ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($complete) 0 0 0
73 0 die('could not parse request');
74 }
75 0 my $obj = $class->new(
76 'attributes' => $attr,
77 @_
78 );
79 0 return $obj;
80 }
81  
82  
83 sub do_cached {
84 8 ▶ my ( $self, $key, $call ) = @_;
Subroutine do_cached: called CC=4 SCAR=20.1
Pod documented
85 8 my $session = $self->session;
86  
87 # we cant cache a result without session
88 8 ▶ if( ! defined $session || ! $self->use_caching ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session and $self->use_caching) 8 0 8
Condition33%1/3
defined $session and $self->use_caching
!ll&&!rl&&r
080
MC/DC0%0/2
defined $session and $self->use_caching
defined $session $self->use_caching
Truth table33%1/3
A and B
A= defined $sessionB= $self->use_caching
ABresult
0X0
100
111
89 8 return( $call->() );
90 }
91 0 ▶ if( ! defined $session->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session->{$key}) 0 0 0
92 0 $session->{$key} = [ $call->() ];
93 }
94 0 return( @{$session->{$key}} );
95 }
96  
97  
98 sub is_already_done {
99 3 ▶ my ( $self, $key ) = @_;
Subroutine is_already_done: called CC=4 SCAR=14.2
Pod documented
100 3 my $session = $self->session;
101  
102 # we cant cache a result without session
103 3 ▶ if( ! defined $session || ! $self->use_caching ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $session and $self->use_caching) 2 1 3
Condition66%2/3
defined $session and $self->use_caching
!ll&&!rl&&r
021
MC/DC50%1/2
defined $session and $self->use_caching
defined $session $self->use_caching
Truth table66%2/3
A and B
A= defined $sessionB= $self->use_caching
ABresult
0X0
100
111
104 2 return 0;
105 }
106 1 ▶ if( defined $session->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $session->{$key}) 0 1 1
107 0 return(1);
108 }
109 1 $session->{$key} = 1;
110 1 return 0;
111 }
112  
113 13 __PACKAGE__->meta->make_immutable;
114  
115 13 1;
116  
117  
118 __END__
119 =pod
120  
121 =head1 NAME
122  
123 Mail::MtPolicyd::Request - the request object
124  
125 =head1 VERSION
126  
127 version 1.12
128  
129 =head1 ATTRIBUTES
130  
131 =head2 attributes
132  
133 Contains an HashRef with all attributes of the request.
134  
135 To retrieve a single attribute the attr method could be used:
136  
137 $obj->attr('sender');
138  
139 =head2 session
140  
141 Contains a HashRef with all values stored in the session.
142  
143 mtpolicyd will persist the content of this HashRef across requests with the same instance_id.
144  
145 =head2 server
146  
147 Contains the Net::Server object of mtpolicyd.
148  
149 =head2 type
150  
151 The type of the request. Postfix will always use 'smtpd_access_policy'.
152  
153 =head2 use_caching
154  
155 Could be used to disable caching. Only used within the unit tests.
156  
157 =head1 METHODS
158  
159 =head2 dump_attr
160  
161 Returns an string to dump the content of a request.
162  
163 =head2 new_from_fh($fh)
164  
165 An object constructor for creating an request object with the content read
166 for the supplied filehandle $fh.
167  
168 =head2 do_cached( $key, $sub )
169  
170 This method will execute the function reference give in $sub and store
171 the return values in $key within the session.
172 If there is already a cached result stored within $key of the session
173 it will return the content instead of calling the reference again.
174  
175 Returns an Array with the return values of the function call.
176  
177 Example:
178  
179 my ( $ip_result, $info ) = $r->do_cached('rbl-'.$self->name.'-result',
180 sub { $self->_rbl->check( $ip ) } );
181  
182 =head2 is_already_done( $key )
183  
184 This function will raise an flag with name of $key within the session and return true if the
185 flag is already set. False otherwise.
186  
187 This could be used to prevent scores or headers from being applied a second time.
188  
189 Example:
190  
191 if( defined $self->score && ! $r->is_already_done('rbl-'.$self->name.'-score') ) {
192 $self->add_score($r, $self->name => $self->score);
193 }
194  
195 =head1 AUTHOR
196  
197 Markus Benning <ich@markusbenning.de>
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
202  
203 This is free software, licensed under:
204  
205 The GNU General Public License, Version 2, June 1991
206  
207 =cut
208  
« MtPolicyd/Plugin/Greylist.pm ↑ Summary MtPolicyd/Client/Response.pm »