VirtualMailManager/Account.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 11 Jun 2008 05:07:07 +0000
changeset 38 c44ea4526546
parent 34 6d74e20c5b3b
child 41 fbcb7e314510
permissions -rw-r--r--
* 'VirtualMailManager/Alias.py' - replaced all »« with '' * 'VirtualMailManager/Account.py' - replaced all »« with '' - added Account.setPassword() * 'VirtualMailManager/VirtualMailManager.py' - replaced all »« with '' - added VirtualMailManager._readpass() - check if domain/account exists before prompting for password (VirtualMailManager.user_add(), VirtualMailManager.user_password()) * 'vmm' - removed _readpass() * 'UPGRADE' * 'upgrade.sh' - added to repository again * 'po/vmm.pot' * 'po/de.po' - 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 Account class to manage e-mail accounts."""
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
32
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    16
import gettext
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    17
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
from Exceptions import VMMAccountException
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
from Domain import Domain
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    20
from Transport import Transport
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    21
from MailLocation import MailLocation
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
import constants.ERROR as ERR
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
32
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    24
gettext.bindtextdomain('vmm', '/usr/local/share/locale')
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    25
gettext.textdomain('vmm')
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    26
_ = gettext.gettext
ceb700bc4a80 * 'install.sh'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 24
diff changeset
    27
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
class Account:
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    29
    """Class to manage e-mail accounts."""
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    30
    def __init__(self, dbh, address, password=None):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        self._dbh = dbh
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        self._base = None
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        self._addr = address
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        self._localpart = None
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
        self._name = None
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        self._uid = 0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        self._gid = 0
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    38
        self._mid = 0
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    39
        self._tid = 0
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        self._passwd = password
24
48ea255e8a85 * 'vmm.cfg.5'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 20
diff changeset
    41
        self._setAddr()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
        self._exists()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        if self._isAlias():
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
            raise VMMAccountException(
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    45
            (_(u"There is already an alias with address '%s'") % address,
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
                ERR.ALIAS_EXISTS))
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
    def _exists(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
        dbc = self._dbh.cursor()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    50
        dbc.execute("SELECT uid, mid, tid FROM users \
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    51
WHERE gid=%s AND local_part=%s",
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
                self._gid, self._localpart)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    53
        result = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        dbc.close()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    55
        if result is not None:
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    56
            self._uid, self._mid, self._tid = result
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
            return True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
    def _isAlias(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        dbc = self._dbh.cursor()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    63
        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
    64
                self._gid, self._localpart)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    65
        gid = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        dbc.close()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    67
        if gid is not None:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
            return True
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
24
48ea255e8a85 * 'vmm.cfg.5'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 20
diff changeset
    72
    def _setAddr(self):
48ea255e8a85 * 'vmm.cfg.5'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 20
diff changeset
    73
        self._localpart, d = self._addr.split('@')
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    74
        dom = Domain(self._dbh, d)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
        self._gid = dom.getID()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        if self._gid == 0:
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    77
            raise VMMAccountException((_(u"Domain '%s' doesn't exist.") % d,
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
    78
                ERR.NO_SUCH_DOMAIN))
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    79
        self._base = dom.getDir()
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    80
        self._tid = dom.getTransportID()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
    def _setID(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        dbc.execute("SELECT nextval('users_uid')")
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        self._uid = dbc.fetchone()[0]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    88
    def _prepare(self, maillocation):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        self._setID()
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    90
        self._mid = MailLocation(self._dbh, maillocation=maillocation).getID()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
    92
    def _switchState(self, state, service):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        if not isinstance(state, bool):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
            return False
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
    95
        if not service in ['smtp', 'pop3', 'imap', 'managesieve', 'all', None]:
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
    96
            raise VMMAccountException((_(u"Unknown service '%s'") % service,
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
    97
                ERR.UNKNOWN_SERVICE))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        if self._uid < 1:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
    99
            raise VMMAccountException((_("Account doesn't exists"),
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   100
                ERR.NO_SUCH_ACCOUNT))
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   101
        dbc = self._dbh.cursor()
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   102
        if service in ['smtp', 'pop3', 'imap', 'managesieve']:
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   103
            dbc.execute(
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   104
                    "UPDATE users SET %s=%s WHERE local_part='%s' AND gid=%s"
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   105
                    % (service, state, self._localpart, self._gid))
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   106
        elif state:
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   107
            dbc.execute("UPDATE users SET smtp = TRUE, pop3 = TRUE,\
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   108
 imap = TRUE, managesieve = TRUE WHERE local_part = %s AND gid = %s",
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   109
                self._localpart, self._gid)
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   110
        else:
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   111
            dbc.execute("UPDATE users SET smtp = FALSE, pop3 = FALSE,\
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   112
 imap = FALSE, managesieve = FALSE WHERE local_part = %s AND gid = %s",
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   113
                self._localpart, self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   114
        if dbc.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
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   117
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   118
    def setPassword(self, password):
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   119
        self._passwd = password
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   120
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   121
    def getUID(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   122
        return self._uid
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   123
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   124
    def getGID(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   125
        return self._gid
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   126
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   127
    def getDir(self, directory):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   128
        if directory == 'domain':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
            return '%s' % self._base
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   130
        elif directory == 'home':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   131
            return '%s/%i' % (self._base, self._uid)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   133
    def enable(self, service=None):
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   134
        self._switchState(True, service)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   135
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   136
    def disable(self, service=None):
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   137
        self._switchState(False, service)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   138
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   139
    def save(self, maillocation, smtp, pop3, imap, managesieve):
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   140
        if self._uid < 1:
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   141
            self._prepare(maillocation)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   142
            dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   143
            dbc.execute("""INSERT INTO users (local_part, passwd, uid, gid,\
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   144
 mid, tid, smtp, pop3, imap, managesieve)\
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   145
 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   146
                self._localpart, self._passwd, self._uid, self._gid, self._mid,
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   147
                self._tid, smtp, pop3, imap, managesieve)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   150
        else:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   151
            raise VMMAccountException((_('Account already exists.'),
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   152
                ERR.ACCOUNT_EXISTS))
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   153
       
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   154
    def modify(self, what, value):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   155
        if self._uid == 0:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   156
            raise VMMAccountException((_("Account doesn't exists"),
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   157
                ERR.NO_SUCH_ACCOUNT))
17
fe9be0081e5f * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   158
        if what not in ['name', 'password', 'transport']:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   159
            return False
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   160
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   161
        if what == 'password':
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   162
            dbc.execute("UPDATE users SET passwd=%s WHERE local_part=%s AND\
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   163
 gid=%s", value, self._localpart, self._gid)
17
fe9be0081e5f * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   164
        elif what == 'transport':
fe9be0081e5f * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   165
            self._tid = Transport(self._dbh, transport=value).getID()
fe9be0081e5f * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   166
            dbc.execute("UPDATE users SET tid=%s WHERE local_part=%s AND\
fe9be0081e5f * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 9
diff changeset
   167
 gid=%s", self._tid, self._localpart, self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   168
        else:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   169
            dbc.execute("UPDATE users SET name=%s WHERE local_part=%s AND\
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   170
 gid=%s", value, self._localpart, self._gid)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   171
        if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   172
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   173
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   174
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   175
    def getInfo(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   176
        dbc = self._dbh.cursor()
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   177
        dbc.execute("SELECT name, uid, gid, mid, tid, smtp, pop3, imap, \
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   178
 managesieve FROM users WHERE local_part=%s AND gid=%s",
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   179
            self._localpart, self._gid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   180
        info = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   181
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   182
        if info is None:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   183
            raise VMMAccountException((_("Account doesn't exists"),
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   184
                ERR.NO_SUCH_ACCOUNT))
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   185
        else:
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   186
            keys = ['name', 'uid', 'gid', 'maildir', 'transport', 'smtp',
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   187
                    'pop3', 'imap', 'managesieve']
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   188
            info = dict(zip(keys, info))
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   189
            for service in ['smtp', 'pop3', 'imap', 'managesieve']:
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   190
                if bool(info[service]):
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   191
                    info[service] = 'enabled'
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   192
                else:
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   193
                    info[service] = 'disabled'
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   194
            info['address'] = self._addr
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   195
            info['maildir'] = '%s/%s/%s' % (self._base, info['uid'],
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   196
                    MailLocation(self._dbh,
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   197
                        mid=info['maildir']).getMailLocation())
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   198
            info['transport'] = Transport(self._dbh,
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
   199
                    tid=info['transport']).getTransport()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   200
            return info
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   201
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   202
    def delete(self):
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   203
        if self._uid > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   204
            dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   205
            dbc.execute("DELETE FROM users WHERE gid=%s AND local_part=%s",
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   206
                    self._gid, self._localpart)
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   207
            if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   208
                self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   209
            dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   210
        else:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   211
            raise VMMAccountException((_("Account doesn't exists"),
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   212
                ERR.NO_SUCH_ACCOUNT))
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   213
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   214
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   215
def getAccountByID(uid, dbh):
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   216
    try:
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   217
        uid = long(uid)
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   218
    except ValueError:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   219
        raise VMMAccountException((_('uid must be an int/long.'),
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   220
            ERR.INVALID_AGUMENT))
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   221
    if uid < 1:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   222
        raise VMMAccountException((_('uid must be greater than 0.'),
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   223
            ERR.INVALID_AGUMENT))
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   224
    dbc = dbh.cursor()
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   225
    dbc.execute("SELECT local_part||'@'||domains.domainname AS address, uid,\
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   226
 gid FROM users LEFT JOIN domains USING(gid) WHERE uid=%s", uid)
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   227
    info = dbc.fetchone()
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   228
    dbc.close()
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   229
    if info is None:
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
   230
        raise VMMAccountException((_("Account doesn't exists"),
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   231
            ERR.NO_SUCH_ACCOUNT))
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   232
    keys = ['address', 'uid', 'gid']
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   233
    info = dict(zip(keys, info))
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   234
    return info
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   235