File Coverage

blib/lib/Mail/Karmasphere/Parser/RBL/Base.pm
Criterion Covered Total %
statement 53 57 92.9
branch 21 30 70.0
condition 4 8 50.0
subroutine 10 13 76.9
pod 0 7 0.0
total 88 115 76.5


line stmt bran cond sub pod time code
1             package Mail::Karmasphere::Parser::RBL::Base;
2              
3 3     3   1335 use strict;
  3         5  
  3         85  
4 3     3   13 use warnings;
  3         5  
  3         63  
5 3     3   14 use base 'Mail::Karmasphere::Parser::Base';
  3         5  
  3         1101  
6 3     3   19 use Mail::Karmasphere::Parser::Record;
  3         5  
  3         2328  
7              
8             sub new {
9 2     2 0 1789 my $class = shift;
10 2 50       13 my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ };
  0         0  
11 2   50     17 $self->{Streams} ||= [ $class->_streams() ];
12 2         17 $self = $class->SUPER::new($self);
13 2         18 $self->{default}->{a} = "127.0.0.2";
14 2         5 $self->{default}->{txt} = undef;
15 2         5 return $self;
16             }
17              
18 0     0   0 sub _streams { die "subclass of RBL::Base must define _streams()" }
19              
20 0     0 0 0 sub my_format { die "subclass of RBL::Base must define my_format()" }
21              
22             sub _parse {
23 23     23   24 my $self = shift;
24              
25             GETLINE:
26 23         69 while ( not eof($self->fh)) {
27 44         106 local $_ = $self->fh->getline;
28              
29 44         1207 my $additional;
30             my $value; #[ -1000 .. 1000 ]
31              
32             # XXX This should be an argument to a_to_value, or
33             # it should be handled in this routine. It should NOT
34             # be an instance variable.
35 44         55 $self->{is_exclusion} = 0;
36            
37 44 100       139 /^\#/ and next GETLINE;
38 29 50       46 /^\@/ and $self->handle_fancy($_), next GETLINE;
39 29 100       63 /^\$/ and $self->handle_dollar($_), next GETLINE;
40 23 100       56 /^\:/ and $self->handle_colon($_), next GETLINE;
41 21 100       43 s/^!// and $self->{is_exclusion} = 1;
42 21 50       50 /\S/ or next GETLINE;
43              
44 21         29 chomp;
45 21         53 my @F = split /\s*:\s*/;
46              
47 21         61 $value = $self->a_to_value($F[1]);
48 21         73 $additional = $self->txt_to_additional($F[2]);
49            
50 21 50       74 my ($type, $stream, $identity) = $self->tweaks(@F) or next GETLINE;
51              
52 21 50       63 warn ("returning Record: identity=$identity; value=$value; additional=$additional; stream=$stream\n") if $self->debug;
53              
54 21 100       109 return new Mail::Karmasphere::Parser::Record
    50          
55             (
56             s => $stream,
57             i => $identity,
58             v => $value,
59             (defined $additional ? (d => $additional) : ()), # this is what elsewhere thinks of as "data".
60             (defined $type ? (t => $type) : ()),
61             );
62             }
63              
64 2         7 return;
65             }
66              
67              
68             # ----------------------------------------------------------
69             # functions
70             # ----------------------------------------------------------
71              
72             sub txt_to_additional {
73 21     21 0 24 my $self = shift;
74 21         25 my $txt = shift;
75 21   66     266 return $txt || $self->{default}->{txt};
76             }
77              
78             sub a_to_value {
79 21     21 0 22 my $self = shift;
80 21   33     92 my $a = shift || $self->{default}->{a};
81              
82             # if 127.0.0.2 means "black"
83             # and 127.0.0.4 means "white",
84             # this is where we would put the logic to return the appropriate value.
85              
86 21 50       49 my $value = defined $self->{Value} ? $self->{Value} : 1000;
87              
88             # exclusions are operated as whitelists
89 21 100       52 return - $value if $self->{is_exclusion};
90              
91 20         34 return $value;
92             }
93              
94 0     0 0 0 sub handle_fancy { my $self = shift; }
95 6     6 0 22 sub handle_dollar { my $self = shift; }
96              
97             sub handle_colon {
98 2     2 0 2 my $self = shift;
99 2         3 my $line = shift;
100 2         3 chomp $line;
101 2         7 my ($null, $default_a, $default_txt) = split /:/, $line, 3;
102 2 50       7 $self->{default}->{a} = $default_a if $default_a;
103 2 50       11 $self->{default}->{txt} = $default_txt if $default_txt;
104             }
105              
106             1;