VirtualMailManager/Account.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 21 Jul 2010 23:55:32 +0000
branchv0.6.x
changeset 302 32b4a39b5640
parent 293 a77b67673aa6
child 306 504fd29b4712
permissions -rw-r--r--
VMM/maillocation: Reworked class MailLocation to match the new database structure.
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
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 156
diff changeset
     2
# Copyright (c) 2007 - 2010, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     5
"""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     6
    VirtualMailManager.Account
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 293
diff changeset
     7
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     9
    Virtual Mail Manager's Account class to manage e-mail accounts.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    10
"""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    11
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    12
from VirtualMailManager.Domain import Domain
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    13
from VirtualMailManager.EmailAddress import EmailAddress
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    14
from VirtualMailManager.Transport import Transport
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    15
from VirtualMailManager.common import version_str
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    16
from VirtualMailManager.constants.ERROR import \
252
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
    17
     ACCOUNT_EXISTS, ACCOUNT_MISSING_PASSWORD, ALIAS_PRESENT, \
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    18
     INVALID_AGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, NO_SUCH_DOMAIN, \
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    19
     UNKNOWN_SERVICE
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    20
from VirtualMailManager.errors import AccountError as AErr
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    21
from VirtualMailManager.maillocation import MailLocation
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    22
from VirtualMailManager.password import pwhash
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    24
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    25
_ = lambda msg: msg
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    26
cfg_dget = lambda option: None
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    27
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    28
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
    29
class Account(object):
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    30
    """Class to manage e-mail accounts."""
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    31
    __slots__ = ('_addr', '_dbh', '_domain', '_mail', '_new', '_passwd',
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    32
                 '_transport', '_uid')
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    33
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    34
    def __init__(self, dbh, address):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    35
        """Creates a new Account instance.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    36
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    37
        When an account with the given *address* could be found in the
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    38
        database all relevant data will be loaded.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    39
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    40
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    41
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    42
        `dbh` : pyPgSQL.PgSQL.Connection
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    43
          A database connection for the database access.
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    44
        `address` : VirtualMailManager.EmailAddress.EmailAddress
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    45
          The e-mail address of the (new) Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    46
        """
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    47
        if not isinstance(address, EmailAddress):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    48
            raise TypeError("Argument 'address' is not an EmailAddress")
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    49
        self._addr = address
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
        self._dbh = dbh
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    51
        self._domain = Domain(self._dbh, self._addr.domainname)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    52
        if not self._domain.gid:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    53
            raise AErr(_(u"The domain '%s' doesn't exist.") %
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    54
                       self._addr.domainname, NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        self._uid = 0
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    56
        self._mail = None
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    57
        self._transport = self._domain.transport
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    58
        self._passwd = None
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    59
        self._new = True
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    60
        self._load()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    62
    def __nonzero__(self):
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    63
        """Returns `True` if the Account is known, `False` if it's new."""
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    64
        return not self._new
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    65
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    66
    def _load(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    67
        """Load 'uid', 'mid' and 'tid' from the database and set _new to
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    68
        `False` - if the user could be found. """
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    70
        dbc.execute('SELECT uid, mid, tid FROM users WHERE gid = %s AND '
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    71
                    'local_part = %s', self._domain.gid, self._addr.localpart)
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    72
        result = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
        dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    74
        if result:
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    75
            self._uid, _mid, _tid = result
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    76
            if _tid != self._transport.tid:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    77
                self._transport = Transport(self._dbh, tid=_tid)
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    78
            self._mail = MailLocation(mid=_mid)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    79
            self._new = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    81
    def _set_uid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    82
        """Set the unique ID for the new Account."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    83
        assert self._uid == 0
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        dbc.execute("SELECT nextval('users_uid')")
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        self._uid = dbc.fetchone()[0]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    89
    def _prepare(self, maillocation):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    90
        """Check and set different attributes - before we store the
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    91
        information in the database.
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    92
        """
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    93
        if maillocation.dovecot_version > cfg_dget('misc.dovecot_version'):
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    94
            raise AErr(_(u"The mail_location prefix '%(prefix)s' requires "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    95
                         u"Dovecot >= v%(version)s") %
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    96
                       {'prefix': maillocation.prefix,
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
    97
                        'version': version_str(maillocation.dovecot_version)},
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    98
                       INVALID_MAIL_LOCATION)
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    99
        if not maillocation.postfix and \
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   100
          self._transport.transport.lower() in ('virtual:', 'virtual'):
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   101
            raise AErr(_(u"Invalid transport '%(transport)s' for mail_location"
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   102
                         u" prefix '%(prefix)s'") %
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   103
                       {'transport': self._transport,
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   104
                        'prefix': maillocation.prefix}, INVALID_MAIL_LOCATION)
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   105
        self._mail = maillocation
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   106
        self._set_uid()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   107
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   108
    def _switch_state(self, state, service):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   109
        """Switch the state of the Account's services on or off. See
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   110
        Account.enable()/Account.disable() for more information."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   111
        self._chk_state()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   112
        if service not in (None, 'all', 'imap', 'pop3', 'sieve', 'smtp'):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   113
            raise AErr(_(u"Unknown service: '%s'.") % service, UNKNOWN_SERVICE)
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   114
        if cfg_dget('misc.dovecot_version') >= 0x10200b02:
134
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   115
            sieve_col = 'sieve'
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   116
        else:
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   117
            sieve_col = 'managesieve'
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   118
        if service in ('smtp', 'pop3', 'imap'):
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   119
            sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (service, state,
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   120
                                                               self._uid)
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   121
        elif service == 'sieve':
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   122
            sql = 'UPDATE users SET %s = %s WHERE uid = %d' % (sieve_col,
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   123
                                                               state,
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   124
                                                               self._uid)
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   125
        else:
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   126
            sql = 'UPDATE users SET smtp = %(s)s, pop3 = %(s)s, imap = %(s)s,\
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   127
 %(col)s = %(s)s WHERE uid = %(uid)d' % \
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   128
                {'s': state, 'col': sieve_col, 'uid': self._uid}
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   129
        dbc = self._dbh.cursor()
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   130
        dbc.execute(sql)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   131
        if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   132
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   133
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   134
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   135
    def _count_aliases(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   136
        """Count all alias addresses where the destination address is the
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   137
        address of the Account."""
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   138
        dbc = self._dbh.cursor()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   139
        sql = "SELECT COUNT(destination) FROM alias WHERE destination = '%s'"\
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   140
                % self._addr
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   141
        dbc.execute(sql)
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   142
        a_count = dbc.fetchone()[0]
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   143
        dbc.close()
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   144
        return a_count
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   145
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   146
    def _chk_state(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   147
        """Raise an AccountError if the Account is new - not yet saved in the
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   148
        database."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   149
        if self._new:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   150
            raise AErr(_(u"The account '%s' doesn't exist.") % self._addr,
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   151
                       NO_SUCH_ACCOUNT)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   152
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   153
    @property
250
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   154
    def address(self):
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   155
        """The Account's EmailAddress instance."""
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   156
        return self._addr
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   157
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   158
    @property
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   159
    def domain_directory(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   160
        """The directory of the domain the Account belongs to."""
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   161
        if self._domain:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   162
            return self._domain.directory
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   163
        return None
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   164
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   165
    @property
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   166
    def gid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   167
        """The Account's group ID."""
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   168
        if self._domain:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   169
            return self._domain.gid
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   170
        return None
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   171
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   172
    @property
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   173
    def home(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   174
        """The Account's home directory."""
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   175
        if not self._new:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   176
            return '%s/%s' % (self._domain.directory, self._uid)
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   177
        return None
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   178
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   179
    @property
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   180
    def mail_location(self):
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   181
        """The Account's MailLocation."""
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   182
        return self._mail
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   183
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   184
    @property
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   185
    def uid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   186
        """The Account's unique ID."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   187
        return self._uid
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   188
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   189
    def set_password(self, password):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   190
        """Set a password for the new Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   191
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   192
        If you want to update the password of an existing Account use
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   193
        Account.modify().
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   194
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   195
        Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   196
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   197
        `password` : basestring
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   198
          The password for the new Account.
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   199
        """
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   200
        if not isinstance(password, basestring) or not password:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   201
            raise AErr(_(u"Couldn't accept password: '%s'") % password,
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   202
                       ACCOUNT_MISSING_PASSWORD)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   203
        self._passwd = password
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   204
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   205
    def set_transport(self, transport):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   206
        """Set the transport for the new Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   207
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   208
        If you want to update the transport of an existing Account use
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   209
        Account.modify().
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   210
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   211
        Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   212
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   213
        `transport` : basestring
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   214
          The string representation of the transport, e.g.: 'dovecot:'
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   215
        """
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   216
        self._transport = Transport(self._dbh, transport=transport)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   217
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   218
    def enable(self, service=None):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   219
        """Enable a/all service/s for the Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   220
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   221
        Possible values for the *service* are: 'imap', 'pop3', 'sieve' and
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   222
        'smtp'. When all services should be enabled, use 'all' or the
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   223
        default value `None`.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   224
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   225
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   226
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   227
        `service` : basestring
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   228
          The name of a service ('imap', 'pop3', 'smtp', 'sieve'), 'all'
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   229
          or `None`.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   230
        """
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   231
        self._switch_state(True, service)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   232
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   233
    def disable(self, service=None):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   234
        """Disable a/all service/s for the Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   235
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   236
        For more information see: Account.enable()."""
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   237
        self._switch_state(False, service)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   238
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   239
    def save(self):
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   240
        """Save the new Account in the database."""
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   241
        if not self._new:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   242
            raise AErr(_(u"The account '%s' already exists.") % self._addr,
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   243
                       ACCOUNT_EXISTS)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   244
        if not self._passwd:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   245
            raise AErr(_(u"No password set for '%s'.") % self._addr,
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   246
                       ACCOUNT_MISSING_PASSWORD)
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   247
        if cfg_dget('misc.dovecot_version') >= 0x10200b02:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   248
            sieve_col = 'sieve'
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   249
        else:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   250
            sieve_col = 'managesieve'
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 293
diff changeset
   251
        self._prepare(MailLocation(directory=cfg_dget('mailbox.root'),
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 293
diff changeset
   252
                                   mbfmt=cfg_dget('mailbox.format')))
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   253
        sql = "INSERT INTO users (local_part, passwd, uid, gid, mid, tid,\
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   254
 smtp, pop3, imap, %s) VALUES ('%s', '%s', %d, %d, %d, %d, %s, %s, %s, %s)" % (
276
f2ecfe0a0e09 VMM/Account: always pass the EmailAddress to the pwhash() call.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 275
diff changeset
   255
            sieve_col, self._addr.localpart, pwhash(self._passwd,
f2ecfe0a0e09 VMM/Account: always pass the EmailAddress to the pwhash() call.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 275
diff changeset
   256
                                                    user=self._addr),
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   257
            self._uid, self._domain.gid, self._mail.mid, self._transport.tid,
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   258
            cfg_dget('account.smtp'), cfg_dget('account.pop3'),
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   259
            cfg_dget('account.imap'), cfg_dget('account.sieve'))
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   260
        dbc = self._dbh.cursor()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   261
        dbc.execute(sql)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   262
        self._dbh.commit()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   263
        dbc.close()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   264
        self._new = False
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   265
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   266
    def modify(self, field, value):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   267
        """Update the Account's *field* to the new *value*.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 121
diff changeset
   268
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   269
        Possible values for *field* are: 'name', 'password' and
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   270
        'transport'.  *value* is the *field*'s new value.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   271
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   272
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   273
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   274
        `field` : basestring
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   275
          The attribute name: 'name', 'password' or 'transport'
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   276
        `value` : basestring
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   277
          The new value of the attribute.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   278
        """
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   279
        if field not in ('name', 'password', 'transport'):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   280
            raise AErr(_(u"Unknown field: '%s'") % field, INVALID_AGUMENT)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   281
        self._chk_state()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   282
        dbc = self._dbh.cursor()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   283
        if field == 'password':
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
   284
            dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s',
276
f2ecfe0a0e09 VMM/Account: always pass the EmailAddress to the pwhash() call.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 275
diff changeset
   285
                        pwhash(value, user=self._addr), self._uid)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   286
        elif field == 'transport':
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   287
            if value != self._transport.transport:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   288
                self._transport = Transport(self._dbh, transport=value)
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   289
                dbc.execute('UPDATE users SET tid = %s WHERE uid = %s',
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   290
                            self._transport.tid, self._uid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   291
        else:
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
   292
            dbc.execute('UPDATE users SET name = %s WHERE uid = %s',
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   293
                        value, self._uid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   294
        if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   295
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   296
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   297
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   298
    def get_info(self):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   299
        """Returns a dict with some information about the Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   300
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   301
        The keys of the dict are: 'address', 'gid', 'home', 'imap'
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   302
        'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport' and
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   303
        'uid'.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   304
        """
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   305
        self._chk_state()
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   306
        if cfg_dget('misc.dovecot_version') >= 0x10200b02:
134
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   307
            sieve_col = 'sieve'
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   308
        else:
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   309
            sieve_col = 'managesieve'
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   310
        sql = 'SELECT name, smtp, pop3, imap, %s FROM users WHERE uid = %d' % \
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   311
            (sieve_col, self._uid)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   312
        dbc = self._dbh.cursor()
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   313
        dbc.execute(sql)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   314
        info = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   315
        dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   316
        if info:
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   317
            keys = ('name', 'smtp', 'pop3', 'imap', sieve_col)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   318
            info = dict(zip(keys, info))
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   319
            for service in keys[1:]:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   320
                if info[service]:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   321
                    # TP: A service (pop3/imap) is enabled/usable for a user
41
fbcb7e314510 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 38
diff changeset
   322
                    info[service] = _('enabled')
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   323
                else:
156
a849843115e9 Added comments for the Translation Project. Updated PO template.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 155
diff changeset
   324
                    # TP: A service (pop3/imap) isn't enabled/usable for a user
41
fbcb7e314510 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 38
diff changeset
   325
                    info[service] = _('disabled')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   326
            info['address'] = self._addr
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   327
            info['gid'] = self._domain.gid
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   328
            info['home'] = '%s/%s' % (self._domain.directory, self._uid)
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   329
            info['mail_location'] = self._mail.mail_location
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   330
            info['transport'] = self._transport.transport
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   331
            info['uid'] = self._uid
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   332
            return info
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   333
        # nearly impossible‽
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   334
        raise AErr(_(u"Couldn't fetch information for account: '%s'") %
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   335
                   self._addr, NO_SUCH_ACCOUNT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   336
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   337
    def get_aliases(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   338
        """Return a list with all alias e-mail addresses, whose destination
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   339
        is the address of the Account."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   340
        self._chk_state()
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   341
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   342
        dbc.execute("SELECT address ||'@'|| domainname FROM alias, "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   343
                    "domain_name WHERE destination = %s AND domain_name.gid = "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   344
                    "alias.gid AND domain_name.is_primary ORDER BY address",
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   345
                    str(self._addr))
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   346
        addresses = dbc.fetchall()
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   347
        dbc.close()
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   348
        aliases = []
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   349
        if addresses:
144
4c6aa6c29dd7 Small optimizations in Account.getAliases() and Alias.getInfo().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   350
            aliases = [alias[0] for alias in addresses]
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   351
        return aliases
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   352
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   353
    def delete(self, delalias=False):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   354
        """Delete the Account from the database.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   355
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   356
        Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   357
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   358
        `delalias` : bool
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   359
          if *delalias* is `True`, all aliases, which points to the Account,
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   360
          will be also deleted.  If there are aliases and *delalias* is
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   361
          `False`, an AccountError will be raised.
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   362
        """
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   363
        assert isinstance(delalias, bool)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   364
        self._chk_state()
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   365
        dbc = self._dbh.cursor()
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   366
        if delalias:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   367
            dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
65
5506433db9a3 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
   368
            # delete also all aliases where the destination address is the same
5506433db9a3 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
   369
            # as for this account.
114
e671210b04b8 Fixed a libpq.OperationalError in Account.delete()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   370
            dbc.execute("DELETE FROM alias WHERE destination = %s",
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   371
                        str(self._addr))
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   372
            self._dbh.commit()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   373
        else:  # check first for aliases
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   374
            a_count = self._count_aliases()
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   375
            if a_count > 0:
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   376
                dbc.close()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   377
                raise AErr(_(u"There are %(count)d aliases with the "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   378
                             u"destination address '%(address)s'.") %
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   379
                           {'count': a_count, 'address': self._addr},
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   380
                           ALIAS_PRESENT)
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   381
            dbc.execute('DELETE FROM users WHERE uid = %s', self._uid)
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   382
            self._dbh.commit()
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   383
        dbc.close()
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   384
        self._new = True
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   385
        self._uid = 0
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   386
        self._addr = self._dbh = self._domain = self._passwd = None
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   387
        self._mail = self._transport = None
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   388
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   389
252
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   390
def get_account_by_uid(uid, dbh):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   391
    """Search an Account by its UID.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   392
252
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   393
    This function returns a dict (keys: 'address', 'gid' and 'uid'), if an
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   394
    Account with the given *uid* exists.
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   395
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   396
    Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   397
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   398
    `uid` : long
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   399
      The Account unique ID.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   400
    `dbh` : pyPgSQL.PgSQL.Connection
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   401
      a database connection for the database access.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   402
    """
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   403
    try:
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   404
        uid = long(uid)
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   405
    except ValueError:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   406
        raise AErr(_(u'UID must be an int/long.'), INVALID_AGUMENT)
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   407
    if uid < 1:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   408
        raise AErr(_(u'UID must be greater than 0.'), INVALID_AGUMENT)
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   409
    dbc = dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   410
    dbc.execute("SELECT local_part||'@'|| domain_name.domainname AS address, "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   411
                "uid, users.gid FROM users LEFT JOIN domain_name ON "
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   412
                "(domain_name.gid = users.gid AND is_primary) WHERE uid = %s",
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   413
                uid)
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   414
    info = dbc.fetchone()
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   415
    dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   416
    if not info:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   417
        raise AErr(_(u"There is no account with the UID '%d'.") % uid,
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   418
                   NO_SUCH_ACCOUNT)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   419
    info = dict(zip(('address', 'uid', 'gid'), info))
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   420
    return info
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   421
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   422
del _, cfg_dget