input:b0011v0002p0013e0006-e0,1,3,k110,111,112,ra0,1,mlsfw
  9   lŠz¾Jj"TŅząøÅ£Ęp!aÜ4ż()ØP@ĖjąS’Å Ž    #!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/refresh-dkim-validity-cache     Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

#----------------------------------------------------------------------
# NOTE: See Cpanel/DKIM/ValidityCache/Sync.pm for a more code-friendly
# analogue to this script.
#----------------------------------------------------------------------

package scripts::refresh_dkim_validity_cache;

use strict;
use warnings;

=encoding utf-8

=head1 NAME

scripts/refresh-dkim-validity-cache

=head1 USAGE

    refresh-dkim-validity-cache ( --help | --all-domains | --initialize )

    refresh-dkim-validity-cache --domain $d1 --domain $d2 ...

=head1 DESCRIPTION

This script refreshes the DKIM validity cache so that the system
will add DKIM signatures for all messages whose sender domain has a
correct DKIM configurationābut B<only> those messages.

It notes all changes made to the cache and prints a summary at the end.

=head1 OPTIONS

=over

=item * C<--all-domains> - Refreshes every domainās validity cache.
(This can take a while!) Excludes C<--domain> and C<--initialize>.

=item * C<--domain> - Accepts a domain as argument. May be given multiple
times.

=item * C<--initialize> - Creates the validity cache anew if it doesnāt
already exist. All users are recorded as having valid DKIM, with the
understanding that a subsequent run of this script with C<--all-domains>
will remove invalid entries. (This is to ensure that DKIM signing doesnāt
fail temporarily while the cache is being created.)
C<--initialize> excludes C<--domain> and implies C<--all-domains>.
This is only useful in rare cases and shouldnāt ordinarily be done.

=back

=cut

use Try::Tiny;

use parent qw( Cpanel::HelpfulScript );

use constant _OPTIONS => (
    'initialize',
    'all-domains',
    'domain=s@',
);

__PACKAGE__->new(@ARGV)->run() if !caller;

sub run {
    my ($self) = @_;

    my $given_domains_ar = $self->getopt('domain');

    my $every_domain_yn = $self->getopt('all-domains');

    my $init_yn = $self->getopt('initialize');

    if ($every_domain_yn) {
        if ( $init_yn || $given_domains_ar ) {
            die $self->help('ā--all-domainsā excludes ā--initializeā and ā--domainā.');
        }
    }
    elsif ($init_yn) {
        if ($given_domains_ar) {
            die $self->help('ā--initializeā excludes ā--all-domainsā and ā--domainā.');
        }
    }
    elsif ( !$given_domains_ar ) {
        die $self->help('No arguments given!');
    }

    require Cpanel::DKIM::ValidityCache;

    my $all_domains_ar = _load_all_domains();

    my $cache_all_ar = Cpanel::DKIM::ValidityCache->get_all();

    my %old_lookup;

    my $init_obj;

    if ($cache_all_ar) {
        if ($init_yn) {
            $self->say('The cache is already initialized.');
            return;
        }

        %old_lookup = map { $_ => undef } @$cache_all_ar;

        require Cpanel::Set;
        my @stale = Cpanel::Set::difference(
            [ keys %old_lookup ],
            $all_domains_ar,
        );

        for (@stale) {
            $self->say("Removing stale entry for ā$_ā ā¦");
            _write_or_warn( 'unset', $_ );
        }
    }
    elsif ($init_yn) {
        require Cpanel::DKIM::ValidityCache::TempDir;
        $init_obj = Cpanel::DKIM::ValidityCache::TempDir->new();
    }

    $_ = 0 for my ( $added, $removed );

    for my $domain ( sort @$all_domains_ar ) {

        # Skip all wildcards.
        next if 0 == index( $domain, '*' );

        if ( !$every_domain_yn && !$init_yn ) {
            my $proceed_yn = grep { $_ eq $domain } @$given_domains_ar;

            next if !$proceed_yn;
        }

        if ($init_yn) {
            _write_