VirtualMailManager/__init__.py
branchv0.6.x
changeset 253 58d1b6f41664
parent 234 e88ba0fb1281
child 254 8aecc83a0d32
equal deleted inserted replaced
252:af555e6967c8 253:58d1b6f41664
    14 import locale
    14 import locale
    15 
    15 
    16 from encodings.idna import ToASCII, ToUnicode
    16 from encodings.idna import ToASCII, ToUnicode
    17 
    17 
    18 from VirtualMailManager.constants.ERROR import \
    18 from VirtualMailManager.constants.ERROR import \
    19      DOMAIN_INVALID, DOMAIN_TOO_LONG, LOCALPART_INVALID, LOCALPART_TOO_LONG, \
    19      DOMAIN_INVALID, DOMAIN_TOO_LONG, NOT_EXECUTABLE, NO_SUCH_BINARY, \
    20      NOT_EXECUTABLE, NO_SUCH_BINARY, NO_SUCH_DIRECTORY
    20      NO_SUCH_DIRECTORY
    21 from VirtualMailManager.constants.version import __author__, __date__, \
    21 from VirtualMailManager.constants.version import __author__, __date__, \
    22      __version__
    22      __version__
    23 from VirtualMailManager.errors import VMMError
    23 from VirtualMailManager.errors import VMMError
    24 
    24 
    25 
    25 
    26 __all__ = [
    26 __all__ = [
    27     # version information from VERSION
    27     # version information from VERSION
    28     '__author__', '__date__', '__version__',
    28     '__author__', '__date__', '__version__',
    29     # defined stuff
    29     # defined stuff
    30     'ENCODING', 'ace2idna', 'check_domainname', 'check_localpart', 'exec_ok',
    30     'ENCODING', 'ace2idna', 'check_domainname', 'exec_ok', 'expand_path',
    31     'expand_path', 'get_unicode', 'idn2ascii', 'is_dir',
    31     'get_unicode', 'idn2ascii', 'is_dir',
    32 ]
    32 ]
    33 
    33 
    34 
    34 
    35 # Try to set all of the locales according to the current
    35 # Try to set all of the locales according to the current
    36 # environment variables and get the character encoding.
    36 # environment variables and get the character encoding.
    40     locale.setlocale(locale.LC_ALL, 'C')
    40     locale.setlocale(locale.LC_ALL, 'C')
    41 ENCODING = locale.nl_langinfo(locale.CODESET)
    41 ENCODING = locale.nl_langinfo(locale.CODESET)
    42 
    42 
    43 # there may be many domain and e-mail address checks
    43 # there may be many domain and e-mail address checks
    44 RE_DOMAIN = re.compile(r"^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$")
    44 RE_DOMAIN = re.compile(r"^(?:[a-z0-9-]{1,63}\.){1,}[a-z]{2,6}$")
    45 RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]")
       
    46 
    45 
    47 gettext.install('vmm', '/usr/local/share/locale', unicode=1)
    46 gettext.install('vmm', '/usr/local/share/locale', unicode=1)
    48 
    47 
    49 
    48 
    50 _ = lambda msg: msg
    49 _ = lambda msg: msg
   124         raise VMMError(_(u'The domain name %r is invalid') % domainname,
   123         raise VMMError(_(u'The domain name %r is invalid') % domainname,
   125                        DOMAIN_INVALID)
   124                        DOMAIN_INVALID)
   126     return domainname
   125     return domainname
   127 
   126 
   128 
   127 
   129 def check_localpart(localpart):
       
   130     """Returns the validated local-part `localpart`.
       
   131 
       
   132     Throws a `VMMError` if the local-part is too long or contains
       
   133     invalid characters.
       
   134 
       
   135     """
       
   136     if len(localpart) > 64:
       
   137         raise VMMError(_(u"The local-part '%s' is too long") % localpart,
       
   138                        LOCALPART_TOO_LONG)
       
   139     invalid_chars = set(RE_LOCALPART.findall(localpart))
       
   140     if invalid_chars:
       
   141         i_chars = u''.join((u'"%s" ' % c for c in invalid_chars))
       
   142         raise VMMError(_(u"The local-part '%(l_part)s' contains invalid \
       
   143 characters: %(i_chars)s") %
       
   144                        {'l_part': localpart, 'i_chars': i_chars},
       
   145                        LOCALPART_INVALID)
       
   146     return localpart
       
   147 
       
   148 
       
   149 del _
   128 del _