| 1 | package Mail::Karmasphere::Parser::Record; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # acceptable identity types include RBLDNSd format identities: | ||||||||||||||||||||||||||||
| 7 | # * IP: a CIDR netblock: 192.168.0.0/24 | ||||||||||||||||||||||||||||
| 8 | # * IP: a CIDR range: 192.168.0.1-192.168.0.255 | ||||||||||||||||||||||||||||
| 9 | # * IP: a single IP address: 192.168.0.1 | ||||||||||||||||||||||||||||
| 10 | # * domain: a domain name: foo.example.com | ||||||||||||||||||||||||||||
| 11 | # * domain: a subdomain mask: .example.com | ||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||
| 13 | # also, | ||||||||||||||||||||||||||||
| 14 | # * URI: some sort of http://whatnot/ or ftp://whatnot/, etc | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # this can all be in UTF-8. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | my %keys = ( | |||||||||||||||||||||||||||
| 19 | s => "stream", | ||||||||||||||||||||||||||||
| 20 | # t => "type", | ||||||||||||||||||||||||||||
| 21 | i => "identity", | ||||||||||||||||||||||||||||
| 22 | v => "value", | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 57 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 57 | my $class = shift; | |||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $self = ($#_ == 0) ? { %{ (shift) } } : { @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | 57 | for (keys %keys) { | |||||||||||||||||||||||||||
| 29 | 171 | ▶ | die "No $keys{$_} ($_) in Record" unless defined $self->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | $self->{t} = guess_identity_type($self->{i}) | ||||||||||||||||||||||||||||
| 32 | 57 | ▶ | unless exists $self->{t}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 57 | return bless $self, $class; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 9 | my $ip4p = q{(?:25[0-5]|2[0-4][0-9]|[0-1]?[0-9]{1,2})}; | |||||||||||||||||||||||||||
| 37 | 9 | my $ip4s = "[.]"; | |||||||||||||||||||||||||||
| 38 | 9 | my $ip4 = qq{(?:$ip4p(?:$ip4s$ip4p){0,3})}; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 57 | ▶ | sub is_ip4 { | ||||||||||||||||||||||||||
|
Subroutine
is_ip4: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 57 | return $_[0] =~ m!^$ip4(?:-$ip4|/[0-9]{1,2})?$!o; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 57 | ▶ | sub guess_identity_type { | ||||||||||||||||||||||||||
|
Subroutine
guess_identity_type: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 57 | my $identity = shift; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 57 | ▶ | if (is_ip4($identity)) { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 48 | 24 | return 'ip4'; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | elsif ($identity =~ /^[0-9a-f:]{2,64}$/i) { | ||||||||||||||||||||||||||||
| 51 | 1 | return 'ip6'; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | elsif ($identity =~ /^(https?|ftp):\/\//) { | ||||||||||||||||||||||||||||
| 54 | 8 | return 'url'; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | elsif ($identity =~ /@/) { | ||||||||||||||||||||||||||||
| 57 | 3 | return 'email'; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | elsif ($identity =~ /\.[a-z]{2,4}\.?$/) { | ||||||||||||||||||||||||||||
| 60 | 21 | return 'domain'; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | return 'unknown'; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 57 | ▶ | sub stream { | ||||||||||||||||||||||||||
|
Subroutine
stream: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 57 | return $_[0]->{s}; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 57 | ▶ | sub type { | ||||||||||||||||||||||||||
|
Subroutine
type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 57 | return $_[0]->{t}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | sub identity { | ||||||||||||||||||||||||||
|
Subroutine
identity: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 75 | 5 | return $_[0]->{i}; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 5 | ▶ | sub value { | ||||||||||||||||||||||||||
|
Subroutine
value: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 5 | return $_[0]->{v}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 5 | ▶ | sub data { | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 83 | 5 | return $_[0]->{d}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _quote { | ||||||||||||||||||||||||||
|
Subroutine
_quote: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 87 | 0 | my $value = shift; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | return $value unless $value =~ m/["', ]/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $value =~ s/"/""/g; | |||||||||||||||||||||||||||
| 90 | 0 | return '"' . $value . '"'; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # poor man's CSV. | ||||||||||||||||||||||||||||
| 94 | # produces one of | ||||||||||||||||||||||||||||
| 95 | # 1.2.3.4 | ||||||||||||||||||||||||||||
| 96 | # 1.2.3.4,-1000 (or some other number) | ||||||||||||||||||||||||||||
| 97 | # 1.2.3.4,1000,"because why" | ||||||||||||||||||||||||||||
| 98 | # | ||||||||||||||||||||||||||||
| 99 | # note that 1.2.3.4,1000 is NOT optimized away to just 1.2.3.4 | ||||||||||||||||||||||||||||
| 100 | # we cannot assume that the feed is a whitelist | ||||||||||||||||||||||||||||
| 101 | # | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 0 | my $out = _quote($self->{i}); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | $out .= "," . $self->{v} if (defined $self->{v} or | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | defined $self->{d}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->{"v"} or defined $self->{"d"}
MC/DC0%0/2
defined $self->{"v"} or defined $self->{"d"}
defined $self->{"v"}
defined $self->{"d"}
Truth table0%0/3
A or B
A= defined $self->{"v"}B= defined $self->{"d"}
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $out .= "," . _quote($self->{d}) if (defined $self->{d}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # print STDERR "v = $self->{v} -> $out\n"; | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | return $out; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 9 | 1; | |||||||||||||||||||||||||||