| 1 | package Mail::Milter::Authentication::App::Blocker::App::Command::add; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Mail::Milter::Authentication::Pragmas; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Command to add a block to a given file | ||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | our $VERSION = '4.20261116'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Mail::Milter::Authentication::App::Blocker::App -command; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Date::Manip::Date; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use TOML; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Text::Table; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub abstract { 'Add a block to a given file' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
abstract: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 0 | ▶ | sub description { 'Add a block to a given toml file' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
description: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub opt_spec { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opt_spec: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | return ( | ||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | [ 'file=s', 'Config file to operate on' ], | |||||||||||||||||||||||||||||||||||||||||
| 19 | [ 'id=s', 'ID of the block to add' ], | ||||||||||||||||||||||||||||||||||||||||||
| 20 | [ 'callback=s', 'callback stage of the block to add' ], | ||||||||||||||||||||||||||||||||||||||||||
| 21 | [ 'value=s', 'value of the block to add' ], | ||||||||||||||||||||||||||||||||||||||||||
| 22 | [ 'with=s', 'SMTP result of the block to add' ], | ||||||||||||||||||||||||||||||||||||||||||
| 23 | [ 'percent=s', 'percent of the mail to block' ], | ||||||||||||||||||||||||||||||||||||||||||
| 24 | [ 'until=s', 'time of block expiry' ], | ||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub validate_args($self,$opt,$args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate_args: not called CC=18 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | # no args allowed but options! | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | $self->usage_error('Must supply a filename') if ( !$opt->{file} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | $self->usage_error('Supplied filename does not exist') if ( ! -e $opt->{file} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $self->usage_error('Must supply an id') if ( !$opt->{id} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | $self->usage_error('Must supply a callback') if ( !$opt->{callback} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | my @valid_callbacks = ( qw{ connect helo envfrom envrcpt header }); | |||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | unless ( grep { $opt->{callback} eq $_ } @valid_callbacks ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $self->usage_error('Callback must be one of '.join(', ',@valid_callbacks)); | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $self->usage_error('Must supply a value') if ( !$opt->{value} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->usage_error('Must supply a with') if ( !$opt->{with} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my ( $rcode, $xcode, $message ) = split( ' ', $opt->{with}, 3 ); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if ($rcode !~ /^[4|5]\d\d$/ || $xcode !~ /^[4|5]\.\d+\.\d+$/ || substr($rcode, 0, 1) ne substr($xcode, 0, 1)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not $rcode =~ /^[4|5]\d\d$/u && $xcode =~ /^[4|5]\.\d+\.\d+$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
$rcode =~ /^[4|5]\d\d$/u && $xcode =~ /^[4|5]\.\d+\.\d+$/u
MC/DC0%0/3
not $rcode =~ /^[4|5]\d\d$/u && $xcode =~ /^[4|5]\.\d+\.\d+$/u or substr($rcode, 0, 1) ne substr($xcode, 0, 1)
$rcode =~ /^[4|5]\d\d$/u
$xcode =~ /^[4|5]\.\d+\.\d+$/u
substr($rcode, 0, 1) ne substr($xcode, 0, 1)
Truth table0%0/4
not(A && B) or C
A= $rcode =~ /^[4|5]\d\d$/uB= $xcode =~ /^[4|5]\.\d+\.\d+$/uC= substr($rcode, 0, 1) ne substr($xcode, 0, 1)
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $self->usage_error('With is invalid, please use extended return code format'); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | $self->usage_error('Must supply a percent') if ( ! defined $opt->{percent} ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $self->usage_error('Percent must be a number') if ( ! ($opt->{percent} =~ /^\d+$/) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $self->usage_error('Percent must be a number between 0 and 100') if ( ( $opt->{percent} < 0 ) || ( $opt->{percent} > 100 ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$opt->{"percent"} < 0 or $opt->{"percent"} > 100
MC/DC0%0/2
$opt->{"percent"} < 0 or $opt->{"percent"} > 100
$opt->{"percent"} < 0
$opt->{"percent"} > 100
Truth table0%0/3
A or B
A= $opt->{"percent"} < 0B= $opt->{"percent"} > 100
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ( $opt->{until} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $dmdate = Date::Manip::Date->new; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( !$dmdate->parse($opt->{until}) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | $opt->{until} = $dmdate->secs_since_1970_GMT(); | |||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | $self->usage_error('Could not parse until date'); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $opt->{until} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $self->usage_error('No args allowed') if @$args; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub execute($self,$opt,$args) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | open ( my $inf, '<', $opt->{file} ); | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my $body = do { local $/; <$inf> }; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | close $inf; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my ( $data, $error ) = from_toml( $body ); | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( $error ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | say 'Error parsing file'; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | say $error; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( exists $data->{$opt->{id}} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | say 'The given ID already exists in that file'; | |||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | exit 1; | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | $data->{$opt->{id}} = { | ||||||||||||||||||||||||||||||||||||||||||
| 87 | callback => $opt->{callback}, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | value => $opt->{value}, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | with => $opt->{with}, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | percent => $opt->{percent}, | ||||||||||||||||||||||||||||||||||||||||||
| 91 | until => $opt->{until}, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | open my $outf, '>', $opt->{file}; | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | print $outf to_toml($data); | |||||||||||||||||||||||||||||||||||||||||
| 96 | 0 | close $outf; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | say 'Block added and file saved'; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||||||||||||||||