8 Virtual Mail Manager's Domain class to manage e-mail domains. |
8 Virtual Mail Manager's Domain class to manage e-mail domains. |
9 """ |
9 """ |
10 |
10 |
11 import os |
11 import os |
12 import re |
12 import re |
13 from encodings.idna import ToASCII, ToUnicode |
|
14 from random import choice |
13 from random import choice |
15 |
14 |
16 from VirtualMailManager.constants.ERROR import \ |
15 from VirtualMailManager.constants.ERROR import \ |
17 ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \ |
16 ACCOUNT_AND_ALIAS_PRESENT, ACCOUNT_PRESENT, ALIAS_PRESENT, \ |
18 DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, DOMAIN_INVALID, DOMAIN_TOO_LONG, \ |
17 DOMAIN_ALIAS_EXISTS, DOMAIN_EXISTS, DOMAIN_INVALID, DOMAIN_TOO_LONG, \ |
317 if anames: |
316 if anames: |
318 aliasdomains = [aname[0] for aname in anames] |
317 aliasdomains = [aname[0] for aname in anames] |
319 return aliasdomains |
318 return aliasdomains |
320 |
319 |
321 |
320 |
322 def ace2idna(domainname): |
|
323 """Converts the domain name `domainname` from ACE according to IDNA.""" |
|
324 return u'.'.join([ToUnicode(lbl) for lbl in domainname.split('.') if lbl]) |
|
325 |
|
326 |
|
327 def check_domainname(domainname): |
321 def check_domainname(domainname): |
328 """Returns the validated domain name `domainname`. |
322 """Returns the validated domain name `domainname`. |
329 |
323 |
330 It also converts the name of the domain from IDN to ASCII, if |
|
331 necessary. |
|
332 |
|
333 Throws an `DomainError`, if the domain name is too long or doesn't |
324 Throws an `DomainError`, if the domain name is too long or doesn't |
334 look like a valid domain name (label.label.label). |
325 look like a valid domain name (label.label.label). |
335 |
326 |
336 """ |
327 """ |
337 if not RE_DOMAIN.match(domainname): |
328 if not RE_DOMAIN.match(domainname): |
338 domainname = idn2ascii(domainname) |
329 domainname = domainname.encode('idna') |
339 if len(domainname) > 255: |
330 if len(domainname) > 255: |
340 raise DomErr(_(u'The domain name is too long'), DOMAIN_TOO_LONG) |
331 raise DomErr(_(u'The domain name is too long'), DOMAIN_TOO_LONG) |
341 if not RE_DOMAIN.match(domainname): |
332 if not RE_DOMAIN.match(domainname): |
342 raise DomErr(_(u"The domain name '%s' is invalid") % domainname, |
333 raise DomErr(_(u"The domain name '%s' is invalid") % domainname, |
343 DOMAIN_INVALID) |
334 DOMAIN_INVALID) |