Mail/Abuse/Incident/Normalize.pm

filter: Statementstmt 95.3% 82 / 86 Branchbran 81.2% 26 / 32 Conditioncond 50.0% 15 / 30 MC/DCmcdc 25.0% 4 / 16 Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 78.4% 138 / 176 CC 30 SCAR 26.8
CC
30
Cov
83%
parse
21.8
__ANON__
15.5
decode_parts
8.5
CRAP 14.5
« Mail/Abuse/Processor/ArchiveDBI.pm ↑ Summary Mail/Abuse/Processor/Score.pm »
1 package Mail::Abuse::Incident::Normalize;
2  
3 8 require 5.005_62;
4  
5 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use MIME::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use base 'Mail::Abuse::Incident';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # The code below should be in a single line
14  
15 8 our $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
16  
17 =pod
18  
19 =head1 NAME
20  
21 Mail::Abuse::Incident::Normalize - Filter the text of a report within an Email
22  
23 =head1 SYNOPSIS
24  
25 use Mail::Abuse::Report;
26 use Mail::Abuse::Incident::Normalize;
27 my $i = new Mail::Abuse::Incident::Normalize;
28 my $report = new Mail::Abuse::Report (incidents => [$i] );
29  
30 =head1 DESCRIPTION
31  
32 This class handles various important tasks related with recognizing an
33 abuse report. The specific tassks are:
34  
35 =over
36  
37 =item *
38  
39 Parse the headers and the body of the email message
40  
41 =item *
42  
43 Provide a translation from HTML to text if no suitable part is
44 included in the original report
45  
46 =item *
47  
48 When a report has been forwarded or quoted multiple times, this class
49 removes the indications at the beginning and left-hand side (E<gt>).
50  
51 =back
52  
53 The parser based on this class must be the first in the list of
54 parsers passed to the report object, so that all parser that need its
55 services can access them automatically.
56  
57 The following new keys are created into the corresponding report after
58 running this class. These are:
59  
60 =over
61  
62 =item B<header>
63  
64 A C<Mail::Header> object with the header of the message.
65  
66 =item B<body>
67  
68 Contains a reference to the body of the message, as a single scalar or
69 string.
70  
71 =item B<normalized>
72  
73 Set to the scalar C<Mail::Abuse::Incident::Normalize> to indicate that
74 this class did the normalization.
75  
76 =item B<tz>
77  
78 The guessed timezone from the message. This might come from matching
79 it in the body of the message, from the Date header or from other
80 sources. This should be used as a "last-resort hint" by other Incident
81 parsers.
82  
83 =back
84  
85 The following method is implemented bu this class:
86  
87 =over
88  
89 =item C<parse($report)>
90  
91 Pushes incidents into the given report, based on parsing of the text
92 in the report itself.
93  
94 It must return a list of objects of the same class, with the incident data
95 (IP address, timestamp and other information) filled.
96  
97 =cut
98  
99 sub parse
100 19 ▶{
Subroutine parse: called CC=19 SCAR=30.8
Pod documented
101 19 my $self = shift;
102 19 my $rep = shift;
103  
104 # Phase 0 - Build the parser we'll
105 # be using this time. We do so with
106 # closures, so that they can reach
107 # $rep w/o black magic
108  
109 my $p = HTML::Parser->new
110 (
111 api_version => 3,
112 default_h => [ "" ],
113 0 ▶ start_h => [ sub { $ {$rep->body} .=
Subroutine __ANON__: called CC=5 SCAR=27.4
Condition0%0/5
$_[1]{'alt'} || $_[1]{'src'} || 'n/a'
l!l
00
$_[1]{"alt"} or $_[1]{"src"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1]{'alt'} || $_[1]{'src'} || 'n/a'
$_[1]{"alt"} $_[1]{"src"}
Truth table0%0/3
A or B
A= $_[1]{"alt"}B= $_[1]{"src"}
ABresult
1X1
011
000
114 "[IMG " .
115 40 ▶ ($_[1]->{alt} || $_[1]->{src} || 'n/a') .
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and $_[0] eq "img" 0 40 40
Condition33%1/3
$_[0] and $_[0] eq "img"
!ll&&!rl&&r
0400
MC/DC0%0/2
$_[0] and $_[0] eq "img"
$_[0] $_[0] eq "img"
Truth table33%1/3
A and B
A= $_[0]B= $_[0] eq "img"
ABresult
0X0
100
111
116 "]\n"
117 if $_[0] && $_[0] eq 'img';
118 }, "tagname, attr" ],
119 19 ▶ text_h => [ sub { $ {$rep->body} .= shift; }, "dtext" ],
Subroutine __ANON__: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
my $p = "HTML::Parser"->new("api_version", 3, "default_h", [""], "start_h", [sub { ${$rep->body;} .= '[IMG ' . ($_[1]{"alt"} || $_[1]{"src"} || "n/a") . "]\n" if $_[0] and $_[0] eq "img"; } , "tagname, attr"], "text_h", [sub { ${$rep->body;} .= shift(); } , "dtext"]) or return 19 0 19
120 ) or return;
121
122 19 $p->ignore_elements(qw(script style));
123 19 $p->strict_comment(1);
124  
125 19 $self->html_parser($p);
126  
127 19 my $parser = new MIME::Parser;
128 19 ▶ return unless $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless $parser 0 19 19
129  
130 19 $parser->ignore_errors(1);
131 19 $parser->decode_headers(1);
132 19 $parser->extract_nested_messages(1);
133 19 $parser->extract_uuencode(1);
134  
135 # Phase 1 - Decode and find a suitable
136 # part containing the message text.
137 # Store its headers and body
138  
139 19 my $e = eval { $parser->parse_data($rep->text); };
140  
141 19 ▶ if ($@ or !$e)
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $e) 0 19 19
Condition33%1/3
$@ or not $e
l!l&&r!l&&!r
0019
MC/DC0%0/2
$@ or not $e
$@ not $e
Truth table33%1/3
A or not(B)
A= $@B= not $e
ABresult
1X1
011
000
142 {
143 0 $parser->filer->purge;
144 0 return;
145 }
146  
147 19 my $text;
148 19 $rep->header($e->head);
149 19 $rep->body(\$text);
150  
151 19 ▶ my $decoded_body = ($self->decode_parts($rep, 'any', $e) || '');
Condition100%2/2
$self->decode_parts($rep, 'any', $e) || ''
l!l
181
MC/DC100%1/1
$self->decode_parts($rep, 'any', $e) || ''
$self->decode_parts($rep, 'any', $e)
Truth table100%2/2
A
A= $self->decode_parts($rep, 'any', $e)
Aresult
11
00
152 19 ▶ $rep->body(\$decoded_body) if $decoded_body;
Branch: 1 branch
BranchTrueFalseTotal
if $decoded_body 18 1 19
153  
154 19 $rep->normalized(ref $self);
155 19 $self->html_parser(undef);
156 19 $parser->filer->purge;
157  
158 19 ▶ return unless $rep->body and $ {$rep->body};
Branch: 1 branch
BranchTrueFalseTotal
unless $rep->body and ${$rep->body;} 1 18 19
Condition66%2/3
$rep->body and ${$rep->body;}
!ll&&!rl&&r
0118
MC/DC50%1/2
$rep->body and ${$rep->body;}
$rep->body ${$rep->body;}
Truth table66%2/3
A and B
A= $rep->bodyB= ${$rep->body;}
ABresult
0X0
100
111
159  
160 # Phase 2 - This might be actually a
161 # reply of forward. Remove any
162 # indications from the message body
163  
164 18 $ {$rep->body} =~ s/
165 ^(([\t ]|[^\w\s])* # 0 or more non-space, non-word chars
166 [^\w\s]+ # followed by one or more non-space, non-words
167 )+ # repeated one or more times,
168 [\t ]* # followed by zero or more spaces.
169 //xmsg;
170  
171 # Phase 3 - Try to recognize a
172 # timezone in the body. If this fails,
173 # try at the Date: header. Finally,
174 # use UTC as the guess
175  
176 # These timezones were taken w/o permission from Time::Zone.
177 # Thanks to the authors anyway :)
178  
179 18 my %Zones =
180 (
181 "GMT" => '+0000', # Greenwich Mean
182 "UT" => '+0000', # Universal (Coordinated)
183 "UTC" => '+0000',
184 "WET" => '+0000', # Western European
185 "WAT" => '-0100', # West Africa
186 "AT" => '-0200', # Azores
187 "FNT" => '-0200', # Brazil Time (Extreme East)
188 "BRT" => '-0300', # Brazil Time (East Standard)
189 "MNT" => '-0400', # Brazil Time (West Standard)
190 "EWT" => '-0400', # U.S. Eastern War Time
191 "AST" => '-0400', # Atlantic Standard
192 "VET" => '-0400', # Venezuela Standard Time
193 "EST" => '-0500', # Eastern Standard
194 "ACT" => '-0500', # Brazil Time (Extreme West - Acre)
195 "CST" => '-0600', # Central Standard
196 "MST" => '-0700', # Mountain Standard
197 "PST" => '-0800', # Pacific Standard
198 "YST" => '-0900', # Yukon Standard
199 "HST" => '-1000', # Hawaii Standard
200 "CAT" => '-1000', # Central Alaska
201 "AHST" => '-1000', # Alaska-Hawaii Standard
202 "NT" => '-1100', # Nome
203 "IDLW" => '-1200', # International Date Line West
204 "CET" => '+0100', # Central European
205 "MEZ" => '+0100', # Central European (German)
206 "ECT" => '+0100', # Central European (French)
207 "MET" => '+0100', # Middle European
208 "MEWT" => '+0100', # Middle European Winter
209 "SWT" => '+0100', # Swedish Winter
210 "SET" => '+0100', # Seychelles
211 "FWT" => '+0100', # French Winter
212 "EET" => '+0200', # Eastern Europe, USSR Zone 1
213 "UKR" => '+0200', # Ukraine
214 "BT" => '+0300', # Baghdad, USSR Zone 2
215 "IT" => '+0330', # Iran
216 "ZP4" => '+0400', # USSR Zone 3
217 "ZP5" => '+0500', # USSR Zone 4
218 "IST" => '+0530', # Indian Standard
219 "ZP6" => '+0600', # USSR Zone 5
220 "WST" => '+0800', # West Australian Standard
221 "HKT" => '+0800', # Hong Kong
222 "CCT" => '+0800', # China Coast, USSR Zone 7
223 "JST" => '+0900', # Japan Standard, USSR Zone 8
224 "KST" => '+0900', # Korean Standard
225 "CAST" => '+0930', # Central Australian Standard
226 "EAST" => '+1000', # Eastern Australian Standard
227 "GST" => '+1000', # Guam Standard, USSR Zone 9
228 "NZT" => '+1200', # New Zealand
229 "NZST" => '+1200', # New Zealand Standard
230 "IDLE" => '+1200', # International Date Line East
231 );
232  
233 # In the following matches, we'll attempt to use the inverted
234 # message to avoid mistaking a timezone in a forwarded header for
235 # our target timestamp...
236  
237 18 my $rev = join "\n", reverse split /\n/, $ {$rep->body};
238  
239 # Step 3a - Try to locate a numeric
240 # timezone in the format [+-]\d\d:?\d\d
241  
242 # warn "Body: ${$rep->body}\n*********************\n";
243 # warn "Rev: $rev\n******************\n";
244  
245 18 ▶ if ($rev =~ m!([-+]\d\d:?\d\d)!ms)
Branch: 1 branch
BranchTrueFalseTotal
if ($rev =~ /([-+]\d\d:?\d\d)/ms) 9 9 18
246 {
247 9 $rep->tz($1);
248 }
249  
250 # Step 3b - If failed, try to locate the
251 # longest timezone posssible
252  
253 18 ▶ unless ($rep->tz)
Branch: 1 branch
BranchTrueFalseTotal
unless ($rep->tz) 9 9 18
254 {
255 9 my $zone = '';
256  
257 9 for my $tz ( sort keys %Zones )
258 {
259 450 ▶ next if length($tz) < length($zone);
Branch: 1 branch
BranchTrueFalseTotal
if length $tz < length $zone 8 442 450
260 442 ▶ next unless ($rev =~ m/\W ${tz} \W /msx
Branch: 1 branch
BranchTrueFalseTotal
unless $rev =~ /\W $tz \W /msx or $rev =~ /^ $tz \W /msx or $rev =~ /\W $tz $ /msx or $rev =~ /^ $tz $ /msx 432 10 442
Condition77%7/9
$rev =~ /\W $tz \W /msx or $rev =~ /^ $tz \W /msx or $rev =~ /\W $tz $ /msx or $rev =~ /^ $tz $ /msx
l!l&&r!l&&!r
100432
$rev =~ /\W $tz \W /msx or $rev =~ /^ $tz \W /msx or $rev =~ /\W $tz $ /msx
l!l&&r!l&&!r
91432
$rev =~ /\W $tz \W /msx or $rev =~ /^ $tz \W /msx
l!l&&r!l&&!r
90433
MC/DC50%2/4
$rev =~ /\W $tz \W /msx or $rev =~ /^ $tz \W /msx or $rev =~ /\W $tz $ /msx or $rev =~ /^ $tz $ /msx
$rev =~ /\W $tz \W /msx $rev =~ /^ $tz \W /msx $rev =~ /\W $tz $ /msx $rev =~ /^ $tz $ /msx
Truth table60%3/5
A or B or C or D
A= $rev =~ /\W $tz \W /msxB= $rev =~ /^ $tz \W /msxC= $rev =~ /\W $tz $ /msxD= $rev =~ /^ $tz $ /msx
ABCDresult
1XXX1
01XX1
001X1
00011
00000
261 or $rev =~ m/^ ${tz} \W /msx
262 or $rev =~ m/\W ${tz} $ /msx
263 or $rev =~ m/^ ${tz} $ /msx);
264 # warn "*** Zone $tz matched\n";
265 10 $zone = $tz;
266 }
267  
268 9 ▶ $rep->tz($Zones{$zone}) if $zone;
Branch: 1 branch
BranchTrueFalseTotal
if $zone 8 1 9
269 }
270  
271 # Step 3c - If failed, declare UTC
272  
273 18 ▶ $rep->tz('UTC') unless defined $rep->tz;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rep->tz 1 17 18
274  
275 18 return;
276 }
277  
278 sub decode_parts
279 24 ▶{
Subroutine decode_parts: called CC=8 SCAR=21.4
Pod undocumented
280 24 my $self = shift;
281 24 my $rep = shift;
282 24 my $type = shift;
283 24 my $e = shift;
284  
285 24 ▶ if (my @parts = $e->parts)
Branch: 2 branches
BranchTrueFalseTotal
if (my(@parts) = $e->parts) { } 3 21 24
elsif (my $body = $e->bodyhandle) { } 21 0 21
286 {
287 3 my $r = '';
288 3 ▶ $r .= ($self->decode_parts($rep, $type, $_) || '') for @parts;
Condition50%1/2
$self->decode_parts($rep, $type, $_) || ''
l!l
50
MC/DC0%0/1
$self->decode_parts($rep, $type, $_) || ''
$self->decode_parts($rep, $type, $_)
Truth table50%1/2
A
A= $self->decode_parts($rep, $type, $_)
Aresult
11
00
289 3 return $r;
290 }
291 elsif (my $body = $e->bodyhandle)
292 {
293 21 my $mime = $e->head->mime_type;
294 21 ▶ if (grep { $mime eq $_ } qw(text/plain message/rfc822))
Branch: 2 branches
BranchTrueFalseTotal
if (grep {$mime eq $_;} 'text/plain', 'message/rfc822') { } 19 2 21
elsif ($type eq 'any' and $mime eq 'text/html') { } 2 0 2
295 {
296 19 return $body->as_string;
297 }
298 ▶ elsif ($type eq 'any' and $mime eq 'text/html')
Condition33%1/3
$type eq "any" and $mime eq "text/html"
!ll&&!rl&&r
002
MC/DC0%0/2
$type eq "any" and $mime eq "text/html"
$type eq "any" $mime eq "text/html"
Truth table33%1/3
A and B
A= $type eq "any"B= $mime eq "text/html"
ABresult
0X0
100
111
299 {
300 2 my $b = $body->as_string;
301 2 $self->html_parser->parse($b);
302 2 return $ {$rep->body};
303 }
304 }
305 0 return; # False by default
306 }
307  
308 __END__
« Mail/Abuse/Processor/ArchiveDBI.pm ↑ Summary Mail/Abuse/Processor/Score.pm »