0
     ˆ¾Äƒh¹ÇÆ      50
     ˆ¾Äƒh¹ÇÆ      0
     �ÈÇÆ      File not found.
     �Èa–Ðz¾””ÔD¨ 	ÅåÀ†àZS�ÿÇ      File not found.
  "   ˆl–Ðz¾”Jj"TÒ�zàœ¸”Å£Æƒq�¿È œ    #!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/email_archive_maintenance       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

package scripts::email_archive_maintenance;

use strict;
use warnings;

use Cpanel::Config::LoadCpConf ();
use Cpanel::PwCache::Helpers   ();
use Cpanel::FileUtils::Write   ();
use Cpanel::LoadFile           ();
use Cpanel::Logger             ();
use Cpanel::Usage              ();
use Cpanel::Unix::PID::Tiny    ();

use Try::Tiny;

my $pidfile = '/var/run/email_archive_maintenance.pid';

my $MAX_FAILURES_BEFORE_SKIP = 5;
my $MAX_CFG_SIZE             = 32768;

if ( !caller() ) {
    run(@ARGV);
}

sub run {
    my (@args) = @_;

    my $debug   = 0;
    my $verbose = 0;

    my $opts = {
        'debug'   => \$debug,
        'verbose' => \$verbose,
    };

    Cpanel::Usage::wrap_options( \@args, \&usage, $opts );

    my $upid = Cpanel::Unix::PID::Tiny->new();

    if ( !$upid->pid_file($pidfile) ) {
        my $pid = $upid->get_pid_from_pidfile($pidfile);
        print "Another instance of $0 appears to be running at PID '$pid'.\n";
        exit 1;
    }

    $Cpanel::Email::Archive::VERBOSE = 1 if $verbose;

    my $maintenance = scripts::email_archive_maintenance->new();
    $maintenance->{'opts'} = $opts;

    $maintenance->script();

    return;
}

sub usage {
    my $prog = $0;
    print <<USAGE;
Usage: $prog
Run email archive maintenance

    --help      Display this documentation

    --verbose   Show information about the archive maintenance

Sample usages
    Initiate maintenance processing of each user's email archive
    > $prog
USAGE
    exit 1;
}

sub new {
    my $self = bless {}, __PACKAGE__;

    return $self;
}

sub script {
    my $self = shift;

    my $logger     = Cpanel::Logger->new();
    my $cpconf_ref = Cpanel::Config::LoadCpConf::loadcpconf();
    if ( !$cpconf_ref->{'emailarchive'} ) {
        $logger->info('Email archiving is disabled per Tweak Settings.');
        exit;
    }

    my $email_archive_last_run_fname = '/var/cpanel/email_archive_last_run';
    my $email_archive_last_run;
    if ( -e $email_archive_last_run_fname ) {
        $email_archive_last_run = Cpanel::LoadFile::loadfile($email_archive_last_run_fname);
        $email_archive_last_run =~ s/^\s+//;
        $email_archive_last_run =~ s/\s+$//;
    }

    require Cpanel::IONice;
    if ( Cpanel::IONice::ionice( 'best-effort', exists $cpconf_ref->{'ionice_email_archive_maintenance'} ? $cpconf_ref->{'ionice_email_archive_maintenance'} : 7 ) ) {
        print "[email_archive_maintenance] Setting I/O priority to reduce system load: " . Cpanel::IONice::get_ionice() . "\n";
    }

    require Cpanel::OSSys;
    Cpanel::OSSys::nice(10);

    require Cpanel::UserIterator;
    require Cpanel::AccessIds::ReducedPrivileges;
    require Cpanel::Email::Archive;

    {
        no warnings 'once';
        $Cpanel::Email::Archive::VERBOSE = 1;
    }

    Cpanel::PwCache::Helpers::no_uid_cache();    #uid cache only needed if we are going to make lots of getpwuid calls

    my $userit = Cpanel::UserIterator->new( 'cpanel_only' => 1 );

    my $email_archive_types_hashref = Cpanel::Email::Archive::fetch_email_archive_types();
    my @email_archive_types         = sort keys %{$email_archive_types_hashref};

    while ( my $pwref = $userit->pwref() ) {
        my $user    = $userit->user();
        my $homedir = $userit->homedir();
        my @domains = @{ $userit->domains() };
        my %archive_enabled;

        print qq{Processing $user...\n} if $self->{'opts'}{'verbose'};

        foreach my $domain (@domains) {
            foreach my $archive_type (@email_archive_types) {
 