Skip to content

Instantly share code, notes, and snippets.

@troglodyne
Last active November 2, 2017 23:33
Show Gist options
  • Save troglodyne/7c664ba398d0a0b960f495755ed2b394 to your computer and use it in GitHub Desktop.
Save troglodyne/7c664ba398d0a0b960f495755ed2b394 to your computer and use it in GitHub Desktop.
Script for disabling iContact notifications of the types you pass in for ALL cPanel Accounts
#!/usr/local/cpanel/3rdparty/bin/perl
package skiddie::iContactDisabler4Users;
use strict;
use warnings;
use Cpanel::CustInfo::Model ();
use Cpanel::ArrayFunc::Uniq ();
use JSON::MaybeXS ();
use File::Slurp ();
use File::Copy ();
use Capture::Tiny qw{capture};
use Try::Tiny;
exit script() unless caller;
sub script {
die "Must be ran as root!" if $>;
my @args = grep { $_ !~ m/-v|--verbose/ } @ARGV;
my $verbose = scalar( @ARGV ) != scalar( @args );
help() if !scalar( @args ) || grep { m/-h|--help/ } @args;
# Get the list of valid notification types, barf if what you passed in is garbage
my @valid_notification_types = grep { $_ !~ m/pushbullet_access_token|email|second_email/ } keys( %{ Cpanel::CustInfo::Model::get_contact_fields() } );
my @invalid_types_passed_in;
foreach my $arg ( @args ) {
push( @invalid_types_passed_in, $arg ) if !grep { $arg eq $_ } @valid_notification_types;
}
die "Invalid notification type(s) passed in: " . join( ", ", @invalid_types_passed_in ) . "\nValid notification types:\n " . join( "\n ", @valid_notification_types ) . "\n"
if scalar( @invalid_types_passed_in );
# Get the users per cPanel
my @listaccts_call = qw{/usr/local/cpanel/bin/whmapi1 --output=json listaccts};
print "[INFO] exec `" . join( " ", @listaccts_call ) . "`\n" if $verbose;
my ( $stdout, $stderr ) = capture { system(@listaccts_call); };
my $decoded;
try {
$decoded = JSON::MaybeXS::decode_json( $stdout );
die "Error indicated in WHMAPI return!" if !$decoded->{'metadata'}{'result'};
die "No accounts returned by listaccts!" if !scalar( @{ $decoded->{'data'}{'acct'} } );
}
catch {
warn $_ if $verbose;
die "[FATAL] listaccts attempt failed! Printing output of WHM API 1 call:\n" . join( " ", @listaccts_call ) . "\nSTDOUT: $stdout\nSTDERR:$stderr\n";
};
# Get the list of uses per /var/cpanel/users
my @user_conf_files = glob('/var/cpanel/users/*');
# Filter out non-users (I know, eww)
@user_conf_files = grep { my $file = $_; grep { "/var/cpanel/users/$_->{'user'}" eq $file } @{ $decoded->{'data'}{'acct'} } } @user_conf_files;
# Process the users
foreach my $file ( @user_conf_files ) {
my @conf_lines = File::Slurp::read_file($file);
my @old_lines = @conf_lines;
foreach my $i ( 0..scalar(@conf_lines) - 1 ) {
my ( $found_arg ) = grep { index( $conf_lines[$i], $_ ) != -1 } @args;
if( $found_arg ) {
# Replace it with 0
$conf_lines[$i] = "$found_arg=0\n";
}
}
# Detect whether any changes happened
my @combined = Cpanel::ArrayFunc::Uniq::uniq( @old_lines, @conf_lines );
if( scalar( @combined ) > scalar( @old_lines ) ) {
print "[INFO] Backing up $file...\n" if $verbose;
# Back that up... yeah
File::Copy::copy( $file, "$file.bak" );
try {
print "[INFO] Writing out changes to $file...\n" if $verbose;
File::Slurp::write_file( $file, { 'atomic' => 1 }, @conf_lines );
}
catch {
warn $_ if $verbose;
print "[ERROR] Writing out config file '$file' failed!\n Attempting to restore backup, but if this fails you'll have to manually intervene.\n";
File::Copy::copy( "$file.bak", $file );
};
}
elsif($verbose) {
print "[INFO] Nothing to do for $file. Skipping...\n";
}
}
return 0;
}
sub help {
print "disable_all_user_notifications_of_types.pl -- Disable whatever notification type you want to disable for ALL cPanel Accounts.\n\nUsage:\n\n";
print " disable_all_user_notifications_of_types.pl [FLAGS] option2disable1 option2disable2 ...\n\n";
print " FLAGS:\n --help: Show this message\n --verbose: Show extended information about what this script is doing.\n\n";
print "Normally this script will produce no output without the --verbose flag unless an error occurs.\n";
exit 0;
}
0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment