VirtualMailManager/Alias.py
author Pascal Volk <neverseen@users.sourceforge.net>
Thu, 21 Aug 2008 01:23:31 +0000
changeset 52 c152d7714802
parent 48 0d5f58f8b8f5
child 76 14c0a092d7d2
permissions -rw-r--r--
* 'VirtualMailManager/Alias.py' - call VirtualMailManager.chkEmailAddress() in Alias.__init__() * 'VirtualMailManager/VirtualMailManager.py' - VirtualMailManager.chkEmailAddress() checks also for len(domainname) > 0 * 'po/de.po' * 'po/vmm.pot' - updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
#!/usr/bin/env python
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     2
# -*- coding: UTF-8 -*-
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 4
diff changeset
     3
# Copyright 2007-2008 VEB IT
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
# See COPYING for distribution information.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
# $Id$
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
     7
"""Virtual Mail Manager's Alias class to manage e-mail aliases."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
4
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
     9
from constants.VERSION import VERSION
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
    10
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
__author__ = 'Pascal Volk <p.volk@veb-it.de>'
4
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
    12
__version__ = VERSION
f3c30b7421b9 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 3
diff changeset
    13
__revision__ = 'rev '+'$Rev$'.split()[1]
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
__date__ = '$Date$'.split()[1]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    16
from Exceptions import VMMAliasException as VMMAE
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
from Domain import Domain
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
import constants.ERROR as ERR
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    19
import VirtualMailManager as VMM
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
class Alias:
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    22
    """Class to manage e-mail accounts."""
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    23
    def __init__(self, dbh, address, destination=None):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
        if address == destination:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    25
            raise VMMAE(_(u"Address and destination are identical."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    26
                ERR.ALIAS_ADDR_DEST_IDENTICAL)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        self._dbh = dbh
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    28
        self._addr = VMM.VirtualMailManager.chkEmailAddress(address)
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    29
        if destination is None:
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    30
            self._dest = None
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    31
        elif destination.count('@'):
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    32
            self._dest = VMM.VirtualMailManager.chkEmailAddress(destination)
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    33
        else:
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    34
            self._dest = VMM.VirtualMailManager.chkLocalpart(destination)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        self._localpart = None
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        self._gid = 0
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    37
        self._isNew = False
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    38
        self._setAddr()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        if not self._dest is None:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
            self._exists()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        if self._isAccount():
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    42
            raise VMMAE(_(u"There is already an account with address »%s«.") %
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    43
                    self._addr, ERR.ACCOUNT_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
    def _exists(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
        dbc = self._dbh.cursor()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    47
        dbc.execute("SELECT gid FROM alias WHERE gid=%s AND address=%s\
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
 AND destination=%s", self._gid, self._localpart, self._dest)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    49
        gid = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
        dbc.close()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    51
        if gid is None:
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    52
            self._isNew = True
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        else:
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    54
            self._isNew = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
    def _isAccount(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        dbc.execute("SELECT uid FROM users WHERE gid=%s AND local_part=%s",
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
                self._gid, self._localpart)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        uid = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        if uid is not None:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
            return True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    67
    def _setAddr(self):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        self._localpart, d = self._addr.split('@')
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    69
        dom = Domain(self._dbh, d)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        self._gid = dom.getID()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        if self._gid == 0:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    72
            raise VMMAE(_(u"The domain »%s« doesn't exist yet.") % d,
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    73
                ERR.NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    def save(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        if self._dest is None:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    77
           raise VMMAE(_(u"No destination address for alias denoted."),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    78
               ERR.ALIAS_MISSING_DEST)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    79
        if self._isNew:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
            dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
            dbc.execute("INSERT INTO alias (gid, address, destination) VALUES\
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
 (%s, %s, %s)", self._gid, self._localpart, self._dest)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        else:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    86
            raise VMMAE(_(u"The alias »%s« already exists.") % self._addr,
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    87
                    ERR.ALIAS_EXISTS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
    def getInfo(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        dbc.execute('SELECT destination FROM alias WHERE gid=%s AND address=%s',
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
                self._gid, self._localpart)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        destinations = dbc.fetchall()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        if len(destinations) > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
            targets = []
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
            for destination in destinations:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
                targets.append(destination[0])
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    99
            return targets
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
        else:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   101
            raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   102
                    ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   103
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   104
    def delete(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   105
        dbc = self._dbh.cursor()
12
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   106
        if self._dest is None:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   107
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s",
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   108
                    self._gid, self._localpart)
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   109
        else:
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   110
            dbc.execute("DELETE FROM alias WHERE gid=%s AND address=%s AND \
44161dbb1518 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   111
 destination=%s", self._gid, self._localpart, self._dest)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   112
        rowcount = dbc.rowcount
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   113
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        if rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   115
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
        else:
52
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   117
            raise VMMAE(_(u"The alias »%s« doesn't exists.") % self._addr,
c152d7714802 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   118
                    ERR.NO_SUCH_ALIAS)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   119