BIMI/App/Command/svgfromvmc.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 130 CC 14 SCAR 36.4
CC
14
Cov
0%
validate_args
72.0
execute
56.0
BEGIN
2.0
CRAP 38.1
1 package Mail::BIMI::App::Command::svgfromvmc;
2 # ABSTRACT: Extract the SVG from a VMC
3 our $VERSION = '3.20261116'; # VERSION
4 use 5.20.0;
5 BEGIN { $ENV{MAIL_BIMI_CACHE_DEFAULT_BACKEND} = 'Null' };
6 use Mail::BIMI::Prelude;
7 use Mail::BIMI::App -command;
8 use Mail::BIMI;
9 use Mail::BIMI::Indicator;
10 use Mail::BIMI::VMC;
11 use File::Slurp;
12  
13  
14 sub description { 'Extract the SVG from a VMC' }
15 sub usage_desc { "%c svgfromvmc %o" }
16  
17 sub opt_spec {
18 return (
19 [ 'domain=s', 'Extract SVG from VMC in BIMI record at domain' ],
20 [ 'selector=s', 'Optional selector when domain specified' ],
21 [ 'uri=s', 'Extract SVG from VMC at URI' ],
22 [ 'file=s', 'Extract SVG from VMC in file' ],
23 [ 'output=s', 'Write SVG to this file, if not specified will write to STDOUT' ],
24 );
25 }
26  
27 sub validate_args($self,$opt,$args) {
28 $self->usage_error('Extra args specified') if scalar @$args;
29 my $options;
30 $options++ if $opt->domain;
31 $options++ if $opt->uri;
32 $options++ if $opt->file;
33 $self->usage_error('Must specify domain, uri, or file') if $options==0;
34 $self->usage_error('Must specify ONLY ONE of domain, uri, or file') if $options>1;
35 $self->usage_error('Selector cannot be specified without domain') if $opt->selector && !$opt->domain;
36 }
37  
38 sub execute($self,$opt,$args) {
39  
40 my $indicator;
41  
42 my $domain = $opt->domain // 'example.com';
43 my $selector = $opt->selector // 'default';
44 my $dmarc = Mail::DMARC::PurePerl->new;
45 $dmarc->header_from($domain);
46 $dmarc->validate;
47 $dmarc->result->result('pass');
48 my %bimi_options = (
49 dmarc_object => $dmarc,
50 domain => $domain,
51 selector => $selector,
52 options => {
53 vmc_no_check_alt => 1,
54 },
55 );
56 $bimi_options{options}->{vmc_from_file} = $opt->file if $opt->file;
57 my $bimi = Mail::BIMI->new(%bimi_options);
58 my $vmc;
59  
60 if ($opt->domain) {
61 $vmc = eval{$bimi->record->authority->vmc};
62 $indicator = eval{$vmc->indicator};
63 }
64 elsif($opt->uri) {
65 $vmc = Mail::BIMI::VMC->new( check_domain => $domain, check_selector => $selector, uri => $opt->uri, bimi_object => $bimi );
66 $indicator = eval{$vmc->indicator};
67 }
68 elsif($opt->file) {
69 $vmc = Mail::BIMI::VMC->new( check_domain => $domain, check_selector => $selector, uri => $opt->file, bimi_object => $bimi );
70 $indicator = eval{$vmc->indicator};
71 }
72  
73 my $svg = eval{$indicator->data_uncompressed};
74 if (!$svg) {
75 warn "Could not extract SVG";
76 exit 1;
77 }
78  
79 if ($opt->output) {
80 write_file($opt->output, $svg) || warn "Could not write file";
81 }
82 else {
83 say $svg;
84 }
85  
86 }
87  
88 1;
89  
90 __END__