VirtualMailManager/catchall.py
author Pascal Volk <user@localhost.localdomain.org>
Mon, 24 Mar 2014 19:22:04 +0200
changeset 748 659c4476c57c
parent 703 58815c004a61
child 711 2a75058fc064
permissions -rw-r--r--
po: Updated Finnish translation (translated by Jorma Karvonen).
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 618
diff changeset
     2
# Copyright (c) 2012 - 2014, martin f. krafft
504
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.pycompat import all
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    27
from VirtualMailManager.constants import \
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    28
     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
    29
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    30
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    31
_ = lambda msg: msg
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    32
cfg_dget = lambda option: None
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
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    35
class CatchallAlias(object):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    36
    """Class to manage domain catch-all aliases."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    37
    __slots__ = ('_domain', '_dests', '_gid', '_dbh')
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    38
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    39
    def __init__(self, dbh, domain):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    40
        self._domain = domain
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    41
        self._dbh = dbh
507
2aae58cddfa9 Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 504
diff changeset
    42
        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
    43
        if not self._gid:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    44
            raise AErr(_(u"The domain '%s' does not exist.") %
513
557c4703986c Fix syntax errors
martin f. krafft <madduck@madduck.net>
parents: 509
diff changeset
    45
                       self.domain, NO_SUCH_DOMAIN)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    46
        self._dests = []
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    47
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    48
        self._load_dests()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    49
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    50
    def _load_dests(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    51
        """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
    52
        dbc = self._dbh.cursor()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    53
        dbc.execute('SELECT destination FROM catchall WHERE gid = %s',
507
2aae58cddfa9 Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 504
diff changeset
    54
                    (self._gid,))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    55
        dests = dbc.fetchall()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    56
        if dbc.rowcount > 0:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    57
            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
    58
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    59
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    60
    def _check_expansion(self, count_new):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    61
        """Checks the current expansion limit of the alias."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    62
        postconf = Postconf(cfg_dget('bin.postconf'))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    63
        limit = long(postconf.read('virtual_alias_expansion_limit'))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    64
        dcount = len(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    65
        failed = False
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    66
        if dcount == limit or dcount + count_new > limit:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    67
            failed = True
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    68
            errmsg = _(
608
0ed93eb8b364 VMM: Corrected/updated some msgids.
Pascal Volk <user@localhost.localdomain.org>
parents: 513
diff changeset
    69
u"""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
    70
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
    71
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
    72
unusable.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    73
Hint: Increase Postfix' virtual_alias_expansion_limit""")
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    74
        elif dcount > limit:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    75
            failed = True
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    76
            errmsg = _(
611
8e9b0046bc8f PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents: 608
diff changeset
    77
u"""Cannot add %(count_new)i new destination(s) to catch-all alias for domain
8e9b0046bc8f PEP8: Fixed all PEP8 related issues.
Pascal Volk <user@localhost.localdomain.org>
parents: 608
diff changeset
    78
'%(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
    79
(%(count)i/%(limit)i).
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    80
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
    81
Hint: Delete some destination addresses.""")
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    82
        if failed:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    83
            raise AErr(errmsg % {'domain': self._domain, 'count': dcount,
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    84
                                 'limit': limit, 'count_new': count_new},
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    85
                       ALIAS_EXCEEDS_EXPANSION_LIMIT)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    86
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    87
    def _delete(self, destinations=None):
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    88
        """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
    89
        ``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
    90
        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
    91
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    92
        """
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
    93
        dbc = self._dbh.cursor()
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    94
        if not destinations:
509
1a9247e9763f Correct syntax errors
martin f. krafft <madduck@madduck.net>
parents: 507
diff changeset
    95
            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
    96
        else:
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    97
            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
    98
                            'destination = %%s' % self._gid,
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
    99
                            ((str(dest),) for dest in destinations))
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   100
        if dbc.rowcount > 0:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   101
            self._dbh.commit()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   102
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   103
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   104
    def __len__(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   105
        """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
   106
        return len(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   107
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   108
    @property
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   109
    def domain(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   110
        """The Alias' domain."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   111
        return self._domain
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   112
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   113
    def add_destinations(self, destinations, warnings=None):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   114
        """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
   115
        destinations of the catchall alias.
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   116
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   117
        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
   118
        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
   119
        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
   120
        """
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   121
        destinations = set(destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   122
        assert destinations and \
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   123
                all(isinstance(dest, EmailAddress) for dest in destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   124
        if not warnings is None:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   125
            assert isinstance(warnings, list)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   126
        duplicates = destinations.intersection(set(self._dests))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   127
        if duplicates:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   128
            destinations.difference_update(set(self._dests))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   129
            if not warnings is None:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   130
                warnings.extend(duplicates)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   131
        if not destinations:
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   132
            return destinations
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   133
        self._check_expansion(len(destinations))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   134
        dbc = self._dbh.cursor()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   135
        dbc.executemany("INSERT INTO catchall (gid, destination) "
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   136
                        "VALUES (%d, %%s)" % self._gid,
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   137
                        ((str(destination),) for destination in destinations))
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   138
        self._dbh.commit()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   139
        dbc.close()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   140
        self._dests.extend(destinations)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   141
        return destinations
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   142
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   143
    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
   144
        """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
   145
        destinations = set(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   146
        assert destinations and \
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
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: 611
diff changeset
   148
        if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   149
            assert isinstance(warnings, list)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   150
        if not self._dests:
608
0ed93eb8b364 VMM: Corrected/updated some msgids.
Pascal Volk <user@localhost.localdomain.org>
parents: 513
diff changeset
   151
            raise AErr(_(u"There are no catch-all aliases defined for "
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   152
                         u"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
   153
        unknown = destinations.difference(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   154
        if unknown:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   155
            destinations.intersection_update(set(self._dests))
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   156
            if not warnings is None:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   157
                warnings.extend(unknown)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   158
        if not destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   159
            raise AErr(_(u"No suitable destinations left to remove from the "
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   160
                         u"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
   161
                       NO_SUCH_ALIAS)
618
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   162
        self._delete(destinations)
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   163
        for destination in destinations:
d8736bb80bdc VMM: {alias,catchall}delete: Accept multiple destinations.
Pascal Volk <user@localhost.localdomain.org>
parents: 611
diff changeset
   164
            self._dests.remove(destination)
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   165
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   166
    def get_destinations(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   167
        """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
   168
        if not self._dests:
608
0ed93eb8b364 VMM: Corrected/updated some msgids.
Pascal Volk <user@localhost.localdomain.org>
parents: 513
diff changeset
   169
            raise AErr(_(u"There are no catch-all aliases defined for "
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   170
                         u"domain '%s'.") % self._domain, NO_SUCH_ALIAS)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   171
        return iter(self._dests)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   172
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   173
    def delete(self):
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   174
        """Deletes all catchall destinations for the domain."""
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   175
        if not self._dests:
608
0ed93eb8b364 VMM: Corrected/updated some msgids.
Pascal Volk <user@localhost.localdomain.org>
parents: 513
diff changeset
   176
            raise AErr(_(u"There are no catch-all aliases defined for "
504
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   177
                         u"domain '%s'.") % self._domain, NO_SUCH_ALIAS)
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   178
        self._delete()
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   179
        del self._dests[:]
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   180
f180ead60568 First version of a CatchallAlias class
martin f. krafft <madduck@madduck.net>
parents:
diff changeset
   181
del _, cfg_dget