File: //usr/local/mailchannels/hooks/PostAccountsSuspend.php
<?php
namespace MailChannels;
class PostAccountsSuspend extends Hook {
protected EximSuspendedListReader $eximSuspendedReader;
/**
* @throws StorageNotSetException
* @throws FileNotReadableException
*/
public function __construct(string $eximPath = "/etc/exim_suspended_list")
{
parent::__construct();
$this->eximSuspendedReader = new EximSuspendedListReader($eximPath);
}
public function execute($hookData) {
return array(true, "not deprovisioning any MailChannels protected domains");
}
protected function whostMgrAccountsDeProvisionEximCheck($user) {
$isInboundEnabled = $this->inboundEnabled();
if ($isInboundEnabled) {
$autoDomainDeProvisioning = $this->inboundConfig->autoDomainDeProvisioning();
if ($autoDomainDeProvisioning) {
try {
$domains = $this->outboundSMTPService->getAllDomainsForUser($user);
$result = true;
$message = "";
foreach ($domains as $domain) {
$domainName = $domain->getDomain();
if ($this->eximSuspendedReader->getAction($domainName) == 'fail') {
try {
$deprovisionResult = $this->inboundSmtpService->deProvisionDomain($domainName);
if ($deprovisionResult) {
$message .= " domain '$domainName' no longer uses MailChannels;";
} else {
$message .= " action not taken; domain '$domainName' not found;";
}
} catch (InboundSMTPServiceInternalErrorException $e) {
$errorMessage = $e->getMessage() . ": " . $e->getTraceAsString();
$message .= " unexpected error deprovisioning domain '$domainName': $errorMessage;";
if( !is_a($e->getPrevious(), get_class(new InboundApiUnprocessableException())) ) {
$result = false;
}
} catch (\Exception $e) {
$errorMessage = $e->getMessage() . ": " . $e->getTraceAsString();
$message .= " unexpected error deprovisioning domain '$domainName': $errorMessage;";
$result = false;
}
} else {
$message .= " action not taken; the Email Delivery Behavior for '$domainName' is not set to 'Reject messages at SMTP time';";
}
}
return array($result, $message);
} catch (\Exception $e) {
$errorMessage = $e->getTraceAsString();
$message = "unexpected error getting domains for user '$user' (deprovisioning): $errorMessage";
return array(false, $message);
}
}
$isAutoProtectionEnabled = var_export($autoDomainDeProvisioning, true);
return array(true, "action not taken; autoDomainDeProvisioning: $isAutoProtectionEnabled");
}
return array(true, "action not taken; inbound not enabled");
}
public static function category() {
return 'Whostmgr';
}
public static function event() {
return 'Accounts::suspendacct';
}
public static function stage() {
return 'post';
}
}