Move mailer details into a separate table

This commit is contained in:
2023-07-07 23:59:04 +10:00
parent ad4ea699a5
commit ccdce6bb62
12 changed files with 274 additions and 112 deletions

View File

@@ -9,10 +9,11 @@ use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use App\Classes\Protocol;
use App\Classes\Protocol\{Binkp,EMSI};
use App\Classes\Sock\SocketClient;
use App\Classes\Sock\SocketException;
use App\Models\{Address,Setup};
use App\Models\{Address,Mailer,Setup};
class AddressPoll implements ShouldQueue
{
@@ -21,11 +22,12 @@ class AddressPoll implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private Address $ao;
private ?Mailer $mo;
public function __construct(Address $ao)
public function __construct(Address $ao,Mailer $mo=NULL)
{
// Some checks
$this->ao = $ao;
$this->mo = $mo;
}
/**
@@ -33,34 +35,49 @@ class AddressPoll implements ShouldQueue
*/
public function handle()
{
if ((! $this->ao->system->mailer_address) || (! $this->ao->system->mailer_port))
if (! $this->ao->system->mailer_preferred->count() || ($this->mo && (! $this->ao->system->mailer_preferred->find($this->mo))))
throw new \Exception(sprintf('Unable to poll [%s] missing mailer details',$this->ao->ftn));
try {
$client = SocketClient::create($this->ao->system->mailer_address,$this->ao->system->mailer_port);
} catch (SocketException $e) {
Log::error(sprintf('%s:! Unable to connect to [%s]: %s',self::LOGKEY,$this->ao->ftn,$e->getMessage()));
abort(500);
}
foreach ($this->ao->system->mailer_preferred as $o) {
// If we chose a protocol, skip to find the mailer details for it
if ($this->mo && ($o->id !== $this->mo->id))
continue;
switch ($this->ao->system->mailer_type) {
case Setup::O_BINKP:
$o = new Binkp(Setup::findOrFail(config('app.id')));
$o->session(Binkp::SESSION_BINKP,$client,$this->ao);
Log::info(sprintf('%s:- Starting a [%s] session to [%s:%d]',self::LOGKEY,$o->name,$this->ao->system->address,$o->pivot->port));
try {
$client = SocketClient::create($this->ao->system->address,$o->pivot->port);
} catch (SocketException $e) {
Log::error(sprintf('%s:! Unable to connect to [%s]: %s',self::LOGKEY,$this->ao->ftn,$e->getMessage()));
abort(500);
}
switch ($o->name) {
case 'BINKP':
$s = new Binkp(Setup::findOrFail(config('app.id')));
$session = Binkp::SESSION_BINKP;
break;
case 'EMSI':
$s = new EMSI(Setup::findOrFail(config('app.id')));
$session = EMSI::SESSION_AUTO;
break;
default:
throw new \Exception(sprintf('Node [%s] has a mailer type that is unhandled',$this->ao->ftn));
}
if (($s->session($session,$client,$this->ao) & Protocol::S_MASK) === Protocol::S_OK) {
Log::info(sprintf('%s:= Connection ended successfully with [%s]',self::LOGKEY,$client->address_remote));
break;
case Setup::O_EMSI:
$o = new EMSI(Setup::findOrFail(config('app.id')));
$o->session(EMSI::SESSION_AUTO,$client,$this->ao);
break;
default:
throw new \Exception(sprintf('Node [%s] has a mailer type that is unhandled',$this->ao->ftn));
} else {
Log::alert(sprintf('%s:! Connection failed to [%s]',self::LOGKEY,$client->address_remote));
}
}
Log::info(sprintf('%s:Connection ended: %s',self::LOGKEY,$client->address_remote));
}
}