VirtualMailManager/Alias.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 11 Sep 2008 20:38:30 +0000
changeset 82 6c85915f3815
parent 78 8f1e501b1bb1
child 88 b3debcfea7bc
permissions -rw-r--r--
„speedup commit“ ;-) * 'VirtualMailManager/Account.py' - Account.__init__() checks only the existence of an alias or relocated record if there is no account with the supplied address yet * 'VirtualMailManager/Alias.py' - Alias.__init__() checks only the existence of an account or relocated record if there is no alias with the supplied address yet * 'VirtualMailManager/Relocated.py' - Relocated.__init__() checks only the existence of an account or alias record if there is no relocated user with the supplied address yet * 'create_optional_types_and_functions.pgsql' - Modified the 2nd part of postfix_smtpd_sender_login_map() in order to save 0.3 ms
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 4
diff changeset
     2
# Copyright 2007-2008 VEB IT
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
# $Id$
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
     6
"""Virtual Mail Manager's Alias class to manage e-mail aliases."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
4
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
     8
from constants.VERSION import VERSION
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
     9
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
__author__ = 'Pascal Volk <p.volk@veb-it.de>'
4
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
    11
__version__ = VERSION
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
    12
__revision__ = 'rev '+'$Rev$'.split()[1]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    13
__date__ = '$Date$'.split()[1]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    15
from Exceptions import VMMAliasException as VMMAE
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
from Domain import Domain
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    17
from EmailAddress import EmailAddress
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
import constants.ERROR as ERR
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    19
import VirtualMailManager as VMM
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
class Alias:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    22
    """Class to manage e-mail aliases."""
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    23
    def __init__(self, dbh, address, destination=None):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    24
        if isinstance(address, EmailAddress):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    25
            self._addr = address
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    26
        else:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    27
            raise TypeError("Argument 'address' is not an EmailAddress")
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    28
        if destination is None:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    29
            self._dest = None
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    30
        elif isinstance(destination, EmailAddress):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    31
            self._dest = destination
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    32
        else:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    33
            raise TypeError("Argument 'destination' is not an EmailAddress")
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        if address == destination:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    35
            raise VMMAE(_(u"Address and destination are identical."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    36
                ERR.ALIAS_ADDR_DEST_IDENTICAL)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        self._dbh = dbh
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
        self._gid = 0
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    39
        self._isNew = False
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    40
        self._setAddr()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        if not self._dest is None:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
            self._exists()
82
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    43
        if self._isNew and VMM.VirtualMailManager.accountExists(self._dbh,
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    44
                self._addr):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    45
            raise VMMAE(_(u"There is already an account with address »%s«.") %\
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    46
                    self._addr, ERR.ACCOUNT_EXISTS)
82
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    47
        if self._isNew and VMM.VirtualMailManager.relocatedExists(self._dbh,
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    48
                self._addr):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    49
            raise VMMAE(
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    50
              _(u"There is already a relocated user with the address »%s«.") %\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    51
                    self._addr, ERR.RELOCATED_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
    def _exists(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        dbc = self._dbh.cursor()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    55
        dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    56
 AND destination=%s", self._gid, self._addr._localpart, str(self._dest))
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    57
        gid = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        dbc.close()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    59
        if gid is None:
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    60
            self._isNew = True
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    62
    def _setAddr(self):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    63
        dom = Domain(self._dbh, self._addr._domainname)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        self._gid = dom.getID()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        if self._gid == 0:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    66
            raise VMMAE(_(u"The domain »%s« doesn't exist yet.") %\
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    67
                    self._addr._domainname, ERR.NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
    def save(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        if self._dest is None:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    71
           raise VMMAE(_(u"No destination address for alias denoted."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    72
               ERR.ALIAS_MISSING_DEST)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    73
        if self._isNew:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
            dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
            dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    76
 (%s, %s, %s)", self._gid, self._addr._localpart, str(self._dest))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
        else:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    80
            raise VMMAE(
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    81
               _(u"The alias »%(a)s« with destination »%(d)s« already exists.")\
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    82
                       % {'a': self._addr, 'd': self._dest}, ERR.ALIAS_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    def getInfo(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    87
                self._gid, self._addr._localpart)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        destinations = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        if len(destinations) > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
            targets = []
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
            for destination in destinations:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
                targets.append(destination[0])
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            return targets
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        else:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    96
            raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    97
                    ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
    def delete(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
        dbc = self._dbh.cursor()
12
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   101
        if self._dest is None:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   102
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   103
                    self._gid, self._addr._localpart)
12
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   104
        else:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   105
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   106
 destination=%s", self._gid, self._addr._localpart, str(self._dest))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
        rowcount = dbc.rowcount
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   108
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   109
        if rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   110
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   111
        else:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   112
            if self._dest is None:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   113
                msg = u"The alias »%s« doesn't exists." % self._addr
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   114
            else:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   115
                msg = u"The alias »%(a)s« with destination »%(d)s« doesn't\
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   116
 exists." % {'a': self._addr, 'd': self._dest}
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   117
            raise VMMAE(_(msg), ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   118