2021-06-20 13:03:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2021-07-18 12:10:21 +00:00
|
|
|
use Carbon\Carbon;
|
2021-07-30 14:35:52 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2021-06-20 13:03:20 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-06-25 06:42:12 +00:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2022-03-14 11:28:54 +00:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2021-09-06 13:39:32 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2022-02-13 00:27:12 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2021-06-20 13:03:20 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
use App\Classes\FTN\Packet;
|
2021-06-20 13:03:20 +00:00
|
|
|
use App\Http\Controllers\DomainController;
|
2022-01-01 05:59:35 +00:00
|
|
|
use App\Traits\ScopeActive;
|
2021-06-20 13:03:20 +00:00
|
|
|
|
2021-11-20 00:11:17 +00:00
|
|
|
/**
|
|
|
|
* @todo Need to stop this from happening:
|
|
|
|
* In this example nn:3/1 can be defined 3 different ways.
|
|
|
|
* + id | zone_id | region_id | host_id | node_id | point_id | status | role | system_id | hub_id
|
|
|
|
* + ----+---------+-----------+---------+---------+----------+--------+------+-----------+--------
|
|
|
|
* + 533 | 6 | 3 | 0 | 1 | 0 | | 4 | 1 |
|
2021-11-20 00:50:45 +00:00
|
|
|
* + 534 | 6 | n | 3 | 1 | 0 | | 2 | 1 |
|
2021-11-20 00:11:17 +00:00
|
|
|
* + 535 | 6 | 0 | 3 | 1 | 0 | | 2 | 1 |
|
|
|
|
*/
|
2021-06-20 13:03:20 +00:00
|
|
|
class Address extends Model
|
|
|
|
{
|
2022-02-13 00:27:12 +00:00
|
|
|
private const LOGKEY = 'MA-';
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
use ScopeActive,SoftDeletes;
|
|
|
|
|
|
|
|
protected $with = ['zone'];
|
2021-06-24 10:16:37 +00:00
|
|
|
|
2022-11-01 05:39:58 +00:00
|
|
|
// http://ftsc.org/docs/frl-1028.002
|
|
|
|
public const ftn_regex = '([0-9]+):([0-9]+)/([0-9]+)(\.([0-9]+))?(@([a-z0-9\-_~]{0,8}))?';
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
public const NODE_ZC = 1<<0; // Zone
|
|
|
|
public const NODE_RC = 1<<1; // Region
|
|
|
|
public const NODE_NC = 1<<2; // Host
|
|
|
|
public const NODE_HC = 1<<3; // Hub
|
|
|
|
public const NODE_ACTIVE = 1<<4; // Node
|
|
|
|
public const NODE_PVT = 1<<5; // Pvt
|
|
|
|
public const NODE_HOLD = 1<<6; // Hold
|
|
|
|
public const NODE_DOWN = 1<<7; // Down
|
|
|
|
public const NODE_POINT = 1<<8; // Point
|
|
|
|
public const NODE_UNKNOWN = 1<<15; // Unknown
|
|
|
|
|
2022-11-11 12:04:28 +00:00
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
// For indexes when deleting, we need to change active to FALSE
|
|
|
|
static::softDeleted(function($model) {
|
|
|
|
Log::debug(sprintf('%s:Deleting [%d], updating active to FALSE',self::LOGKEY,$model->id));
|
|
|
|
|
|
|
|
$model->active = FALSE;
|
|
|
|
$model->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
/* SCOPES */
|
|
|
|
|
2023-06-23 11:28:29 +00:00
|
|
|
public function scopeActive($query)
|
|
|
|
{
|
|
|
|
return $query->select($this->getTable().'.*')
|
|
|
|
->join('zones',['zones.id'=>'addresses.zone_id'])
|
|
|
|
->join('domains',['domains.id'=>'zones.domain_id'])
|
|
|
|
->where('addresses.active',TRUE)
|
|
|
|
->where('zones.active',TRUE)
|
|
|
|
->where('domains.active',TRUE)
|
|
|
|
->orderBy('domains.name')
|
|
|
|
->FTNorder();
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
public function scopeTrashed($query)
|
|
|
|
{
|
|
|
|
return $query->select($this->getTable().'.*')
|
|
|
|
->join('zones',['zones.id'=>'addresses.zone_id'])
|
|
|
|
->join('domains',['domains.id'=>'zones.domain_id'])
|
|
|
|
->orderBy('domains.name')
|
|
|
|
->withTrashed()
|
|
|
|
->FTNorder();
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
public function scopeFTNOrder($query)
|
|
|
|
{
|
|
|
|
return $query
|
|
|
|
->orderBy('region_id')
|
2021-06-25 06:42:12 +00:00
|
|
|
->orderBy('host_id')
|
2021-06-24 13:09:09 +00:00
|
|
|
->orderBy('node_id')
|
|
|
|
->orderBy('point_id');
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/* RELATIONS */
|
|
|
|
|
2021-07-04 11:47:23 +00:00
|
|
|
/**
|
2021-07-26 11:21:58 +00:00
|
|
|
* Find children dependent on this record
|
2021-07-04 11:47:23 +00:00
|
|
|
*/
|
2021-07-02 13:19:50 +00:00
|
|
|
public function children()
|
|
|
|
{
|
2021-07-26 11:21:58 +00:00
|
|
|
// We have no session data for this address, by definition it has no children
|
|
|
|
if (! $this->session('sespass'))
|
|
|
|
return $this->hasMany(self::class,'id','void');
|
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
if (! $this->session('default')) {
|
|
|
|
switch ($this->role) {
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_ZC:
|
2021-08-09 13:35:22 +00:00
|
|
|
$children = self::select('addresses.*')
|
|
|
|
->where('zone_id',$this->zone_id);
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
break;
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_RC:
|
2021-08-09 13:35:22 +00:00
|
|
|
$children = self::select('addresses.*')
|
|
|
|
->where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',$this->region_id);
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
break;
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_NC:
|
2021-08-09 13:35:22 +00:00
|
|
|
$children = self::select('addresses.*')
|
|
|
|
->where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',$this->region_id)
|
|
|
|
->where('host_id',$this->host_id);
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
break;
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_HC:
|
2021-08-09 13:35:22 +00:00
|
|
|
// Identify our children.
|
|
|
|
$children = self::select('addresses.*')
|
|
|
|
->where('hub_id',$this->id);
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
break;
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_UNKNOWN:
|
|
|
|
case self::NODE_ACTIVE:
|
|
|
|
case self::NODE_POINT:
|
2022-11-19 11:15:08 +00:00
|
|
|
case self::NODE_PVT:
|
2021-08-09 13:35:22 +00:00
|
|
|
// Nodes dont have children, but must return a relationship instance
|
|
|
|
return $this->hasOne(self::class,NULL,'void');
|
2021-07-26 11:21:58 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
default:
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception('Unknown role: '.serialize($this->role));
|
2021-08-09 13:35:22 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-08-09 13:35:22 +00:00
|
|
|
} else {
|
|
|
|
$children = self::select('addresses.*')
|
|
|
|
->where('zone_id',$this->zone_id);
|
2021-07-26 11:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove any children that we have session details for (SAME AS HC)
|
|
|
|
$sessions = self::select('hubnodes.*')
|
|
|
|
->join('system_zone',['system_zone.system_id'=>'addresses.system_id','system_zone.zone_id'=>'addresses.zone_id'])
|
|
|
|
->join('addresses as hubnodes',['hubnodes.zone_id'=>'addresses.zone_id','hubnodes.id'=>'addresses.id'])
|
|
|
|
->where('addresses.zone_id',$this->zone_id)
|
|
|
|
->where('addresses.system_id','<>',$this->system_id)
|
|
|
|
->whereIN('addresses.system_id',$children->get()->pluck('system_id'));
|
|
|
|
|
|
|
|
// For each of the session, identify their children
|
|
|
|
$session_kids = collect();
|
|
|
|
foreach ($sessions->get() as $so)
|
|
|
|
$session_kids = $session_kids->merge(($x=$so->children) ? $x->pluck('id') : []);
|
|
|
|
|
|
|
|
// ZC's receive all mail, except for defined nodes, and defined hubs/hosts/rcs
|
|
|
|
return $this->hasMany(self::class,'zone_id','zone_id')
|
|
|
|
->whereIn('id',$children->get()->pluck('id')->toArray())
|
|
|
|
->whereNotIn('id',$sessions->get()->pluck('id')->toArray())
|
|
|
|
->whereNotIn('id',$session_kids->toArray())
|
|
|
|
->where('system_id','<>',$this->system_id)
|
|
|
|
->select('addresses.*')
|
|
|
|
->orderBy('region_id')
|
|
|
|
->orderBy('host_id')
|
|
|
|
->orderBy('node_id')
|
|
|
|
->orderBy('point_id')
|
|
|
|
->with(['zone.domain']);
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
/**
|
|
|
|
* Echoareas this address is subscribed to
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
2021-08-25 12:13:49 +00:00
|
|
|
public function echoareas()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Echoarea::class)
|
|
|
|
->withPivot(['subscribed']);
|
|
|
|
}
|
|
|
|
|
2022-01-15 02:06:15 +00:00
|
|
|
/**
|
|
|
|
* Echomails that this address has seen
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function echomails()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Echomail::class,'echomail_seenby')
|
2022-11-01 11:24:36 +00:00
|
|
|
->withPivot(['sent_at','export_at','packet']);
|
|
|
|
}
|
|
|
|
|
2022-11-25 10:44:03 +00:00
|
|
|
public function echomail_from()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Echomail::class,'fftn_id','id');
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
/**
|
|
|
|
* Files that this address has seen
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function files()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(File::class,'file_seenby')
|
|
|
|
->withPivot(['sent_at','export_at']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Echoareas this address is subscribed to
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
|
|
*/
|
|
|
|
public function fileareas()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany(Filearea::class)
|
|
|
|
->withPivot(['subscribed']);
|
2022-01-15 02:06:15 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
/**
|
|
|
|
* Who we send this systems mail to.
|
|
|
|
*
|
|
|
|
* @return Address|null
|
2023-06-27 07:39:11 +00:00
|
|
|
* @throws \Exception
|
2021-07-26 11:21:58 +00:00
|
|
|
*/
|
|
|
|
public function parent(): ?Address
|
|
|
|
{
|
|
|
|
// If we are have session password, then we dont have a parent
|
|
|
|
if ($this->session('sespass'))
|
|
|
|
return $this;
|
|
|
|
|
|
|
|
switch ($this->role) {
|
2021-08-09 13:35:22 +00:00
|
|
|
// ZCs dont have parents, but we may have a default
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_ZC:
|
2021-08-09 13:35:22 +00:00
|
|
|
if (($x=$this->zone->systems->where('pivot.default',TRUE))->count())
|
2022-01-24 11:56:13 +00:00
|
|
|
return $x->first()->match($this->zone,255)->first();
|
2021-08-09 13:35:22 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
2021-07-26 11:21:58 +00:00
|
|
|
|
|
|
|
// RC
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_RC:
|
2021-07-26 11:21:58 +00:00
|
|
|
$parent = self::where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',0)
|
|
|
|
->where('host_id',0)
|
|
|
|
->where('node_id',0)
|
|
|
|
->single();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Hosts
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_NC:
|
2021-07-26 11:21:58 +00:00
|
|
|
// See if we have a RC
|
|
|
|
$parent = self::where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',$this->region_id)
|
|
|
|
->where('host_id',0)
|
|
|
|
->where('node_id',0)
|
|
|
|
->single();
|
|
|
|
|
|
|
|
if (! $parent) {
|
|
|
|
// See if we have a RC
|
|
|
|
$parent = self::where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',0)
|
|
|
|
->where('host_id',0)
|
|
|
|
->where('node_id',0)
|
|
|
|
->single();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Hubs
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_HC:
|
2021-07-26 11:21:58 +00:00
|
|
|
// Normal Nodes
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_ACTIVE:
|
2021-07-26 11:21:58 +00:00
|
|
|
// If we are a child of a hub, then check our hub
|
|
|
|
$parent = (($this->hub_id)
|
|
|
|
? self::where('id',$this->hub_id)
|
|
|
|
: self::where('zone_id',$this->zone_id)
|
|
|
|
->where('region_id',$this->region_id)
|
|
|
|
->where('host_id',$this->host_id)
|
|
|
|
->where('node_id',0))
|
|
|
|
->single();
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_UNKNOWN:
|
|
|
|
case self::NODE_POINT:
|
|
|
|
case self::NODE_DOWN:
|
2021-07-26 11:21:58 +00:00
|
|
|
// @todo Points - if the boss is defined, we should return it.
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
default:
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception('Unknown role: '.serialize($this->role));
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
2021-07-26 11:21:58 +00:00
|
|
|
|
|
|
|
return $parent?->parent();
|
2021-07-02 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
public function system()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(System::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function zone()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Zone::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2023-06-23 11:28:29 +00:00
|
|
|
/**
|
|
|
|
* Return if this address is active
|
|
|
|
*
|
|
|
|
* @param bool $value
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function getActiveAttribute(bool $value): bool
|
|
|
|
{
|
|
|
|
return $value && $this->zone->active && $this->zone->domain->active;
|
|
|
|
}
|
|
|
|
|
2021-06-20 13:03:20 +00:00
|
|
|
/**
|
|
|
|
* Render the node name in full 5D
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTNAttribute(): string
|
2021-06-20 13:03:20 +00:00
|
|
|
{
|
2021-06-26 01:48:55 +00:00
|
|
|
return sprintf('%s@%s',$this->getFTN4DAttribute(),$this->zone->domain->name);
|
|
|
|
}
|
|
|
|
|
2021-08-29 01:48:27 +00:00
|
|
|
public function getFTN2DAttribute(): string
|
|
|
|
{
|
|
|
|
return sprintf('%d/%d',$this->host_id ?: $this->region_id,$this->node_id);
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTN3DAttribute(): string
|
2021-06-26 01:48:55 +00:00
|
|
|
{
|
|
|
|
return sprintf('%d:%d/%d',$this->zone->zone_id,$this->host_id ?: $this->region_id,$this->node_id);
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
public function getFTN4DAttribute(): string
|
2021-06-26 01:48:55 +00:00
|
|
|
{
|
|
|
|
return sprintf('%s.%d',$this->getFTN3DAttribute(),$this->point_id);
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
public function getRoleNameAttribute(): string
|
2021-06-20 13:03:20 +00:00
|
|
|
{
|
2021-07-26 11:21:58 +00:00
|
|
|
switch ($this->role) {
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_ZC:
|
2021-07-26 11:21:58 +00:00
|
|
|
return 'ZC';
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_RC:
|
2021-07-26 11:21:58 +00:00
|
|
|
return 'RC';
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_NC:
|
2021-07-26 11:21:58 +00:00
|
|
|
return 'NC';
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_HC:
|
2021-07-26 11:21:58 +00:00
|
|
|
return 'HUB';
|
2022-01-24 11:56:13 +00:00
|
|
|
case self::NODE_ACTIVE:
|
|
|
|
return 'NODE';
|
|
|
|
case self::NODE_POINT:
|
2021-07-26 11:21:58 +00:00
|
|
|
return 'POINT';
|
2021-06-20 13:03:20 +00:00
|
|
|
default:
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 10:16:37 +00:00
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/* METHODS */
|
2021-06-24 10:16:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a record in the DB for a node string, eg: 10:1/1.0
|
|
|
|
*
|
2022-12-01 12:51:43 +00:00
|
|
|
* @param string $address
|
|
|
|
* @param bool $create
|
|
|
|
* @param System|null $so
|
2021-06-29 10:43:29 +00:00
|
|
|
* @return Address|null
|
2023-06-27 07:39:11 +00:00
|
|
|
* @throws \Exception
|
2021-06-24 10:16:37 +00:00
|
|
|
*/
|
2023-07-02 13:40:08 +00:00
|
|
|
public static function findFTN(string $address,bool $create=FALSE,System $so=NULL,bool $trashed=FALSE): ?self
|
2021-06-24 10:16:37 +00:00
|
|
|
{
|
2022-12-01 12:51:43 +00:00
|
|
|
$ftn = self::parseFTN($address);
|
2021-06-24 10:16:37 +00:00
|
|
|
|
2021-07-26 11:21:58 +00:00
|
|
|
// Are we looking for a region address
|
|
|
|
if (($ftn['f'] === 0) && $ftn['p'] === 0) {
|
2023-07-02 13:40:08 +00:00
|
|
|
$o = (new self)
|
|
|
|
->when($trashed,function($query) {
|
|
|
|
$query->trashed();
|
|
|
|
},function($query) {
|
|
|
|
$query->active();
|
|
|
|
})
|
2021-07-26 11:21:58 +00:00
|
|
|
->where('zones.zone_id',$ftn['z'])
|
|
|
|
->where(function($q) use ($ftn) {
|
|
|
|
return $q
|
|
|
|
->where(function($q) use ($ftn) {
|
|
|
|
return $q->where('region_id',$ftn['n'])
|
|
|
|
->where('host_id',0);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
->where('node_id',$ftn['f'])
|
|
|
|
->where('point_id',$ftn['p'])
|
|
|
|
->when($ftn['d'],function($query,$domain) {
|
|
|
|
$query->where('domains.name',$domain);
|
|
|
|
})
|
|
|
|
->when((! $ftn['d']),function($query) {
|
2021-08-16 12:26:33 +00:00
|
|
|
$query->where('zones.default',TRUE);
|
2021-07-26 11:21:58 +00:00
|
|
|
})
|
|
|
|
->single();
|
|
|
|
|
|
|
|
if ($o && $o->system->active)
|
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:40:08 +00:00
|
|
|
$o = (new self)
|
|
|
|
->when($trashed,function($query) {
|
|
|
|
$query->trashed();
|
|
|
|
},function($query) {
|
|
|
|
$query->active();
|
|
|
|
})
|
2021-06-29 10:43:29 +00:00
|
|
|
->where('zones.zone_id',$ftn['z'])
|
2021-11-20 00:11:17 +00:00
|
|
|
->where(function($q) use ($ftn) {
|
|
|
|
return $q->where(function($qq) use ($ftn) {
|
|
|
|
return $qq
|
2021-11-20 00:50:45 +00:00
|
|
|
->where('region_id',$ftn['n'])
|
|
|
|
->where('host_id',0);
|
2021-11-20 00:11:17 +00:00
|
|
|
})
|
|
|
|
->orWhere(function($qq) use ($ftn) {
|
2021-11-20 00:50:45 +00:00
|
|
|
return $qq
|
|
|
|
->where('host_id',$ftn['n']);
|
2021-11-20 00:11:17 +00:00
|
|
|
});
|
|
|
|
})
|
2021-07-26 11:21:58 +00:00
|
|
|
->where('node_id',$ftn['f'])
|
|
|
|
->where('point_id',$ftn['p'])
|
2021-06-29 10:43:29 +00:00
|
|
|
->when($ftn['d'],function($query,$domain) {
|
|
|
|
$query->where('domains.name',$domain);
|
2021-06-24 10:16:37 +00:00
|
|
|
})
|
2021-06-29 10:43:29 +00:00
|
|
|
->when((! $ftn['d']),function($query) {
|
2021-08-16 12:26:33 +00:00
|
|
|
$query->where('zones.default',TRUE);
|
2021-06-24 10:16:37 +00:00
|
|
|
})
|
|
|
|
->single();
|
|
|
|
|
2022-12-01 12:51:43 +00:00
|
|
|
if ($create) {
|
|
|
|
if (! $so)
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception(sprintf('%s:AKA create requested for [%s], but system not provided',self::LOGKEY,$address));
|
2022-12-01 12:51:43 +00:00
|
|
|
|
|
|
|
if (! $ftn['d']) {
|
|
|
|
Log::alert(sprintf('%s:! Refusing to create address [%s] no domain available',self::LOGKEY,$address));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::debug(sprintf('%s:Creating AKA [%s] for [%s]',self::LOGKEY,$address,$so->name));
|
|
|
|
|
|
|
|
// Check Domain exists
|
|
|
|
Domain::unguard();
|
|
|
|
$do = Domain::singleOrNew(['name'=>$ftn['d']]);
|
|
|
|
Domain::reguard();
|
|
|
|
|
|
|
|
if (! $do->exists) {
|
|
|
|
$do->public = TRUE;
|
|
|
|
$do->active = TRUE;
|
|
|
|
$do->notes = 'Auto created';
|
|
|
|
$do->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create zone
|
|
|
|
Zone::unguard();
|
|
|
|
$zo = Zone::singleOrNew(['domain_id'=>$do->id,'zone_id'=>$ftn['z']]);
|
|
|
|
Zone::reguard();
|
|
|
|
|
|
|
|
if (! $zo->exists) {
|
|
|
|
$zo->active = TRUE;
|
|
|
|
$zo->notes = 'Auto created';
|
|
|
|
$zo->system_id = System::where('name','Discovered System')->single()->id;
|
|
|
|
$do->zones()->save($zo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $zo->active || ! $do->active) {
|
|
|
|
Log::alert(sprintf('%s:! Refusing to create address [%s] in disabled zone or domain',self::LOGKEY,$address));
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create Address, assigned to $so
|
|
|
|
$o = new self;
|
|
|
|
$o->active = TRUE;
|
|
|
|
$o->zone_id = $zo->id;
|
|
|
|
$o->region_id = 0;
|
|
|
|
$o->host_id = $ftn['n'];
|
|
|
|
$o->node_id = $ftn['f'];
|
|
|
|
$o->point_id = $ftn['p'];
|
|
|
|
$o->role = self::NODE_UNKNOWN;
|
|
|
|
$so->addresses()->save($o);
|
|
|
|
}
|
|
|
|
|
2021-06-24 10:16:37 +00:00
|
|
|
return ($o && $o->system->active) ? $o : NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-14 11:28:54 +00:00
|
|
|
/**
|
|
|
|
* Create an activation code for this address
|
|
|
|
*
|
|
|
|
* @param User $uo
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function set_activation(User $uo): string
|
|
|
|
{
|
|
|
|
return sprintf('%x:%s',
|
|
|
|
$this->id,
|
|
|
|
substr(md5(sprintf('%d:%x',$uo->id,timew($this->updated_at))),0,10)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the user's activation code for this address is correct
|
|
|
|
*
|
|
|
|
* @param User $uo
|
|
|
|
* @param string $code
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function check_activation(User $uo,string $code): bool
|
|
|
|
{
|
|
|
|
try {
|
2023-02-11 12:06:13 +00:00
|
|
|
Log::info(sprintf('%s:Checking Activation code [%s] is valid for user [%d]',self::LOGKEY,$code,$uo->id));
|
2022-03-14 11:28:54 +00:00
|
|
|
|
2023-06-27 07:39:11 +00:00
|
|
|
return ($code === $this->set_activation($uo));
|
2022-03-14 11:28:54 +00:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error(sprintf('%s:! Activation code [%s] invalid for user [%d]',self::LOGKEY,$code,$uo->id));
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
/**
|
2022-11-01 11:24:36 +00:00
|
|
|
* Echomail waiting to be sent to this system
|
2022-01-01 05:59:35 +00:00
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function echomailWaiting(): Collection
|
|
|
|
{
|
2022-01-15 02:06:15 +00:00
|
|
|
return $this->echomails()
|
2022-01-01 05:59:35 +00:00
|
|
|
->whereNull('echomail_seenby.sent_at')
|
|
|
|
->whereNotNull('echomail_seenby.export_at')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:24:36 +00:00
|
|
|
/**
|
|
|
|
* Files waiting to be sent to this system
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function filesWaiting(): Collection
|
|
|
|
{
|
|
|
|
return $this->files()
|
|
|
|
->whereNull('file_seenby.sent_at')
|
|
|
|
->whereNotNull('file_seenby.export_at')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
/**
|
|
|
|
* Get echomail for this node
|
|
|
|
*
|
2022-01-20 06:54:02 +00:00
|
|
|
* @param bool $update
|
2022-02-13 00:27:12 +00:00
|
|
|
* @param Collection|null $echomail
|
2021-07-30 14:35:52 +00:00
|
|
|
* @return Packet|null
|
|
|
|
*/
|
2022-01-24 11:56:13 +00:00
|
|
|
public function getEchomail(bool $update=TRUE,Collection $echomail=NULL): ?Packet
|
2021-07-30 14:35:52 +00:00
|
|
|
{
|
2021-09-06 13:39:32 +00:00
|
|
|
$pkt = NULL;
|
2022-01-24 11:56:13 +00:00
|
|
|
if ($echomail)
|
|
|
|
return $this->getPacket($echomail);
|
2021-09-06 13:39:32 +00:00
|
|
|
|
2022-12-02 13:22:56 +00:00
|
|
|
$s = Setup::findOrFail(config('app.id'));
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
if (($x=$this->echomailWaiting())
|
2021-07-30 14:35:52 +00:00
|
|
|
->count())
|
|
|
|
{
|
2022-12-02 13:22:56 +00:00
|
|
|
// Limit to max messages
|
|
|
|
Log::info(sprintf('%s:= Got [%d] echomails for [%s] for sending',self::LOGKEY,$x->count(),$this->ftn));
|
|
|
|
|
|
|
|
if ($x->count() > $s->max_msgs_pkt) {
|
|
|
|
$x = $x->take($s->max_msgs_pkt);
|
|
|
|
Log::alert(sprintf('%s:= Only sending [%d] echomails for [%s]',self::LOGKEY,$x->count(),$this->ftn));
|
|
|
|
}
|
2022-02-13 00:27:12 +00:00
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
$pkt = $this->getPacket($x);
|
2021-09-06 13:39:32 +00:00
|
|
|
|
2022-01-20 06:54:02 +00:00
|
|
|
if ($pkt && $pkt->count() && $update)
|
|
|
|
DB::table('echomail_seenby')
|
|
|
|
->whereIn('echomail_id',$x->pluck('id'))
|
|
|
|
->where('address_id',$this->id)
|
|
|
|
->whereNull('sent_at')
|
|
|
|
->whereNull('packet')
|
|
|
|
->whereNotNull('echomail_seenby.export_at')
|
|
|
|
->update(['sent_at'=>Carbon::now(),'packet'=>$pkt->name]);
|
2021-07-30 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 13:39:32 +00:00
|
|
|
return $pkt;
|
2021-07-30 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 07:36:22 +00:00
|
|
|
/**
|
|
|
|
* Get files for this node (including it's children)
|
|
|
|
*
|
|
|
|
* @param bool $update
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function getFiles(bool $update=TRUE): Collection
|
|
|
|
{
|
|
|
|
if (($files=$this->filesWaiting())
|
|
|
|
->count())
|
|
|
|
{
|
|
|
|
Log::debug(sprintf('%s:= Got [%d] files for [%s] for sending',self::LOGKEY,$files->count(),$this->ftn));
|
|
|
|
|
|
|
|
// @todo This should be transactional, incase the transfer fails
|
|
|
|
if ($files->count() && $update)
|
|
|
|
DB::table('file_seenby')
|
|
|
|
->whereIn('file_id',$files->pluck('id'))
|
|
|
|
->where('address_id',$this->id)
|
|
|
|
->whereNull('sent_at')
|
|
|
|
->whereNotNull('export_at')
|
|
|
|
->update(['sent_at'=>Carbon::now()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
2021-07-15 14:54:23 +00:00
|
|
|
/**
|
|
|
|
* Get netmail for this node (including it's children)
|
2021-07-17 05:48:07 +00:00
|
|
|
*
|
2022-01-20 06:54:02 +00:00
|
|
|
* @param bool $update
|
2021-07-17 05:48:07 +00:00
|
|
|
* @return Packet|null
|
2021-07-15 14:54:23 +00:00
|
|
|
*/
|
2022-01-20 06:54:02 +00:00
|
|
|
public function getNetmail(bool $update=TRUE): ?Packet
|
2021-07-15 14:54:23 +00:00
|
|
|
{
|
2022-01-01 05:59:35 +00:00
|
|
|
$pkt = NULL;
|
|
|
|
|
|
|
|
if (($x=$this->netmailWaiting())
|
2021-07-18 12:10:21 +00:00
|
|
|
->count())
|
|
|
|
{
|
2022-02-13 00:27:12 +00:00
|
|
|
Log::debug(sprintf('%s:= Got [%d] netmails for [%s] for sending',self::LOGKEY,$x->count(),$this->ftn));
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
$pkt = $this->getPacket($x);
|
|
|
|
|
2022-01-20 06:54:02 +00:00
|
|
|
if ($pkt && $pkt->count() && $update)
|
|
|
|
DB::table('netmails')
|
|
|
|
->whereIn('id',$x->pluck('id'))
|
|
|
|
->update(['sent_at'=>Carbon::now(),'sent_pkt'=>$pkt->name]);
|
2021-07-30 14:35:52 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
return $pkt;
|
2021-07-30 14:35:52 +00:00
|
|
|
}
|
2021-07-17 05:48:07 +00:00
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
/**
|
|
|
|
* Return a packet of mail
|
|
|
|
*
|
2022-12-02 13:22:56 +00:00
|
|
|
* @param Collection $msgs of message models (Echomail/Netmail)
|
2022-01-20 12:25:47 +00:00
|
|
|
* @return Packet|null
|
2021-07-30 14:35:52 +00:00
|
|
|
*/
|
2022-12-02 13:22:56 +00:00
|
|
|
private function getPacket(Collection $msgs): ?Packet
|
2021-07-30 14:35:52 +00:00
|
|
|
{
|
2022-12-02 13:22:56 +00:00
|
|
|
$s = Setup::findOrFail(config('app.id'));
|
|
|
|
$ao = $s->system->match($this->zone)->first();
|
2022-01-20 06:54:02 +00:00
|
|
|
|
|
|
|
// If we dont match on the address, we cannot pack mail for that system
|
|
|
|
if (! $ao)
|
|
|
|
return NULL;
|
|
|
|
|
2023-06-26 09:19:42 +00:00
|
|
|
// Get packet type
|
|
|
|
$type = collect(Packet::PACKET_TYPES)->get($this->system->pkt_type ?: config('app.default_pkt'));
|
|
|
|
$o = new $type;
|
2023-06-25 10:45:02 +00:00
|
|
|
$o->addressHeader($ao,$this);
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2022-02-13 00:27:12 +00:00
|
|
|
// $oo = Netmail/Echomail Model
|
2022-12-02 13:22:56 +00:00
|
|
|
$c = 0;
|
|
|
|
foreach ($msgs as $oo) {
|
|
|
|
// Only bundle up to max messages
|
|
|
|
if (++$c > $s->max_msgs_pkt)
|
|
|
|
break;
|
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
$o->addMail($oo->packet($this));
|
2022-12-02 13:22:56 +00:00
|
|
|
}
|
2021-07-15 14:54:23 +00:00
|
|
|
|
2021-07-30 14:35:52 +00:00
|
|
|
return $o;
|
2021-07-15 14:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-01 05:59:35 +00:00
|
|
|
/**
|
|
|
|
* Netmail waiting to be sent to this system
|
|
|
|
*
|
|
|
|
* @return Collection
|
|
|
|
*/
|
|
|
|
public function netmailWaiting(): Collection
|
|
|
|
{
|
|
|
|
return Netmail::whereIn('tftn_id',(($x=$this->children) ? $x->pluck('id') : collect())->push($this->id))
|
2023-01-24 11:37:41 +00:00
|
|
|
->where('local',FALSE)
|
2022-01-01 05:59:35 +00:00
|
|
|
->whereNull('sent_at')
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:43:29 +00:00
|
|
|
/**
|
2022-11-01 05:39:58 +00:00
|
|
|
*
|
2021-06-29 10:43:29 +00:00
|
|
|
* Parse a string and split it out as an FTN array
|
|
|
|
*
|
|
|
|
* @param string $ftn
|
|
|
|
* @return array
|
2023-06-27 07:39:11 +00:00
|
|
|
* @throws \Exception
|
2021-06-29 10:43:29 +00:00
|
|
|
*/
|
|
|
|
public static function parseFTN(string $ftn): array
|
|
|
|
{
|
2022-11-01 05:39:58 +00:00
|
|
|
if (! preg_match(sprintf('#^%s$#',self::ftn_regex),strtolower($ftn),$matches))
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception('Invalid FTN: '.$ftn);
|
2021-06-29 10:43:29 +00:00
|
|
|
|
|
|
|
// Check our numbers are correct.
|
|
|
|
foreach ([1,2,3] as $i) {
|
2021-07-01 11:56:55 +00:00
|
|
|
if ((! is_numeric($matches[$i])) || ($matches[$i] > DomainController::NUMBER_MAX))
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception('Invalid FTN: '.$ftn);
|
2021-06-29 10:43:29 +00:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:56:55 +00:00
|
|
|
if (isset($matches[5]) AND ((! is_numeric($matches[$i])) || ($matches[5] > DomainController::NUMBER_MAX)))
|
2023-06-27 07:39:11 +00:00
|
|
|
throw new \Exception('Invalid FTN: '.$ftn);
|
2021-06-29 10:43:29 +00:00
|
|
|
|
|
|
|
return [
|
|
|
|
'z'=>(int)$matches[1],
|
|
|
|
'n'=>(int)$matches[2],
|
|
|
|
'f'=>(int)$matches[3],
|
|
|
|
'p'=>isset($matches[5]) && $matches[5] ? (int)$matches[5] : 0,
|
|
|
|
'd'=>$matches[7] ?? NULL
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-06-24 13:09:09 +00:00
|
|
|
/**
|
|
|
|
* Retrieve the address session details/passwords
|
|
|
|
*
|
|
|
|
* @param string $type
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2021-06-24 10:16:37 +00:00
|
|
|
public function session(string $type): ?string
|
|
|
|
{
|
2021-07-31 07:43:58 +00:00
|
|
|
return ($x=$this->system->sessions->where('id',$this->zone_id)->first()) ? $x->pivot->{$type} : NULL;
|
2021-06-24 10:16:37 +00:00
|
|
|
}
|
2021-06-20 13:03:20 +00:00
|
|
|
}
|