VirtualMailManager/cli/handler.py
author martin f. krafft <madduck@madduck.net>
Sat, 07 Apr 2012 00:45:57 +0200
branchv0.6.x
changeset 492 e5c2b3647971
parent 422 33a45e4c80c4
child 555 499c63f52462
permissions -rw-r--r--
Modify address check query to obtain well-defined result The way in which UNION does not yield the desired result, because (a) UNION merges results and (b) the result order is undefined. This patch changes the query to select the counts as columns and hence provides a well-defined order.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
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: 361
diff changeset
     2
# Copyright (c) 2010 - 2011, Pascal Volk
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
     5
    VirtualMailManager.cli.handler
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
    A derived Handler class with a few changes/additions for cli use.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
"""
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
import os
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    13
from VirtualMailManager.errors import VMMError
375
4cbaf8d229f9 VMM/{,cli/}handler: Moved the 'address-in-use check' (introduced
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    14
from VirtualMailManager.handler import Handler, TYPE_ACCOUNT
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
from VirtualMailManager.cli import read_pass
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 319
diff changeset
    16
from VirtualMailManager.cli.config import CliConfig as Cfg
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    17
from VirtualMailManager.constants import ACCOUNT_EXISTS, INVALID_SECTION, \
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    18
     NO_SUCH_ACCOUNT
361
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    19
from VirtualMailManager.password import randompw
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    21
_ = lambda msg: msg
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    22
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
class CliHandler(Handler):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
    """This class uses a `CliConfig` for configuration stuff, instead of
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
    the non-interactive `Config` class.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
    It provides the additional methods cfgSet() and configure().
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
    Additionally it uses `VirtualMailManager.cli.read_pass()` for for the
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
    interactive password dialog.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
    """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    34
    __slots__ = ()  # nothing additional, also no __dict__/__weakref__
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
    def __init__(self):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
        """Creates a new CliHandler instance.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    39
        Throws a NotRootError if your uid is greater 0.
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
        # Overwrite the parent CTor partly, we use the CliConfig class
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    42
        # and add some command line checks.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
        skip_some_checks = os.sys.argv[1] in ('cf', 'configure', 'h', 'help',
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
                                              'v', 'version')
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    45
        super(CliHandler, self).__init__(skip_some_checks)
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    46
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    47
        self._cfg = Cfg(self._cfg_fname)
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    48
        self._cfg.load()
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
319
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    50
    def cfg_set(self, option, value):
f4956b4ceba1 VMM//{,cli/}Handler: PEP-8-ified the Handler classes:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 318
diff changeset
    51
        """Set a new value for the given option."""
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    52
        return self._cfg.set(option, value)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
    def configure(self, section=None):
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        """Starts the interactive configuration.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        Configures in interactive mode options in the given ``section``.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        If no section is given (default) all options from all sections
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        will be prompted.
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        """
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
        if section is None:
318
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    62
            self._cfg.configure(self._cfg.sections())
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    63
        elif self._cfg.has_section(section):
4dc2edf02d11 VMM//{,cli/}Handler: Reworked configuration related parts.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
    64
            self._cfg.configure([section])
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        else:
341
6709d0faf2f5 VMM/…: Unified messages and removed non-ASCII quotes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 339
diff changeset
    66
            raise VMMError(_(u"Invalid section: '%s'") % section,
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 190
diff changeset
    67
                           INVALID_SECTION)
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    69
    def user_add(self, emailaddress, password=None):
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    70
        """Override the parent user_add() - add the interactive password
361
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    71
        dialog.
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    72
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    73
        Returns the generated password, if account.random_password == True.
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    74
        """
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    75
        acc = self._get_account(emailaddress)
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    76
        if acc:
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    77
            raise VMMError(_(u"The account '%s' already exists.") %
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    78
                           acc.address, ACCOUNT_EXISTS)
422
33a45e4c80c4 VMM: Deleted a few unused variables.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 417
diff changeset
    79
        self._is_other_address(acc.address, TYPE_ACCOUNT)
361
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    80
        rand_pass = self._cfg.dget('account.random_password')
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        if password is None:
361
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    82
            password = (read_pass, randompw)[rand_pass]()
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    83
        acc.set_password(password)
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    84
        acc.save()
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    85
        self._make_account_dirs(acc)
361
971577b89d26 VMM/cli/handler: Added support for random passwords in user_add().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 346
diff changeset
    86
        return (None, password)[rand_pass]
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    88
    def user_password(self, emailaddress, password=None):
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    89
        """Override the parent user_password() - add the interactive
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    90
        password dialog."""
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    91
        acc = self._get_account(emailaddress)
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    92
        if not acc:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 375
diff changeset
    93
            raise VMMError(_(u"The account '%s' does not exist.") %
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    94
                           acc.address, NO_SUCH_ACCOUNT)
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    95
        if not isinstance(password, basestring) or not password:
190
1903d4ce97d7 VMM/{,cli/}Handler: reverted most of cs cf1b5f22dbd2 added a cli handler.
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
            password = read_pass()
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    97
        acc.modify('password', password)
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    98
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    99
del _