VirtualMailManager/maillocation.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 20 Jul 2010 03:15:57 +0000
branchv0.6.x
changeset 297 e21ceaabe871
parent 295 18086c6a2521
child 302 32b4a39b5640
permissions -rw-r--r--
pgsql: Added support for different mailbox formats. - users.passwd can store sha512-crypt.hex hashes now - Added new update scripts.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
     2
# Copyright (c) 2008 - 2010, Pascal Volk
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     5
"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     6
    VirtualMailManager.maillocation
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     7
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     8
    Virtual Mail Manager's maillocation module to handle Dovecot's
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     9
    mail_location setting for accounts.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    11
"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    12
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    13
from VirtualMailManager.pycompat import any
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    14
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    15
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    16
__all__ = ('MailLocation', 'known_format',
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    17
           'ID_MAILDIR', 'ID_MBOX', 'ID_MDBOX', 'ID_SDBOX')
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    18
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    19
ID_MAILDIR = 0x1
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    20
ID_MBOX = 0x2
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    21
ID_MDBOX = 0x3
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    22
ID_SDBOX = 0x4
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    24
_storage = {
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    25
    ID_MAILDIR: dict(dovecot_version=0x10000f00, postfix=True,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    26
                     prefix='maildir:', directory='Maildir', mid=ID_MAILDIR),
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    27
    ID_MBOX: dict(dovecot_version=0x10000f00, postfix=True, prefix='mbox:',
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    28
                  directory='mail', mid=ID_MBOX),
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    29
    ID_MDBOX: dict(dovecot_version=0x20000a01, postfix=False,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    30
                   prefix='mdbox:', directory='mdbox', mid=ID_MDBOX),
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    31
    ID_SDBOX: dict(dovecot_version=0x10000f00, postfix=False, prefix='dbox:',
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    32
                   directory='dbox', mid=ID_SDBOX),
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    33
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    34
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    35
_format_id = {
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    36
    'maildir': ID_MAILDIR,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    37
    'mbox': ID_MBOX,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    38
    'mdbox': ID_MDBOX,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    39
    'dbox': ID_SDBOX,
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    40
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    41
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    42
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    43
class MailLocation(object):
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    44
    """A small class for mail_location relevant information."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    45
    __slots__ = ('_info')
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    46
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    47
    def __init__(self, mid=None, format=None):
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    48
        """Creates a new MailLocation instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    49
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    50
        Either a mid or the format must be specified.
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    51
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    52
        Keyword arguments:
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    53
        mid -- the id of a mail_location (int)
295
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    54
          one of the maillocation constants: `ID_MAILDIR`, `ID_MBOX`,
18086c6a2521 VMM/maillocation: Code cleanups.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 267
diff changeset
    55
          `ID_MDBOX` and `ID_SDBOX`
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    56
        format -- the mailbox format of the mail_location. One out of:
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    57
        ``maildir``, ``mbox``, ``dbox`` and ``mdbox``.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        """
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    59
        assert any((mid, format))
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    60
        if mid:
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    61
            assert isinstance(mid, (int, long)) and mid in _storage
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    62
            self._info = _storage[mid]
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        else:
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    64
            assert isinstance(format, basestring) and \
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    65
                    format.lower() in _format_id
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    66
            self._info = _storage[_format_id[format.lower()]]
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    67
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    68
    def __str__(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    69
        return '%(prefix)s~/%(directory)s' % self._info
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    71
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    72
    def directory(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    73
        """The mail_location's directory name."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    74
        return self._info['directory']
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    75
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    76
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    77
    def dovecot_version(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    78
        """The required Dovecot version (concatenated major and minor
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    79
        parts) for this mailbox format."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    80
        return self._info['dovecot_version']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    82
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    83
    def postfix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
        """`True` if Postfix supports this mailbox format, else `False`."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    85
        return self._info['postfix']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    87
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    88
    def prefix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    89
        """The prefix of the mail_location."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    90
        return self._info['prefix']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    92
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    93
    def mail_location(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
        """The mail_location, e.g. ``maildir:~/Maildir``"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
        return self.__str__()
227
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
    96
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
    97
    @property
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
    98
    def mid(self):
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
    99
        """The mail_location's unique ID."""
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   100
        return self._info['mid']
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   101
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   102
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   103
def known_format(format):
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   104
    """Checks if the mailbox *format* is known, returns bool."""
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   105
    return format.lower() in _format_id