| 1 | package Mail::BIMI::App::Command::svgfromvmc; | ||
| 2 | # ABSTRACT: Extract the SVG from a VMC | ||
| 3 | our $VERSION = '3.20260302'; # 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__ | ||
| 91 | |||
| 92 | =pod | ||
| 93 | |||
| 94 | =encoding UTF-8 | ||
| 95 | |||
| 96 | =head1 NAME | ||
| 97 | |||
| 98 | Mail::BIMI::App::Command::svgfromvmc - Extract the SVG from a VMC | ||
| 99 | |||
| 100 | =head1 VERSION | ||
| 101 | |||
| 102 | version 3.20260302 | ||
| 103 | |||
| 104 | =head1 DESCRIPTION | ||
| 105 | |||
| 106 | App::Cmd class implementing the 'mailbimi svgfromvmc' command | ||
| 107 | |||
| 108 | =head1 REQUIRES | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item * L<File::Slurp|File::Slurp> | ||
| 113 | |||
| 114 | =item * L<Mail::BIMI|Mail::BIMI> | ||
| 115 | |||
| 116 | =item * L<Mail::BIMI::App|Mail::BIMI::App> | ||
| 117 | |||
| 118 | =item * L<Mail::BIMI::Indicator|Mail::BIMI::Indicator> | ||
| 119 | |||
| 120 | =item * L<Mail::BIMI::Prelude|Mail::BIMI::Prelude> | ||
| 121 | |||
| 122 | =item * L<Mail::BIMI::VMC|Mail::BIMI::VMC> | ||
| 123 | |||
| 124 | =back | ||
| 125 | |||
| 126 | =head1 AUTHOR | ||
| 127 | |||
| 128 | Marc Bradshaw <marc@marcbradshaw.net> | ||
| 129 | |||
| 130 | =head1 COPYRIGHT AND LICENSE | ||
| 131 | |||
| 132 | This software is copyright (c) 2020 by Marc Bradshaw. | ||
| 133 | |||
| 134 | This is free software; you can redistribute it and/or modify it under | ||
| 135 | the same terms as the Perl 5 programming language system itself. | ||
| 136 | |||
| 137 | =cut |