VirtualMailManager/Handler.py
branchv0.6.x
changeset 188 cf1b5f22dbd2
parent 187 38b9a9859749
child 190 1903d4ce97d7
equal deleted inserted replaced
187:38b9a9859749 188:cf1b5f22dbd2
    19 from subprocess import Popen, PIPE
    19 from subprocess import Popen, PIPE
    20 
    20 
    21 from pyPgSQL import PgSQL # python-pgsql - http://pypgsql.sourceforge.net
    21 from pyPgSQL import PgSQL # python-pgsql - http://pypgsql.sourceforge.net
    22 
    22 
    23 import VirtualMailManager.constants.ERROR as ERR
    23 import VirtualMailManager.constants.ERROR as ERR
    24 from VirtualMailManager import ENCODING, ace2idna, exec_ok, read_pass
    24 from VirtualMailManager import ENCODING, ace2idna, exec_ok
    25 from VirtualMailManager.Account import Account
    25 from VirtualMailManager.Account import Account
    26 from VirtualMailManager.Alias import Alias
    26 from VirtualMailManager.Alias import Alias
    27 from VirtualMailManager.AliasDomain import AliasDomain
    27 from VirtualMailManager.AliasDomain import AliasDomain
    28 from VirtualMailManager.Config import Config as Cfg
       
    29 from VirtualMailManager.Domain import Domain
    28 from VirtualMailManager.Domain import Domain
    30 from VirtualMailManager.EmailAddress import EmailAddress
    29 from VirtualMailManager.EmailAddress import EmailAddress
    31 from VirtualMailManager.Exceptions import *
    30 from VirtualMailManager.Exceptions import *
    32 from VirtualMailManager.Relocated import Relocated
    31 from VirtualMailManager.Relocated import Relocated
    33 from VirtualMailManager.ext.Postconf import Postconf
    32 from VirtualMailManager.ext.Postconf import Postconf
    38 RE_MBOX_NAMES = """^[\x20-\x25\x27-\x7E]*$"""
    37 RE_MBOX_NAMES = """^[\x20-\x25\x27-\x7E]*$"""
    39 
    38 
    40 class Handler(object):
    39 class Handler(object):
    41     """Wrapper class to simplify the access on all the stuff from
    40     """Wrapper class to simplify the access on all the stuff from
    42     VirtualMailManager"""
    41     VirtualMailManager"""
    43     # TODO: accept a LazyConfig object as argument
       
    44     __slots__ = ('__Cfg', '__cfgFileName', '__dbh', '__scheme', '__warnings',
    42     __slots__ = ('__Cfg', '__cfgFileName', '__dbh', '__scheme', '__warnings',
    45                  '_postconf')
    43                  '_postconf')
    46     def __init__(self):
    44     def __init__(self, config_type='default'):
    47         """Creates a new Handler instance.
    45         """Creates a new Handler instance.
       
    46 
       
    47         Accepted ``config_type``s are 'default' and 'cli'.
       
    48 
    48         Throws a VMMNotRootException if your uid is greater 0.
    49         Throws a VMMNotRootException if your uid is greater 0.
    49         """
    50         """
    50         self.__cfgFileName = ''
    51         self.__cfgFileName = ''
    51         self.__warnings = []
    52         self.__warnings = []
    52         self.__Cfg = None
    53         self.__Cfg = None
    53         self.__dbh = None
    54         self.__dbh = None
       
    55 
       
    56         if config_type == 'default':
       
    57             from VirtualMailManager.Config import Config as Cfg
       
    58         elif config_type == 'cli':
       
    59             from VirtualMailManager.cli.CliConfig import CliConfig as Cfg
       
    60             from VirtualMailManager.cli import read_pass
       
    61         else:
       
    62             raise ValueError('invalid config_type: %r' % config_type)
    54 
    63 
    55         if os.geteuid():
    64         if os.geteuid():
    56             raise VMMNotRootException(_(u"You are not root.\n\tGood bye!\n"),
    65             raise VMMNotRootException(_(u"You are not root.\n\tGood bye!\n"),
    57                 ERR.CONF_NOPERM)
    66                 ERR.CONF_NOPERM)
    58         if self.__chkCfgFile():
    67         if self.__chkCfgFile():
   152         return Handler._exists(dbh, sql)
   161         return Handler._exists(dbh, sql)
   153     accountExists = staticmethod(accountExists)
   162     accountExists = staticmethod(accountExists)
   154 
   163 
   155     def aliasExists(dbh, address):
   164     def aliasExists(dbh, address):
   156         sql = "SELECT DISTINCT gid FROM alias WHERE gid = (SELECT gid FROM\
   165         sql = "SELECT DISTINCT gid FROM alias WHERE gid = (SELECT gid FROM\
   157  domain_name WHERE domainname = '%s') AND address = '%s'" %
   166  domain_name WHERE domainname = '%s') AND address = '%s'" % (
   158             (address._domainname, address._localpart)
   167                                         address._domainname, address._localpart)
   159         return Handler._exists(dbh, sql)
   168         return Handler._exists(dbh, sql)
   160     aliasExists = staticmethod(aliasExists)
   169     aliasExists = staticmethod(aliasExists)
   161 
   170 
   162     def relocatedExists(dbh, address):
   171     def relocatedExists(dbh, address):
   163         sql = "SELECT gid FROM relocated WHERE gid = (SELECT gid FROM\
   172         sql = "SELECT gid FROM relocated WHERE gid = (SELECT gid FROM\
   164  domain_name WHERE domainname = '%s') AND address = '%s'" %
   173  domain_name WHERE domainname = '%s') AND address = '%s'" % (
   165             (address._domainname, address._localpart)
   174                                         address._domainname, address._localpart)
   166         return Handler._exists(dbh, sql)
   175         return Handler._exists(dbh, sql)
   167     relocatedExists = staticmethod(relocatedExists)
   176     relocatedExists = staticmethod(relocatedExists)
   168 
   177 
   169 
   178 
   170     def __getAccount(self, address, password=None):
   179     def __getAccount(self, address, password=None):