VirtualMailManager/Handler.py
branchv0.6.x
changeset 236 084331dd1e4c
parent 233 55503d63ba30
child 237 09b7e3fe29b3
equal deleted inserted replaced
235:9d3405ed08e5 236:084331dd1e4c
    28 from VirtualMailManager.Config import Config as Cfg
    28 from VirtualMailManager.Config import Config as Cfg
    29 from VirtualMailManager.Domain import Domain, get_gid
    29 from VirtualMailManager.Domain import Domain, get_gid
    30 from VirtualMailManager.EmailAddress import EmailAddress
    30 from VirtualMailManager.EmailAddress import EmailAddress
    31 from VirtualMailManager.errors import VMMError, AliasError, DomainError
    31 from VirtualMailManager.errors import VMMError, AliasError, DomainError
    32 from VirtualMailManager.Relocated import Relocated
    32 from VirtualMailManager.Relocated import Relocated
       
    33 from VirtualMailManager.Transport import Transport
    33 from VirtualMailManager.ext.Postconf import Postconf
    34 from VirtualMailManager.ext.Postconf import Postconf
    34 
    35 
    35 
    36 
    36 SALTCHARS = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    37 SALTCHARS = './0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    37 RE_DOMAIN_SRCH = """^[a-z0-9-\.]+$"""
    38 RE_DOMAIN_SRCH = """^[a-z0-9-\.]+$"""
   189     def __getRelocated(self, address):
   190     def __getRelocated(self, address):
   190         address = EmailAddress(address)
   191         address = EmailAddress(address)
   191         self.__dbConnect()
   192         self.__dbConnect()
   192         return Relocated(self._dbh, address)
   193         return Relocated(self._dbh, address)
   193 
   194 
   194     def __getDomain(self, domainname, transport=None):
   195     def __getDomain(self, domainname):
   195         if transport is None:
   196         self.__dbConnect()
   196             transport = self._Cfg.dget('misc.transport')
   197         return Domain(self._dbh, domainname)
   197         self.__dbConnect()
       
   198         return Domain(self._dbh, domainname,
       
   199                 self._Cfg.dget('misc.base_directory'), transport)
       
   200 
   198 
   201     def __getDiskUsage(self, directory):
   199     def __getDiskUsage(self, directory):
   202         """Estimate file space usage for the given directory.
   200         """Estimate file space usage for the given directory.
   203 
   201 
   204         Keyword arguments:
   202         Keyword arguments:
   225             gid = 0
   223             gid = 0
   226         os.makedirs(directory, mode)
   224         os.makedirs(directory, mode)
   227         os.chown(directory, uid, gid)
   225         os.chown(directory, uid, gid)
   228 
   226 
   229     def __domDirMake(self, domdir, gid):
   227     def __domDirMake(self, domdir, gid):
       
   228         #TODO: clenaup!
   230         os.umask(0006)
   229         os.umask(0006)
   231         oldpwd = os.getcwd()
   230         oldpwd = os.getcwd()
   232         basedir = self._Cfg.dget('misc.base_directory')
   231         basedir = self._Cfg.dget('misc.base_directory')
   233         domdirdirs = domdir.replace(basedir + '/', '').split('/')
   232         domdirdirs = domdir.replace(basedir + '/', '').split('/')
   234 
   233 
   409 
   408 
   410     def cfgPget(self, option):
   409     def cfgPget(self, option):
   411         return self._Cfg.pget(option)
   410         return self._Cfg.pget(option)
   412 
   411 
   413     def domainAdd(self, domainname, transport=None):
   412     def domainAdd(self, domainname, transport=None):
   414         dom = self.__getDomain(domainname, transport)
   413         dom = self.__getDomain(domainname)
       
   414         if transport is None:
       
   415             dom.set_transport(Transport(self._dbh,
       
   416                               transport=self._Cfg.dget('misc.transport')))
       
   417         else:
       
   418             dom.set_transport(Transport(self._dbh, transport=transport))
       
   419         dom.set_directory(self._Cfg.dget('misc.base_directory'))
   415         dom.save()
   420         dom.save()
   416         self.__domDirMake(dom.getDir(), dom.getID())
   421         self.__domDirMake(dom.directory, dom.gid)
   417 
   422 
   418     def domainTransport(self, domainname, transport, force=None):
   423     def domainTransport(self, domainname, transport, force=None):
   419         if force is not None and force != 'force':
   424         if force is not None and force != 'force':
   420             raise DomainError(_(u"Invalid argument: “%s”") % force,
   425             raise DomainError(_(u"Invalid argument: “%s”") % force,
   421                 ERR.INVALID_OPTION)
   426                 ERR.INVALID_OPTION)
   422         dom = self.__getDomain(domainname, None)
   427         dom = self.__getDomain(domainname)
       
   428         trsp = Transport(self._dbh, transport=transport)
   423         if force is None:
   429         if force is None:
   424             dom.updateTransport(transport)
   430             dom.update_transport(trsp)
   425         else:
   431         else:
   426             dom.updateTransport(transport, force=True)
   432             dom.update_transport(trsp, force=True)
   427 
   433 
   428     def domainDelete(self, domainname, force=None):
   434     def domainDelete(self, domainname, force=None):
   429         if not force is None and force not in ['deluser', 'delalias',
   435         if not force is None and force not in ['deluser', 'delalias',
   430                                                'delall']:
   436                                                'delall']:
   431                 raise DomainError(_(u'Invalid argument: “%s”') %
   437                 raise DomainError(_(u'Invalid argument: “%s”') %
   432                                          force, ERR.INVALID_OPTION)
   438                                          force, ERR.INVALID_OPTION)
   433         dom = self.__getDomain(domainname)
   439         dom = self.__getDomain(domainname)
   434         gid = dom.getID()
   440         gid = dom.gid
   435         domdir = dom.getDir()
   441         domdir = dom.directory
   436         if self._Cfg.dget('domain.force_deletion') or force == 'delall':
   442         if self._Cfg.dget('domain.force_deletion') or force == 'delall':
   437             dom.delete(True, True)
   443             dom.delete(True, True)
   438         elif force == 'deluser':
   444         elif force == 'deluser':
   439             dom.delete(delUser=True)
   445             dom.delete(deluser=True)
   440         elif force == 'delalias':
   446         elif force == 'delalias':
   441             dom.delete(delAlias=True)
   447             dom.delete(delalias=True)
   442         else:
   448         else:
   443             dom.delete()
   449             dom.delete()
   444         if self._Cfg.dget('domain.delete_directory'):
   450         if self._Cfg.dget('domain.delete_directory'):
   445             self.__domDirDelete(domdir, gid)
   451             self.__domDirDelete(domdir, gid)
   446 
   452 
   448         if details not in [None, 'accounts', 'aliasdomains', 'aliases', 'full',
   454         if details not in [None, 'accounts', 'aliasdomains', 'aliases', 'full',
   449                            'relocated']:
   455                            'relocated']:
   450             raise VMMError(_(u'Invalid argument: “%s”') % details,
   456             raise VMMError(_(u'Invalid argument: “%s”') % details,
   451                                ERR.INVALID_AGUMENT)
   457                                ERR.INVALID_AGUMENT)
   452         dom = self.__getDomain(domainname)
   458         dom = self.__getDomain(domainname)
   453         dominfo = dom.getInfo()
   459         dominfo = dom.get_info()
   454         if dominfo['domainname'].startswith('xn--'):
   460         if dominfo['domainname'].startswith('xn--'):
   455             dominfo['domainname'] += ' (%s)' % ace2idna(dominfo['domainname'])
   461             dominfo['domainname'] += ' (%s)' % ace2idna(dominfo['domainname'])
   456         if details is None:
   462         if details is None:
   457             return dominfo
   463             return dominfo
   458         elif details == 'accounts':
   464         elif details == 'accounts':
   459             return (dominfo, dom.getAccounts())
   465             return (dominfo, dom.get_accounts())
   460         elif details == 'aliasdomains':
   466         elif details == 'aliasdomains':
   461             return (dominfo, dom.getAliaseNames())
   467             return (dominfo, dom.get_aliase_names())
   462         elif details == 'aliases':
   468         elif details == 'aliases':
   463             return (dominfo, dom.getAliases())
   469             return (dominfo, dom.get_aliases())
   464         elif details == 'relocated':
   470         elif details == 'relocated':
   465             return(dominfo, dom.getRelocated())
   471             return(dominfo, dom.get_relocated())
   466         else:
   472         else:
   467             return (dominfo, dom.getAliaseNames(), dom.getAccounts(),
   473             return (dominfo, dom.get_aliase_names(), dom.get_accounts(),
   468                     dom.getAliases(), dom.getRelocated())
   474                     dom.get_aliases(), dom.get_relocated())
   469 
   475 
   470     def aliasDomainAdd(self, aliasname, domainname):
   476     def aliasDomainAdd(self, aliasname, domainname):
   471         """Adds an alias domain to the domain.
   477         """Adds an alias domain to the domain.
   472 
   478 
   473         Keyword arguments:
   479         Keyword arguments: