VirtualMailManager/Alias.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 05 Sep 2009 18:46:33 +0000
changeset 144 4c6aa6c29dd7
parent 138 617f27715b01
child 146 a08d78344706
permissions -rw-r--r--
Small optimizations in Account.getAliases() and Alias.getInfo().
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 -*-
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 89
diff changeset
     2
# Copyright (c) 2007 - 2009, 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
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
     5
"""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
     6
138
617f27715b01 Reorganized module import.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
     7
from __main__ import ERR
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
     8
from Exceptions import VMMAliasException as VMMAE
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
from Domain import Domain
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    10
from EmailAddress import EmailAddress
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    11
import VirtualMailManager as VMM
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    13
class Alias(object):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    14
    """Class to manage e-mail aliases."""
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    15
    __slots__ = ('_addr', '_dest', '_gid', '_isNew', '_dbh')
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    16
    def __init__(self, dbh, address, destination=None):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    17
        if isinstance(address, EmailAddress):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    18
            self._addr = address
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    19
        else:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    20
            raise TypeError("Argument 'address' is not an EmailAddress")
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    21
        if destination is None:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    22
            self._dest = None
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    23
        elif isinstance(destination, EmailAddress):
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    24
            self._dest = destination
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    25
        else:
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    26
            raise TypeError("Argument 'destination' is not an EmailAddress")
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        if address == destination:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    28
            raise VMMAE(_(u"Address and destination are identical."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    29
                ERR.ALIAS_ADDR_DEST_IDENTICAL)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
        self._dbh = dbh
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        self._gid = 0
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    32
        self._isNew = False
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    33
        self._setAddr()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        if not self._dest is None:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
            self._exists()
82
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    36
        if self._isNew and VMM.VirtualMailManager.accountExists(self._dbh,
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    37
                self._addr):
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
    38
            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
    39
                    self._addr, ERR.ACCOUNT_EXISTS)
82
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    40
        if self._isNew and VMM.VirtualMailManager.relocatedExists(self._dbh,
6c85915f3815 „speedup commit“ ;-)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
    41
                self._addr):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    42
            raise VMMAE(
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
    43
              _(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
    44
                    self._addr, ERR.RELOCATED_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
    def _exists(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
        dbc = self._dbh.cursor()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    48
        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
    49
 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
    50
        gid = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    51
        dbc.close()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    52
        if gid is None:
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    53
            self._isNew = True
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    55
    def _setAddr(self):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    56
        dom = Domain(self._dbh, self._addr._domainname)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        self._gid = dom.getID()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        if self._gid == 0:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
    59
            raise VMMAE(_(u"The domain “%s” doesn't exist yet.") %\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    60
                    self._addr._domainname, ERR.NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    62
    def _checkExpansion(self, limit):
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    63
        dbc = self._dbh.cursor()
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    64
        dbc.execute('SELECT count(gid) FROM alias where gid=%s AND address=%s',
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    65
                self._gid, self._addr._localpart)
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    66
        curEx = dbc.fetchone()[0]
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    67
        dbc.close()
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    68
        if curEx == limit:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
    69
            errmsg = _(u"""Can't add new destination to alias “%(address)s”.
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    70
Currently this alias expands into %(count)i recipients.
89
752d3b57ebb6 small fixes/updates
Pascal Volk <neverseen@users.sourceforge.net>
parents: 88
diff changeset
    71
One more destination will render this alias unusable.
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    72
Hint: Increase Postfix' virtual_alias_expansion_limit
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    73
""") % {'address': self._addr, 'count': curEx}
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    74
            raise VMMAE(errmsg, ERR.ALIAS_EXCEEDS_EXPANSION_LIMIT)
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    75
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    76
    def save(self, expansion_limit):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        if self._dest is None:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    78
           raise VMMAE(_(u"No destination address for alias denoted."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    79
               ERR.ALIAS_MISSING_DEST)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    80
        if self._isNew:
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    81
            self._checkExpansion(expansion_limit)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
            dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
            dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    84
 (%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
    85
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        else:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    88
            raise VMMAE(
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
    89
               _(u"The alias “%(a)s” with destination “%(d)s” already exists.")\
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    90
                       % {'a': self._addr, 'd': self._dest}, ERR.ALIAS_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
    def getInfo(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        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
    95
                self._gid, self._addr._localpart)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        destinations = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        if len(destinations) > 0:
144
4c6aa6c29dd7 Small optimizations in Account.getAliases() and Alias.getInfo().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
    99
            targets = [destination[0] for destination in destinations]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
            return targets
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        else:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
   102
            raise VMMAE(_(u"The alias “%s” doesn't exists.") % self._addr,
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   103
                    ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
    def delete(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   106
        dbc = self._dbh.cursor()
12
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   107
        if self._dest is None:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   108
            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
   109
                    self._gid, self._addr._localpart)
12
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   110
        else:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   111
            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
   112
 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
   113
        rowcount = dbc.rowcount
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
        if rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
        else:
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   118
            if self._dest is None:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
   119
                msg = _(u"The alias “%s” doesn't exists.") % self._addr
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
   120
            else:
133
2d5c4745efec Replaced angle quotes by quotation marks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 129
diff changeset
   121
                msg = _(u"The alias “%(a)s” with destination “%(d)s” doesn't\
129
4ffb50de00d5 Updated translation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
   122
 exists.") % {'a': self._addr, 'd': self._dest}
4ffb50de00d5 Updated translation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 122
diff changeset
   123
            raise VMMAE(msg, ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124