VirtualMailManager/maillocation.py
branchv0.6.x
changeset 226 311eee429f67
parent 216 0c8c053b451c
child 227 87db9f1f95ea
equal deleted inserted replaced
225:a51809f7940b 226:311eee429f67
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2008 - 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 
       
     5 """
       
     6     VirtualMailManager.maillocation
       
     7 
       
     8     Virtual Mail Manager's maillocation module to handle Dovecot's
       
     9     mail_location setting for accounts.
       
    10 
       
    11 """
       
    12 
       
    13 from VirtualMailManager.pycompat import any
       
    14 
       
    15 
       
    16 __all__ = ('MailLocation',
       
    17            'MAILDIR_ID', 'MBOX_ID', 'MDBOX_ID', 'SDBOX_ID',
       
    18            'MAILDIR_NAME', 'MBOX_NAME', 'MDBOX_NAME', 'SDBOX_NAME')
       
    19 
       
    20 MAILDIR_ID = 0x1
       
    21 MBOX_ID = 0x2
       
    22 MDBOX_ID = 0x3
       
    23 SDBOX_ID = 0x4
       
    24 MAILDIR_NAME = 'Maildir'
       
    25 MBOX_NAME = 'mail'
       
    26 MDBOX_NAME = 'mdbox'
       
    27 SDBOX_NAME = 'dbox'
       
    28 
       
    29 _storage = {
       
    30     MAILDIR_ID: dict(dovecot_version=10, postfix=True, prefix='maildir:',
       
    31                      directory=MAILDIR_NAME),
       
    32     MBOX_ID: dict(dovecot_version=10, postfix=True, prefix='mbox:',
       
    33                   directory=MBOX_NAME),
       
    34     MDBOX_ID: dict(dovecot_version=20, postfix=False, prefix='mdbox:',
       
    35                    directory=MDBOX_NAME),
       
    36     SDBOX_ID: dict(dovecot_version=12, postfix=False, prefix='dbox:',
       
    37                    directory=SDBOX_NAME),
       
    38 }
       
    39 
       
    40 _type_id = {
       
    41     'maildir': MAILDIR_ID,
       
    42     MBOX_NAME: MBOX_ID,
       
    43     MDBOX_NAME: MDBOX_ID,
       
    44     SDBOX_NAME: SDBOX_ID,
       
    45 }
       
    46 
       
    47 
       
    48 class MailLocation(object):
       
    49     """A small class for mail_location relevant information."""
       
    50     __slots__ = ('_info')
       
    51 
       
    52     def __init__(self, mid=None, type_=None):
       
    53         """Creates a new MailLocation instance.
       
    54 
       
    55         Either mid or type_ must be specified.
       
    56 
       
    57         Keyword arguments:
       
    58         mid -- the id of a mail_location (int)
       
    59           one of the maillocation constants: `MAILDIR_ID`, `MBOX_ID`,
       
    60           `MDBOX_ID` and `SDBOX_ID`
       
    61         type_ -- the type/mailbox format of the mail_location (str)
       
    62           one of the maillocation constants: `MAILDIR_NAME`, `MBOX_NAME`,
       
    63           `MDBOX_NAME` and `SDBOX_NAME`
       
    64         """
       
    65         assert any((mid, type_))
       
    66         if mid:
       
    67             assert isinstance(mid, (int, long)) and mid in _storage
       
    68             self._info = _storage[mid]
       
    69         else:
       
    70             assert isinstance(type_, basestring) and type_.lower() in _type_id
       
    71             self._info = _storage[_type_id[type_.lower()]]
       
    72 
       
    73     def __str__(self):
       
    74         return '%(prefix)s~/%(directory)s' % self._info
       
    75 
       
    76     @property
       
    77     def directory(self):
       
    78         """The mail_location's directory name."""
       
    79         return self._info['directory']
       
    80 
       
    81     @property
       
    82     def dovecot_version(self):
       
    83         """The required Dovecot version (concatenated major and minor
       
    84         parts) for this mailbox format."""
       
    85         return self._info['dovecot_version']
       
    86 
       
    87     @property
       
    88     def postfix(self):
       
    89         """`True` if Postfix supports this mailbox format, else `False`."""
       
    90         return self._info['postfix']
       
    91 
       
    92     @property
       
    93     def prefix(self):
       
    94         """The prefix of the mail_location."""
       
    95         return self._info['prefix']
       
    96 
       
    97     @property
       
    98     def mail_location(self):
       
    99         """The mail_location, e.g. ``maildir:~/Maildir``"""
       
   100         return self.__str__()