Mail/Abuse/Incident.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 14 / 16 Podpod 75.0% 3 / 4 totaltotal 89.7% 105 / 117 CC 14 SCAR 12.6
CC
14
Cov
93%
_helper
8.0
new
3.1
serialize
3.0
CRAP 3.5
« Mail/Abuse/Reader/Stdin.pm ↑ Summary Mail/Abuse/Reader.pm »
1 package Mail::Abuse::Incident;
2  
3 20 require 5.005_62;
4  
5 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 20 ▶use overload '""' => "serialize";
Subroutine BEGIN: called CC=1 SCAR=0.0
10 0 ▶use overload 'eq' => sub { "$_[0]" eq "$_[1]" };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
11 # The code below should be in a single line
12  
13 20 our $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\d+/g); sprintf " %d."."%03d" x $#r, @r };
14  
15 =pod
16  
17 =head1 NAME
18  
19 Mail::Abuse::Incident - Parses a Mail::Abuse::Report to extract incidents
20  
21 =head1 SYNOPSIS
22  
23 package Mail::Abuse::Incident::MyIncident;
24 use Mail::Abuse::Incident;
25  
26 use base 'Mail::Abuse::Incident';
27 sub ip { ... };
28 sub time { ... };
29 sub type { ... };
30 sub data { ... };
31 sub parse { ... }
32 package main;
33  
34 use Mail::Abuse::Report;
35 my $i = new Mail::Abuse::Incident::MyIncident;
36 my $report = new Mail::Abuse::Report (incidents => [$i] );
37  
38 =head1 DESCRIPTION
39  
40 This class implements the reception of an abuse report and its
41 conversion to a C<Mail::Abuse::Report> object.
42  
43 An object must respond to all the methods in the synopsis, returning
44 the required information about the incident (after it has been
45 parsed).
46  
47 The following items of information have been defined:
48  
49 =over
50  
51 =item B<ip>
52  
53 A C<NetAddr::IP> object encoding the origin of the particular
54 incident.
55  
56 =item B<time>
57  
58 A timestamp of the incident, extracted from the report. It must be a a
59 timestamp in the UTC timezone, for consistency.
60  
61 =item B<type>
62  
63 A string identifying the type of incident. Normally of the form
64 B<spam/SpamCom> or B<virus/Nimda>, if such filters exist.
65  
66 =item B<data>
67  
68 Any additional data that the class might want to keep regarding the
69 incident.
70  
71 =back
72  
73 Although specific incident parsers are free to define further class
74 methodsor information items.
75  
76 The following functions are provided for the customization of the
77 behavior of the class.
78  
79 =cut
80  
81 sub new
82 126 ▶{
Subroutine new: called CC=3 SCAR=11.3
Pod undocumented
83 126 my $type = shift;
84 126 ▶ my $class = ref($type) || $type;
Condition66%2/3
ref $type || $type
l!l&&r!l&&!r
80460
MC/DC0%0/2
ref $type || $type
ref $type $type
Truth table66%2/3
A || B
A= ref $typeB= $type
ABresult
1X1
011
000
85  
86 126 ▶ croak "Invalid call to Mail::Abuse::Incident::new"
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 126 126
87 unless $class;
88  
89 126 bless {}, $class;
90 }
91  
92 =pod
93  
94 =over
95  
96 =item C<items()>
97  
98 Enumerates the defined information items that have been defined for
99 this object. Essentially, returns a list of the accessor methods. The
100 object is overloaded so that this method is invoked automatically when
101 serialization is required. This means that
102  
103 print $incident, "\n";
104  
105 Will produce human-readable information.
106  
107 =cut
108  
109 sub items
110 62 ▶{
Subroutine items: called CC=2 SCAR=6.9
Pod documented
111 62 my $self = shift;
112 62 ▶ return wantarray ? sort keys %$self : scalar keys %$self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 58 4 62
113 }
114  
115 =pod
116  
117 =item C<serialize()>
118  
119 Produces a C<print()>able representation of the Incident object,
120 suitable for showing it to a human being.
121  
122 =cut
123  
124 sub _helper
125 49 ▶{
Subroutine _helper: called CC=8 SCAR=20.8
126 49 my $thing = shift;
127  
128 49 ▶ if (ref $thing eq 'SCALAR')
Branch: 4 branches
BranchTrueFalseTotal
if (ref $thing eq 'SCALAR') { } 0 49 49
elsif (ref $thing eq 'HASH') { } 2 47 49
elsif (ref $thing eq 'ARRAY') { } 1 46 47
elsif (ref $thing eq 'NetAddr::IP') { } 6 40 46
129 {
130 0 return $$thing;
131 }
132 elsif (ref $thing eq 'HASH')
133 {
134 2 my $ret = '{ ';
135 2 for my $k (sort keys %$thing)
136 {
137 2 $ret .= "$k => ";
138 2 $ret .= _helper($thing->{$k});
139 2 $ret .= " ";
140 }
141 2 $ret .= '}';
142 2 return $ret;
143 }
144 elsif (ref $thing eq 'ARRAY')
145 {
146 1 my $ret = '[ ';
147 1 for my $k (0 .. $#$thing)
148 {
149 4 $ret .= "$k => ";
150 4 $ret .= _helper($thing->[$k]);
151 4 $ret .= " ";
152 }
153 1 $ret .= ']';
154 1 return $ret;
155 }
156 elsif (ref $thing eq 'NetAddr::IP')
157 {
158 6 return "$thing";
159 }
160 else
161 {
162 40 ▶ if (defined $thing)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thing) { } 38 2 40
163 {
164 38 $thing =~ s/\n/\\n/g;
165 38 return $thing;
166 }
167 else
168 {
169 2 return 'undef';
170 }
171 }
172 }
173  
174 sub serialize
175 52 ▶{
Subroutine serialize: called CC=3 SCAR=11.0
Pod documented
176 52 my $self = shift;
177 52 my $ret = ref($self) . ":";
178 52 for my $k ($self->items)
179 {
180 20 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
181 43 $ret .= " $k=" . _helper($self->$k);
182 }
183 52 ▶ $ret .= "no further data" unless $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless $ret 0 52 52
184 52 return $ret;
185 }
186  
187 =pod
188  
189 =item C<parse($report)>
190  
191 Pushes incidents into the given report, based on parsing of the text
192 in the report itself.
193  
194 It must return a list of objects of the same class, with the incident data
195 (IP address, timestamp and other information) filled.
196  
197 =cut
198  
199 sub parse
200 0 ▶{
Subroutine parse: not called CC=1 SCAR=6.9
Pod documented
201 0 croak "Mail::Abuse::Incident is a virtual class";
202 }
203  
204 # This helps subclasses to provide accessors
205 # automagically
206 sub AUTOLOAD
207 57 ▶{
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
208 20 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
209 20 ▶ use vars qw($AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
210 57 my $method = $AUTOLOAD;
211 57 $method =~ s/^.*:://;
212 *$method = sub
213 1588 ▶ {
Subroutine __ANON__: called CC=2 SCAR=6.9
214 1588 my $self = shift;
215 1588 my $ret = $self->{$method};
216 1588 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 560 1028 1588
217 {
218 560 $ret = $self->{$method};
219 560 $self->{$method} = shift;
220 }
221 1588 return $ret;
222 57 };
223 57 goto \&$method;
224 }
225  
226 __END__
« Mail/Abuse/Reader/Stdin.pm ↑ Summary Mail/Abuse/Reader.pm »