VirtualMailManager/EmailAddress.py
changeset 76 14c0a092d7d2
child 87 f9090d1a0730
equal deleted inserted replaced
75:af813ede1e19 76:14c0a092d7d2
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright 2008 VEB IT
       
     3 # See COPYING for distribution information.
       
     4 # $Id$
       
     5 
       
     6 """Virtual Mail Manager's EmailAddress class to handle e-mail addresses."""
       
     7 
       
     8 from constants.VERSION import VERSION
       
     9 
       
    10 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    11 __version__ = VERSION
       
    12 __revision__ = 'rev '+'$Rev$'.split()[1]
       
    13 __date__ = '$Date$'.split()[1]
       
    14 
       
    15 import re
       
    16 
       
    17 from Exceptions import VMMEmailAddressException as VMMEAE
       
    18 import VirtualMailManager as VMM
       
    19 import constants.ERROR as ERR
       
    20 
       
    21 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
       
    22 
       
    23 class EmailAddress(object):
       
    24     def __init__(self, address):
       
    25         self._localpart = None
       
    26         self._domainname = None
       
    27         self.__chkAddress(address)
       
    28     
       
    29     def __eq__(self, other):
       
    30         if hasattr(other, '_localpart') and hasattr(other, '_domainname'):
       
    31             return self._localpart == other._localpart\
       
    32                     and self._domainname == other._domainname
       
    33         else:
       
    34             return NotImplemented
       
    35 
       
    36     def __ne__(self, other):
       
    37         if hasattr(other, '_localpart') and hasattr(other, '_domainname'):
       
    38             return not self._localpart == other._localpart\
       
    39                     and self._domainname == other._domainname
       
    40         else:
       
    41             return NotImplemented
       
    42 
       
    43     def __repr__(self):
       
    44         return "EmailAddress('%s@%s')" % (self._localpart, self._domainname)
       
    45 
       
    46     def __str__(self):
       
    47         return "%s@%s" % (self._localpart, self._domainname)
       
    48 
       
    49     def __chkAddress(self, address):
       
    50         try:
       
    51             localpart, domain = address.split('@')
       
    52         except ValueError:
       
    53             raise VMMEAE(_(u"Missing '@' sign in e-mail address »%s«.") %
       
    54                 address, ERR.INVALID_ADDRESS)
       
    55         except AttributeError:
       
    56             raise VMMEAE(_(u"»%s« looks not like an e-mail address.") %
       
    57                 address, ERR.INVALID_ADDRESS)
       
    58         if len(domain) > 0:
       
    59             domain = VMM.VirtualMailManager.chkDomainname(domain)
       
    60         else:
       
    61             raise VMMEAE(_(u"Missing domain name after »%s@«.") %
       
    62                     localpart, ERR.DOMAIN_NO_NAME)
       
    63         localpart = self.__chkLocalpart(localpart)
       
    64         self._localpart, self._domainname = localpart, domain
       
    65 
       
    66     def __chkLocalpart(self, localpart):
       
    67         """Validates the local part of an e-mail address.
       
    68         
       
    69         Keyword arguments:
       
    70         localpart -- of the e-mail address that should be validated (str)
       
    71         """
       
    72         if len(localpart) < 1:
       
    73             raise VMMEAE(_(u'No localpart specified.'),
       
    74                 ERR.LOCALPART_INVALID)
       
    75         if len(localpart) > 64:
       
    76             raise VMMEAE(_(u'The local part »%s« is too long') %
       
    77                 localpart, ERR.LOCALPART_TOO_LONG)
       
    78         ic = re.compile(RE_LOCALPART).findall(localpart)
       
    79         if len(ic):
       
    80             ichrs = ''
       
    81             for c in set(ic):
       
    82                 ichrs += u"»%s« " % c
       
    83             raise VMMEAE(_(u"The local part »%(lpart)s« contains invalid\
       
    84  characters: %(ichrs)s") % {'lpart': localpart, 'ichrs': ichrs},
       
    85                 ERR.LOCALPART_INVALID)
       
    86         return localpart
       
    87