0
  "   	ˆl–Ðz¾”Jj"TÒ�zàœ¸”Å£¿ƒy·ÙÃÂ !‘    	#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - scripts/import_exim_data                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::import_exim_data;

=encoding utf-8

=head1 NAME

import_exim_data

=head1 SYNOPSIS

    import_exim_data ( --help | <logfile1> [ <logfile2> .. ] )

=head1 DESCRIPTION

This command re-populates the email statistics database based on the contents
of the log files at the given paths.

The format of the data in each exim log file should conform to the data format
of F</var/log/exim_mainlog>. Any of the files may be in gzip format, in which
case the filename must end with C<.gz>.

=cut

use strict;
use warnings;

use parent qw( Cpanel::HelpfulScript );

use Cpanel::Config::LoadCpConf          ();
use Cpanel::EximStats::ImportInProgress ();
use Cpanel::Finally                     ();
use Cpanel::IONice                      ();
use Cpanel::IP::Loopback                ();
use Cpanel::Locale                      ();
use Cpanel::PIDFile                     ();
use Cpanel::Sys::Load                   ();
use Cpanel::TailWatch                   ();    # PPI USE OK - inline below
use Cpanel::TailWatch::Eximstats        ();
use Cpanel::TimeHiRes                   ();
use Cpanel::IO                          ();
use Cpanel::Timezones                   ();

use Try::Tiny;

use constant _OPTIONS => ();

use constant _ACCEPT_UNNAMED => 1;

{

    package FakeTailWatch;    # Because Cpanel::TailWatch::Eximstats::process_line()
                              # thinks it needs a tailwatch obj

    use parent 'Cpanel::TailWatch';    # PPI USE OK - needed to mock this properly

    sub new {
        my ($class) = @_;
        return bless {}, $class;
    }

    sub register_module        { }
    sub register_reload_module { }
    sub register_action_module { }
    *_is_loopback = sub {
        my $self = shift;

        goto \&Cpanel::IP::Loopback::is_loopback;
    };
    sub log              { shift @_; printf STDERR "@_\n" }
    sub log_sql          { shift @_; printf STDERR "@_\n" }
    sub debug            { shift @_; printf STDERR "@_\n" }
    sub error            { shift @_; printf STDERR "@_\n" }
    sub clear_data_cache { }
    {
        no warnings 'once';
        no warnings 'redefine';
        *Cpanel::TailWatch::Eximstats::_send_limit_exceeded_notification = sub { return 1; };
        *Cpanel::TailWatch::Eximstats::_validate_email_limits_data       = sub { return 1; };
    }
}

$Cpanel::TailWatch::Eximstats::LOG_DUPLICATES = 0;

# Our for tests
our $LOOP_SIZE = 10000;

our $MAX_SLEEP_TIME = 120;    # seconds

__PACKAGE__->new(@ARGV)->run() unless caller();

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

    Cpanel::PIDFile->do(
        $Cpanel::EximStats::ImportInProgress::PATH,
        sub { $self->script( $self->getopt_unnamed() ) },
    );

    return;
}

sub script {
    my ( $self, @infiles ) = @_;

    local $ENV{'TZ'} = Cpanel::Timezones::calculate_TZ_env();

    if ( !@infiles ) {
        die $self->help( Cpanel::Locale->get_handle()->maketext('Give at least one filesystem path.') );
    }

    my $tailwatch_obj = FakeTailWatch->new;
    $tailwatch_obj->init_global_share();
    my $logfile = '/dev/null';
    my $now     = time;

    Cpanel::TailWatch::Eximstats->init($tailwatch_obj);

    my $eximstats = Cpanel::TailWatch::Eximstats->new( $tailwatch_obj, 'buffered_sql' => 1, 'import' => 1 );

    my $cpconf_ref = Cpanel::Config::LoadCpConf::loadcpconf();

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

    setpriority( 0, 0, 19 );

    $eximstats->_ensure_dbh();

    $eximstats->{'dbh'}->do(