VirtualMailManager/Alias.py
changeset 0 bb0aa2102206
child 3 a9b44e04bf01
equal deleted inserted replaced
-1:000000000000 0:bb0aa2102206
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # opyright 2007-2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """Virtual Mail Manager's Alias class to manage email aliases."""
       
     8 
       
     9 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    10 __version__ = 'rev '+'$Rev$'.split()[1]
       
    11 __date__ = '$Date$'.split()[1]
       
    12 
       
    13 from Exceptions import VMMAliasException
       
    14 from Domain import Domain
       
    15 import constants.ERROR as ERR
       
    16 
       
    17 class Alias:
       
    18     """Class to manage email accounts."""
       
    19     def __init__(self, dbh, address, basedir, destination=None):
       
    20         if address == destination:
       
    21             raise VMMAliasException(('Address and destination are identical.',
       
    22                 ERR.ALIAS_ADDR_DEST_IDENTICAL))
       
    23         self._dbh = dbh
       
    24         self._addr = address
       
    25         self._dest = destination
       
    26         self._localpart = None
       
    27         self._gid = 0
       
    28         self._aid = 0
       
    29         self._setAddr(basedir)
       
    30         if not self._dest is None:
       
    31             self._exists()
       
    32         if self._isAccount():
       
    33             raise VMMAliasException(
       
    34             ('There is already an account with address «%s»' % self._addr,
       
    35                 ERR.ACCOUNT_EXISTS))
       
    36 
       
    37     def _exists(self):
       
    38         dbc = self._dbh.cursor()
       
    39         dbc.execute("SELECT id FROM alias WHERE gid=%s AND address=%s\
       
    40  AND destination=%s", self._gid, self._localpart, self._dest)
       
    41         aid = dbc.fetchone()
       
    42         dbc.close()
       
    43         if aid is not None:
       
    44             self._aid = aid[0]
       
    45             return True
       
    46         else:
       
    47             return False
       
    48 
       
    49     def _isAccount(self):
       
    50         dbc = self._dbh.cursor()
       
    51         dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s",
       
    52                 self._gid, self._localpart)
       
    53         uid = dbc.fetchone()
       
    54         dbc.close()
       
    55         if uid is not None:
       
    56             return True
       
    57         else:
       
    58             return False
       
    59         
       
    60     def _setAddr(self, basedir):
       
    61         self._localpart, d = self._addr.split('@')
       
    62         dom = Domain(self._dbh, d, basedir)
       
    63         self._gid = dom.getID()
       
    64         if self._gid == 0:
       
    65             raise VMMAliasException(("Domain «%s» doesn't exist." % d,
       
    66                 ERR.NO_SUCH_DOMAIN))
       
    67 
       
    68     def save(self):
       
    69         if self._dest is None:
       
    70            raise VMMAliasException(('No destination address for alias denoted.',
       
    71                ERR.ALIAS_MISSING_DEST))
       
    72         if self._aid < 1:
       
    73             dbc = self._dbh.cursor()
       
    74             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
       
    75  (%s, %s, %s)", self._gid, self._localpart, self._dest)
       
    76             self._dbh.commit()
       
    77             dbc.close()
       
    78         else:
       
    79             raise VMMAliasException(("Alias already exists.", ERR.ALIAS_EXISTS))
       
    80 
       
    81     def getInfo(self):
       
    82         dbc = self._dbh.cursor()
       
    83         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
       
    84                 self._gid, self._localpart)
       
    85         destinations = dbc.fetchall()
       
    86         dbc.close()
       
    87         if len(destinations) > 0:
       
    88             targets = []
       
    89             for destination in destinations:
       
    90                 targets.append(destination[0])
       
    91             return targets
       
    92         else:
       
    93             raise VMMAliasException(("Alias doesn't exists", ERR.NO_SUCH_ALIAS))
       
    94 
       
    95     def delete(self):
       
    96         dbc = self._dbh.cursor()
       
    97         dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
       
    98                 self._gid, self._localpart)
       
    99         rowcount = dbc.rowcount
       
   100         dbc.close()
       
   101         if rowcount > 0:
       
   102             self._dbh.commit()
       
   103         else:
       
   104             raise VMMAliasException(("Alias doesn't exists", ERR.NO_SUCH_ALIAS))
       
   105