VirtualMailManager/catchall.py
author Pascal Volk <user@localhost.localdomain.org>
Sat, 08 Jun 2013 15:12:16 +0000
branchv0.7.x
changeset 698 18a528d44055
parent 643 df1e3b67882a
child 711 2a75058fc064
permissions -rw-r--r--
merged changes from default(61aaa7a98ec0).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     2
# Copyright (c) 2012 martin f. krafft
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     4
"""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     5
    VirtualMailManager.catchall
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     7
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     8
    Virtual Mail Manager's CatchallAlias class to manage domain catch-all
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
     9
    aliases.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    10
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    11
    This is heavily based on (more or less a copy of) the Alias class, because
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    12
    fundamentally, catchall aliases are aliases, but without a localpart.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    13
    While Alias could potentially derive from CatchallAlias to reuse some of
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    14
    the functionality, it's probably not worth it. I found no sensible way to
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    15
    derive CatchallAlias from Alias, or at least none that would harness the
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    16
    powers of polymorphism.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    17
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    18
    Yet, we reuse the AliasError exception class, which makes sense.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    19
"""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    20
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    21
from VirtualMailManager.domain import get_gid
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    22
from VirtualMailManager.emailaddress import \
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    23
     EmailAddress, DestinationEmailAddress as DestAddr
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    24
from VirtualMailManager.errors import AliasError as AErr
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    25
from VirtualMailManager.ext.postconf import Postconf
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    26
from VirtualMailManager.constants import \
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    27
     ALIAS_EXCEEDS_EXPANSION_LIMIT, NO_SUCH_ALIAS, NO_SUCH_DOMAIN
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    28
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    29
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    30
_ = lambda msg: msg
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    31
cfg_dget = lambda option: None
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    32
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    33
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    34
class CatchallAlias(object):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    35
    """Class to manage domain catch-all aliases."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    36
    __slots__ = ('_domain', '_dests', '_gid', '_dbh')
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    37
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    38
    def __init__(self, dbh, domain):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    39
        self._domain = domain
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    40
        self._dbh = dbh
507
2aae58cddfa9 Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 504
diff changeset
    41
        self._gid = get_gid(self._dbh, self.domain)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    42
        if not self._gid:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    43
            raise AErr(_("The domain '%s' does not exist.") %
513
557c4703986c Fix syntax errors
martin f. krafft <madduck@madduck.net>
parents: 509
diff changeset
    44
                       self.domain, NO_SUCH_DOMAIN)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    45
        self._dests = []
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    46
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    47
        self._load_dests()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    48
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    49
    def _load_dests(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    50
        """Loads all known destination addresses into the _dests list."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    51
        dbc = self._dbh.cursor()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    52
        dbc.execute('SELECT destination FROM catchall WHERE gid = %s',
507
2aae58cddfa9 Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 504
diff changeset
    53
                    (self._gid,))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    54
        dests = dbc.fetchall()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    55
        if dbc.rowcount > 0:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    56
            self._dests.extend(DestAddr(dest[0], self._dbh) for dest in dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    57
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    58
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    59
    def _check_expansion(self, count_new):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    60
        """Checks the current expansion limit of the alias."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    61
        postconf = Postconf(cfg_dget('bin.postconf'))
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    62
        limit = int(postconf.read('virtual_alias_expansion_limit'))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    63
        dcount = len(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    64
        failed = False
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    65
        if dcount == limit or dcount + count_new > limit:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    66
            failed = True
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    67
            errmsg = _(
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    68
"""Cannot add %(count_new)i new destination(s) to catch-all alias for
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    69
domain '%(domain)s'. Currently this alias expands into %(count)i/%(limit)i
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    70
recipients. %(count_new)i additional destination(s) will render this alias
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    71
unusable.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    72
Hint: Increase Postfix' virtual_alias_expansion_limit""")
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    73
        elif dcount > limit:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    74
            failed = True
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    75
            errmsg = _(
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
    76
"""Cannot add %(count_new)i new destination(s) to catch-all alias for domain
611
8e9b0046bc8f PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents: 608
diff changeset
    77
'%(domain)s'. This alias already exceeds its expansion limit \
8e9b0046bc8f PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents: 608
diff changeset
    78
(%(count)i/%(limit)i).
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    79
So its unusable, all messages addressed to this alias will be bounced.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    80
Hint: Delete some destination addresses.""")
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    81
        if failed:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    82
            raise AErr(errmsg % {'domain': self._domain, 'count': dcount,
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    83
                                 'limit': limit, 'count_new': count_new},
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    84
                       ALIAS_EXCEEDS_EXPANSION_LIMIT)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    85
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    86
    def _delete(self, destinations=None):
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    87
        """Delete one ore multiple destinations from the catchall alias, if
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    88
        ``destinations`` is not ``None``.  If ``destinations`` is None, the
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    89
        catchall alias with all its destination addresses will be deleted.
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    90
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    91
        """
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    92
        dbc = self._dbh.cursor()
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    93
        if not destinations:
509
1a9247e9763f Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 507
diff changeset
    94
            dbc.execute('DELETE FROM catchall WHERE gid = %s', (self._gid,))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    95
        else:
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    96
            dbc.executemany('DELETE FROM catchall WHERE gid = %d AND '
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    97
                            'destination = %%s' % self._gid,
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    98
                            ((str(dest),) for dest in destinations))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    99
        if dbc.rowcount > 0:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   100
            self._dbh.commit()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   101
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   102
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   103
    def __len__(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   104
        """Returns the number of destinations of the catchall alias."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   105
        return len(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   106
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   107
    @property
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   108
    def domain(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   109
        """The Alias' domain."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   110
        return self._domain
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   111
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   112
    def add_destinations(self, destinations, warnings=None):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   113
        """Adds the `EmailAddress`es from *destinations* list to the
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   114
        destinations of the catchall alias.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   115
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   116
        Destinations, that are already assigned to the alias, will be
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   117
        removed from *destinations*.  When done, this method will return
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   118
        a set with all destinations, that were saved in the database.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   119
        """
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   120
        destinations = set(destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   121
        assert destinations and \
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   122
                all(isinstance(dest, EmailAddress) for dest in destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   123
        if not warnings is None:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   124
            assert isinstance(warnings, list)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   125
        duplicates = destinations.intersection(set(self._dests))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   126
        if duplicates:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   127
            destinations.difference_update(set(self._dests))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   128
            if not warnings is None:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   129
                warnings.extend(duplicates)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   130
        if not destinations:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   131
            return destinations
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   132
        self._check_expansion(len(destinations))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   133
        dbc = self._dbh.cursor()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   134
        dbc.executemany("INSERT INTO catchall (gid, destination) "
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   135
                        "VALUES (%d, %%s)" % self._gid,
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   136
                        ((str(destination),) for destination in destinations))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   137
        self._dbh.commit()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   138
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   139
        self._dests.extend(destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   140
        return destinations
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   141
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   142
    def del_destinations(self, destinations, warnings=None):
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   143
        """Deletes the specified ``destinations`` from the catchall alias."""
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   144
        destinations = set(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   145
        assert destinations and \
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   146
                all(isinstance(dest, EmailAddress) for dest in destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   147
        if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   148
            assert isinstance(warnings, list)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   149
        if not self._dests:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   150
            raise AErr(_("There are no catch-all aliases defined for "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   151
                         "domain '%s'.") % self._domain, NO_SUCH_ALIAS)
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   152
        unknown = destinations.difference(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   153
        if unknown:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   154
            destinations.intersection_update(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   155
            if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   156
                warnings.extend(unknown)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   157
        if not destinations:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   158
            raise AErr(_("No suitable destinations left to remove from the "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   159
                         "catch-all alias of domain '%s'.") % self._domain,
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   160
                       NO_SUCH_ALIAS)
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   161
        self._delete(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   162
        for destination in destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   163
            self._dests.remove(destination)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   164
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   165
    def get_destinations(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   166
        """Returns an iterator for all destinations of the catchall alias."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   167
        if not self._dests:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   168
            raise AErr(_("There are no catch-all aliases defined for "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   169
                         "domain '%s'.") % self._domain, NO_SUCH_ALIAS)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   170
        return iter(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   171
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   172
    def delete(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   173
        """Deletes all catchall destinations for the domain."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   174
        if not self._dests:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   175
            raise AErr(_("There are no catch-all aliases defined for "
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 637
diff changeset
   176
                         "domain '%s'.") % self._domain, NO_SUCH_ALIAS)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   177
        self._delete()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   178
        del self._dests[:]
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   179
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   180
del _, cfg_dget