2021-10-13 13:32:27 +02:00
|
|
|
import arrow
|
2021-10-12 14:36:47 +02:00
|
|
|
import re2 as re
|
2019-12-27 23:50:09 +01:00
|
|
|
from flask import render_template, request, redirect, url_for, flash
|
|
|
|
from flask_login import login_required, current_user
|
2021-09-20 18:28:43 +02:00
|
|
|
from flask_wtf import FlaskForm
|
|
|
|
from wtforms import StringField, validators, IntegerField
|
2019-12-27 23:50:09 +01:00
|
|
|
|
2021-10-13 11:43:44 +02:00
|
|
|
from app.config import EMAIL_SERVERS_WITH_PRIORITY, EMAIL_DOMAIN, JOB_DELETE_DOMAIN
|
2019-12-27 23:50:09 +01:00
|
|
|
from app.dashboard.base import dashboard_bp
|
2021-10-12 14:36:47 +02:00
|
|
|
from app.db import Session
|
2019-12-27 23:50:09 +01:00
|
|
|
from app.dns_utils import (
|
|
|
|
get_mx_domains,
|
|
|
|
get_spf_domain,
|
|
|
|
get_txt_record,
|
2020-05-03 11:19:14 +02:00
|
|
|
get_cname_record,
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
2020-11-12 17:41:47 +01:00
|
|
|
from app.log import LOG
|
2021-09-20 18:28:43 +02:00
|
|
|
from app.models import (
|
|
|
|
CustomDomain,
|
|
|
|
Alias,
|
|
|
|
DomainDeletedAlias,
|
|
|
|
Mailbox,
|
|
|
|
DomainMailbox,
|
|
|
|
AutoCreateRule,
|
|
|
|
AutoCreateRuleMailbox,
|
2021-10-13 11:43:44 +02:00
|
|
|
Job,
|
2021-09-20 18:28:43 +02:00
|
|
|
)
|
2021-08-17 19:03:15 +02:00
|
|
|
from app.utils import random_string
|
2019-12-27 23:50:09 +01:00
|
|
|
|
|
|
|
|
2019-12-30 17:52:08 +01:00
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>/dns", methods=["GET", "POST"])
|
2019-12-27 23:50:09 +01:00
|
|
|
@login_required
|
2019-12-30 17:52:08 +01:00
|
|
|
def domain_detail_dns(custom_domain_id):
|
2021-08-17 19:03:15 +02:00
|
|
|
custom_domain: CustomDomain = CustomDomain.get(custom_domain_id)
|
2019-12-27 23:50:09 +01:00
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2021-08-17 19:03:15 +02:00
|
|
|
# generate a domain ownership txt token if needed
|
|
|
|
if not custom_domain.ownership_verified and not custom_domain.ownership_txt_token:
|
|
|
|
custom_domain.ownership_txt_token = random_string(30)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-08-17 19:03:15 +02:00
|
|
|
|
2021-09-23 10:30:59 +02:00
|
|
|
spf_record = f"v=spf1 include:{EMAIL_DOMAIN} ~all"
|
2020-05-03 11:19:14 +02:00
|
|
|
|
|
|
|
# hardcode the DKIM selector here
|
|
|
|
dkim_cname = f"dkim._domainkey.{EMAIL_DOMAIN}"
|
|
|
|
|
2020-05-03 12:01:31 +02:00
|
|
|
dmarc_record = "v=DMARC1; p=quarantine; pct=100; adkim=s; aspf=s"
|
|
|
|
|
2021-08-17 19:05:12 +02:00
|
|
|
mx_ok = spf_ok = dkim_ok = dmarc_ok = ownership_ok = True
|
|
|
|
mx_errors = spf_errors = dkim_errors = dmarc_errors = ownership_errors = []
|
2019-12-27 23:50:09 +01:00
|
|
|
|
|
|
|
if request.method == "POST":
|
2021-08-17 19:05:12 +02:00
|
|
|
if request.form.get("form-name") == "check-ownership":
|
|
|
|
txt_records = get_txt_record(custom_domain.domain)
|
|
|
|
|
2021-08-17 19:19:49 +02:00
|
|
|
if custom_domain.get_ownership_dns_txt_value() in txt_records:
|
2021-08-17 19:05:12 +02:00
|
|
|
flash(
|
|
|
|
"Domain ownership is verified. Please proceed to the other records setup",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
custom_domain.ownership_verified = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-08-17 19:05:12 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_dns",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
_anchor="dns-setup",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash("We can't find the needed TXT record", "error")
|
2021-08-17 19:39:58 +02:00
|
|
|
ownership_ok = False
|
2021-08-17 19:05:12 +02:00
|
|
|
ownership_errors = txt_records
|
|
|
|
|
|
|
|
elif request.form.get("form-name") == "check-mx":
|
2019-12-27 23:50:09 +01:00
|
|
|
mx_domains = get_mx_domains(custom_domain.domain)
|
|
|
|
|
2019-12-30 19:34:38 +01:00
|
|
|
if sorted(mx_domains) != sorted(EMAIL_SERVERS_WITH_PRIORITY):
|
2020-01-02 22:22:09 +01:00
|
|
|
flash("The MX record is not correctly set", "warning")
|
2020-08-31 17:11:18 +02:00
|
|
|
|
2019-12-27 23:50:09 +01:00
|
|
|
mx_ok = False
|
2019-12-30 19:34:38 +01:00
|
|
|
# build mx_errors to show to user
|
|
|
|
mx_errors = [
|
|
|
|
f"{priority} {domain}" for (priority, domain) in mx_domains
|
|
|
|
]
|
2019-12-27 23:50:09 +01:00
|
|
|
else:
|
|
|
|
flash(
|
2020-05-03 12:01:31 +02:00
|
|
|
"Your domain can start receiving emails. You can now use it to create alias",
|
2019-12-27 23:50:09 +01:00
|
|
|
"success",
|
|
|
|
)
|
|
|
|
custom_domain.verified = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-12-27 23:50:09 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
elif request.form.get("form-name") == "check-spf":
|
|
|
|
spf_domains = get_spf_domain(custom_domain.domain)
|
2019-12-30 19:34:38 +01:00
|
|
|
if EMAIL_DOMAIN in spf_domains:
|
2019-12-27 23:50:09 +01:00
|
|
|
custom_domain.spf_verified = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-03 12:01:31 +02:00
|
|
|
flash("SPF is setup correctly", "success")
|
2019-12-27 23:50:09 +01:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2020-05-14 15:05:04 +02:00
|
|
|
custom_domain.spf_verified = False
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-01-02 22:22:09 +01:00
|
|
|
flash(
|
|
|
|
f"SPF: {EMAIL_DOMAIN} is not included in your SPF record.",
|
|
|
|
"warning",
|
|
|
|
)
|
2019-12-30 19:34:38 +01:00
|
|
|
spf_ok = False
|
2019-12-27 23:50:09 +01:00
|
|
|
spf_errors = get_txt_record(custom_domain.domain)
|
|
|
|
|
|
|
|
elif request.form.get("form-name") == "check-dkim":
|
2020-05-03 12:48:42 +02:00
|
|
|
dkim_record = get_cname_record("dkim._domainkey." + custom_domain.domain)
|
2020-05-03 11:19:14 +02:00
|
|
|
if dkim_record == dkim_cname:
|
2020-05-03 12:01:31 +02:00
|
|
|
flash("DKIM is setup correctly.", "success")
|
2019-12-27 23:50:09 +01:00
|
|
|
custom_domain.dkim_verified = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-12-27 23:50:09 +01:00
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2019-12-30 21:11:31 +01:00
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
2019-12-27 23:50:09 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2020-05-14 15:05:04 +02:00
|
|
|
custom_domain.dkim_verified = False
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-03 11:19:14 +02:00
|
|
|
flash("DKIM: the CNAME record is not correctly set", "warning")
|
2019-12-27 23:50:09 +01:00
|
|
|
dkim_ok = False
|
2020-05-03 11:19:14 +02:00
|
|
|
dkim_errors = [dkim_record or "[Empty]"]
|
2019-12-27 23:50:09 +01:00
|
|
|
|
2020-05-03 12:01:31 +02:00
|
|
|
elif request.form.get("form-name") == "check-dmarc":
|
|
|
|
txt_records = get_txt_record("_dmarc." + custom_domain.domain)
|
|
|
|
if dmarc_record in txt_records:
|
|
|
|
custom_domain.dmarc_verified = True
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-03 12:01:31 +02:00
|
|
|
flash("DMARC is setup correctly", "success")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_dns", custom_domain_id=custom_domain.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
2020-05-14 15:05:04 +02:00
|
|
|
custom_domain.dmarc_verified = False
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-03 12:01:31 +02:00
|
|
|
flash(
|
2020-12-06 22:01:38 +01:00
|
|
|
"DMARC: The TXT record is not correctly set",
|
2020-08-27 10:20:48 +02:00
|
|
|
"warning",
|
2020-05-03 12:01:31 +02:00
|
|
|
)
|
|
|
|
dmarc_ok = False
|
|
|
|
dmarc_errors = txt_records
|
|
|
|
|
2019-12-30 18:58:44 +01:00
|
|
|
return render_template(
|
|
|
|
"dashboard/domain_detail/dns.html",
|
|
|
|
EMAIL_SERVERS_WITH_PRIORITY=EMAIL_SERVERS_WITH_PRIORITY,
|
|
|
|
**locals(),
|
|
|
|
)
|
2019-12-30 17:52:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>/info", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def domain_detail(custom_domain_id):
|
2021-09-17 17:41:36 +02:00
|
|
|
custom_domain: CustomDomain = CustomDomain.get(custom_domain_id)
|
2021-09-17 11:54:37 +02:00
|
|
|
mailboxes = current_user.mailboxes()
|
|
|
|
|
2019-12-30 17:52:08 +01:00
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
2019-12-30 18:20:49 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "switch-catch-all":
|
|
|
|
custom_domain.catch_all = not custom_domain.catch_all
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2019-12-30 18:20:49 +01:00
|
|
|
|
|
|
|
if custom_domain.catch_all:
|
|
|
|
flash(
|
|
|
|
f"The catch-all has been enabled for {custom_domain.domain}",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
f"The catch-all has been disabled for {custom_domain.domain}",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
|
|
|
|
)
|
2020-05-03 16:05:34 +02:00
|
|
|
elif request.form.get("form-name") == "set-name":
|
2020-11-24 12:02:47 +01:00
|
|
|
if request.form.get("action") == "save":
|
|
|
|
custom_domain.name = request.form.get("alias-name").replace("\n", "")
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-24 12:02:47 +01:00
|
|
|
flash(
|
|
|
|
f"Default alias name for Domain {custom_domain.domain} has been set",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
custom_domain.name = None
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-11-24 12:02:47 +01:00
|
|
|
flash(
|
|
|
|
f"Default alias name for Domain {custom_domain.domain} has been removed",
|
|
|
|
"info",
|
|
|
|
)
|
2020-05-03 16:05:34 +02:00
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
|
|
|
|
)
|
2020-10-09 22:54:13 +02:00
|
|
|
elif request.form.get("form-name") == "switch-random-prefix-generation":
|
2020-10-09 23:00:10 +02:00
|
|
|
custom_domain.random_prefix_generation = (
|
|
|
|
not custom_domain.random_prefix_generation
|
|
|
|
)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-10-09 22:54:13 +02:00
|
|
|
|
|
|
|
if custom_domain.random_prefix_generation:
|
|
|
|
flash(
|
|
|
|
f"Random prefix generation has been enabled for {custom_domain.domain}",
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
f"Random prefix generation has been disabled for {custom_domain.domain}",
|
|
|
|
"warning",
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
|
|
|
|
)
|
2021-09-17 11:54:37 +02:00
|
|
|
elif request.form.get("form-name") == "update":
|
|
|
|
mailbox_ids = request.form.getlist("mailbox_ids")
|
|
|
|
# check if mailbox is not tempered with
|
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
|
|
|
flash("Something went wrong, please retry", "warning")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail", custom_domain_id=custom_domain.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
flash("You must select at least 1 mailbox", "warning")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail", custom_domain_id=custom_domain.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# first remove all existing domain-mailboxes links
|
2021-10-12 14:36:47 +02:00
|
|
|
DomainMailbox.filter_by(domain_id=custom_domain.id).delete()
|
|
|
|
Session.flush()
|
2021-09-17 11:54:37 +02:00
|
|
|
|
|
|
|
for mailbox in mailboxes:
|
|
|
|
DomainMailbox.create(domain_id=custom_domain.id, mailbox_id=mailbox.id)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-09-17 11:54:37 +02:00
|
|
|
flash(f"{custom_domain.domain} mailboxes has been updated", "success")
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for("dashboard.domain_detail", custom_domain_id=custom_domain.id)
|
|
|
|
)
|
2021-09-17 17:41:36 +02:00
|
|
|
|
2019-12-30 18:51:55 +01:00
|
|
|
elif request.form.get("form-name") == "delete":
|
|
|
|
name = custom_domain.domain
|
2020-11-12 17:41:47 +01:00
|
|
|
LOG.d("Schedule deleting %s", custom_domain)
|
2021-10-13 11:43:44 +02:00
|
|
|
|
|
|
|
# Schedule delete domain job
|
|
|
|
LOG.w("schedule delete domain job for %s", custom_domain)
|
|
|
|
Job.create(
|
|
|
|
name=JOB_DELETE_DOMAIN,
|
|
|
|
payload={"custom_domain_id": custom_domain.id},
|
|
|
|
run_at=arrow.now(),
|
|
|
|
commit=True,
|
|
|
|
)
|
|
|
|
|
2020-11-12 17:41:47 +01:00
|
|
|
flash(
|
|
|
|
f"{name} scheduled for deletion."
|
|
|
|
f"You will receive a confirmation email when the deletion is finished",
|
|
|
|
"success",
|
|
|
|
)
|
2019-12-30 18:51:55 +01:00
|
|
|
|
|
|
|
return redirect(url_for("dashboard.custom_domain"))
|
2019-12-30 18:20:49 +01:00
|
|
|
|
2020-03-17 11:51:40 +01:00
|
|
|
nb_alias = Alias.filter_by(custom_domain_id=custom_domain.id).count()
|
2019-12-30 17:52:08 +01:00
|
|
|
|
|
|
|
return render_template("dashboard/domain_detail/info.html", **locals())
|
2020-05-23 11:48:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
@dashboard_bp.route("/domains/<int:custom_domain_id>/trash", methods=["GET", "POST"])
|
|
|
|
@login_required
|
|
|
|
def domain_detail_trash(custom_domain_id):
|
|
|
|
custom_domain = CustomDomain.get(custom_domain_id)
|
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "empty-all":
|
|
|
|
DomainDeletedAlias.filter_by(domain_id=custom_domain.id).delete()
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-23 11:48:43 +02:00
|
|
|
|
|
|
|
flash("All deleted aliases can now be re-created", "success")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_trash", custom_domain_id=custom_domain.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif request.form.get("form-name") == "remove-single":
|
|
|
|
deleted_alias_id = request.form.get("deleted-alias-id")
|
|
|
|
deleted_alias = DomainDeletedAlias.get(deleted_alias_id)
|
|
|
|
if not deleted_alias or deleted_alias.domain_id != custom_domain.id:
|
|
|
|
flash("Unknown error, refresh the page", "warning")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_trash",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
DomainDeletedAlias.delete(deleted_alias.id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2020-05-23 11:48:43 +02:00
|
|
|
flash(
|
2020-08-27 10:20:48 +02:00
|
|
|
f"{deleted_alias.email} can now be re-created",
|
|
|
|
"success",
|
2020-05-23 11:48:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_trash", custom_domain_id=custom_domain.id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
domain_deleted_aliases = DomainDeletedAlias.filter_by(
|
|
|
|
domain_id=custom_domain.id
|
|
|
|
).all()
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/domain_detail/trash.html",
|
|
|
|
domain_deleted_aliases=domain_deleted_aliases,
|
|
|
|
custom_domain=custom_domain,
|
|
|
|
)
|
2021-09-20 18:28:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
class AutoCreateRuleForm(FlaskForm):
|
|
|
|
regex = StringField(
|
|
|
|
"regex", validators=[validators.DataRequired(), validators.Length(max=128)]
|
|
|
|
)
|
|
|
|
|
|
|
|
order = IntegerField(
|
|
|
|
"order",
|
|
|
|
validators=[validators.DataRequired(), validators.NumberRange(min=0, max=100)],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-21 10:14:36 +02:00
|
|
|
class AutoCreateTestForm(FlaskForm):
|
|
|
|
local = StringField(
|
|
|
|
"local part", validators=[validators.DataRequired(), validators.Length(max=128)]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-20 18:28:43 +02:00
|
|
|
@dashboard_bp.route(
|
|
|
|
"/domains/<int:custom_domain_id>/auto-create", methods=["GET", "POST"]
|
|
|
|
)
|
|
|
|
@login_required
|
|
|
|
def domain_detail_auto_create(custom_domain_id):
|
|
|
|
custom_domain: CustomDomain = CustomDomain.get(custom_domain_id)
|
|
|
|
mailboxes = current_user.mailboxes()
|
|
|
|
new_auto_create_rule_form = AutoCreateRuleForm()
|
|
|
|
|
2021-09-21 10:14:36 +02:00
|
|
|
auto_create_test_form = AutoCreateTestForm()
|
|
|
|
auto_create_test_local, auto_create_test_result, auto_create_test_passed = (
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
False,
|
|
|
|
)
|
|
|
|
|
2021-09-20 18:28:43 +02:00
|
|
|
if not custom_domain or custom_domain.user_id != current_user.id:
|
|
|
|
flash("You cannot see this page", "warning")
|
|
|
|
return redirect(url_for("dashboard.index"))
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if request.form.get("form-name") == "create-auto-create-rule":
|
|
|
|
if new_auto_create_rule_form.validate():
|
|
|
|
# make sure order isn't used before
|
|
|
|
for auto_create_rule in custom_domain.auto_create_rules:
|
|
|
|
auto_create_rule: AutoCreateRule
|
|
|
|
if auto_create_rule.order == int(
|
|
|
|
new_auto_create_rule_form.order.data
|
|
|
|
):
|
|
|
|
flash(
|
|
|
|
"Another rule with the same order already exists", "error"
|
|
|
|
)
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
mailbox_ids = request.form.getlist("mailbox_ids")
|
|
|
|
# check if mailbox is not tempered with
|
|
|
|
mailboxes = []
|
|
|
|
for mailbox_id in mailbox_ids:
|
|
|
|
mailbox = Mailbox.get(mailbox_id)
|
|
|
|
if (
|
|
|
|
not mailbox
|
|
|
|
or mailbox.user_id != current_user.id
|
|
|
|
or not mailbox.verified
|
|
|
|
):
|
|
|
|
flash("Something went wrong, please retry", "warning")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
mailboxes.append(mailbox)
|
|
|
|
|
|
|
|
if not mailboxes:
|
|
|
|
flash("You must select at least 1 mailbox", "warning")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2021-09-21 10:42:02 +02:00
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
re.compile(new_auto_create_rule_form.regex.data)
|
|
|
|
except:
|
|
|
|
flash(
|
|
|
|
f"Invalid regex {new_auto_create_rule_form.regex.data}",
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
2021-09-20 18:28:43 +02:00
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
rule = AutoCreateRule.create(
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
order=int(new_auto_create_rule_form.order.data),
|
|
|
|
regex=new_auto_create_rule_form.regex.data,
|
|
|
|
flush=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
for mailbox in mailboxes:
|
|
|
|
AutoCreateRuleMailbox.create(
|
|
|
|
auto_create_rule_id=rule.id, mailbox_id=mailbox.id
|
|
|
|
)
|
|
|
|
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-09-20 18:28:43 +02:00
|
|
|
|
|
|
|
flash("New auto create rule has been created", "success")
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
elif request.form.get("form-name") == "delete-auto-create-rule":
|
|
|
|
rule_id = request.form.get("rule-id")
|
|
|
|
rule: AutoCreateRule = AutoCreateRule.get(int(rule_id))
|
|
|
|
|
|
|
|
if not rule or rule.custom_domain_id != custom_domain.id:
|
|
|
|
flash("Something wrong, please retry", "error")
|
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
rule_order = rule.order
|
|
|
|
AutoCreateRule.delete(rule_id)
|
2021-10-12 14:36:47 +02:00
|
|
|
Session.commit()
|
2021-09-20 18:28:43 +02:00
|
|
|
flash(f"Rule #{rule_order} has been deleted", "success")
|
2021-09-21 10:42:32 +02:00
|
|
|
return redirect(
|
|
|
|
url_for(
|
|
|
|
"dashboard.domain_detail_auto_create",
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
)
|
|
|
|
)
|
2021-09-21 10:14:36 +02:00
|
|
|
elif request.form.get("form-name") == "test-auto-create-rule":
|
|
|
|
if auto_create_test_form.validate():
|
|
|
|
local = auto_create_test_form.local.data
|
|
|
|
auto_create_test_local = local
|
|
|
|
|
|
|
|
for rule in custom_domain.auto_create_rules:
|
|
|
|
rule: AutoCreateRule
|
|
|
|
regex = re.compile(rule.regex)
|
|
|
|
if re.fullmatch(regex, local):
|
|
|
|
auto_create_test_result = (
|
|
|
|
f"{local}@{custom_domain.domain} passes rule #{rule.order}"
|
|
|
|
)
|
|
|
|
auto_create_test_passed = True
|
|
|
|
break
|
|
|
|
else: # no rule passes
|
|
|
|
auto_create_test_result = (
|
|
|
|
f"{local}@{custom_domain.domain} doesn't pass any rule"
|
|
|
|
)
|
|
|
|
|
|
|
|
return render_template(
|
|
|
|
"dashboard/domain_detail/auto-create.html", **locals()
|
|
|
|
)
|
2021-09-20 18:28:43 +02:00
|
|
|
|
|
|
|
return render_template("dashboard/domain_detail/auto-create.html", **locals())
|