Files
reactive-mail-server/backend/src/main/java/com/mailserver/config/MailDomains.java
T
Liosha84andClaude Opus 5.5 cf84797e68
continuous-integration/drone/pr Build is passing
Рассылки, папки по +метке, ловушка только для чужих адресов, служебное API для Jambotron
- V6 mail_lists + mail_list_members: письмо на alerts-prod@ → каждому участнику своя копия в папку рассылки;
  закрытая рассылка — только от вошедших отправителей (550 снаружи)
- bob+prod@ → ящик bob@, папка prod (создаётся сама; системные папки нельзя)
- TRAP: чужие адреса → одна копия в INBOX отправителя; наши ящики и рассылки доставляются
- /api/service/** (SERVICE_TOKEN из secrets-init): аккаунты (пароль один раз, выключение → вон из рассылок), рассылки
- MailDirectory: одно пространство адресов для ящиков и рассылок, без «+» в именах
- Настройки → LISTS для администраторов
- 86 тестов; живой прогон на Postgres 17: миграции V1–V6, API, SMTP (рассылка/метка/ловушка)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-07 14:15:24 +01:00

70 lines
2.5 KiB
Java

package com.mailserver.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Domains whose mailboxes live on this server ({@code mailserver.domains}), and the optional catch-all mailbox
* ({@code mailserver.catch-all}): when set, mail to ANY address is stored there and nothing is delivered outside —
* a mail trap for dev/staging.
*/
@Component
public class MailDomains {
private final Set<String> domains;
private final String catchAll;
public MailDomains(@Value("${mailserver.domains:localhost}") String domains,
@Value("${mailserver.catch-all:}") String catchAll) {
this.domains = Arrays.stream(domains.split(","))
.map(d -> d.trim().toLowerCase(Locale.ROOT))
.filter(d -> !d.isEmpty())
.collect(Collectors.toUnmodifiableSet());
this.catchAll = catchAll.trim().toLowerCase(Locale.ROOT);
if (this.domains.isEmpty()) {
throw new IllegalStateException("mailserver.domains is empty");
}
}
public boolean isLocal(String address) {
return domains.contains(domainOf(address));
}
/** Mailbox that receives everything, or null. */
public String catchAll() {
return catchAll.isEmpty() ? null : catchAll;
}
public Set<String> domains() {
return domains;
}
/**
* Address without its "+tag": {@code bob+prod@x} → {@code bob@x}. Mailbox and list names never contain "+",
* so the part after it is a label chosen by the sender — the folder the message goes to.
*/
public static String baseOf(String address) {
String lower = address.toLowerCase(Locale.ROOT);
int at = lower.lastIndexOf('@');
int plus = lower.indexOf('+');
return plus > 0 && plus < at ? lower.substring(0, plus) + lower.substring(at) : lower;
}
/** The "+tag" of an address, or null: {@code bob+prod@x} → {@code prod}. */
public static String tagOf(String address) {
int at = address.lastIndexOf('@');
int plus = address.indexOf('+');
return plus > 0 && plus < at - 1 ? address.substring(plus + 1, at) : null;
}
public static String domainOf(String address) {
int at = address == null ? -1 : address.lastIndexOf('@');
return at < 0 ? "" : address.substring(at + 1).toLowerCase(Locale.ROOT);
}
}