VirtualMailManager/alias.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 05 Jan 2013 23:49:42 +0000
changeset 675 d24f094d1cb5
parent 619 4ec5c015b7aa
child 676 2bc11dada296
child 703 58815c004a61
permissions -rw-r--r--
Updated copyright notices to include the year 2013.
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 -*-
675
d24f094d1cb5 Updated copyright notices to include the year 2013.
Pascal Volk <user@localhost.localdomain.org>
parents: 619
diff changeset
     2
# Copyright (c) 2007 - 2013, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.alias
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     8
    Virtual Mail Manager's Alias class to manage e-mail aliases.
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     9
"""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    10
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    11
from VirtualMailManager.domain import get_gid
424
46c296c6e231 VMM/alias: Alias._load_dests() use class DestinationEmailAddress
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    12
from VirtualMailManager.emailaddress import \
46c296c6e231 VMM/alias: Alias._load_dests() use class DestinationEmailAddress
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    13
     EmailAddress, DestinationEmailAddress as DestAddr
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    14
from VirtualMailManager.errors import AliasError as AErr
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    15
from VirtualMailManager.ext.postconf import Postconf
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    16
from VirtualMailManager.pycompat import all
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 295
diff changeset
    17
from VirtualMailManager.constants import \
222
d0c16e70a9fb VMM/Domain: get_gid() return 0 instead of raising an Exception,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 221
diff changeset
    18
     ALIAS_EXCEEDS_EXPANSION_LIMIT, NO_SUCH_ALIAS, NO_SUCH_DOMAIN
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    19
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    20
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    21
_ = lambda msg: msg
281
59ff7c719697 VMM/Alias: moved the postconf stuff from the Handlers to Alias class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
    22
cfg_dget = lambda option: None
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    23
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    25
class Alias(object):
76
14c0a092d7d2 * 'VirtualMailManager/EmailAddress.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 52
diff changeset
    26
    """Class to manage e-mail aliases."""
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    27
    __slots__ = ('_addr', '_dests', '_gid', '_dbh')
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    28
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    29
    def __init__(self, dbh, address):
213
1a9fee6b93bc VMM:/{Alias,EmailAddress,Relocated}: use assertions for argument checks.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 203
diff changeset
    30
        assert isinstance(address, EmailAddress)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
    31
        self._addr = address
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        self._dbh = dbh
198
02d467e4fbab VMM/Domain: added function get_gid() to the Domain module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 196
diff changeset
    33
        self._gid = get_gid(self._dbh, self._addr.domainname)
222
d0c16e70a9fb VMM/Domain: get_gid() return 0 instead of raising an Exception,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 221
diff changeset
    34
        if not self._gid:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    35
            raise AErr(_(u"The domain '%s' does not exist.") %
222
d0c16e70a9fb VMM/Domain: get_gid() return 0 instead of raising an Exception,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 221
diff changeset
    36
                       self._addr.domainname, NO_SUCH_DOMAIN)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    37
        self._dests = []
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    39
        self._load_dests()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    41
    def _load_dests(self):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    42
        """Loads all known destination addresses into the _dests list."""
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    43
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 281
diff changeset
    44
        dbc.execute('SELECT destination FROM alias WHERE gid = %s AND '
619
4ec5c015b7aa VMM/alias: Load destination addresses sorted.
Pascal Volk <user@localhost.localdomain.org>
parents: 618
diff changeset
    45
                    'address = %s ORDER BY destination',
4ec5c015b7aa VMM/alias: Load destination addresses sorted.
Pascal Volk <user@localhost.localdomain.org>
parents: 618
diff changeset
    46
                    (self._gid, self._addr.localpart))
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    47
        dests = dbc.fetchall()
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    48
        if dbc.rowcount > 0:
424
46c296c6e231 VMM/alias: Alias._load_dests() use class DestinationEmailAddress
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    49
            self._dests.extend(DestAddr(dest[0], self._dbh) for dest in dests)
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    50
        dbc.close()
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    51
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    52
    def _check_expansion(self, count_new):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    53
        """Checks the current expansion limit of the alias."""
281
59ff7c719697 VMM/Alias: moved the postconf stuff from the Handlers to Alias class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
    54
        postconf = Postconf(cfg_dget('bin.postconf'))
59ff7c719697 VMM/Alias: moved the postconf stuff from the Handlers to Alias class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
    55
        limit = long(postconf.read('virtual_alias_expansion_limit'))
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    56
        dcount = len(self._dests)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    57
        failed = False
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    58
        if dcount == limit or dcount + count_new > limit:
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    59
            failed = True
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    60
            errmsg = _(
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    61
u"""Cannot add %(count_new)i new destination(s) to alias '%(address)s'.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    62
Currently this alias expands into %(count)i/%(limit)i recipients.
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    63
%(count_new)i additional destination(s) will render this alias unusable.
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    64
Hint: Increase Postfix' virtual_alias_expansion_limit""")
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    65
        elif dcount > limit:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    66
            failed = True
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    67
            errmsg = _(
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    68
u"""Cannot add %(count_new)i new destination(s) to alias '%(address)s'.
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 250
diff changeset
    69
This alias already exceeds its expansion limit (%(count)i/%(limit)i).
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    70
So its unusable, all messages addressed to this alias will be bounced.
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    71
Hint: Delete some destination addresses.""")
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    72
        if failed:
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 250
diff changeset
    73
            raise AErr(errmsg % {'address': self._addr, 'count': dcount,
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    74
                                 'limit': limit, 'count_new': count_new},
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
    75
                       ALIAS_EXCEEDS_EXPANSION_LIMIT)
88
b3debcfea7bc * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
    76
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    77
    def _delete(self, destinations=None):
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    78
        """Deletes the *destinations* from the alias, if ``destinations``
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    79
        is not ``None``.  If ``destinations`` is None, the alias with all
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 250
diff changeset
    80
        its destination addresses will be deleted.
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    81
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
    82
        """
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    83
        dbc = self._dbh.cursor()
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    84
        if not destinations:
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 281
diff changeset
    85
            dbc.execute('DELETE FROM alias WHERE gid = %s AND address = %s',
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 322
diff changeset
    86
                        (self._gid, self._addr.localpart))
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    87
        else:
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    88
            dbc.executemany("DELETE FROM alias WHERE gid = %d AND address = "
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    89
                            "'%s' AND destination = %%s" % (self._gid,
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    90
                                                         self._addr.localpart),
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    91
                            ((str(dest),) for dest in destinations))
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    92
        if dbc.rowcount > 0:
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    93
            self._dbh.commit()
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    94
        dbc.close()
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    95
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    96
    def __len__(self):
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    97
        """Returns the number of destinations of the alias."""
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    98
        return len(self._dests)
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    99
250
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 223
diff changeset
   100
    @property
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 223
diff changeset
   101
    def address(self):
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 223
diff changeset
   102
        """The Alias' EmailAddress instance."""
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 223
diff changeset
   103
        return self._addr
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 223
diff changeset
   104
281
59ff7c719697 VMM/Alias: moved the postconf stuff from the Handlers to Alias class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
   105
    def add_destinations(self, destinations, warnings=None):
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   106
        """Adds the `EmailAddress`es from *destinations* list to the
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   107
        destinations of the alias.
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   108
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   109
        Destinations, that are already assigned to the alias, will be
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   110
        removed from *destinations*.  When done, this method will return
257
5b8fde01e4f0 VMM/Alias.py: Replaced some %r with '%s'.
Tobias Berling <mail@tobiasberling.de>
parents: 250
diff changeset
   111
        a set with all destinations, that were saved in the database.
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   112
        """
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   113
        destinations = set(destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   114
        assert destinations and \
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   115
                all(isinstance(dest, EmailAddress) for dest in destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   116
        if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   117
            assert isinstance(warnings, list)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   118
        if self._addr in destinations:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   119
            destinations.remove(self._addr)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   120
            if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   121
                warnings.append(self._addr)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   122
        duplicates = destinations.intersection(set(self._dests))
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   123
        if duplicates:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   124
            destinations.difference_update(set(self._dests))
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   125
            if not warnings is None:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   126
                warnings.extend(duplicates)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   127
        if not destinations:
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   128
            return destinations
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   129
        self._check_expansion(len(destinations))
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   130
        dbc = self._dbh.cursor()
379
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 370
diff changeset
   131
        dbc.executemany("INSERT INTO alias (gid, address, destination) "
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 370
diff changeset
   132
                        "VALUES (%d, '%s', %%s)" % (self._gid,
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 370
diff changeset
   133
                                                    self._addr.localpart),
370
1b1f8f0cc687 VMM/alias.py: Fixed TypeError when calling dbc.executemany()
Tobias Berling <t-obi@users.sourceforge.net>
parents: 366
diff changeset
   134
                        ((str(destination),) for destination in destinations))
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   135
        self._dbh.commit()
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   136
        dbc.close()
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   137
        self._dests.extend(destinations)
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   138
        return destinations
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   139
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   140
    def del_destinations(self, destinations, warnings=None):
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   141
        """Delete the specified `EmailAddress`es of *destinations* from
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   142
        the alias's destinations.
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   143
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   144
        """
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   145
        destinations = set(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   146
        assert destinations and \
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   147
                all(isinstance(dest, EmailAddress) for dest in destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   148
        if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   149
            assert isinstance(warnings, list)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   150
        if self._addr in destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   151
            destinations.remove(self._addr)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   152
            if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   153
                warnings.append(self._addr)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   154
        if not self._dests:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
   155
            raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   156
                       NO_SUCH_ALIAS)
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   157
        unknown = destinations.difference(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   158
        if unknown:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   159
            destinations.intersection_update(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   160
            if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   161
                warnings.extend(unknown)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   162
        if not destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   163
            raise AErr(_(u"No suitable destinations left to remove from alias "
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   164
                         u"'%s'.") % self._addr, NO_SUCH_ALIAS)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   165
        self._delete(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   166
        for destination in destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   167
            self._dests.remove(destination)
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   168
215
33f727efa7c4 PEP-8-ified the work of the last days.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 213
diff changeset
   169
    def get_destinations(self):
196
65a3163bd113 VMM/{Alias,Handler}: reworked Alias class, adjusted Handler class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
   170
        """Returns an iterator for all destinations of the alias."""
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   171
        if not self._dests:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
   172
            raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   173
                       NO_SUCH_ALIAS)
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   174
        return iter(self._dests)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   175
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   176
    def delete(self):
221
371ae0b4443d VMM/Alias: renamed Alias.add_destination() to add_destinations().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   177
        """Deletes the alias with all its destinations."""
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   178
        if not self._dests:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
   179
            raise AErr(_(u"The alias '%s' does not exist.") % self._addr,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 215
diff changeset
   180
                       NO_SUCH_ALIAS)
322
94bd10e237e5 VMM/…: More PEP-8 fixes; eliminated __names.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   181
        self._delete()
203
4d601240b7db VMM/Alias: small code cleanups and cosmetic.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 198
diff changeset
   182
        del self._dests[:]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   183
281
59ff7c719697 VMM/Alias: moved the postconf stuff from the Handlers to Alias class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 257
diff changeset
   184
del _, cfg_dget