VirtualMailManager/Alias.py
changeset 52 c152d7714802
parent 48 0d5f58f8b8f5
child 76 14c0a092d7d2
equal deleted inserted replaced
51:f5ca21303871 52:c152d7714802
    11 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
    11 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
    12 __version__ = VERSION
    12 __version__ = VERSION
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    13 __revision__ = 'rev '+'$Rev$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    14 __date__ = '$Date$'.split()[1]
    15 
    15 
    16 from Exceptions import VMMAliasException
    16 from Exceptions import VMMAliasException as VMMAE
    17 from Domain import Domain
    17 from Domain import Domain
    18 import constants.ERROR as ERR
    18 import constants.ERROR as ERR
       
    19 import VirtualMailManager as VMM
    19 
    20 
    20 class Alias:
    21 class Alias:
    21     """Class to manage e-mail accounts."""
    22     """Class to manage e-mail accounts."""
    22     def __init__(self, dbh, address, destination=None):
    23     def __init__(self, dbh, address, destination=None):
    23         if address == destination:
    24         if address == destination:
    24             raise VMMAliasException(
    25             raise VMMAE(_(u"Address and destination are identical."),
    25                 _(u'Address and destination are identical.'),
       
    26                 ERR.ALIAS_ADDR_DEST_IDENTICAL)
    26                 ERR.ALIAS_ADDR_DEST_IDENTICAL)
    27         self._dbh = dbh
    27         self._dbh = dbh
    28         self._addr = address
    28         self._addr = VMM.VirtualMailManager.chkEmailAddress(address)
    29         self._dest = destination
    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)
    30         self._localpart = None
    35         self._localpart = None
    31         self._gid = 0
    36         self._gid = 0
    32         self._isNew = False
    37         self._isNew = False
    33         self._setAddr()
    38         self._setAddr()
    34         if not self._dest is None:
    39         if not self._dest is None:
    35             self._exists()
    40             self._exists()
    36         if self._isAccount():
    41         if self._isAccount():
    37             raise VMMAliasException(
    42             raise VMMAE(_(u"There is already an account with address »%s«.") %
    38             _(u"There is already an account with address '%s'") % self._addr,
    43                     self._addr, ERR.ACCOUNT_EXISTS)
    39                 ERR.ACCOUNT_EXISTS)
       
    40 
    44 
    41     def _exists(self):
    45     def _exists(self):
    42         dbc = self._dbh.cursor()
    46         dbc = self._dbh.cursor()
    43         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    47         dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
    44  AND destination=%s", self._gid, self._localpart, self._dest)
    48  AND destination=%s", self._gid, self._localpart, self._dest)
    63     def _setAddr(self):
    67     def _setAddr(self):
    64         self._localpart, d = self._addr.split('@')
    68         self._localpart, d = self._addr.split('@')
    65         dom = Domain(self._dbh, d)
    69         dom = Domain(self._dbh, d)
    66         self._gid = dom.getID()
    70         self._gid = dom.getID()
    67         if self._gid == 0:
    71         if self._gid == 0:
    68             raise VMMAliasException(_(u"Domain '%s' doesn't exist.") % d,
    72             raise VMMAE(_(u"The domain »%s« doesn't exist yet.") % d,
    69                 ERR.NO_SUCH_DOMAIN)
    73                 ERR.NO_SUCH_DOMAIN)
    70 
    74 
    71     def save(self):
    75     def save(self):
    72         if self._dest is None:
    76         if self._dest is None:
    73            raise VMMAliasException(
    77            raise VMMAE(_(u"No destination address for alias denoted."),
    74                _('No destination address for alias denoted.'),
       
    75                ERR.ALIAS_MISSING_DEST)
    78                ERR.ALIAS_MISSING_DEST)
    76         if self._isNew:
    79         if self._isNew:
    77             dbc = self._dbh.cursor()
    80             dbc = self._dbh.cursor()
    78             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    81             dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
    79  (%s, %s, %s)", self._gid, self._localpart, self._dest)
    82  (%s, %s, %s)", self._gid, self._localpart, self._dest)
    80             self._dbh.commit()
    83             self._dbh.commit()
    81             dbc.close()
    84             dbc.close()
    82         else:
    85         else:
    83             raise VMMAliasException(_("Alias already exists."),
    86             raise VMMAE(_(u"The alias »%s« already exists.") % self._addr,
    84                 ERR.ALIAS_EXISTS)
    87                     ERR.ALIAS_EXISTS)
    85 
    88 
    86     def getInfo(self):
    89     def getInfo(self):
    87         dbc = self._dbh.cursor()
    90         dbc = self._dbh.cursor()
    88         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    91         dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
    89                 self._gid, self._localpart)
    92                 self._gid, self._localpart)
    93             targets = []
    96             targets = []
    94             for destination in destinations:
    97             for destination in destinations:
    95                 targets.append(destination[0])
    98                 targets.append(destination[0])
    96             return targets
    99             return targets
    97         else:
   100         else:
    98             raise VMMAliasException(_("Alias doesn't exists"),
   101             raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
    99                 ERR.NO_SUCH_ALIAS)
   102                     ERR.NO_SUCH_ALIAS)
   100 
   103 
   101     def delete(self):
   104     def delete(self):
   102         dbc = self._dbh.cursor()
   105         dbc = self._dbh.cursor()
   103         if self._dest is None:
   106         if self._dest is None:
   104             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   107             dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
   109         rowcount = dbc.rowcount
   112         rowcount = dbc.rowcount
   110         dbc.close()
   113         dbc.close()
   111         if rowcount > 0:
   114         if rowcount > 0:
   112             self._dbh.commit()
   115             self._dbh.commit()
   113         else:
   116         else:
   114             raise VMMAliasException(_("Alias doesn't exists"),
   117             raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
   115                 ERR.NO_SUCH_ALIAS)
   118                     ERR.NO_SUCH_ALIAS)
   116 
   119