VirtualMailManager/account.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 26 Oct 2011 03:20:40 +0000
branchv0.6.x
changeset 433 c2543ddde9f5
parent 417 8209da83e256
child 442 abcd73f5e980
permissions -rw-r--r--
man: Reworked/updated German manual pages.
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 -*-
366
d6573da35b5f Updated copyright notices to include the year 2011.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 352
diff changeset
     2
# Copyright (c) 2007 - 2011, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.account
302
32b4a39b5640 VMM/maillocation: Reworked class MailLocation to match the new
Pascal Volk <neverseen@users.sourceforge.net>
parents: 293
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
     8
    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
     9
"""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    10
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    11
from VirtualMailManager.domain import Domain
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    12
from VirtualMailManager.emailaddress import EmailAddress
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    13
from VirtualMailManager.quotalimit import QuotaLimit
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
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
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 306
diff changeset
    16
from VirtualMailManager.constants 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, \
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 306
diff changeset
    18
     INVALID_ARGUMENT, INVALID_MAIL_LOCATION, NO_SUCH_ACCOUNT, \
404
0c52094447b0 VMM/{account,domain}: Added a versions check to the update_quotalimit
Pascal Volk <neverseen@users.sourceforge.net>
parents: 390
diff changeset
    19
     NO_SUCH_DOMAIN, UNKNOWN_SERVICE, VMM_ERROR
0c52094447b0 VMM/{account,domain}: Added a versions check to the update_quotalimit
Pascal Volk <neverseen@users.sourceforge.net>
parents: 390
diff changeset
    20
from VirtualMailManager.errors import VMMError, 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
338
45834dcc280e VMM/account: Made Account._services available as account.SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 334
diff changeset
    24
__all__ = ('SERVICES', 'Account', 'get_account_by_uid')
45834dcc280e VMM/account: Made Account._services available as account.SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 334
diff changeset
    25
45834dcc280e VMM/account: Made Account._services available as account.SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 334
diff changeset
    26
SERVICES = ('imap', 'pop3', 'smtp', 'sieve')
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
_ = 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
    29
cfg_dget = lambda option: None
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    30
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    31
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
    32
class Account(object):
3
a9b44e04bf01 * VirtualMailManager/Account.py:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 0
diff changeset
    33
    """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
    34
    __slots__ = ('_addr', '_dbh', '_domain', '_mail', '_new', '_passwd',
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    35
                 '_qlimit', '_transport', '_uid')
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    36
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    37
    def __init__(self, dbh, address):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    38
        """Creates a new Account instance.
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
        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
    41
        database all relevant data will be loaded.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    42
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    43
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    44
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    45
        `dbh` : pyPgSQL.PgSQL.Connection
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    46
          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
    47
        `address` : VirtualMailManager.EmailAddress.EmailAddress
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    48
          The e-mail address of the (new) Account.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    49
        """
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    50
        if not isinstance(address, EmailAddress):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    51
            raise TypeError("Argument 'address' is not an EmailAddress")
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    52
        self._addr = address
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        self._dbh = dbh
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    54
        self._domain = Domain(self._dbh, self._addr.domainname)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    55
        if not self._domain.gid:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
    56
            # TP: Hm, what “quotation marks” should be used?
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
    57
            # If you are unsure have a look at:
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
    58
            # http://en.wikipedia.org/wiki/Quotation_mark,_non-English_usage
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
    59
            raise AErr(_(u"The domain '%s' does not exist.") %
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    60
                       self._addr.domainname, NO_SUCH_DOMAIN)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        self._uid = 0
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
    62
        self._mail = None
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    63
        self._qlimit = self._domain.quotalimit
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    64
        self._transport = self._domain.transport
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    65
        self._passwd = None
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    66
        self._new = True
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    67
        self._load()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    69
    def __nonzero__(self):
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    70
        """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
    71
        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
    72
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    73
    def _load(self):
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    74
        """Load 'uid', 'mid', 'qid' and 'tid' from the database and set
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    75
        _new to `False` - if the user could be found. """
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        dbc = self._dbh.cursor()
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    77
        dbc.execute('SELECT uid, mid, qid, tid FROM users WHERE gid = %s AND '
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
    78
                    'local_part=%s', (self._domain.gid, self._addr.localpart))
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    79
        result = dbc.fetchone()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    81
        if result:
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    82
            self._uid, _mid, _qid, _tid = result
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    83
            if _qid != self._qlimit.qid:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
    84
                self._qlimit = QuotaLimit(self._dbh, qid=_qid)
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
    85
            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
    86
                self._transport = Transport(self._dbh, tid=_tid)
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
    87
            self._mail = MailLocation(self._dbh, mid=_mid)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    88
            self._new = False
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    90
    def _set_uid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    91
        """Set the unique ID for the new Account."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    92
        assert self._uid == 0
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        dbc = self._dbh.cursor()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    94
        dbc.execute("SELECT nextval('users_uid')")
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    95
        self._uid = dbc.fetchone()[0]
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
9
e3d3dbeb5b84 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 8
diff changeset
    98
    def _prepare(self, maillocation):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
    99
        """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
   100
        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
   101
        """
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   102
        if maillocation.dovecot_version > cfg_dget('misc.dovecot_version'):
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   103
            raise AErr(_(u"The mailbox format '%(mbfmt)s' requires Dovecot "
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   104
                         u">= v%(version)s.") % {
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   105
                       'mbfmt': maillocation.mbformat,
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   106
                       '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
   107
                       INVALID_MAIL_LOCATION)
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   108
        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
   109
          self._transport.transport.lower() in ('virtual:', 'virtual'):
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   110
            raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   111
                         u"format '%(mbfmt)s'.") %
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   112
                       {'transport': self._transport,
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   113
                        'mbfmt': maillocation.mbformat}, INVALID_MAIL_LOCATION)
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   114
        self._mail = maillocation
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   115
        self._set_uid()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   116
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   117
    def _update_services(self, activate, *services):
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   118
        """Activate or deactivate the Account's services.
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   119
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   120
        Arguments:
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   121
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   122
        `activate`: bool
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   123
          When `True` the Account's user will be able to login to the
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   124
          services, otherwise the login will fail.
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   125
        `*services`
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   126
          No or one or more of the services: imap, pop3, smtp and sieve
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   127
        """
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   128
        self._chk_state()
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   129
        if services:
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   130
            services = set(services)
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   131
            for service in services:
338
45834dcc280e VMM/account: Made Account._services available as account.SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 334
diff changeset
   132
                if service not in SERVICES:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
   133
                    raise AErr(_(u"Unknown service: '%s'") % service,
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   134
                               UNKNOWN_SERVICE)
134
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   135
        else:
338
45834dcc280e VMM/account: Made Account._services available as account.SERVICES.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 334
diff changeset
   136
            services = SERVICES
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   137
        state = ('FALSE', 'TRUE')[activate]
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   138
        sql = 'UPDATE users SET %s WHERE uid = %u' % (
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   139
                    (' = %(s)s, '.join(services) + ' = %(s)s') % {'s': state},
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   140
                    self._uid)
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   141
        if 'sieve' in services and \
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   142
           cfg_dget('misc.dovecot_version') < 0x10200b02:
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   143
            sql = sql.replace('sieve', 'managesieve')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   144
        dbc = self._dbh.cursor()
115
21f264a88ab2 Added sieve/managesieve switching stuff,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 114
diff changeset
   145
        dbc.execute(sql)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   146
        if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   147
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   148
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   149
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   150
    def _update_tables(self, column, value):
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   151
        """Update various columns in the users table.
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   152
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   153
        Arguments:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   154
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   155
        `column` : basestring
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   156
          Name of the table column. Currently: qid and tid
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   157
        `value` : long
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   158
          The referenced key
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   159
        """
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   160
        if column not in ('qid', 'tid'):
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   161
            raise ValueError('Unknown column: %r' % column)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   162
        dbc = self._dbh.cursor()
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   163
        dbc.execute('UPDATE users SET %s = %%s WHERE uid = %%s' % column,
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   164
                    (value, self._uid))
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   165
        if dbc.rowcount > 0:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   166
            self._dbh.commit()
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   167
        dbc.close()
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   168
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   169
    def _count_aliases(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   170
        """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
   171
        address of the Account."""
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   172
        dbc = self._dbh.cursor()
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   173
        dbc.execute('SELECT COUNT(destination) FROM alias WHERE destination '
374
ecdba68c7a2c VMM/account: Account._count_aliases() cast the EmailAddress to str,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
   174
                    '= %s', (str(self._addr),))
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   175
        a_count = dbc.fetchone()[0]
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   176
        dbc.close()
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   177
        return a_count
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   178
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   179
    def _chk_state(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   180
        """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
   181
        database."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   182
        if self._new:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   183
            raise AErr(_(u"The account '%s' does not exist.") % self._addr,
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   184
                       NO_SUCH_ACCOUNT)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   185
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   186
    @property
250
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   187
    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
   188
        """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
   189
        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
   190
73cd082cd724 VMM/{Account,Alias,Relocated}: added a address property to the classes
Pascal Volk <neverseen@users.sourceforge.net>
parents: 246
diff changeset
   191
    @property
329
41789df75339 VMM/account: Replaced property domain_directory by domain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   192
    def domain(self):
41789df75339 VMM/account: Replaced property domain_directory by domain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   193
        """The Domain to which 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
   194
        if self._domain:
329
41789df75339 VMM/account: Replaced property domain_directory by domain.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   195
            return self._domain
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   196
        return None
38
c44ea4526546 * 'VirtualMailManager/Alias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 34
diff changeset
   197
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   198
    @property
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   199
    def gid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   200
        """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
   201
        if self._domain:
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   202
            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
   203
        return None
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   204
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   205
    @property
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   206
    def home(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   207
        """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
   208
        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
   209
            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
   210
        return None
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   211
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   212
    @property
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   213
    def mail_location(self):
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   214
        """The Account's MailLocation."""
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   215
        return self._mail
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   216
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   217
    @property
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   218
    def uid(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   219
        """The Account's unique ID."""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   220
        return self._uid
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   221
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   222
    def set_password(self, password):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   223
        """Set a password for the new Account.
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
        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
   226
        Account.modify().
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   227
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   228
        Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   229
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   230
        `password` : basestring
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   231
          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
   232
        """
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   233
        if not self._new:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   234
            raise AErr(_(u"The account '%s' already exists.") % self._addr,
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   235
                       ACCOUNT_EXISTS)
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   236
        if not isinstance(password, basestring) or not password:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
   237
            raise AErr(_(u"Could not accept password: '%s'") % password,
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   238
                       ACCOUNT_MISSING_PASSWORD)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   239
        self._passwd = password
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   240
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   241
    def enable(self, *services):
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   242
        """Enable all or the given service/s for the Account.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   243
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   244
        Possible *services* are: 'imap', 'pop3', 'sieve' and 'smtp'.
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   245
        When all services should be enabled, give no service name.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   246
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   247
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   248
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   249
        `*services` : basestring
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   250
          No or one or more of the services 'imap', 'pop3', 'smtp', and
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   251
          'sieve'.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   252
        """
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   253
        self._update_services(True, *services)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   254
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   255
    def disable(self, *services):
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   256
        """Disable all or the given service/s for the Account.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   257
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   258
        For more information see: Account.enable()."""
334
3f550826b1cc VMM/account: Reworked methods enable/disable in order to accept
Pascal Volk <neverseen@users.sourceforge.net>
parents: 333
diff changeset
   259
        self._update_services(False, *services)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   260
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   261
    def save(self):
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   262
        """Save the new Account in the database."""
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   263
        if not self._new:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   264
            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
   265
                       ACCOUNT_EXISTS)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   266
        if not self._passwd:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   267
            raise AErr(_(u"No password set for account: '%s'") % self._addr,
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   268
                       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
   269
        if cfg_dget('misc.dovecot_version') >= 0x10200b02:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   270
            sieve_col = 'sieve'
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   271
        else:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   272
            sieve_col = 'managesieve'
306
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   273
        self._prepare(MailLocation(self._dbh, mbfmt=cfg_dget('mailbox.format'),
504fd29b4712 VMM/Account: s/prefix/mbformat/ and pass the dbh to MailLocation.__init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 302
diff changeset
   274
                                   directory=cfg_dget('mailbox.root')))
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   275
        dbc = self._dbh.cursor()
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   276
        dbc.execute('INSERT INTO users (local_part, passwd, uid, gid, mid, '
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   277
                    'qid, tid, smtp, pop3, imap, %s) VALUES' % (sieve_col,) + \
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   278
                    '(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   279
                    (self._addr.localpart,
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   280
                     pwhash(self._passwd, user=self._addr), self._uid,
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   281
                     self._domain.gid, self._mail.mid, self._qlimit.qid,
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   282
                     self._transport.tid, cfg_dget('account.smtp'),
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   283
                     cfg_dget('account.pop3'), cfg_dget('account.imap'),
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   284
                     cfg_dget('account.sieve')))
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   285
        self._dbh.commit()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   286
        dbc.close()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   287
        self._new = False
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   288
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   289
    def modify(self, field, value):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   290
        """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
   291
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   292
        Possible values for *field* are: 'name', 'password'.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   293
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   294
        Arguments:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   295
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   296
        `field` : basestring
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   297
          The attribute name: 'name' or 'password'
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   298
        `value` : basestring
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   299
          The new value of the attribute.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   300
        """
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   301
        if field not in ('name', 'password'):
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 306
diff changeset
   302
            raise AErr(_(u"Unknown field: '%s'") % field, INVALID_ARGUMENT)
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   303
        self._chk_state()
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   304
        dbc = self._dbh.cursor()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   305
        if field == 'password':
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
   306
            dbc.execute('UPDATE users SET passwd = %s WHERE uid = %s',
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   307
                        (pwhash(value, user=self._addr), self._uid))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   308
        else:
121
7ccc05774118 Converted to new-style class, added __slots__, updated queries.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 115
diff changeset
   309
            dbc.execute('UPDATE users SET name = %s WHERE uid = %s',
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   310
                        (value, self._uid))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   311
        if dbc.rowcount > 0:
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   312
            self._dbh.commit()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   313
        dbc.close()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   314
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   315
    def update_quotalimit(self, quotalimit):
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   316
        """Update the user's quota limit.
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   317
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   318
        Arguments:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   319
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   320
        `quotalimit` : VirtualMailManager.quotalimit.QuotaLimit
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   321
          the new quota limit of the domain.
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   322
        """
404
0c52094447b0 VMM/{account,domain}: Added a versions check to the update_quotalimit
Pascal Volk <neverseen@users.sourceforge.net>
parents: 390
diff changeset
   323
        if cfg_dget('misc.dovecot_version') < 0x10102f00:
0c52094447b0 VMM/{account,domain}: Added a versions check to the update_quotalimit
Pascal Volk <neverseen@users.sourceforge.net>
parents: 390
diff changeset
   324
            raise VMMError(_(u'PostgreSQL-based dictionary quota requires '
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   325
                             u'Dovecot >= v1.1.2.'), VMM_ERROR)
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   326
        self._chk_state()
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   327
        assert isinstance(quotalimit, QuotaLimit)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   328
        if quotalimit == self._qlimit:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   329
            return
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   330
        self._update_tables('qid', quotalimit.qid)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   331
        self._qlimit = quotalimit
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   332
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   333
    def update_transport(self, transport):
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   334
        """Sets a new transport for the Account.
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   335
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   336
        Arguments:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   337
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   338
        `transport` : VirtualMailManager.transport.Transport
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   339
          the new transport
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   340
        """
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   341
        self._chk_state()
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   342
        assert isinstance(transport, Transport)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   343
        if transport == self._transport:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   344
            return
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   345
        if transport.transport.lower() in ('virtual', 'virtual:') and \
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   346
           not self._mail.postfix:
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   347
            raise AErr(_(u"Invalid transport '%(transport)s' for mailbox "
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   348
                         u"format '%(mbfmt)s'.") %
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   349
                       {'transport': transport, 'mbfmt': self._mail.mbformat},
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   350
                       INVALID_MAIL_LOCATION)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   351
        self._update_tables('tid', transport.tid)
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   352
        self._transport = transport
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   353
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   354
    def get_info(self):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   355
        """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
   356
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   357
        The keys of the dict are: 'address', 'gid', 'home', 'imap'
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   358
        'mail_location', 'name', 'pop3', 'sieve', 'smtp', transport', 'uid',
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   359
        'uq_bytes', 'uq_messages', 'ql_bytes', and 'ql_messages'.
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   360
        """
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   361
        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
   362
        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
   363
            sieve_col = 'sieve'
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   364
        else:
6ca3d22e5dd0 Fixed a Python2.4.4 SyntaxError
Pascal Volk <neverseen@users.sourceforge.net>
parents: 133
diff changeset
   365
            sieve_col = 'managesieve'
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   366
        dbc = self._dbh.cursor()
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   367
        dbc.execute('SELECT name, smtp, pop3, imap, %s, CASE WHEN bytes IS '
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   368
                    'NULL THEN 0 ELSE bytes END, CASE WHEN messages IS NULL '
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   369
                    'THEN 0 ELSE messages END FROM users LEFT JOIN userquota '
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   370
                    'USING (uid) WHERE users.uid = %u' % (sieve_col,
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   371
                        self._uid))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   372
        info = dbc.fetchone()
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   373
        dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   374
        if info:
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   375
            keys = ('name', 'smtp', 'pop3', 'imap', sieve_col, 'uq_bytes',
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   376
                    'uq_messages')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   377
            info = dict(zip(keys, info))
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   378
            for service in keys[1:5]:
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   379
                if info[service]:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   380
                    # 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
   381
                    info[service] = _('enabled')
20
55146c78b3fb * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 19
diff changeset
   382
                else:
156
a849843115e9 Added comments for the Translation Project. Updated PO template.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 155
diff changeset
   383
                    # 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
   384
                    info[service] = _('disabled')
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   385
            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
   386
            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
   387
            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
   388
            info['mail_location'] = self._mail.mail_location
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   389
            info['ql_bytes'] = self._qlimit.bytes
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   390
            info['ql_messages'] = self._qlimit.messages
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   391
            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
   392
            info['uid'] = self._uid
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   393
            return info
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   394
        # nearly impossible‽
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 338
diff changeset
   395
        raise AErr(_(u"Could not fetch information for account: '%s'") %
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   396
                   self._addr, NO_SUCH_ACCOUNT)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
   397
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   398
    def get_aliases(self):
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   399
        """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
   400
        is the address of the Account."""
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   401
        self._chk_state()
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   402
        dbc = self._dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   403
        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
   404
                    "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
   405
                    "alias.gid AND domain_name.is_primary ORDER BY address",
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   406
                    (str(self._addr),))
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   407
        addresses = dbc.fetchall()
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   408
        dbc.close()
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   409
        aliases = []
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   410
        if addresses:
144
4c6aa6c29dd7 Small optimizations in Account.getAliases() and Alias.getInfo().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
   411
            aliases = [alias[0] for alias in addresses]
90
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   412
        return aliases
1734eb5101c6 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 82
diff changeset
   413
333
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   414
    def delete(self, force=False):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   415
        """Delete the Account from the database.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   416
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   417
        Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   418
333
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   419
        `force` : bool
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   420
          if *force* is `True`, all aliases, which points to the Account,
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   421
          will be also deleted.  If there are aliases and *force* is
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   422
          `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
   423
        """
333
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   424
        if not isinstance(force, bool):
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   425
            raise TypeError('force must be a bool')
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   426
        self._chk_state()
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   427
        dbc = self._dbh.cursor()
333
1ed85e696748 VMM/{account,handler}: Account delete, use the boolean keyword force too.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 329
diff changeset
   428
        if force:
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   429
            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
   430
            # 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
   431
            # as for this account.
114
e671210b04b8 Fixed a libpq.OperationalError in Account.delete()
Pascal Volk <neverseen@users.sourceforge.net>
parents: 113
diff changeset
   432
            dbc.execute("DELETE FROM alias WHERE destination = %s",
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   433
                        (str(self._addr),))
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   434
            self._dbh.commit()
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   435
        else:  # check first for aliases
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   436
            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
   437
            if a_count > 0:
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   438
                dbc.close()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   439
                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
   440
                             u"destination address '%(address)s'.") %
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   441
                           {'count': a_count, 'address': self._addr},
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   442
                           ALIAS_PRESENT)
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   443
            dbc.execute('DELETE FROM users WHERE uid = %s', (self._uid,))
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   444
            self._dbh.commit()
66
995d538a6eb5 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 65
diff changeset
   445
        dbc.close()
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   446
        self._new = True
293
a77b67673aa6 VMM/Account: replaced attribute _mid : int by _mail : MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 290
diff changeset
   447
        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
   448
        self._addr = self._dbh = self._domain = self._passwd = None
390
660b42391c8e VMM/{account,domain,handler}: Added quota limit support. A few
Pascal Volk <neverseen@users.sourceforge.net>
parents: 389
diff changeset
   449
        self._mail = self._qlimit = self._transport = None
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   450
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   451
252
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   452
def get_account_by_uid(uid, dbh):
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   453
    """Search an Account by its UID.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   454
252
af555e6967c8 VMM/Account: renamed function getAccountByID -> get_account_by_uid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 250
diff changeset
   455
    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
   456
    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
   457
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   458
    Argument:
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   459
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   460
    `uid` : long
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   461
      The Account unique ID.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   462
    `dbh` : pyPgSQL.PgSQL.Connection
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   463
      a database connection for the database access.
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   464
    """
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   465
    try:
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   466
        uid = long(uid)
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   467
    except ValueError:
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 306
diff changeset
   468
        raise AErr(_(u'UID must be an int/long.'), INVALID_ARGUMENT)
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   469
    if uid < 1:
316
31d8931dc535 VMM/constants: Replaced the constants subpackage by a module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 306
diff changeset
   470
        raise AErr(_(u'UID must be greater than 0.'), INVALID_ARGUMENT)
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   471
    dbc = dbh.cursor()
290
e2785e04f92e VMM/…: re-indented long queries and error messages.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 276
diff changeset
   472
    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
   473
                "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
   474
                "(domain_name.gid = users.gid AND is_primary) WHERE uid = %s",
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 341
diff changeset
   475
                (uid,))
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   476
    info = dbc.fetchone()
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   477
    dbc.close()
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   478
    if not info:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 404
diff changeset
   479
        raise AErr(_(u"There is no account with the UID: '%d'") % uid,
245
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   480
                   NO_SUCH_ACCOUNT)
f9a6b6701cf9 VMM/Account: reworked class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 236
diff changeset
   481
    info = dict(zip(('address', 'uid', 'gid'), info))
19
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   482
    return info
bf9a03c476fc * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 17
diff changeset
   483
275
524f7ed5ad5b VMM/Account: some modifications and small improvements in class Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 266
diff changeset
   484
del _, cfg_dget