VirtualMailManager/cli/handler.py
author Pascal Volk <user@localhost.localdomain.org>
Mon, 09 Jun 2014 18:54:44 +0000
branchv0.7.x
changeset 757 3d592b5d5e2d
parent 729 26a6231b9c6a
permissions -rw-r--r--
{man1,doc/web}: Updated userdelete's --delete-home -> --delete-directory.
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2010 - 2014, 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
555
499c63f52462 Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents: 422
diff changeset
    14
from VirtualMailManager.handler import Handler
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, \
555
499c63f52462 Provide list{addresses,aliases,users,relocated} subcommands
martin f. krafft <madduck@madduck.net>
parents: 422
diff changeset
    18
     NO_SUCH_ACCOUNT, TYPE_ACCOUNT
729
26a6231b9c6a VMM/cli/handler: Improved CliHandler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 727
diff changeset
    19
from VirtualMailManager.password import randompw, verify_scheme
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.
714
95252b15fffb VMM/cli/handler: Updated `skip_some_checks' conditions.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    43
        skip_some_checks = os.sys.argv[1] in ('cf', 'configure',
95252b15fffb VMM/cli/handler: Updated `skip_some_checks' conditions.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    44
                                              'cs', 'configset')
190
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:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    66
            raise VMMError(_("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
686
5f4abdab738e VMM: Added option `-n NOTE' to {domain,user}add.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    69
    def user_add(self, emailaddress, password=None, note=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:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    77
            raise VMMError(_("The account '%s' already exists.") %
337
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)
686
5f4abdab738e VMM: Added option `-n NOTE' to {domain,user}add.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    84
        if note:
5f4abdab738e VMM: Added option `-n NOTE' to {domain,user}add.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    85
            acc.set_note(note)
337
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    86
        acc.save()
150ddcc8b315 VMM/{,cli/}handler: user_add: Check earlier if the account exists.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    87
        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
    88
        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
    89
727
695426b49de0 VMM/{cli,}/handler: Adjusted Handler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 714
diff changeset
    90
    def user_password(self, emailaddress, password=None, scheme=None):
339
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    91
        """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
    92
        password dialog."""
abff2de9eed0 VMM/cli/handler: CliHandler.user_password: Only prompt for the
Pascal Volk <neverseen@users.sourceforge.net>
parents: 337
diff changeset
    93
        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
    94
        if not acc:
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
    95
            raise VMMError(_("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
    96
                           acc.address, NO_SUCH_ACCOUNT)
729
26a6231b9c6a VMM/cli/handler: Improved CliHandler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 727
diff changeset
    97
        if scheme:
26a6231b9c6a VMM/cli/handler: Improved CliHandler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 727
diff changeset
    98
            scheme, encoding = verify_scheme(scheme)
26a6231b9c6a VMM/cli/handler: Improved CliHandler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 727
diff changeset
    99
            if encoding:
26a6231b9c6a VMM/cli/handler: Improved CliHandler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 727
diff changeset
   100
                scheme = '%s.%s' % (scheme, encoding)
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 568
diff changeset
   101
        if not isinstance(password, str) 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
   102
            password = read_pass()
727
695426b49de0 VMM/{cli,}/handler: Adjusted Handler.user_password.
Pascal Volk <user@localhost.localdomain.org>
parents: 714
diff changeset
   103
        acc.update_password(password, scheme)
277
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   104
e50ffc0b8468 VMM{,/cli}/Handler: adjusted user* methods to the changes in Account.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   105
del _