# -*- coding: UTF-8 -*-# Copyright (c) 2008 - 2010, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.Relocated Virtual Mail Manager's Relocated class to handle relocated users."""fromVirtualMailManager.DomainimportDomainfromVirtualMailManager.EmailAddressimportEmailAddressfromVirtualMailManager.ExceptionsimportVMMRelocatedExceptionasVMMREfromVirtualMailManager.constants.ERRORimportNO_SUCH_DOMAIN, \NO_SUCH_RELOCATED,RELOCATED_ADDR_DEST_IDENTICAL,RELOCATED_EXISTS_=lambdamsg:msgclassRelocated(object):"""Class to handle e-mail addresses of relocated users."""__slots__=('_addr','_dest','_gid','_dbh')def__init__(self,dbh,address):"""Creates a new *Relocated* instance. The ``address`` is the old e-mail address of the user. Use `setDestination()` to set/update the new address, where the user has moved to."""ifisinstance(address,EmailAddress):self._addr=addresselse:raiseTypeError("Argument 'address' is not an EmailAddress")self._dbh=dbhself._gid=0self._dest=Noneself.__set_gid()self.__load()def__set_gid(self):"""Sets the `_gid` attribute, based on the `_addr.domainname`."""dom=Domain(self._dbh,self._addr.domainname)self._gid=dom.getID()ifself._gid==0:raiseVMMRE(_(u"The domain “%s” doesn't exist.")%self._addr.domainname,NO_SUCH_DOMAIN)def__load(self):"""Loads the destination address from the database into the `_dest` attribute."""dbc=self._dbh.cursor()dbc.execute('SELECT destination FROM relocated WHERE gid=%s AND address=%s',self._gid,self._addr.localpart)destination=dbc.fetchone()dbc.close()ifdestination:self._dest=EmailAddress(destination[0])defsetDestination(self,destination):"""Sets/updates the new address of the relocated user."""update=Falseifisinstance(destination,EmailAddress):ifself._addr==destination:raiseVMMRE(_(u'Address and destination are identical.'),RELOCATED_ADDR_DEST_IDENTICAL)ifself._dest:ifself._dest==destination:raiseVMMRE(_(u'The relocated user “%s” already exists.')%self._addr,RELOCATED_EXISTS)else:self._dest=destinationupdate=Trueelse:self._dest=destinationelse:raiseTypeError("Argument 'destination' is not an EmailAddress")dbc=self._dbh.cursor()ifnotupdate:dbc.execute('INSERT INTO relocated VALUES (%s, %s, %s)',self._gid,self._addr.localpart,str(self._dest))else:dbc.execute('UPDATE relocated SET destination=%s\WHERE gid=%s AND address=%s',str(self._dest),self._gid,self._addr.localpart)self._dbh.commit()dbc.close()defgetInfo(self):"""Returns the address to which mails should be sent."""ifself._dest:returnself._destelse:raiseVMMRE(_(u"The relocated user “%s” doesn't exist.")%self._addr,NO_SUCH_RELOCATED)defdelete(self):"""Deletes the relocated entry from the database."""ifnotself._dest:raiseVMMRE(_(u"The relocated user “%s” doesn't exist.")%self._addr,NO_SUCH_RELOCATED)dbc=self._dbh.cursor()dbc.execute("DELETE FROM relocated WHERE gid = %s AND address = %s",self._gid,self._addr.localpart)ifdbc.rowcount>0:self._dbh.commit()dbc.close()self._dest=Nonedel_