Authentication/App/ProcessLog/App/Command/arex.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 166 CC 18 SCAR 32.0
CC
18
Cov
0%
validate_args
72.0
parse_file
30.0
formatted_object
20.0
CRAP 24.5
1 package Mail::Milter::Authentication::App::ProcessLog::App::Command::arex;
2 use 5.20.0;
3 use strict;
4 use warnings;
5 use Mail::Milter::Authentication::Pragmas;
6 # ABSTRACT: Command to convert an authentication milter arex log back into an A-R header
7 our $VERSION = '4.20261116'; # VERSION
8 use Mail::Milter::Authentication::App::ProcessLog::App -command;
9 use JSON::XS;
10 use Mail::AuthenticationResults 2.20230112;
11 use Mail::AuthenticationResults::Parser;
12 use Mail::AuthenticationResults::Header;
13  
14 sub abstract { 'Convert ARex format log lines into Authentication-Results format ' }
15 sub description { 'Read ARex format line(s) and output standard Authentication-Results headers' }
16 sub usage_desc { "%c arex %o [<log_file>] [<log_file>]" }
17  
18 sub opt_spec {
19 return (
20 [ 'combine|c',
21 'Combine multiple headers per log entry into one',
22 { default => 0 } ],
23 [ 'fold_at|a=s',
24 'Column to fold returned headers at',
25 { default => 99999 } ],
26 [ 'indent_by|b=s',
27 'How many spaces to indent folders headers by', ],
28 [ 'indent_style|s=s',
29 'Indent/folding style (none/entry/subentry/full)' ],
30 [ 'space|p=s',
31 'Number of blank lines between results',
32 { default => 0 } ],
33 );
34 }
35  
36 sub bad_number($value) {
37 return 0 unless defined $value;
38 return 0 if $value =~ /^\d+$/;
39 return 1;
40 }
41  
42 sub validate_args($self,$opt,$args) {
43  
44 if ($opt->{indent_style}) {
45 $self->usage_error('Unknown indent style')
46 unless $opt->{indent_style} eq 'none'
47 || $opt->{indent_style} eq 'entry'
48 || $opt->{indent_style} eq 'subentry'
49 || $opt->{indent_style} eq 'full';
50 }
51  
52 $self->usage_error('indent_by must be numeric') if bad_number($opt->{indent_by});
53 $self->usage_error('space must be numeric') if bad_number($opt->{space});
54 $self->usage_error('fold_at must be numeric') if bad_number($opt->{fold_at});
55  
56 if (@$args) {
57 for my $filename (@$args) {
58 $self->usage_error("File $filename does not exist or is unreadable")
59 unless -f $filename && -r $filename;
60 }
61 }
62 }
63  
64 sub formatted_object($self, $opt, $object) {
65 $object->set_fold_at($opt->fold_at) if $opt->fold_at;
66 $object->set_indent_by($opt->indent_by) if $opt->indent_by;
67 $object->set_indent_style($opt->indent_style) if $opt->indent_style;
68 return $object->as_string;
69 }
70  
71 sub execute($self, $opt, $args) {
72 if (@$args == 0) {
73 $self->parse_file(\*STDIN, $opt, $args);
74 } else {
75 for my $filename (@$args) {
76 open my $file, '<', $filename;
77 $self->parse_file($file, $opt, $args);
78 close $file;
79 }
80 }
81 }
82  
83 sub parse_file($self, $file, $opt, $args) {
84 state $j = JSON::XS->new;
85  
86 LINE: while (my $line = <$file>) {
87 $line =~ s/^.* ARex: //;
88 my $parser = Mail::AuthenticationResults::Parser->new;
89 my $hashref = eval{ $j->decode($line) };
90 unless ($hashref) {
91 print "Error decoding line.\n";
92 next LINE;
93 }
94  
95 my $object;
96 AR: for my $ar ( $hashref->{ar}->@* ) {
97 my $type = $ar->{type};
98 {
99 no warnings; ## no critic
100  
101 unless ($opt->combine) {
102 # If we are not combining entries, just output this one.
103 $object = $parser->_from_hashref($ar->{payload});
104 print "$type: ".$self->formatted_object($opt, $object)."\n";
105 next AR;
106 }
107  
108 # When combining, add to the current object if we have one, or create
109 # a new one if we do not.
110 if ($object) {
111 $object->copy_children_from($parser->_from_hashref($ar->{payload}));
112 } else {
113 $object = $parser->_from_hashref($ar->{payload});
114 }
115 }
116 }
117
118 # When combining, output the combined object here.
119 print "Authentication-Results: ".$self->formatted_object($opt, $object)."\n"
120 if $opt->combine;
121  
122 # And add a bunch of spacers if/as requested
123 print "\n" x $opt->space;
124 }
125 }
126  
127 1;
128  
129 __END__