VirtualMailManager/EmailAddress.py
branchv0.6.x
changeset 253 58d1b6f41664
parent 218 84094c7fa28b
child 254 8aecc83a0d32
equal deleted inserted replaced
252:af555e6967c8 253:58d1b6f41664
     5 """
     5 """
     6     VirtualMailManager.EmailAddress
     6     VirtualMailManager.EmailAddress
     7 
     7 
     8     Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
     8     Virtual Mail Manager's EmailAddress class to handle e-mail addresses.
     9 """
     9 """
       
    10 import re
    10 
    11 
    11 from VirtualMailManager import check_domainname, check_localpart
    12 from VirtualMailManager import check_domainname
    12 from VirtualMailManager.constants.ERROR import \
    13 from VirtualMailManager.constants.ERROR import \
    13      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID
    14      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG
    14 from VirtualMailManager.errors import EmailAddressError as EAErr
    15 from VirtualMailManager.errors import EmailAddressError as EAErr
    15 
    16 
    16 
    17 
       
    18 RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]")
    17 _ = lambda msg: msg
    19 _ = lambda msg: msg
    18 
    20 
    19 
    21 
    20 class EmailAddress(object):
    22 class EmailAddress(object):
    21     """Simple class for validated e-mail addresses."""
    23     """Simple class for validated e-mail addresses."""
    79                         DOMAIN_NO_NAME)
    81                         DOMAIN_NO_NAME)
    80         self._localpart = check_localpart(parts[0])
    82         self._localpart = check_localpart(parts[0])
    81         self._domainname = check_domainname(parts[1])
    83         self._domainname = check_domainname(parts[1])
    82 
    84 
    83 
    85 
       
    86 def check_localpart(localpart):
       
    87     """Returns the validated local-part `localpart`.
       
    88 
       
    89     Throws a `EmailAddressError` if the local-part is too long or contains
       
    90     invalid characters.
       
    91     """
       
    92     if len(localpart) > 64:
       
    93         raise EAErr(_(u"The local-part '%s' is too long") % localpart,
       
    94                     LOCALPART_TOO_LONG)
       
    95     invalid_chars = set(RE_LOCALPART.findall(localpart))
       
    96     if invalid_chars:
       
    97         i_chars = u''.join((u'"%s" ' % c for c in invalid_chars))
       
    98         raise EAErr(_(u"The local-part '%(l_part)s' contains invalid \
       
    99 characters: %(i_chars)s") %
       
   100                     {'l_part': localpart, 'i_chars': i_chars},
       
   101                     LOCALPART_INVALID)
       
   102     return localpart
       
   103 
       
   104 
    84 del _
   105 del _