VirtualMailManager/Domain.py
branchv0.6.x
changeset 294 7d1bafc6fa30
parent 290 e2785e04f92e
child 316 31d8931dc535
equal deleted inserted replaced
293:a77b67673aa6 294:7d1bafc6fa30
     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)
   355     gid = dbc.fetchone()
   346     gid = dbc.fetchone()
   356     dbc.close()
   347     dbc.close()
   357     if gid:
   348     if gid:
   358         return gid[0]
   349         return gid[0]
   359     return 0
   350     return 0
   360 
       
   361 
       
   362 def idn2ascii(domainname):
       
   363     """Converts the idn domain name `domainname` into punycode."""
       
   364     return '.'.join([ToASCII(lbl) for lbl in domainname.split('.') if lbl])
       
   365 
   351 
   366 
   352 
   367 def search(dbh, pattern=None, like=False):
   353 def search(dbh, pattern=None, like=False):
   368     """'Search' for domains by *pattern* in the database.
   354     """'Search' for domains by *pattern* in the database.
   369 
   355