2026-11-23 23:27:02 +01:00
|
|
|
from dataclasses import replace
|
2026-11-22 11:03:26 +01:00
|
|
|
from datetime import UTC
|
|
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
from django.contrib.auth.mixins import PermissionRequiredMixin
|
2026-11-23 23:27:02 +01:00
|
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
2026-11-22 11:03:26 +01:00
|
|
|
from django.http import Http404
|
|
|
|
|
from django.shortcuts import redirect
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
|
from django.views.decorators.cache import never_cache
|
|
|
|
|
from django.views.generic import FormView
|
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
|
|
|
|
|
2026-11-23 23:02:32 +01:00
|
|
|
from coulobre.mail_policy import (
|
|
|
|
|
DEFAULT_REPEAT_DAYS,
|
|
|
|
|
MAX_REPEAT_DAYS,
|
|
|
|
|
MailRouting,
|
|
|
|
|
MailVacation,
|
|
|
|
|
parse_mail_policy,
|
|
|
|
|
)
|
2026-11-22 11:03:26 +01:00
|
|
|
|
2026-11-23 23:27:02 +01:00
|
|
|
from .access import (
|
|
|
|
|
TenantScopeMixin,
|
|
|
|
|
can_manage_hosting,
|
|
|
|
|
owns_customer_operations,
|
|
|
|
|
reseller_for_user,
|
|
|
|
|
)
|
2026-11-22 11:03:26 +01:00
|
|
|
from .mail_policies import (
|
|
|
|
|
mail_policy_available,
|
|
|
|
|
policy_account,
|
|
|
|
|
policy_revision,
|
|
|
|
|
queue_policy,
|
|
|
|
|
validate_policy,
|
|
|
|
|
)
|
|
|
|
|
from .models import (
|
|
|
|
|
HostingMailbox,
|
|
|
|
|
HostingMailDomain,
|
|
|
|
|
MailRoutingPublication,
|
|
|
|
|
MailVacationPublication,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
POLICY_PERMISSIONS = {
|
|
|
|
|
"routing": "hosting.change_hostingmaildomain",
|
|
|
|
|
"vacation": "hosting.change_hostingmailbox",
|
|
|
|
|
}
|
|
|
|
|
VIEW_PERMISSIONS = (
|
|
|
|
|
"hosting.view_hostingaccount",
|
|
|
|
|
"hosting.view_hostingmailbox",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def mail_policy_enabled(request, kind):
|
|
|
|
|
return (
|
|
|
|
|
mail_policy_available(kind)
|
|
|
|
|
and getattr(request, "impersonation", None) is None
|
2026-11-23 22:36:14 +01:00
|
|
|
and can_manage_hosting(
|
|
|
|
|
request.user, (*VIEW_PERMISSIONS, POLICY_PERMISSIONS[kind])
|
2026-11-22 11:03:26 +01:00
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailPolicyForm(forms.Form):
|
|
|
|
|
revision = forms.CharField(required=False, widget=forms.HiddenInput)
|
|
|
|
|
|
2026-11-23 23:27:02 +01:00
|
|
|
def __init__(self, *args, resource, policy=None, **kwargs):
|
2026-11-22 11:03:26 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
self.resource = resource
|
2026-11-23 23:27:02 +01:00
|
|
|
self.policy = policy
|
2026-11-22 11:03:26 +01:00
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
|
cleaned = super().clean()
|
|
|
|
|
if not self.errors:
|
|
|
|
|
try:
|
|
|
|
|
self.spec = self.make_spec(cleaned)
|
|
|
|
|
validate_policy(
|
|
|
|
|
self.resource, self.spec, policy_account(self.resource)
|
|
|
|
|
)
|
|
|
|
|
except ValueError as error:
|
|
|
|
|
raise ValidationError(str(error)) from error
|
|
|
|
|
return cleaned
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailRoutingForm(MailPolicyForm):
|
|
|
|
|
aliases = forms.CharField(
|
|
|
|
|
required=False,
|
|
|
|
|
max_length=65536,
|
|
|
|
|
widget=forms.Textarea(attrs={"rows": 8, "spellcheck": "false"}),
|
|
|
|
|
help_text=(
|
|
|
|
|
"Enter one alias per line, such as "
|
|
|
|
|
"sales = alice@example.net, bob@example.net. "
|
|
|
|
|
"Use the part before @ for each alias."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
catchall = forms.CharField(
|
|
|
|
|
label="Catch-all mailbox",
|
|
|
|
|
required=False,
|
|
|
|
|
max_length=254,
|
|
|
|
|
help_text=(
|
|
|
|
|
"Enter a mailbox on this domain if your plan allows catch-all. "
|
|
|
|
|
"Leave blank to reject unknown recipients."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def make_spec(self, cleaned):
|
|
|
|
|
aliases = {}
|
|
|
|
|
for line in cleaned["aliases"].splitlines():
|
|
|
|
|
if not line.strip():
|
|
|
|
|
continue
|
|
|
|
|
name, equals, targets = line.partition("=")
|
|
|
|
|
name = name.strip()
|
|
|
|
|
if not equals or not name:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"Enter one alias and its destinations per line."
|
|
|
|
|
)
|
|
|
|
|
if name in aliases:
|
|
|
|
|
raise ValueError(f"The alias {name} occurs twice.")
|
|
|
|
|
aliases[name] = [target.strip() for target in targets.split(",")]
|
|
|
|
|
return MailRouting(self.resource.domain, aliases, cleaned["catchall"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailVacationForm(MailPolicyForm):
|
|
|
|
|
enabled = forms.BooleanField(
|
|
|
|
|
label="Send automatic replies", required=False
|
|
|
|
|
)
|
|
|
|
|
subject = forms.CharField(max_length=160, required=False)
|
|
|
|
|
message = forms.CharField(
|
|
|
|
|
max_length=4000,
|
|
|
|
|
required=False,
|
|
|
|
|
widget=forms.Textarea(attrs={"rows": 7}),
|
|
|
|
|
)
|
|
|
|
|
starts_at = forms.DateTimeField(
|
|
|
|
|
label="Start time (UTC)",
|
|
|
|
|
required=False,
|
|
|
|
|
widget=forms.DateTimeInput(
|
|
|
|
|
attrs={"type": "datetime-local", "step": 1}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
ends_at = forms.DateTimeField(
|
|
|
|
|
label="End time (UTC)",
|
|
|
|
|
required=False,
|
|
|
|
|
widget=forms.DateTimeInput(
|
|
|
|
|
attrs={"type": "datetime-local", "step": 1}
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-23 23:02:32 +01:00
|
|
|
repeat_days = forms.IntegerField(
|
|
|
|
|
label="Days between replies to the same sender",
|
|
|
|
|
min_value=1,
|
|
|
|
|
max_value=MAX_REPEAT_DAYS,
|
|
|
|
|
initial=DEFAULT_REPEAT_DAYS,
|
|
|
|
|
required=False,
|
|
|
|
|
help_text=(
|
|
|
|
|
"Leave blank for seven days. The mail node enforces its limits."
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2026-11-22 11:03:26 +01:00
|
|
|
def make_spec(self, cleaned):
|
|
|
|
|
values = {
|
|
|
|
|
key: cleaned[key] for key in ("enabled", "subject", "message")
|
|
|
|
|
}
|
|
|
|
|
for key in ("starts_at", "ends_at"):
|
|
|
|
|
timestamp = cleaned[key]
|
|
|
|
|
values[key] = (
|
|
|
|
|
timestamp.astimezone(UTC).isoformat(timespec="seconds")
|
|
|
|
|
if timestamp
|
|
|
|
|
else ""
|
|
|
|
|
)
|
2026-11-23 23:02:32 +01:00
|
|
|
return MailVacation(
|
|
|
|
|
self.resource.spec,
|
|
|
|
|
repeat_days=cleaned.get("repeat_days") or DEFAULT_REPEAT_DAYS,
|
2026-11-23 23:27:02 +01:00
|
|
|
move_spam=self.policy.move_spam if self.policy else False,
|
|
|
|
|
junk_senders=self.policy.junk_senders if self.policy else (),
|
2026-11-23 23:02:32 +01:00
|
|
|
**values,
|
|
|
|
|
)
|
2026-11-22 11:03:26 +01:00
|
|
|
|
|
|
|
|
|
2026-11-23 23:27:02 +01:00
|
|
|
class MailSpamForm(MailPolicyForm):
|
|
|
|
|
move_spam = forms.BooleanField(
|
|
|
|
|
required=False, label="Move messages tagged as spam to Junk"
|
|
|
|
|
)
|
|
|
|
|
junk_senders = forms.CharField(
|
|
|
|
|
required=False,
|
|
|
|
|
max_length=5500,
|
|
|
|
|
label="Senders to move to Junk",
|
|
|
|
|
help_text="Enter up to 20 full sender addresses, one per line.",
|
|
|
|
|
widget=forms.Textarea(attrs={"rows": 5}),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def make_spec(self, cleaned):
|
|
|
|
|
policy = self.policy or MailVacation(self.resource.spec, enabled=False)
|
|
|
|
|
senders = tuple(
|
|
|
|
|
line.strip()
|
|
|
|
|
for line in cleaned["junk_senders"].splitlines()
|
|
|
|
|
if line.strip()
|
|
|
|
|
)
|
|
|
|
|
return replace(
|
|
|
|
|
policy,
|
|
|
|
|
move_spam=cleaned["move_spam"],
|
|
|
|
|
junk_senders=senders,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2026-11-22 11:03:26 +01:00
|
|
|
@method_decorator(never_cache, name="dispatch")
|
|
|
|
|
class MailPolicyEditor(
|
|
|
|
|
TenantScopeMixin, PermissionRequiredMixin, SingleObjectMixin, FormView
|
|
|
|
|
):
|
|
|
|
|
template_name = "hosting/mail_policy_form.html"
|
|
|
|
|
|
|
|
|
|
def has_permission(self):
|
2026-11-23 23:02:32 +01:00
|
|
|
return (
|
|
|
|
|
reseller_for_user(self.request.user) is None
|
|
|
|
|
and getattr(self.request, "impersonation", None) is None
|
|
|
|
|
and super().has_permission()
|
2026-11-22 11:03:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def get_permission_required(self):
|
|
|
|
|
return (*VIEW_PERMISSIONS, POLICY_PERMISSIONS[self.kind])
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
self.load_resource()
|
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
|
self.load_resource()
|
|
|
|
|
with timezone.override(UTC):
|
|
|
|
|
return super().post(request, *args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def load_resource(self):
|
|
|
|
|
if not mail_policy_available(self.kind):
|
|
|
|
|
raise Http404
|
|
|
|
|
self.object = self.get_object()
|
2026-11-23 23:27:02 +01:00
|
|
|
if not owns_customer_operations(
|
|
|
|
|
self.request.user, policy_account(self.object)
|
|
|
|
|
):
|
|
|
|
|
raise PermissionDenied
|
2026-11-22 11:03:26 +01:00
|
|
|
self.job = getattr(self.object, self.kind, None)
|
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
2026-11-23 23:27:02 +01:00
|
|
|
return super().get_form_kwargs() | {
|
|
|
|
|
"resource": self.object,
|
|
|
|
|
"policy": parse_mail_policy(self.job.manifest)
|
|
|
|
|
if self.job
|
|
|
|
|
else None,
|
|
|
|
|
}
|
2026-11-22 11:03:26 +01:00
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
initial = {"revision": policy_revision(self.job)}
|
|
|
|
|
if self.job is None:
|
|
|
|
|
return initial
|
|
|
|
|
spec = parse_mail_policy(self.job.manifest)
|
|
|
|
|
if self.kind == "routing":
|
|
|
|
|
initial["aliases"] = "\n".join(
|
|
|
|
|
f"{name} = {', '.join(targets)}"
|
|
|
|
|
for name, targets in spec.aliases.items()
|
|
|
|
|
)
|
|
|
|
|
initial["catchall"] = spec.catchall
|
|
|
|
|
else:
|
|
|
|
|
initial.update(spec.manifest())
|
2026-11-23 23:02:32 +01:00
|
|
|
initial["repeat_days"] = spec.repeat_days
|
2026-11-22 11:03:26 +01:00
|
|
|
for name in ("starts_at", "ends_at"):
|
|
|
|
|
initial[name] = initial[name].removesuffix("+00:00")
|
|
|
|
|
return initial
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
applied = (
|
|
|
|
|
parse_mail_policy(self.job.applied_manifest)
|
|
|
|
|
if self.job and self.job.applied_manifest
|
|
|
|
|
else None
|
|
|
|
|
)
|
|
|
|
|
return super().get_context_data(
|
|
|
|
|
account=policy_account(self.object),
|
|
|
|
|
job=self.job,
|
|
|
|
|
applied=applied,
|
|
|
|
|
title=self.title,
|
|
|
|
|
routing=self.kind == "routing",
|
|
|
|
|
**kwargs,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
if self.kind == "routing" and "preview" in self.request.POST:
|
|
|
|
|
return self.render_to_response(
|
|
|
|
|
self.get_context_data(form=form, preview=form.spec)
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
queue_policy(
|
|
|
|
|
self.publications.objects,
|
|
|
|
|
self.target,
|
|
|
|
|
self.object,
|
|
|
|
|
form.spec,
|
|
|
|
|
form.cleaned_data["revision"],
|
|
|
|
|
self.request.user,
|
|
|
|
|
)
|
|
|
|
|
except ValidationError as error:
|
|
|
|
|
form.add_error(None, error)
|
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
return redirect(self.request.path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailRoutingEditor(MailPolicyEditor):
|
|
|
|
|
queryset = HostingMailDomain.objects.select_related(
|
|
|
|
|
"account__hosting_plan"
|
|
|
|
|
)
|
|
|
|
|
customer_relation = "account__customer"
|
|
|
|
|
reseller_relation = "account__reseller"
|
|
|
|
|
form_class = MailRoutingForm
|
|
|
|
|
publications = MailRoutingPublication
|
|
|
|
|
kind = "routing"
|
|
|
|
|
target = "mail_domain"
|
|
|
|
|
title = "Mail routing"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailVacationEditor(MailPolicyEditor):
|
|
|
|
|
queryset = HostingMailbox.objects.select_related(
|
|
|
|
|
"mail_domain__account__hosting_plan",
|
|
|
|
|
)
|
|
|
|
|
customer_relation = "mail_domain__account__customer"
|
|
|
|
|
reseller_relation = "mail_domain__account__reseller"
|
|
|
|
|
form_class = MailVacationForm
|
|
|
|
|
publications = MailVacationPublication
|
|
|
|
|
kind = "vacation"
|
|
|
|
|
target = "mailbox"
|
|
|
|
|
title = "Automatic replies"
|
2026-11-23 23:27:02 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailSpamEditor(MailVacationEditor):
|
|
|
|
|
form_class = MailSpamForm
|
|
|
|
|
title = "Spam protection"
|
|
|
|
|
extra_context = {"spam_editor": True}
|
|
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
|
initial = super().get_initial()
|
|
|
|
|
if self.job:
|
|
|
|
|
spec = parse_mail_policy(self.job.manifest)
|
|
|
|
|
initial["move_spam"] = spec.move_spam
|
|
|
|
|
initial["junk_senders"] = "\n".join(spec.junk_senders)
|
|
|
|
|
return initial
|