2020-04-04 15:24:27 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2020-05-07 22:27:27 +02:00
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
|
|
|
2020-04-04 15:24:27 +02:00
|
|
|
from app.email_utils import (
|
|
|
|
get_email_domain_part,
|
|
|
|
send_cannot_create_directory_alias,
|
|
|
|
send_cannot_create_domain_alias,
|
|
|
|
email_belongs_to_alias_domains,
|
|
|
|
)
|
2020-05-23 19:45:26 +02:00
|
|
|
from app.errors import AliasInTrashError
|
2020-04-04 15:24:27 +02:00
|
|
|
from app.extensions import db
|
|
|
|
from app.log import LOG
|
|
|
|
from app.models import (
|
|
|
|
Alias,
|
|
|
|
CustomDomain,
|
|
|
|
Directory,
|
|
|
|
User,
|
|
|
|
DeletedAlias,
|
2020-05-23 11:49:34 +02:00
|
|
|
DomainDeletedAlias,
|
2020-06-05 22:30:32 +02:00
|
|
|
AliasMailbox,
|
2020-04-04 15:24:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def try_auto_create(address: str) -> Optional[Alias]:
|
2020-08-27 10:20:48 +02:00
|
|
|
"""Try to auto-create the alias using directory or catch-all domain"""
|
2020-04-04 15:24:27 +02:00
|
|
|
alias = try_auto_create_catch_all_domain(address)
|
|
|
|
if not alias:
|
|
|
|
alias = try_auto_create_directory(address)
|
|
|
|
|
|
|
|
return alias
|
|
|
|
|
|
|
|
|
|
|
|
def try_auto_create_directory(address: str) -> Optional[Alias]:
|
|
|
|
"""
|
|
|
|
Try to create an alias with directory
|
|
|
|
"""
|
|
|
|
# check if alias belongs to a directory, ie having directory/anything@EMAIL_DOMAIN format
|
|
|
|
if email_belongs_to_alias_domains(address):
|
|
|
|
# if there's no directory separator in the alias, no way to auto-create it
|
|
|
|
if "/" not in address and "+" not in address and "#" not in address:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# alias contains one of the 3 special directory separator: "/", "+" or "#"
|
|
|
|
if "/" in address:
|
|
|
|
sep = "/"
|
|
|
|
elif "+" in address:
|
|
|
|
sep = "+"
|
|
|
|
else:
|
|
|
|
sep = "#"
|
|
|
|
|
|
|
|
directory_name = address[: address.find(sep)]
|
|
|
|
LOG.d("directory_name %s", directory_name)
|
|
|
|
|
|
|
|
directory = Directory.get_by(name=directory_name)
|
|
|
|
if not directory:
|
|
|
|
return None
|
|
|
|
|
|
|
|
dir_user: User = directory.user
|
|
|
|
|
|
|
|
if not dir_user.can_create_new_alias():
|
|
|
|
send_cannot_create_directory_alias(dir_user, address, directory_name)
|
|
|
|
return None
|
|
|
|
|
2020-05-23 19:45:26 +02:00
|
|
|
try:
|
|
|
|
LOG.d("create alias %s for directory %s", address, directory)
|
|
|
|
|
2020-06-05 22:30:32 +02:00
|
|
|
mailboxes = directory.mailboxes
|
|
|
|
|
2020-05-23 19:45:26 +02:00
|
|
|
alias = Alias.create(
|
|
|
|
email=address,
|
|
|
|
user_id=directory.user_id,
|
|
|
|
directory_id=directory.id,
|
2020-06-05 22:30:32 +02:00
|
|
|
mailbox_id=mailboxes[0].id,
|
2020-05-23 19:45:26 +02:00
|
|
|
)
|
2020-06-05 22:30:32 +02:00
|
|
|
db.session.flush()
|
|
|
|
for i in range(1, len(mailboxes)):
|
|
|
|
AliasMailbox.create(
|
2020-08-27 10:20:48 +02:00
|
|
|
alias_id=alias.id,
|
|
|
|
mailbox_id=mailboxes[i].id,
|
2020-06-05 22:30:32 +02:00
|
|
|
)
|
2020-05-23 19:45:26 +02:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return alias
|
|
|
|
except AliasInTrashError:
|
2020-04-04 15:24:27 +02:00
|
|
|
LOG.warning(
|
|
|
|
"Alias %s was deleted before, cannot auto-create using directory %s, user %s",
|
|
|
|
address,
|
|
|
|
directory_name,
|
|
|
|
dir_user,
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def try_auto_create_catch_all_domain(address: str) -> Optional[Alias]:
|
|
|
|
"""Try to create an alias with catch-all domain"""
|
|
|
|
|
|
|
|
# try to create alias on-the-fly with custom-domain catch-all feature
|
|
|
|
# check if alias is custom-domain alias and if the custom-domain has catch-all enabled
|
|
|
|
alias_domain = get_email_domain_part(address)
|
2020-08-01 12:31:43 +02:00
|
|
|
custom_domain: CustomDomain = CustomDomain.get_by(domain=alias_domain)
|
2020-04-04 15:24:27 +02:00
|
|
|
|
|
|
|
if not custom_domain:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# custom_domain exists
|
|
|
|
if not custom_domain.catch_all:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# custom_domain has catch-all enabled
|
|
|
|
domain_user: User = custom_domain.user
|
|
|
|
|
|
|
|
if not domain_user.can_create_new_alias():
|
|
|
|
send_cannot_create_domain_alias(domain_user, address, alias_domain)
|
|
|
|
return None
|
|
|
|
|
2020-05-23 19:45:26 +02:00
|
|
|
try:
|
|
|
|
LOG.d("create alias %s for domain %s", address, custom_domain)
|
2020-08-01 12:31:43 +02:00
|
|
|
mailboxes = custom_domain.mailboxes
|
2020-05-23 19:45:26 +02:00
|
|
|
alias = Alias.create(
|
|
|
|
email=address,
|
|
|
|
user_id=custom_domain.user_id,
|
|
|
|
custom_domain_id=custom_domain.id,
|
|
|
|
automatic_creation=True,
|
2020-08-01 12:31:43 +02:00
|
|
|
mailbox_id=mailboxes[0].id,
|
2020-05-23 19:45:26 +02:00
|
|
|
)
|
2020-08-01 12:31:43 +02:00
|
|
|
db.session.flush()
|
|
|
|
for i in range(1, len(mailboxes)):
|
|
|
|
AliasMailbox.create(
|
2020-08-27 10:20:48 +02:00
|
|
|
alias_id=alias.id,
|
|
|
|
mailbox_id=mailboxes[i].id,
|
2020-08-01 12:31:43 +02:00
|
|
|
)
|
2020-05-23 19:45:26 +02:00
|
|
|
db.session.commit()
|
|
|
|
return alias
|
|
|
|
except AliasInTrashError:
|
2020-04-04 15:24:27 +02:00
|
|
|
LOG.warning(
|
|
|
|
"Alias %s was deleted before, cannot auto-create using domain catch-all %s, user %s",
|
|
|
|
address,
|
|
|
|
custom_domain,
|
|
|
|
domain_user,
|
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
2020-05-07 22:27:27 +02:00
|
|
|
|
|
|
|
def delete_alias(alias: Alias, user: User):
|
2020-08-14 12:02:33 +02:00
|
|
|
"""
|
|
|
|
Delete an alias and add it to either global or domain trash
|
|
|
|
Should be used instead of Alias.delete, DomainDeletedAlias.create, DeletedAlias.create
|
|
|
|
"""
|
2020-05-23 11:49:34 +02:00
|
|
|
# save deleted alias to either global or domain trash
|
|
|
|
if alias.custom_domain_id:
|
2020-08-23 20:24:46 +02:00
|
|
|
if not DomainDeletedAlias.get_by(
|
2020-08-23 20:17:50 +02:00
|
|
|
email=alias.email, domain_id=alias.custom_domain_id
|
|
|
|
):
|
|
|
|
LOG.debug("add %s to domain %s trash", alias, alias.custom_domain_id)
|
2020-08-14 12:02:33 +02:00
|
|
|
db.session.add(
|
|
|
|
DomainDeletedAlias(
|
|
|
|
user_id=user.id, email=alias.email, domain_id=alias.custom_domain_id
|
|
|
|
)
|
2020-05-23 11:49:34 +02:00
|
|
|
)
|
|
|
|
db.session.commit()
|
|
|
|
else:
|
2020-08-23 20:17:50 +02:00
|
|
|
if not DeletedAlias.get_by(email=alias.email):
|
|
|
|
LOG.d("add %s to global trash", alias)
|
2020-08-14 12:02:33 +02:00
|
|
|
db.session.add(DeletedAlias(email=alias.email))
|
2020-05-23 11:49:34 +02:00
|
|
|
db.session.commit()
|
2020-08-14 12:02:33 +02:00
|
|
|
|
|
|
|
Alias.query.filter(Alias.id == alias.id).delete()
|
|
|
|
db.session.commit()
|