# -*- coding: UTF-8 -*-# Copyright (c) 2007 - 2009, VEB IT# See COPYING for distribution information."""Virtual Mail Manager's Alias class to manage e-mail aliases."""from__main__importERRfromExceptionsimportVMMAliasExceptionasVMMAEfromDomainimportDomainfromEmailAddressimportEmailAddressimportVirtualMailManagerasVMMclassAlias(object):"""Class to manage e-mail aliases."""__slots__=('_addr','_dest','_gid','_isNew','_dbh')def__init__(self,dbh,address,destination=None):ifisinstance(address,EmailAddress):self._addr=addresselse:raiseTypeError("Argument 'address' is not an EmailAddress")ifdestinationisNone:self._dest=Noneelifisinstance(destination,EmailAddress):self._dest=destinationelse:raiseTypeError("Argument 'destination' is not an EmailAddress")ifaddress==destination:raiseVMMAE(_(u"Address and destination are identical."),ERR.ALIAS_ADDR_DEST_IDENTICAL)self._dbh=dbhself._gid=0self._isNew=Falseself._setAddr()ifnotself._destisNone:self._exists()ifVMM.VirtualMailManager.accountExists(self._dbh,self._addr):raiseVMMAE(_(u"There is already an account with address “%s”.")%\self._addr,ERR.ACCOUNT_EXISTS)ifVMM.VirtualMailManager.relocatedExists(self._dbh,self._addr):raiseVMMAE(_(u"There is already a relocated user with the address “%s”.")%\self._addr,ERR.RELOCATED_EXISTS)def_exists(self):dbc=self._dbh.cursor()dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\ AND destination=%s",self._gid,self._addr._localpart,str(self._dest))gid=dbc.fetchone()dbc.close()ifgidisNone:self._isNew=Truedef_setAddr(self):dom=Domain(self._dbh,self._addr._domainname)self._gid=dom.getID()ifself._gid==0:raiseVMMAE(_(u"The domain “%s” doesn't exist.")%\self._addr._domainname,ERR.NO_SUCH_DOMAIN)def_checkExpansion(self,limit):dbc=self._dbh.cursor()dbc.execute('SELECT count(gid) FROM alias where gid=%s AND address=%s',self._gid,self._addr._localpart)curEx=dbc.fetchone()[0]dbc.close()ifcurEx==limit:errmsg=_(u"""Can't add new destination to alias “%(address)s”.Currently this alias expands into %(count)i recipients.One more destination will render this alias unusable.Hint: Increase Postfix' virtual_alias_expansion_limit""")%{'address':self._addr,'count':curEx}raiseVMMAE(errmsg,ERR.ALIAS_EXCEEDS_EXPANSION_LIMIT)defsave(self,expansion_limit):ifself._destisNone:raiseVMMAE(_(u"No destination address specified for alias."),ERR.ALIAS_MISSING_DEST)ifself._isNew:self._checkExpansion(expansion_limit)dbc=self._dbh.cursor()dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\ (%s, %s, %s)",self._gid,self._addr._localpart,str(self._dest))self._dbh.commit()dbc.close()else:raiseVMMAE(_(u"The alias “%(a)s” with destination “%(d)s” already exists.")\%{'a':self._addr,'d':self._dest},ERR.ALIAS_EXISTS)defgetInfo(self):dbc=self._dbh.cursor()dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',self._gid,self._addr._localpart)destinations=dbc.fetchall()dbc.close()iflen(destinations)>0:targets=[destination[0]fordestinationindestinations]returntargetselse:raiseVMMAE(_(u"The alias “%s” doesn't exist.")%self._addr,ERR.NO_SUCH_ALIAS)defdelete(self):dbc=self._dbh.cursor()ifself._destisNone:dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",self._gid,self._addr._localpart)else:dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \ destination=%s",self._gid,self._addr._localpart,str(self._dest))rowcount=dbc.rowcountdbc.close()ifrowcount>0:self._dbh.commit()else:ifself._destisNone:msg=_(u"The alias “%s” doesn't exist.")%self._addrelse:msg=_(u"The alias “%(a)s” with destination “%(d)s” doesn't\ exist.")%{'a':self._addr,'d':self._dest}raiseVMMAE(msg,ERR.NO_SUCH_ALIAS)