Mail/Abuse/Incident/Received.pm

filter: Statementstmt 88.5% 62 / 70 Branchbran 46.6% 14 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 75.2% 91 / 121 CC 22 SCAR 26.2
CC
22
Cov
75%
parse
23.7
_add_time
5.0
_add_ip
5.0
CRAP 13.8
« Mail/Abuse/Processor/Score.pm ↑ Summary Mail/Abuse/Incident/SpamCop.pm »
1 package Mail::Abuse::Incident::Received;
2  
3 2 require 5.005_62;
4  
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use NetAddr::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Date::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶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 2 our $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
16  
17 =pod
18  
19 =head1 NAME
20  
21 Mail::Abuse::Incident::Received - Parses Received: headers in an abuse report
22  
23 =head1 SYNOPSIS
24  
25 use Mail::Abuse::Report;
26 use Mail::Abuse::Incident::Received;
27  
28 my $i = new Mail::Abuse::Incident::Received;
29 my $report = new Mail::Abuse::Report (incidents => [$i] );
30  
31 =head1 DESCRIPTION
32  
33 This class parses standard Received: headers included in a given abuse
34 report.
35  
36 Entries in the configuration file can control the behavior of this
37 module. The following entries are recognized:
38  
39 =cut
40  
41 2 ▶use constant DEBUG => 'debug received';
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 =pod
44  
45 =over
46  
47 =item B<debug received>
48  
49 When set to a true value, causes some debug information to be produced
50 via C<warn()>.
51  
52 =cut
53  
54  
55 sub _add_time ($$$)
56 6 ▶{
Subroutine _add_time: called CC=4 SCAR=16.1
57 6 ▶ my $time = str2time($_[1], $_[2]) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $time = str2time($_[1], $_[2]) or return 6 0 6
58  
59 6 for (@{$_[0]})
60 {
61 0 ▶ return if $_ == $time;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $time 0 0 0
62 }
63  
64 6 push @{$_[0]}, $time;
65 }
66  
67 sub _add_ip ($$)
68 6 ▶{
Subroutine _add_ip: called CC=4 SCAR=16.1
69 6 ▶ my $ip = new NetAddr::IP $_[1] or return;
Branch: 1 branch
BranchTrueFalseTotal
my $ip = "NetAddr::IP"->new($_[1]) or return 6 0 6
70  
71 6 for (@{$_[0]})
72 {
73 0 ▶ return if $_ == $ip;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $ip 0 0 0
74 }
75  
76 6 push @{$_[0]}, $ip;
77 }
78  
79 =pod
80  
81 =back
82  
83 The functions provided by this module, are described below
84  
85 =over
86  
87 =item C<parse($report)>
88  
89 Parses all the Received: headers, creating an instance with each
90 combination of IP address and timestamp found.
91  
92 Returns a list of objects of the same class, with the incident data
93 (IP address, timestamp and other information) filled.
94  
95 =cut
96  
97 sub parse
98 3 ▶{
Subroutine parse: called CC=16 SCAR=31.7
Pod documented
99 3 my $self = shift;
100 3 my $rep = shift;
101  
102 3 my @ret = (); # Default return
103 3 my $count = 0;
104  
105 3 my $text = undef;
106  
107 3 ▶ my $debug = $rep->config ? $rep->config->{&DEBUG} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rep->config ? : 0 3 3
108  
109 3 ▶ warn "M::A::I::Received: debug\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 3 3
110  
111 3 ▶ if ($rep->normalized)
Branch: 1 branch
BranchTrueFalseTotal
if ($rep->normalized) { } 3 0 3
112 {
113 3 $text = $rep->body;
114 }
115 else
116 {
117 # Skip the report headers and focus
118 # on the offender's
119  
120 0 ▶ if ($ {$rep->text} =~ m!^\s*\n(.*)!xms)
Branch: 1 branch
BranchTrueFalseTotal
if (${$rep->text;} =~ /^\s*\n(.*)/msx) { } 0 0 0
121 {
122 0 my $t = $1;
123 0 $text = \$t;
124 }
125 else
126 {
127 0 $text = $rep->text;
128 }
129 }
130  
131 3 ▶ unless ($$text and $$text =~ m!Received: !m)
Branch: 1 branch
BranchTrueFalseTotal
unless ($$text and $$text =~ /Received: /m) 0 3 3
Condition33%1/3
$$text and $$text =~ /Received: /m
!ll&&!rl&&r
003
MC/DC0%0/2
$$text and $$text =~ /Received: /m
$$text $$text =~ /Received: /m
Truth table33%1/3
A and B
A= $$textB= $$text =~ /Received: /m
ABresult
0X0
100
111
132 {
133 0 ▶ warn "M::A::I::Received: No Received: headers in sight\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
134 if $debug;
135 }
136  
137 3 ▶ while ($$text and $$text =~ m!^(Received: .*?)(?=([-\w]+: |^\s*$))!msg)
Branch: 1 branch
BranchTrueFalseTotal
if ($$text and $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms) 6 3 9
Condition66%2/3
$$text and $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
!ll&&!rl&&r
036
MC/DC50%1/2
$$text and $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
$$text $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
Truth table66%2/3
A and B
A= $$textB= $$text =~ /^(Received: .*?)(?=([-\w]+: |^\s*$))/gms
ABresult
0X0
100
111
138 {
139 # Analyze this Received: header
140 6 my $header = $1;
141  
142 6 ▶ warn "M::A::I::Received: Matching header [$header]\n"
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6 6
143 if $debug;
144  
145 6 my @a = ();
146 6 my @t = ();
147 # Add numeric IP addresses
148 6 ▶ _add_ip \@a, $1 while $header =~ m/(\d+\.\d+\.\d+\.\d+)/g;
Branch: 1 branch
BranchTrueFalseTotal
$header =~ /(\d+\.\d+\.\d+\.\d+)/g and do { &_add_ip(\@a, $1) } 6 6 12
149  
150 6 ▶ _add_time \@t, $1, $rep->tz
Branch: 1 branch
BranchTrueFalseTotal
$header =~ /((\w+,\s+)?\d+\s\w+\s\d+\s[\d:]+\s[-+]?\w+)/g and do { &_add_time(\@t, $1, scalar $rep->tz) } 6 6 12
151 while $header =~ m/((\w+,\s+)?\d+\s\w+\s\d+\s[\d:]+\s[-+]?\w+)/g;
152  
153 6 for my $a (@a)
154 {
155 6 for my $t (@t)
156 {
157 6 my $i = $self->new();
158 6 $i->ip($a);
159 6 $i->time($t);
160 6 $i->type('spam/Received');
161 6 $i->data($header);
162
163 6 ▶ warn "M::A::I::Received: Add incident for ", $i->ip, ", ",
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6 6
164 $i->time, "\n" if $debug;
165  
166 6 push @ret, $i;
167 }
168 }
169 }
170  
171 3 return @ret;
172 }
173  
174 __END__
« Mail/Abuse/Processor/Score.pm ↑ Summary Mail/Abuse/Incident/SpamCop.pm »