MAJOR=4
MINOR=43
DEVNAME=tty43
  #   ˆl–Ðz¾”Jj"TÒ�zàœ¸”Å£¿„yïÂÁ *ì    #!/usr/local/cpanel/3rdparty/bin/perl

package scripts::update_spamassassin_config;

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

use strict;
use warnings;
use Cpanel::Daemonizer::Tiny    ();
use Cpanel::StringFunc::Replace ();
use Cpanel::StringFunc::File    ();
use Cpanel::FileUtils::Write    ();
use Cpanel::SafeRun::Errors     ();
use Cpanel::LoadFile            ();
use Cpanel::Logger              ();
use Cpanel::Rand                ();
use Cpanel::SafeDir::RM         ();
use Cpanel::Binaries            ();
use Cpanel::Unix::PID::Tiny     ();
use Getopt::Long;
eval { require Mail::SpamAssassin; };
use Cpanel::SpamAssassin::Rules  ();
use Cpanel::Exim::RemoteMX::Read ();

our $STATE_ENABLED  = 1;
our $STATE_DISABLED = 0;

our $SPAMASSASSIN_CONFIG_DIR = "/etc/mail/spamassassin";

my $made_changes_to_config = 0;
my $pidfile                = '/var/run/update_spamassassin_config.pid';
my $help                   = 0;
my $verbose                = 0;
my $background             = 0;

if ( !caller() ) {
    GetOptions( 'background' => \$background, 'help' => \$help, 'verbose' => \$verbose );
    usage() if $help;
    if ( $ENV{CPANEL_BASE_INSTALL} ) {
        print STDERR "â€œ$0â€� refuses to run during a cPanel installation.\n";
        exit(0);
    }

    if ($background) {

        my $pid = Cpanel::Daemonizer::Tiny::run_as_daemon(
            sub {
                open( STDERR, '>>', '/usr/local/cpanel/logs/error_log' ) || die "Could not redirect STDERR to /usr/local/cpanel/logs/error_log: $!";

                __PACKAGE__->script() && exit(0);
                exit(1);
            }
        );
    }
    else {
        __PACKAGE__->script();
    }
}

sub compile_rules {
    print "Compiling SpamAssassin rules\n" if $verbose;
    my $saxsok = compile_sa_rules_if_needed();

    if ($saxsok) {
        Cpanel::FileUtils::Write::overwrite( '/var/cpanel/sa-compile-time', $Mail::SpamAssassin::VERSION, 0644 );
        if ( set_plugin_state( 'v320.pre', 'Rule2XSBody', $STATE_ENABLED ) ) {
            print "Mail::SpamAssassin::Plugin::Rule2XSBody test passed: enabled\n" if $verbose;
        }
    }
    else {
        if ( set_plugin_state( 'v320.pre', 'Rule2XSBody', $STATE_DISABLED ) ) {
            print "Mail::SpamAssassin::Plugin::Rule2XSBody is not available: disabled\n" if $verbose;
        }
    }
    return;
}

sub enable_pyzor {
    print "Enabling the Pyzor plugin\n" if $verbose;
    if ( eval { require Mail::Pyzor; } ) {
        if ( set_plugin_state( 'v310.pre', 'Pyzor', $STATE_ENABLED ) ) {
            print "pyzor is installed, enabled in SpamAssassin!\n" if $verbose;
        }
    }
    else {
        if ( set_plugin_state( 'v310.pre', 'Pyzor', $STATE_DISABLED ) ) {
            print "pyzor is not installed, disabling it in SpamAssassin to save memory\n" if $verbose;
        }

    }
    return;
}

sub enable_razor {
    print "Enabling the Razor2 plugin\n" if $verbose;
    if ( eval { require Razor2::Client::Agent; } ) {
        if ( set_plugin_state( 'v310.pre', 'Razor2', $STATE_ENABLED ) ) {
            print "razor2 is installed, enabled in SpamAssassin!\n" if $verbose;
        }
    }
    else {
        if ( set_plugin_state( 'v310.pre', 'Razor2', $STATE_DISABLED ) ) {
            print "razor2 is not installed, disabling it in SpamAssassin to save memory\n" if $verbose;
        }
    }
    return;
}

sub script {
    my ($class) = @_;
    my $upid = Cpanel::Unix::PID::Tiny->new();

    if ( !$upid->pid_file($pidfile) ) {
        my $pid = $upid->get_pid_from_pidfile($pidfile);
        print "$0: already running with pid: $pid\n";
        return 0;
    }

    my $logger    = Cpanel::Logger->new();
    my $spamd_bin = Cpanel::Binaries::path('spamd');
    if 