VirtualMailManager/Alias.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 21 Aug 2008 01:23:31 +0000
changeset 52 c152d7714802
parent 48 0d5f58f8b8f5
child 76 14c0a092d7d2
permissions -rw-r--r--
* 'VirtualMailManager/Alias.py' - call VirtualMailManager.chkEmailAddress() in Alias.__init__() * 'VirtualMailManager/VirtualMailManager.py' - VirtualMailManager.chkEmailAddress() checks also for len(domainname) > 0 * 'po/de.po' * 'po/vmm.pot' - updated

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# Copyright 2007-2008 VEB IT
# See COPYING for distribution information.
# $Id$

"""Virtual Mail Manager's Alias class to manage e-mail aliases."""

from constants.VERSION import VERSION

__author__ = 'Pascal Volk <p.volk@veb-it.de>'
__version__ = VERSION
__revision__ = 'rev '+'$Rev$'.split()[1]
__date__ = '$Date$'.split()[1]

from Exceptions import VMMAliasException as VMMAE
from Domain import Domain
import constants.ERROR as ERR
import VirtualMailManager as VMM

class Alias:
    """Class to manage e-mail accounts."""
    def __init__(self, dbh, address, destination=None):
        if address == destination:
            raise VMMAE(_(u"Address and destination are identical."),
                ERR.ALIAS_ADDR_DEST_IDENTICAL)
        self._dbh = dbh
        self._addr = VMM.VirtualMailManager.chkEmailAddress(address)
        if destination is None:
            self._dest = None
        elif destination.count('@'):
            self._dest = VMM.VirtualMailManager.chkEmailAddress(destination)
        else:
            self._dest = VMM.VirtualMailManager.chkLocalpart(destination)
        self._localpart = None
        self._gid = 0
        self._isNew = False
        self._setAddr()
        if not self._dest is None:
            self._exists()
        if self._isAccount():
            raise VMMAE(_(u"There is already an account with address »%s«.") %
                    self._addr, ERR.ACCOUNT_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._localpart, self._dest)
        gid = dbc.fetchone()
        dbc.close()
        if gid is None:
            self._isNew = True
        else:
            self._isNew = False

    def _isAccount(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s",
                self._gid, self._localpart)
        uid = dbc.fetchone()
        dbc.close()
        if uid is not None:
            return True
        else:
            return False
        
    def _setAddr(self):
        self._localpart, d = self._addr.split('@')
        dom = Domain(self._dbh, d)
        self._gid = dom.getID()
        if self._gid == 0:
            raise VMMAE(_(u"The domain »%s« doesn't exist yet.") % d,
                ERR.NO_SUCH_DOMAIN)

    def save(self):
        if self._dest is None:
           raise VMMAE(_(u"No destination address for alias denoted."),
               ERR.ALIAS_MISSING_DEST)
        if self._isNew:
            dbc = self._dbh.cursor()
            dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
 (%s, %s, %s)", self._gid, self._localpart, self._dest)
            self._dbh.commit()
            dbc.close()
        else:
            raise VMMAE(_(u"The alias »%s« already exists.") % self._addr,
                    ERR.ALIAS_EXISTS)

    def getInfo(self):
        dbc = self._dbh.cursor()
        dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
                self._gid, self._localpart)
        destinations = dbc.fetchall()
        dbc.close()
        if len(destinations) > 0:
            targets = []
            for destination in destinations:
                targets.append(destination[0])
            return targets
        else:
            raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
                    ERR.NO_SUCH_ALIAS)

    def delete(self):
        dbc = self._dbh.cursor()
        if self._dest is None:
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
                    self._gid, self._localpart)
        else:
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
 destination=%s", self._gid, self._localpart, self._dest)
        rowcount = dbc.rowcount
        dbc.close()
        if rowcount > 0:
            self._dbh.commit()
        else:
            raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
                    ERR.NO_SUCH_ALIAS)