VirtualMailManager/maillocation.py
author Pascal Volk <neverseen@users.sourceforge.net>
Wed, 28 Apr 2010 05:28:36 +0000
branchv0.6.x
changeset 270 d3389645a91d
parent 267 084300a00ee1
child 295 18086c6a2521
permissions -rw-r--r--
configuration: Dropped setting misc.gid_mail. That setting was never useful, since none of the virtual users was a member of a system group.
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',
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    17
           'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID')
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
    18
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    19
MAILDIR_ID = 0x1
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    20
MBOX_ID = 0x2
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    21
MDBOX_ID = 0x3
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    22
SDBOX_ID = 0x4
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    23
MAILDIR_NAME = 'Maildir'
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    24
MBOX_NAME = 'mail'
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    25
MDBOX_NAME = 'mdbox'
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    26
SDBOX_NAME = 'dbox'
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    28
_storage = {
267
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    29
    MAILDIR_ID: dict(dovecot_version=0x10000f00, postfix=True,
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    30
                     prefix='maildir:', directory=MAILDIR_NAME,
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    31
                     mid=MAILDIR_ID),
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    32
    MBOX_ID: dict(dovecot_version=0x10000f00, postfix=True, prefix='mbox:',
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    33
                  directory=MBOX_NAME, mid=MBOX_ID),
267
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    34
    MDBOX_ID: dict(dovecot_version='0x20000a01', postfix=False,
265
3c0173418d5d VMM/{Account,common,maillocation}: Dovecot version (check) fixes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 264
diff changeset
    35
                   prefix='mdbox:', directory=MDBOX_NAME, mid=MDBOX_ID),
267
084300a00ee1 VMM/maillocation: Use the hex version, since we are able to convert
Pascal Volk <neverseen@users.sourceforge.net>
parents: 265
diff changeset
    36
    SDBOX_ID: dict(dovecot_version=0x10000f00, postfix=False, prefix='dbox:',
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    37
                   directory=SDBOX_NAME, mid=SDBOX_ID),
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    38
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    39
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    40
_format_id = {
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    41
    'maildir': MAILDIR_ID,
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    42
    'mbox': MBOX_ID,
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    43
    'mdbox': MDBOX_ID,
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    44
    'dbox': SDBOX_ID,
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    45
}
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    46
69
0c124160a991 * 'VirtualMailManager/VirtualMailManager.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
122
30abf0abf8f8 Converted to new-style class, added __slots__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    48
class MailLocation(object):
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    49
    """A small class for mail_location relevant information."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    50
    __slots__ = ('_info')
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    51
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    52
    def __init__(self, mid=None, format=None):
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
        """Creates a new MailLocation instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    55
        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
    56
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        Keyword arguments:
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    58
        mid -- the id of a mail_location (int)
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    59
          one of the maillocation constants: `MAILDIR_ID`, `MBOX_ID`,
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    60
          `MDBOX_ID` and `SDBOX_ID`
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    61
        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
    62
        ``maildir``, ``mbox``, ``dbox`` and ``mdbox``.
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
        """
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    64
        assert any((mid, format))
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    65
        if mid:
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    66
            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
    67
            self._info = _storage[mid]
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        else:
228
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    69
            assert isinstance(format, basestring) and \
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    70
                    format.lower() in _format_id
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
    71
            self._info = _storage[_format_id[format.lower()]]
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    72
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    73
    def __str__(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    74
        return '%(prefix)s~/%(directory)s' % self._info
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
226
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 directory(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    78
        """The mail_location's directory name."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    79
        return self._info['directory']
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    80
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    81
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    82
    def dovecot_version(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    83
        """The required Dovecot version (concatenated major and minor
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    84
        parts) for this mailbox format."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    85
        return self._info['dovecot_version']
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 postfix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    89
        """`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
    90
        return self._info['postfix']
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 prefix(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    94
        """The prefix of the mail_location."""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
        return self._info['prefix']
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
226
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    97
    @property
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    98
    def mail_location(self):
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    99
        """The mail_location, e.g. ``maildir:~/Maildir``"""
311eee429f67 VMM/maillocation: rewrote MailLocation class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
   100
        return self.__str__()
227
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   101
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   102
    @property
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   103
    def mid(self):
87db9f1f95ea VMM/Account: Adjusted to changes in maillocation.MailLocation.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 226
diff changeset
   104
        """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
   105
        return self._info['mid']
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   106
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   107
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   108
def known_format(format):
a7b000ca4ac9 VMM/maillocation: MailLocation.__init__(): take a 'format' name,
Pascal Volk <neverseen@users.sourceforge.net>
parents: 227
diff changeset
   109
    """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
   110
    return format.lower() in _format_id