VirtualMailManager/Alias.py
changeset 76 14c0a092d7d2
parent 52 c152d7714802
child 78 8f1e501b1bb1
equal deleted inserted replaced
75:af813ede1e19 76:14c0a092d7d2
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
     1 # -*- coding: UTF-8 -*-
     3 # Copyright 2007-2008 VEB IT
     2 # Copyright 2007-2008 VEB IT
     4 # See COPYING for distribution information.
     3 # See COPYING for distribution information.
     5 # $Id$
     4 # $Id$
     6 
     5 
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    12 __revision__ = 'rev '+'$Rev$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    13 __date__ = '$Date$'.split()[1]
    15 
    14 
    16 from Exceptions import VMMAliasException as VMMAE
    15 from Exceptions import VMMAliasException as VMMAE
    17 from Domain import Domain
    16 from Domain import Domain
       
    17 from EmailAddress import EmailAddress
    18 import constants.ERROR as ERR
    18 import constants.ERROR as ERR
    19 import VirtualMailManager as VMM
    19 import VirtualMailManager as VMM
    20 
    20 
    21 class Alias:
    21 class Alias:
    22     """Class to manage e-mail accounts."""
    22     """Class to manage e-mail aliases."""
    23     def __init__(self, dbh, address, destination=None):
    23     def __init__(self, dbh, address, destination=None):
       
    24         if isinstance(address, EmailAddress):
       
    25             self._addr = address
       
    26         else:
       
    27             raise TypeError("Argument 'address' is not an EmailAddress")
       
    28         if destination is None:
       
    29             self._dest = None
       
    30         elif isinstance(destination, EmailAddress):
       
    31             self._dest = destination
       
    32         else:
       
    33             raise TypeError("Argument 'destination' is not an EmailAddress")
    24         if address == destination:
    34         if address == destination:
    25             raise VMMAE(_(u"Address and destination are identical."),
    35             raise VMMAE(_(u"Address and destination are identical."),
    26                 ERR.ALIAS_ADDR_DEST_IDENTICAL)
    36                 ERR.ALIAS_ADDR_DEST_IDENTICAL)
    27         self._dbh = dbh
    37         self._dbh = dbh
    28         self._addr = VMM.VirtualMailManager.chkEmailAddress(address)
       
    29         if destination is None:
       
    30             self._dest = None
       
    31         elif destination.count('@'):
       
    32             self._dest = VMM.VirtualMailManager.chkEmailAddress(destination)
       
    33         else:
       
    34             self._dest = VMM.VirtualMailManager.chkLocalpart(destination)
       
    35         self._localpart = None
       
    36         self._gid = 0
    38         self._gid = 0
    37         self._isNew = False
    39         self._isNew = False
    38         self._setAddr()
    40         self._setAddr()
    39         if not self._dest is None:
    41         if not self._dest is None:
    40             self._exists()
    42             self._exists()
    41         if self._isAccount():
    43         if VMM.VirtualMailManager.accountExists(self._dbh, self._addr):
    42             raise VMMAE(_(u"There is already an account with address »%s«.") %
    44             raise VMMAE(_(u"There is already an account with address »%s«.") %\
    43                     self._addr, ERR.ACCOUNT_EXISTS)
    45                     self._addr, ERR.ACCOUNT_EXISTS)
       
    46         if VMM.VirtualMailManager.relocatedExists(self._dbh, self._addr):
       
    47             raise VMMAE(
       
    48               _(u"There is already an relocated user with the address »%s«.") %\
       
    49                     self._addr, ERR.RELOCATED_EXISTS)
    44 
    50 
    45     def _exists(self):
    51     def _exists(self):
    46         dbc = self._dbh.cursor()
    52         dbc = self._dbh.cursor()
    47         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    53         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    48  AND destination=%s", self._gid, self._localpart, self._dest)
    54  AND destination=%s", self._gid, self._addr._localpart, str(self._dest))
    49         gid = dbc.fetchone()
    55         gid = dbc.fetchone()
    50         dbc.close()
    56         dbc.close()
    51         if gid is None:
    57         if gid is None:
    52             self._isNew = True
    58             self._isNew = True
    53         else:
       
    54             self._isNew = False
       
    55 
    59 
    56     def _isAccount(self):
       
    57         dbc = self._dbh.cursor()
       
    58         dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s",
       
    59                 self._gid, self._localpart)
       
    60         uid = dbc.fetchone()
       
    61         dbc.close()
       
    62         if uid is not None:
       
    63             return True
       
    64         else:
       
    65             return False
       
    66         
       
    67     def _setAddr(self):
    60     def _setAddr(self):
    68         self._localpart, d = self._addr.split('@')
    61         dom = Domain(self._dbh, self._addr._domainname)
    69         dom = Domain(self._dbh, d)
       
    70         self._gid = dom.getID()
    62         self._gid = dom.getID()
    71         if self._gid == 0:
    63         if self._gid == 0:
    72             raise VMMAE(_(u"The domain »%s« doesn't exist yet.") % d,
    64             raise VMMAE(_(u"The domain »%s« doesn't exist yet.") %\
    73                 ERR.NO_SUCH_DOMAIN)
    65                     self._addr._domainname, ERR.NO_SUCH_DOMAIN)
    74 
    66 
    75     def save(self):
    67     def save(self):
    76         if self._dest is None:
    68         if self._dest is None:
    77            raise VMMAE(_(u"No destination address for alias denoted."),
    69            raise VMMAE(_(u"No destination address for alias denoted."),
    78                ERR.ALIAS_MISSING_DEST)
    70                ERR.ALIAS_MISSING_DEST)
    79         if self._isNew:
    71         if self._isNew:
    80             dbc = self._dbh.cursor()
    72             dbc = self._dbh.cursor()
    81             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    73             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    82  (%s, %s, %s)", self._gid, self._localpart, self._dest)
    74  (%s, %s, %s)", self._gid, self._addr._localpart, str(self._dest))
    83             self._dbh.commit()
    75             self._dbh.commit()
    84             dbc.close()
    76             dbc.close()
    85         else:
    77         else:
    86             raise VMMAE(_(u"The alias »%s« already exists.") % self._addr,
    78             raise VMMAE(
    87                     ERR.ALIAS_EXISTS)
    79                _(u"The alias »%(a)s« with destination »%(d)s« already exists.")\
       
    80                        % {'a': self._addr, 'd': self._dest}, ERR.ALIAS_EXISTS)
    88 
    81 
    89     def getInfo(self):
    82     def getInfo(self):
    90         dbc = self._dbh.cursor()
    83         dbc = self._dbh.cursor()
    91         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    84         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    92                 self._gid, self._localpart)
    85                 self._gid, self._addr._localpart)
    93         destinations = dbc.fetchall()
    86         destinations = dbc.fetchall()
    94         dbc.close()
    87         dbc.close()
    95         if len(destinations) > 0:
    88         if len(destinations) > 0:
    96             targets = []
    89             targets = []
    97             for destination in destinations:
    90             for destination in destinations:
   103 
    96 
   104     def delete(self):
    97     def delete(self):
   105         dbc = self._dbh.cursor()
    98         dbc = self._dbh.cursor()
   106         if self._dest is None:
    99         if self._dest is None:
   107             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   100             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   108                     self._gid, self._localpart)
   101                     self._gid, self._addr._localpart)
   109         else:
   102         else:
   110             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
   103             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
   111  destination=%s", self._gid, self._localpart, self._dest)
   104  destination=%s", self._gid, self._addr._localpart, str(self._dest))
   112         rowcount = dbc.rowcount
   105         rowcount = dbc.rowcount
   113         dbc.close()
   106         dbc.close()
   114         if rowcount > 0:
   107         if rowcount > 0:
   115             self._dbh.commit()
   108             self._dbh.commit()
   116         else:
   109         else:
   117             raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
   110             if self._dest is None:
   118                     ERR.NO_SUCH_ALIAS)
   111                 msg = u"The alias »%s« doesn't exists." % self._addr
       
   112             else:
       
   113                 msg = u"The alias »%(a)s« with destination »%(d)s« doesn't\
       
   114  exists." % {'a': self._addr, 'd': self._dest}
       
   115             raise VMMAE(_(msg), ERR.NO_SUCH_ALIAS)
   119 
   116