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