2014-08-22 15:31:44 +00:00
|
|
|
import hashlib
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
2014-10-09 17:04:12 +00:00
|
|
|
from django.core.management.base import CommandError
|
2014-08-22 15:31:44 +00:00
|
|
|
from django.core.validators import ValidationError, EmailValidator
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.utils import paths
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.utils.sys import run
|
2014-08-22 15:31:44 +00:00
|
|
|
|
|
|
|
from . import settings
|
|
|
|
|
|
|
|
|
|
|
|
def validate_emailname(value):
|
2014-11-13 15:34:00 +00:00
|
|
|
msg = _("'%s' is not a correct email name." % value)
|
2014-08-22 15:31:44 +00:00
|
|
|
if '@' in value:
|
|
|
|
raise ValidationError(msg)
|
|
|
|
value += '@localhost'
|
|
|
|
try:
|
2014-11-12 12:46:41 +00:00
|
|
|
EmailValidator()(value)
|
2014-08-22 15:31:44 +00:00
|
|
|
except ValidationError:
|
|
|
|
raise ValidationError(msg)
|
|
|
|
|
|
|
|
|
|
|
|
def validate_forward(value):
|
|
|
|
""" space separated mailboxes or emails """
|
2014-10-09 17:04:12 +00:00
|
|
|
from .models import Mailbox
|
2014-11-13 15:34:00 +00:00
|
|
|
errors = []
|
2014-11-02 14:33:55 +00:00
|
|
|
destinations = []
|
2014-08-22 15:31:44 +00:00
|
|
|
for destination in value.split():
|
2014-11-02 14:33:55 +00:00
|
|
|
if destination in destinations:
|
2014-11-13 15:34:00 +00:00
|
|
|
errors.append(ValidationError(
|
|
|
|
_("'%s' is already present.") % destination
|
|
|
|
))
|
2014-11-02 14:33:55 +00:00
|
|
|
destinations.append(destination)
|
2014-10-09 17:04:12 +00:00
|
|
|
if '@' in destination:
|
2014-11-13 15:34:00 +00:00
|
|
|
try:
|
|
|
|
EmailValidator()(destination)
|
|
|
|
except ValidationError:
|
|
|
|
errors.append(ValidationError(
|
|
|
|
_("'%s' is not a valid email address.") % destination
|
|
|
|
))
|
|
|
|
elif not Mailbox.objects.filter(name=destination).exists():
|
|
|
|
errors.append(ValidationError(
|
|
|
|
_("'%s' is not an existent mailbox.") % destination
|
|
|
|
))
|
|
|
|
if errors:
|
|
|
|
raise ValidationError(errors)
|
2014-08-22 15:31:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def validate_sieve(value):
|
2015-04-28 15:23:57 +00:00
|
|
|
sieve_name = '%s.sieve' % hashlib.md5(value.encode('utf8')).hexdigest()
|
2015-05-06 15:32:22 +00:00
|
|
|
test_path = os.path.join(settings.MAILBOXES_SIEVETEST_PATH, sieve_name)
|
|
|
|
with open(test_path, 'w') as f:
|
2014-08-22 15:31:44 +00:00
|
|
|
f.write(value)
|
|
|
|
context = {
|
2015-03-31 12:39:08 +00:00
|
|
|
'orchestra_root': paths.get_orchestra_dir()
|
2014-08-22 15:31:44 +00:00
|
|
|
}
|
2014-10-17 10:04:47 +00:00
|
|
|
sievetest = settings.MAILBOXES_SIEVETEST_BIN_PATH % context
|
2015-05-06 15:32:22 +00:00
|
|
|
try:
|
|
|
|
test = run(' '.join([sievetest, test_path, '/dev/null']), silent=True)
|
|
|
|
finally:
|
|
|
|
os.unlink(test_path)
|
2015-04-28 15:23:57 +00:00
|
|
|
if test.return_code:
|
2014-08-22 15:31:44 +00:00
|
|
|
errors = []
|
2015-04-28 15:23:57 +00:00
|
|
|
for line in test.stderr.decode('utf8').splitlines():
|
2014-08-22 15:31:44 +00:00
|
|
|
error = re.match(r'^.*(line\s+[0-9]+:.*)', line)
|
|
|
|
if error:
|
|
|
|
errors += error.groups()
|
|
|
|
raise ValidationError(' '.join(errors))
|