mirror of
https://github.com/mailcow/mailcow-dockerized.git
synced 2026-11-15 21:00:14 +00:00
[Web] Add archive module classes
This commit is contained in:
17 files changed
+1685
No files matched your search
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Archive\Model;
|
||||
|
||||
/**
|
||||
* What the archive learns about one mail besides its content: owners, SMTP envelope, origin and content hash.
|
||||
*/
|
||||
class Journal
|
||||
{
|
||||
public const DIRECTIONS = ['in', 'out', 'internal'];
|
||||
public const SOURCES = ['smtp', 'quarantine', 'massmail', 'test'];
|
||||
|
||||
/**
|
||||
* @param string[] $owners mailbox usernames the mail belongs to
|
||||
* @param string[] $rcpts SMTP envelope recipients as received (incl. BCC)
|
||||
* @param float $received time mailcow accepted the mail (unix time with milliseconds)
|
||||
* @param string $sha256 hash of the content as sent to the archive (CRLF line ends)
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly array $owners,
|
||||
public readonly array $rcpts,
|
||||
public readonly string $from,
|
||||
public readonly string $qid,
|
||||
public readonly string $direction,
|
||||
public readonly string $source,
|
||||
public readonly float $received,
|
||||
public readonly string $sha256
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Archive\Model;
|
||||
|
||||
/**
|
||||
* A mailbox that receives mail (regular kind, active 1 or 2, in an active domain that is no backup MX)
|
||||
* with its archive settings: the mailbox attribute `archive` ('1', '0' or 'inherit') and the domain default.
|
||||
*/
|
||||
class Mailbox
|
||||
{
|
||||
public const INHERIT = 'inherit';
|
||||
|
||||
public function __construct(
|
||||
public readonly string $username,
|
||||
public readonly string $domain,
|
||||
public readonly string $archive,
|
||||
public readonly bool $domainArchive,
|
||||
public readonly string $name = '',
|
||||
public readonly int $active = 1
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromRow(array $row): self
|
||||
{
|
||||
return new self(
|
||||
strtolower((string)$row['username']),
|
||||
strtolower((string)$row['domain']),
|
||||
self::setting($row['archive'] ?? null),
|
||||
(int)($row['domain_archive'] ?? 0) === 1,
|
||||
(string)($row['name'] ?? ''),
|
||||
(int)($row['active'] ?? 1)
|
||||
);
|
||||
}
|
||||
|
||||
/** Mailbox setting as stored: '1', '0', anything else (also a missing attribute) is 'inherit' */
|
||||
public static function setting(mixed $value): string
|
||||
{
|
||||
$value = is_scalar($value) ? (string)$value : '';
|
||||
return in_array($value, ['0', '1'], true) ? $value : self::INHERIT;
|
||||
}
|
||||
|
||||
/** Archiving applies by the mailbox setting, 'inherit' follows the domain (without the global switch) */
|
||||
public static function effective(mixed $archive, mixed $domainArchive): bool
|
||||
{
|
||||
$archive = self::setting($archive);
|
||||
return ($archive === self::INHERIT) ? (int)$domainArchive === 1 : $archive === '1';
|
||||
}
|
||||
|
||||
public function archived(): bool
|
||||
{
|
||||
return self::effective($this->archive, $this->domainArchive);
|
||||
}
|
||||
|
||||
/** Why the mailbox is archived: its own setting or the domain default */
|
||||
public function source(): string
|
||||
{
|
||||
return ($this->archive === '1') ? 'mailbox' : 'domain';
|
||||
}
|
||||
|
||||
/** Entry of GET archive/mailboxes */
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'username' => $this->username,
|
||||
'domain' => $this->domain,
|
||||
'name' => $this->name,
|
||||
'active' => $this->active,
|
||||
'source' => $this->source(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mailcow\Archive\Model;
|
||||
|
||||
/**
|
||||
* The archive target (table `archive_settings`): archive copies of accepted mails go by SMTP to `address`,
|
||||
* through the Postfix queue (table `archive_transport`, service archive_smtp in master.cf).
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
public const TLS_MODES = ['encrypt', 'verify', 'may'];
|
||||
/** Metadata headers of a copy: archivecow (X-Archive-*, signed), envelope (sender/recipients), none */
|
||||
public const PROFILES = ['archivecow', 'envelope', 'none'];
|
||||
/** Envelope sender of copies; Postfix drops every notice to it (archive_discard), nothing reaches a real sender */
|
||||
public const SENDER = 'archive-bounce@localhost';
|
||||
|
||||
/** rspamd hands accepted mails to the archive */
|
||||
public bool $enabled = false;
|
||||
/** Address the archive receives copies at */
|
||||
public string $address = '';
|
||||
/** SMTP server of the archive; empty: MX of the address domain */
|
||||
public string $host = '';
|
||||
public int $port = 25;
|
||||
public string $tls = 'encrypt';
|
||||
/** SMTP AUTH at the archive, optional */
|
||||
public string $username = '';
|
||||
/** Never part of toArray() */
|
||||
public string $password = '';
|
||||
public string $profile = 'archivecow';
|
||||
/** Key of X-Archive-Signature (profile archivecow), never part of toArray() */
|
||||
public string $signingKey = '';
|
||||
/** Header names of the envelope profile */
|
||||
public string $headerFrom = 'X-Envelope-From';
|
||||
public string $headerRcpt = 'X-Envelope-To';
|
||||
|
||||
/** Copies can be sent: an address, and a signing key for archivecow */
|
||||
public function canDeliver(): bool
|
||||
{
|
||||
return $this->address !== '' && ($this->profile !== 'archivecow' || $this->signingKey !== '');
|
||||
}
|
||||
|
||||
/** Next hop for Postfix: [host]:port, else the domain of the address (MX lookup), with the port when not 25 */
|
||||
public function nexthop(): string
|
||||
{
|
||||
if ($this->host !== '') {
|
||||
$host = filter_var($this->host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) ? 'ipv6:' . $this->host : $this->host;
|
||||
return '[' . $host . ']:' . $this->port;
|
||||
}
|
||||
$domain = substr($this->address, (int)strrpos($this->address, '@') + 1);
|
||||
return ($this->port === 25) ? $domain : $domain . ':' . $this->port;
|
||||
}
|
||||
|
||||
/** API representation (GET archive/settings), without secrets */
|
||||
public function toArray(): array
|
||||
{
|
||||
return [
|
||||
'enabled' => (int)$this->enabled,
|
||||
'address' => $this->address,
|
||||
'host' => $this->host,
|
||||
'port' => $this->port,
|
||||
'tls' => $this->tls,
|
||||
'username' => $this->username,
|
||||
'has_password' => (int)($this->password !== ''),
|
||||
'profile' => $this->profile,
|
||||
'has_signing_secret' => (int)($this->signingKey !== ''),
|
||||
'header_from' => $this->headerFrom,
|
||||
'header_rcpt' => $this->headerRcpt,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user