VirtualMailManager/AliasDomain.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 18 Aug 2009 03:00:00 +0000
changeset 115 21f264a88ab2
parent 102 485d3f7d6981
child 122 30abf0abf8f8
permissions -rw-r--r--
Added sieve/managesieve switching stuff, depending on the used Dovecot version.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
     2
# Copyright (c) 2008 - 2009, VEB IT
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
     5
"""Virtual Mail Manager's AliasDomain class to manage alias domains."""
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 78
diff changeset
     7
from constants.VERSION import *
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
     9
from Exceptions import VMMAliasDomainException as VADE
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
import constants.ERROR as ERR
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    11
import VirtualMailManager as VMM
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    13
class AliasDomain:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
    """Class to manage e-mail alias domains."""
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    15
    def __init__(self, dbh, domainname, targetDomain=None):
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
        self._dbh = dbh
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    17
        self.__name = VMM.VirtualMailManager.chkDomainname(domainname)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    18
        self.__gid = 0
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    19
        self._domain = targetDomain
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    20
        self._exists()
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
    def _exists(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    23
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    24
        dbc.execute('SELECT gid, is_primary FROM domain_name WHERE domainname\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    25
 = %s', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    26
        alias = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    27
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    28
        if alias is not None:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    29
            self.__gid, primary = alias
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    30
            if primary:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    31
                raise VADE(_(u"The domain »%s« is a primary domain.") %
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    32
                        self.__name, ERR.ALIASDOMAIN_ISDOMAIN)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
    def save(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    35
        if self.__gid > 0:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    36
            raise VADE(_(u'The alias domain »%s« already exists.') %self.__name,
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    37
                    ERR.ALIASDOMAIN_EXISTS)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    38
        if self._domain is None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    39
            raise VADE(_(u'No destination domain for alias domain denoted.'),
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    40
                    ERR.ALIASDOMAIN_NO_DOMDEST)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    41
        if self._domain._id < 1:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    42
            raise VADE (_(u"The target domain »%s« doesn't exist yet.") %
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    43
                    self._domain._name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    44
        dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    45
        dbc.execute('INSERT INTO domain_name (domainname, gid, is_primary)\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    46
 VALUES (%s, %s, FALSE)', self.__name, self._domain._id)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    47
        self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    48
        dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    49
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    50
    def info(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    51
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    52
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    53
            dbc.execute('SELECT domainname FROM domain_name WHERE gid = %s\
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    54
 AND is_primary', self.__gid)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    55
            domain = dbc.fetchone()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    56
            dbc.close()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    57
            if domain is not None:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    58
                return {'alias': self.__name, 'domain': domain[0]}
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    59
            else:# an almost unlikely case, isn't it?
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    60
                raise VADE(
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    61
                    _(u'There is no primary domain for the alias domain »%s«.')\
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    62
                            % self.__name, ERR.NO_SUCH_DOMAIN)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    63
        else:
78
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    64
            raise VADE(_(u"The alias domain »%s« doesn't exist yet.") %
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    65
                    self.__name, ERR.NO_SUCH_ALIASDOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    66
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    67
    def switch(self):
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    68
        if self._domain is None:
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    69
            raise VADE(_(u'No destination domain for alias domain denoted.'),
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    70
                    ERR.ALIASDOMAIN_NO_DOMDEST)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    71
        if self._domain._id < 1:
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    72
            raise VADE (_(u"The target domain »%s« doesn't exist yet.") %
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    73
                    self._domain._name, ERR.NO_SUCH_DOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    74
        if self.__gid < 1:
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    75
            raise VADE(_(u"The alias domain »%s« doesn't exist yet.") %
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    76
                    self.__name, ERR.NO_SUCH_ALIASDOMAIN)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    77
        if self.__gid == self._domain._id:
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    78
            raise VADE(_(u"The alias domain »%(alias)s« is already assigned to\
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    79
 the domain »%(domain)s«.") %
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    80
                    {'alias': self.__name, 'domain': self._domain._name},
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    81
                    ERR.ALIASDOMAIN_EXISTS)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    82
        dbc = self._dbh.cursor()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    83
        dbc.execute('UPDATE domain_name SET gid = %s WHERE gid = %s\
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    84
 AND domainname = %s AND NOT is_primary',
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    85
                self._domain._id, self.__gid, self.__name)
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    86
        self._dbh.commit()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    87
        dbc.close()
8f1e501b1bb1 * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
    88
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
    def delete(self):
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    90
        if self.__gid > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    91
            dbc = self._dbh.cursor()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    92
            dbc.execute('DELETE FROM domain_name WHERE domainname = %s \
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    93
 AND NOT is_primary', self.__name)
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    94
            if dbc.rowcount > 0:
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    95
                self._dbh.commit()
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
    96
        else:
55
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    97
            raise VADE(
15c873f94ba6 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 53
diff changeset
    98
                  _(u"The alias domain »%s« doesn't exist yet.") % self.__name,
56
9ae1b1b2ee5c * 'VirtualMailManager/AliasDomain.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 55
diff changeset
    99
                  ERR.NO_SUCH_ALIASDOMAIN)
53
5b50eb306d37 * 'VirtualMailManager/DomainAlias.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 48
diff changeset
   100