Provide catchall_* methods to the Handler class v0.6.x
authormartin f. krafft <madduck@madduck.net>
Mon, 09 Apr 2012 23:21:56 +0200
branchv0.6.x
changeset 505 3da8c919584f
parent 504 f180ead60568
child 506 fb0ffde628d6
Provide catchall_* methods to the Handler class
VirtualMailManager/handler.py
--- a/VirtualMailManager/handler.py	Mon Apr 09 18:10:56 2012 +0200
+++ b/VirtualMailManager/handler.py	Mon Apr 09 23:21:56 2012 +0200
@@ -21,6 +21,7 @@
 from VirtualMailManager.account import Account
 from VirtualMailManager.alias import Alias
 from VirtualMailManager.aliasdomain import AliasDomain
+from VirtualMailManager.catchall import CatchallAlias
 from VirtualMailManager.common import exec_ok, lisdir
 from VirtualMailManager.config import Config as Cfg
 from VirtualMailManager.constants import MIN_GID, MIN_UID, \
@@ -258,6 +259,11 @@
         self._db_connect()
         return Alias(self._dbh, address)
 
+    def _get_catchall(self, domain):
+        """Return a CatchallAlias instances for the given domain (str)."""
+        self._db_connect()
+        return CatchallAlias(self._dbh, domain)
+
     def _get_relocated(self, address):
         """Return a Relocated instances for the given address (str)."""
         address = EmailAddress(address)
@@ -671,6 +677,39 @@
             alias.del_destination(DestinationEmailAddress(targetaddress,
                                                           self._dbh))
 
+    def catchall_add(self, domain, *targetaddresses):
+        """Creates a new `CatchallAlias` entry for the given *domain* with
+        the given *targetaddresses*."""
+        catchall = self._get_catchall(domain)
+        destinations = [DestinationEmailAddress(addr, self._dbh) \
+                for addr in targetaddresses]
+        warnings = []
+        destinations = catchall.add_destinations(destinations, warnings)
+        if warnings:
+            self._warnings.append(_('Ignored destination addresses:'))
+            self._warnings.extend(('  * %s' % w for w in warnings))
+        for destination in destinations:
+            if destination.gid and \
+               not self._chk_other_address_types(destination, TYPE_RELOCATED):
+                self._warnings.append(_(u"The destination account/alias '%s' "
+                                        u"does not exist.") % destination)
+
+    def catchall_info(self, domain):
+        """Returns an iterator object for all destinations (`EmailAddress`
+        instances) for the `CatchallAlias` with the given *domain*."""
+        return self._get_catchall(domain)
+
+    def catchall_delete(self, domain, targetaddress=None):
+        """Deletes the `CatchallAlias` for domain *domain* with all its
+        destinations from the database. If *targetaddress* is not ``None``,
+        only this destination will be removed from the alias."""
+        catchall = self._get_catchall(domain)
+        if targetaddress is None:
+            catchall.delete()
+        else:
+            catchall.del_destination(DestinationEmailAddress(targetaddress,
+                                                             self._dbh))
+
     def user_info(self, emailaddress, details=None):
         """Wrapper around Account.get_info(...)"""
         if details not in (None, 'du', 'aliases', 'full'):