VirtualMailManager/emailaddress.py
branchv0.6.x
changeset 427 8e41e77b84e6
parent 417 8209da83e256
child 568 14abdd04ddf5
equal deleted inserted replaced
426:933b9debbec1 427:8e41e77b84e6
     9 """
     9 """
    10 import re
    10 import re
    11 
    11 
    12 from VirtualMailManager.domain import check_domainname, get_gid
    12 from VirtualMailManager.domain import check_domainname, get_gid
    13 from VirtualMailManager.constants import \
    13 from VirtualMailManager.constants import \
    14      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG
    14      DOMAIN_NO_NAME, INVALID_ADDRESS, LOCALPART_INVALID, LOCALPART_TOO_LONG, \
    15 from VirtualMailManager.errors import EmailAddressError as EAErr
    15      DOMAIN_INVALID
       
    16 from VirtualMailManager.errors import DomainError, EmailAddressError as EAErr
    16 
    17 
    17 
    18 
    18 RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]")
    19 RE_LOCALPART = re.compile(r"[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]")
    19 _ = lambda msg: msg
    20 _ = lambda msg: msg
    20 
    21 
    21 
    22 
    22 class EmailAddress(object):
    23 class EmailAddress(object):
    23     """Simple class for validated e-mail addresses."""
    24     """Simple class for validated e-mail addresses."""
    24     __slots__ = ('_localpart', '_domainname')
    25     __slots__ = ('_localpart', '_domainname')
    25 
    26 
    26     def __init__(self, address):
    27     def __init__(self, address, _validate=True):
    27         """Creates a new instance from the string/unicode ``address``."""
    28         """Creates a new instance from the string/unicode ``address``."""
    28         assert isinstance(address, basestring)
    29         assert isinstance(address, basestring)
    29         self._localpart = None
    30         self._localpart = None
    30         self._domainname = None
    31         self._domainname = None
    31         self._chk_address(address)
    32         if _validate:
       
    33             self._chk_address(address)
    32 
    34 
    33     @property
    35     @property
    34     def localpart(self):
    36     def localpart(self):
    35         """The local-part of the address *local-part@domain*"""
    37         """The local-part of the address *local-part@domain*"""
    36         return self._localpart
    38         return self._localpart
    84 
    86 
    85 
    87 
    86 class DestinationEmailAddress(EmailAddress):
    88 class DestinationEmailAddress(EmailAddress):
    87     """Provides additionally the domains group ID - when the domain is known
    89     """Provides additionally the domains group ID - when the domain is known
    88     in the database."""
    90     in the database."""
    89     __slots__ = ('_gid')
    91     __slots__ = ('_gid', '_localhost')
    90 
    92 
    91     def __init__(self, address, dbh):
    93     def __init__(self, address, dbh, _validate=False):
    92         """Creates a new DestinationEmailAddress instance
    94         """Creates a new DestinationEmailAddress instance
    93 
    95 
    94         Arguments:
    96         Arguments:
    95 
    97 
    96         `address`: string/unicode
    98         `address`: string/unicode
    97           a e-mail address like user@example.com
    99           a e-mail address like user@example.com
    98         `dbh`: pyPgSQL.PgSQL.Connection/pyPgSQL.PgSQL.connection
   100         `dbh`: pyPgSQL.PgSQL.Connection/pyPgSQL.PgSQL.connection
    99           a database connection for the database access
   101           a database connection for the database access
   100         """
   102         """
   101         super(DestinationEmailAddress, self).__init__(address)
   103         super(DestinationEmailAddress, self).__init__(address, _validate)
       
   104         self._localhost = False
       
   105         if not _validate:
       
   106             try:
       
   107                 self._chk_address(address)
       
   108             except DomainError, err:
       
   109                 if err.code is DOMAIN_INVALID and \
       
   110                    address.split('@')[1] == 'localhost':
       
   111                     self._localhost = True
       
   112                     self._domainname = 'localhost'
       
   113                 else:
       
   114                     raise
   102         self._gid = 0
   115         self._gid = 0
   103         self._find_domain(dbh)
   116         if not self._localhost:
       
   117             self._find_domain(dbh)
       
   118         else:
       
   119             self._localpart = self._localpart.lower()
   104 
   120 
   105     def _find_domain(self, dbh):
   121     def _find_domain(self, dbh):
   106         """Checks if the domain is known"""
   122         """Checks if the domain is known"""
   107         self._gid = get_gid(dbh, self._domainname)
   123         self._gid = get_gid(dbh, self._domainname)
   108         if self._gid:
   124         if self._gid:
   109             self._localpart = self._localpart.lower()
   125             self._localpart = self._localpart.lower()
       
   126 
       
   127     @property
       
   128     def at_localhost(self):
       
   129         """True when the address is something@localhost."""
       
   130         return self._localhost
   110 
   131 
   111     @property
   132     @property
   112     def gid(self):
   133     def gid(self):
   113         """The domains group ID. 0 if the domain is not known."""
   134         """The domains group ID. 0 if the domain is not known."""
   114         return self._gid
   135         return self._gid