/usr/lib/python2.7/dist-packages/VirtualMailManager/maillocation.py is in vmm 0.6.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | # -*- coding: UTF-8 -*-
# Copyright (c) 2008 - 2014, Pascal Volk
# See COPYING for distribution information.
"""
VirtualMailManager.maillocation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Virtual Mail Manager's maillocation module to handle Dovecot's
mail_location setting for accounts.
"""
from VirtualMailManager.constants import MAILLOCATION_INIT
from VirtualMailManager.errors import MailLocationError as MLErr
from VirtualMailManager.pycompat import all
__all__ = ('MailLocation', 'known_format')
_ = lambda msg: msg
_format_info = {
'maildir': dict(dovecot_version=0x10000f00, postfix=True),
'mdbox': dict(dovecot_version=0x20000b05, postfix=False),
'sdbox': dict(dovecot_version=0x20000c03, postfix=False),
}
class MailLocation(object):
"""Class to handle mail_location relevant information."""
__slots__ = ('_directory', '_mbfmt', '_mid', '_dbh')
_kwargs = ('mid', 'mbfmt', 'directory')
def __init__(self, dbh, **kwargs):
"""Creates a new MailLocation instance.
Either the mid keyword or the mbfmt and directory keywords must be
specified.
Arguments:
`dbh` : pyPgSQL.PgSQL.Connection
A database connection for the database access.
Keyword arguments:
`mid` : int
the id of a mail_location
`mbfmt` : str
the mailbox format of the mail_location. One out of: ``maildir``,
``sdbox`` and ``mdbox``.
`directory` : str
name of the mailbox root directory.
"""
self._dbh = dbh
self._directory = None
self._mbfmt = None
self._mid = 0
for key in kwargs.iterkeys():
if key not in self.__class__._kwargs:
raise ValueError('unrecognized keyword: %r' % key)
mid = kwargs.get('mid')
if mid:
assert isinstance(mid, (int, long))
self._load_by_mid(mid)
else:
args = kwargs.get('mbfmt'), kwargs.get('directory')
assert all(isinstance(arg, basestring) for arg in args)
if args[0].lower() not in _format_info:
raise MLErr(_(u"Unsupported mailbox format: '%s'") % args[0],
MAILLOCATION_INIT)
directory = args[1].strip()
if not directory:
raise MLErr(_(u"Empty directory name"), MAILLOCATION_INIT)
if len(directory) > 20:
raise MLErr(_(u"Directory name is too long: '%s'") % directory,
MAILLOCATION_INIT)
self._load_by_names(args[0].lower(), directory)
def __str__(self):
return u'%s:~/%s' % (self._mbfmt, self._directory)
@property
def directory(self):
"""The mail_location's directory name."""
return self._directory
@property
def dovecot_version(self):
"""The required Dovecot version for this mailbox format."""
return _format_info[self._mbfmt]['dovecot_version']
@property
def postfix(self):
"""`True` if Postfix supports this mailbox format, else `False`."""
return _format_info[self._mbfmt]['postfix']
@property
def mbformat(self):
"""The mail_location's mailbox format."""
return self._mbfmt
@property
def mail_location(self):
"""The mail_location, e.g. ``maildir:~/Maildir``"""
return self.__str__()
@property
def mid(self):
"""The mail_location's unique ID."""
return self._mid
def _load_by_mid(self, mid):
"""Load mail_location relevant information by *mid*"""
dbc = self._dbh.cursor()
dbc.execute('SELECT format, directory FROM mailboxformat, '
'maillocation WHERE mid = %u AND '
'maillocation.fid = mailboxformat.fid' % mid)
result = dbc.fetchone()
dbc.close()
if not result:
raise ValueError('Unknown mail_location id specified: %r' % mid)
self._mid = mid
self._mbfmt, self._directory = result
def _load_by_names(self, mbfmt, directory):
"""Try to load mail_location relevant information by *mbfmt* and
*directory* name. If it fails goto _save()."""
dbc = self._dbh.cursor()
dbc.execute("SELECT mid FROM maillocation WHERE fid = (SELECT fid "
"FROM mailboxformat WHERE format = %s) AND directory = %s",
(mbfmt, directory))
result = dbc.fetchone()
dbc.close()
if not result:
self._save(mbfmt, directory)
else:
self._mid = result[0]
self._mbfmt = mbfmt
self._directory = directory
def _save(self, mbfmt, directory):
"""Save a new mail_location in the database."""
dbc = self._dbh.cursor()
dbc.execute("SELECT nextval('maillocation_id')")
mid = dbc.fetchone()[0]
dbc.execute("INSERT INTO maillocation (fid, mid, directory) VALUES ("
"(SELECT fid FROM mailboxformat WHERE format = %s), %s, "
"%s)", (mbfmt, mid, directory))
self._dbh.commit()
dbc.close()
self._mid = mid
self._mbfmt = mbfmt
self._directory = directory
def known_format(mbfmt):
"""Checks if the mailbox format *mbfmt* is known, returns bool."""
return mbfmt.lower() in _format_info
del _
|