# -*- coding: UTF-8 -*-# Copyright 2008 VEB IT# See COPYING for distribution information.# $Id$"""Virtual Mail Manager's EmailAddress class to handle e-mail addresses."""fromconstants.VERSIONimportVERSION__author__='Pascal Volk <p.volk@veb-it.de>'__version__=VERSION__revision__='rev '+'$Rev$'.split()[1]__date__='$Date$'.split()[1]importrefromExceptionsimportVMMEmailAddressExceptionasVMMEAEimportVirtualMailManagerasVMMimportconstants.ERRORasERRRE_LOCALPART="""[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""classEmailAddress(object):def__init__(self,address):self._localpart=Noneself._domainname=Noneself.__chkAddress(address)def__eq__(self,other):ifhasattr(other,'_localpart')andhasattr(other,'_domainname'):returnself._localpart==other._localpart\andself._domainname==other._domainnameelse:returnNotImplementeddef__ne__(self,other):ifhasattr(other,'_localpart')andhasattr(other,'_domainname'):returnnotself._localpart==other._localpart\andself._domainname==other._domainnameelse:returnNotImplementeddef__repr__(self):return"EmailAddress('%s@%s')"%(self._localpart,self._domainname)def__str__(self):return"%s@%s"%(self._localpart,self._domainname)def__chkAddress(self,address):try:localpart,domain=address.split('@')exceptValueError:raiseVMMEAE(_(u"Missing '@' sign in e-mail address »%s«.")%address,ERR.INVALID_ADDRESS)exceptAttributeError:raiseVMMEAE(_(u"»%s« looks not like an e-mail address.")%address,ERR.INVALID_ADDRESS)iflen(domain)>0:domain=VMM.VirtualMailManager.chkDomainname(domain)else:raiseVMMEAE(_(u"Missing domain name after »%s@«.")%localpart,ERR.DOMAIN_NO_NAME)localpart=self.__chkLocalpart(localpart)self._localpart,self._domainname=localpart,domaindef__chkLocalpart(self,localpart):"""Validates the local part of an e-mail address. Keyword arguments: localpart -- of the e-mail address that should be validated (str) """iflen(localpart)<1:raiseVMMEAE(_(u'No localpart specified.'),ERR.LOCALPART_INVALID)iflen(localpart)>64:raiseVMMEAE(_(u'The local part »%s« is too long')%localpart,ERR.LOCALPART_TOO_LONG)ic=set(re.findall(RE_LOCALPART,localpart))iflen(ic):ichrs=''forcinic:ichrs+=u"»%s« "%craiseVMMEAE(_(u"The local part »%(lpart)s« contains invalid\ characters: %(ichrs)s")%{'lpart':localpart,'ichrs':ichrs},ERR.LOCALPART_INVALID)returnlocalpart