clrghouz/app/Models/Setup.php

189 lines
4.0 KiB
PHP
Raw Normal View History

2021-04-01 10:59:15 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
2021-04-01 10:59:15 +00:00
use Illuminate\Support\Facades\File;
use App\Classes\Protocol\{Binkp,DNS,EMSI};
2021-04-01 10:59:15 +00:00
/**
* This class represents our configuration.
*
* Our 'System' is defined by system_id, and from it we can find out our BBS name and addresses.
2021-04-01 10:59:15 +00:00
*
* @package App\Models
* @property Collection nodes
*/
class Setup extends Model
{
public const PRODUCT_NAME = 'Clearing Houz';
public const PRODUCT_ID = 0xAB8D;
public const PRODUCT_VERSION_MAJ = 0;
public const PRODUCT_VERSION_MIN = 0;
public const BIND = '::';
public const EMSI_PORT = 60179;
public const EMSI_BIND = self::BIND;
2023-04-22 11:30:30 +00:00
public const DNS_PORT = 53;
public const DNS_BIND = '::';
public const O_BINKP = 1<<1; /* Listen for BINKD connections */
public const O_EMSI = 1<<2; /* Listen for EMSI connections */
2023-04-22 11:30:30 +00:00
public const O_DNS = 1<<3; /* List for DNS */
public const O_HIDEAKA = 1<<4; /* Hide AKAs to different Zones */
public const MAX_MSGS_PKT = 50;
2021-04-01 10:59:15 +00:00
// Our non model attributes and values
private array $internal = [];
protected $casts = [
'servers' => 'array',
];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
/* EMSI SETTINGS */
$this->do_prevent = 1; /* EMSI - send an immediate EMSI_INQ on connect */
$this->ignore_nrq = 0;
$this->options = 0; /* EMSI - our capabilities */
}
2021-04-01 10:59:15 +00:00
/**
* @throws \Exception
2021-04-01 10:59:15 +00:00
*/
public function __get($key)
{
switch ($key) {
case 'binkp_bind':
case 'dns_bind':
case 'emsi_bind':
return Arr::get($this->servers,str_replace('_','.',$key),self::BIND);
case 'binkp_port':
return Arr::get($this->servers,str_replace('_','.',$key),Binkp::PORT);
case 'dns_port':
return Arr::get($this->servers,str_replace('_','.',$key),EMSI::PORT);
case 'emsi_port':
return Arr::get($this->servers,str_replace('_','.',$key),DNS::PORT);
2021-04-01 10:59:15 +00:00
case 'binkp_options':
case 'dns_options':
case 'emsi_options':
return Arr::get($this->servers,'binkp.options');
case 'binkp_settings':
2021-04-01 10:59:15 +00:00
case 'ignore_nrq':
case 'do_prevent':
return $this->internal[$key] ?? FALSE;
case 'max_msgs_pkt':
return self::MAX_MSGS_PKT;
2021-04-01 10:59:15 +00:00
case 'version':
return File::exists('VERSION') ? chop(File::get('VERSION')) : 'dev';
default:
return parent::__get($key);
}
}
/**
* @throws \Exception
2021-04-01 10:59:15 +00:00
*/
public function __set($key,$value)
{
switch ($key) {
case 'binkp_bind':
case 'binkp_port':
2021-04-01 10:59:15 +00:00
case 'binkp_options':
case 'dns_bind':
case 'dns_port':
case 'dns_options':
case 'emsi_bind':
case 'emsi_port':
case 'emsi_options':
return Arr::set($this->servers,str_replace('_','.',$key),$value);
case 'binkp_settings':
2021-04-01 10:59:15 +00:00
case 'ignore_nrq':
case 'opt_nr':
case 'opt_nd':
case 'opt_nda':
case 'opt_md':
case 'opt_cr':
case 'opt_mb':
case 'opt_cht':
case 'opt_mpwd':
2021-04-01 10:59:15 +00:00
case 'do_prevent':
$this->internal[$key] = $value;
break;
default:
parent::__set($key,$value);
2021-04-01 10:59:15 +00:00
}
}
/**
* The Mailer Product ID in hex.
*
* @param int $c
* @return string
* @throws \Exception
*/
public static function product_id(int $c=self::PRODUCT_ID): string
{
return hexstr($c);
}
/* RELATIONS */
/**
* The defined system that this setup is valid for
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function system()
{
return $this->belongsTo(System::class);
}
/* ATTRIBUTES */
public function getLocationAttribute()
{
return $this->system->location;
}
public function getSysopAttribute()
{
return $this->system->sysop;
}
public function getSystemNameAttribute()
{
return $this->system->name;
}
/* METHODS */
public function optionClear(int $key,$index='options'): void
{
$this->{$index} &= ~$key;
}
public function optionGet(int $key,$index='options'): int
{
return ($this->{$index} & $key);
}
public function optionSet(int $key,$index='options'): void
{
$this->{$index} |= $key;
}
2021-04-01 10:59:15 +00:00
}