ARF/Report.pm

filter: Statementstmt 100% 76 / 76 Branchbran 76.9% 20 / 26 Conditioncond 53.3% 8 / 15 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 20 / 20 Podpod 100% 10 / 10 totaltotal 87.1% 135 / 155 CC 20 SCAR 17.8
CC
20
Cov
89%
create
10.5
new
10.4
_email_from_body
2.0
CRAP 5.9
↑ Summary ARF.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Email::ARF::Report 0.011;
4 # ABSTRACT: interpret Abuse Reporting Format (ARF) messages
5  
6 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Email::MIME 1.929 (); # content-type attributes
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Email::MIME::ContentType 1.016 (); # type/subtype
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Params::Util qw(_INSTANCE);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 #pod =begin :prelude
13 #pod
14 #pod =head1 WARNING
15 #pod
16 #pod B<Achtung!> This is a prototype. This module will definitely continue to
17 #pod exist, but maybe the interface will change radically once more people have seen
18 #pod it and tried to use it. Don't rely on its interface to keep you employed, just
19 #pod yet.
20 #pod
21 #pod =end :prelude
22 #pod
23 #pod =head1 SYNOPSIS
24 #pod
25 #pod my $report = Email::ARF::Report->new($text);
26 #pod
27 #pod if ($report->field('source-ip') eq $our_ip) {
28 #pod my $sender = $report->original_email->header('from');
29 #pod
30 #pod UserManagement->disable_account($sender);
31 #pod }
32 #pod
33 #pod =head1 DESCRIPTION
34 #pod
35 #pod ARF, the Abuse Feedback Report Format, is used to report email abuse incidents
36 #pod to an email provider. It includes mechanisms for providing machine-readable
37 #pod details about the incident, a human-readable description, and a copy of the
38 #pod offending message.
39 #pod
40 #pod =method new
41 #pod
42 #pod my $report = Email::ARF::Report->new($message);
43 #pod
44 #pod Given either an Email::MIME object or a string containing the text of an email
45 #pod message, this method returns a new Email::ARF::Report object. If the given
46 #pod message source is not a valid report in ARF format, an exception is raised.
47 #pod
48 #pod =cut
49  
50 sub new {
51 16 ▶ my ($class, $source) = @_;
Subroutine new: called CC=10 SCAR=23.4
Pod documented
52  
53 16 ▶ Carp::croak "no report source provided" unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 1 15 16
54  
55 15 ▶ my $mime = Scalar::Util::blessed $source
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source) ? : 7 8 15
56 ? $source
57 : Email::MIME->new($source);
58  
59 Carp::croak "ARF report source could not be interpreted as MIME message"
60 15 ▶ unless eval { $mime->isa('Email::MIME') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $mime->isa("Email::MIME") } } 1 14 15
61  
62 14 my $ct_header = $mime->content_type;
63 14 my $ct = Email::MIME::ContentType::parse_content_type($ct_header);
64  
65 Carp::croak "non-ARF content type '$ct_header' on ARF report source"
66 unless $ct->{type} eq 'multipart'
67 and $ct->{subtype} eq 'report'
68 14 ▶ and $ct->{attributes}{'report-type'} eq 'feedback-report';
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->{"type"} eq "multipart" and $ct->{"subtype"} eq "report" and $ct->{"attributes"}{"report-type"} eq "feedback-report" 0 13 13
Condition66%4/6
$ct->{"type"} eq "multipart" and $ct->{"subtype"} eq "report" and $ct->{"attributes"}{"report-type"} eq "feedback-report"
!ll&&!rl&&r
1013
$ct->{"type"} eq "multipart" and $ct->{"subtype"} eq "report"
!ll&&!rl&&r
1013
MC/DC33%1/3
$ct->{"type"} eq "multipart" and $ct->{"subtype"} eq "report" and $ct->{"attributes"}{"report-type"} eq "feedback-report"
$ct->{"type"} eq "multipart" $ct->{"subtype"} eq "report" $ct->{"attributes"}{"report-type"} eq "feedback-report"
Truth table50%2/4
A and B and C
A= $ct->{"type"} eq "multipart"B= $ct->{"subtype"} eq "report"C= $ct->{"attributes"}{"report-type"} eq "feedback-report"
ABCresult
0XX0
10X0
1100
1111
69  
70 13 ▶ Carp::croak "too few subparts for ARF report" unless $mime->subparts >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $mime->subparts >= 3 1 12 13
71  
72 12 my ($description_part, $report_part, $original_part) = $mime->subparts;
73  
74 12 my $report_header = $report_part->content_type;
75 12 my $report_ct = Email::MIME::ContentType::parse_content_type($report_header);
76 Carp::croak "bad content type '$report_header' for machine-readable section"
77 unless $report_ct->{type} eq 'message'
78 12 ▶ and $report_ct->{subtype} eq 'feedback-report';
Branch: 1 branch
BranchTrueFalseTotal
unless $report_ct->{"type"} eq "message" and $report_ct->{"subtype"} eq "feedback-report" 0 12 12
Condition33%1/3
$report_ct->{"type"} eq "message" and $report_ct->{"subtype"} eq "feedback-report"
!ll&&!rl&&r
0012
MC/DC0%0/2
$report_ct->{"type"} eq "message" and $report_ct->{"subtype"} eq "feedback-report"
$report_ct->{"type"} eq "message" $report_ct->{"subtype"} eq "feedback-report"
Truth table33%1/3
A and B
A= $report_ct->{"type"} eq "message"B= $report_ct->{"subtype"} eq "feedback-report"
ABresult
0X0
100
111
79  
80 12 my $self = bless {
81 mime => $mime,
82 description_part => $description_part,
83 original_part => $original_part,
84 } => $class;
85  
86 12 $self->{fields} = $self->_email_from_body($report_part, 1)->header_obj;
87 12 $self->{original_email} = $self->_email_from_body($original_part);
88  
89 12 return $self;
90 }
91  
92 sub _email_from_body {
93 24 ▶ my ($self, $src_email, $append_nl) = @_;
Subroutine _email_from_body: called CC=2 SCAR=6.9
94  
95 24 my $src_email_body = $src_email->body;
96  
97 24 $src_email_body =~ s/\A(\x0d|\x0a)+//g;
98  
99 24 ▶ my $email = Email::MIME->new(
Branch: 1 branch
BranchTrueFalseTotal
$append_nl ? : 12 12 24
100 $append_nl ? "$src_email_body\n" : $src_email_body
101 );
102 }
103  
104 #pod =method create
105 #pod
106 #pod my $mail = Email::ARF::Report->create(
107 #pod original_email => $email,
108 #pod description => $description,
109 #pod fields => \%fields, # or \@fields
110 #pod header_str => \@headers,
111 #pod );
112 #pod
113 #pod This method creates a new ARF report from scratch.
114 #pod
115 #pod The C<original_email> parameter may be given as a string, a string reference,
116 #pod or as an object that provides an C<as_string> method.
117 #pod
118 #pod The optional C<header_str> parameter is an arrayref of name/value pairs to be
119 #pod added as extra headers in the ARF report. The values are expected to be
120 #pod character strings, and will be MIME-encoded as needed. To pass pre-encoded
121 #pod headers, use the C<header> parameter. These are handled by L<Email::MIME>'s
122 #pod C<create> constructor.
123 #pod
124 #pod Default values are provided for the following fields:
125 #pod
126 #pod version - 1
127 #pod user-agent - Email::ARF::Report/$VERSION
128 #pod feedback-type - other
129 #pod
130 #pod =cut
131  
132 sub create {
133 3 ▶ my ($class, %arg) = @_;
Subroutine create: called CC=10 SCAR=23.5
Pod documented
134  
135 3 require Email::MIME::Creator;
136  
137 my $description_part = Email::MIME->create(
138 attributes => { content_type => 'text/plain' },
139 body => $arg{description},
140 3 );
141  
142 my $original_body = ref $arg{original_email}
143 ? Scalar::Util::blessed $arg{original_email}
144 ? $arg{original_email}->as_string
145 1 : ${ $arg{original_email} }
146 3 ▶ : $arg{original_email};
Branch: 2 branches
BranchTrueFalseTotal
ref $arg{'original_email'} ? : 2 1 3
builtin::blessed($arg{'original_email'}) ? : 1 1 2
147  
148 3 $description_part->header_set('Date');
149  
150 3 my $original_part = Email::MIME->create(
151 attributes => { content_type => 'message/rfc822' },
152 body => $original_body,
153 );
154  
155 3 $original_part->header_set('Date');
156  
157 my $field_pairs = ref $arg{fields} eq 'HASH'
158 3 ? [ %{ $arg{fields} } ]
159 3 ▶ : $arg{fields};
Branch: 1 branch
BranchTrueFalseTotal
ref $arg{'fields'} eq 'HASH' ? : 3 0 3
160  
161 3 my $fields = Email::Simple->create(header => $field_pairs);
162  
163 3 $fields->header_set('Date');
164  
165 3 ▶ unless (defined $fields->header('user-agent')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fields->header("user-agent")) 3 0 3
166 3 ▶ $fields->header_set(
Condition50%1/2
$class->VERSION || '(dev)'
l!l
30
MC/DC0%0/1
$class->VERSION || '(dev)'
$class->VERSION
Truth table50%1/2
A
A= $class->VERSION
Aresult
11
00
167 'User-Agent',
168 "$class/" . ($class->VERSION || '(dev)')
169 );
170 }
171  
172 3 ▶ unless (defined $fields->header('version')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fields->header("version")) 3 0 3
173 3 $fields->header_set('Version', "1");
174 }
175  
176 3 ▶ unless (defined $fields->header('Feedback-Type')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fields->header("Feedback-Type")) 3 0 3
177 3 $fields->header_set('Feedback-Type', "other");
178 }
179  
180 3 my $report_part = Email::MIME->create(
181 attributes => { content_type => 'message/feedback-report' },
182 body => $fields->header_obj->as_string,
183 );
184  
185 3 $report_part->header_set('Date');
186  
187 my $report = Email::MIME->create(
188 attributes => {
189 content_type => 'multipart/report',
190 'report-type' => 'feedback-report',
191 },
192 parts => [ $description_part, $report_part, $original_part ],
193  
194 header => $arg{header} || [],
195 3 ▶ header_str => $arg{header_str} || [],
Condition50%2/4
$arg{'header'} || []
l!l
30
$arg{'header_str'} || []
l!l
03
MC/DC0%0/1
$arg{'header'} || []
$arg{'header'}
MC/DC0%0/1
$arg{'header_str'} || []
$arg{'header_str'}
Truth table50%1/2
A
A= $arg{'header'}
Aresult
11
00
Truth table50%1/2
A
A= $arg{'header_str'}
Aresult
11
00
196 );
197  
198 3 $class->new($report);
199 }
200  
201 #pod =method as_email
202 #pod
203 #pod This method returns an Email::MIME object representing the report.
204 #pod
205 #pod Note! This method returns a B<new> Email::MIME object each time it is called.
206 #pod If you just want to get a string representation of the report, call
207 #pod C<L</as_string>>. If you call C<as_email> and make changes to the Email::MIME
208 #pod object, the Email::ARF::Report will I<not> be affected.
209 #pod
210 #pod =cut
211  
212 sub as_email {
213 9 ▶ return Email::MIME->new($_[0]->as_string)
Subroutine as_email: called CC=1 SCAR=0.0
Pod documented
214 }
215  
216 #pod =method as_string
217 #pod
218 #pod This method returns a string representation of the report.
219 #pod
220 #pod =cut
221  
222 18 ▶sub as_string { $_[0]->{mime}->as_string }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
223  
224 #pod =method original_email
225 #pod
226 #pod This method returns an Email::Simple object containing the original message to
227 #pod which the report refers. Bear in mind that this message may have been edited
228 #pod by the reporter to remove identifying information.
229 #pod
230 #pod =cut
231  
232 sub original_email {
233 $_[0]->{original_email}
234 9 ▶}
Subroutine original_email: called CC=1 SCAR=0.0
Pod documented
235  
236 #pod =method description
237 #pod
238 #pod This method returns the human-readable description of the report, taken from
239 #pod the body of the human-readable (first) subpart of the report.
240 #pod
241 #pod =cut
242  
243 9 ▶sub _description_part { $_[0]->{description_part} }
Subroutine _description_part: called CC=1 SCAR=0.0
244  
245 sub description {
246 9 ▶ $_[0]->_description_part->body;
Subroutine description: called CC=1 SCAR=0.0
Pod documented
247 }
248  
249 42 ▶sub _fields { $_[0]->{fields} }
Subroutine _fields: called CC=1 SCAR=0.0
250  
251 #pod =method field
252 #pod
253 #pod my $value = $report->field($field_name);
254 #pod my @values = $report->field($field_name);
255 #pod
256 #pod This method returns the value for the given field from the second,
257 #pod machine-readable part of the report. In scalar context, it returns the first
258 #pod value for the field.
259 #pod
260 #pod =cut
261  
262 sub field {
263 42 ▶ my ($self, $field) = @_;
Subroutine field: called CC=1 SCAR=0.0
Pod documented
264  
265 42 return $self->_fields->header($field);
266 }
267  
268 #pod =head2 feedback_type
269 #pod
270 #pod =method user_agent
271 #pod
272 #pod =method arf_version
273 #pod
274 #pod These methods are shorthand for retrieving the fields of the same name, except
275 #pod for C<arf_version>, which returns the F<Version> header. It has been renamed
276 #pod to avoid confusion with the universal C<VERSION> method.
277 #pod
278 #pod =cut
279  
280 9 ▶sub feedback_type { $_[0]->field('Feedback-Type'); }
Subroutine feedback_type: called CC=1 SCAR=0.0
Pod documented
281 9 ▶sub user_agent { $_[0]->field('User-Agent'); }
Subroutine user_agent: called CC=1 SCAR=0.0
Pod documented
282 9 ▶sub arf_version { $_[0]->field('Version'); }
Subroutine arf_version: called CC=1 SCAR=0.0
Pod documented
283  
284 #pod =head1 SEE ALSO
285 #pod
286 #pod L<http://www.mipassoc.org/arf/>
287 #pod
288 #pod L<RFC 5965|http://tools.ietf.org/html/rfc5965>
289 #pod
290 #pod =cut
291  
292 3 1;
293  
294 __END__
↑ Summary ARF.pm »