VirtualMailManager/Config.py
changeset 0 bb0aa2102206
child 2 9b39f828aa8a
equal deleted inserted replaced
-1:000000000000 0:bb0aa2102206
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # opyright 2007-2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """Configurtion class for read, modify and write the
       
     8 configuration from Virtual Mail Manager.
       
     9 
       
    10 """
       
    11 
       
    12 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    13 __version__ = 'rev '+'$Rev$'.split()[1]
       
    14 __date__ = '$Date$'.split()[1]
       
    15 
       
    16 import sys
       
    17 from shutil import copy2
       
    18 from ConfigParser import ConfigParser
       
    19 
       
    20 from Exceptions import VMMConfigException
       
    21 import constants.EXIT as EXIT
       
    22 
       
    23 class VMMConfig(ConfigParser):
       
    24     """This class is for configure the mailadmin.
       
    25 
       
    26     You can specify settings for the database connection
       
    27     and maildirectories.
       
    28 
       
    29     """
       
    30     missingOptCtr = -1
       
    31 
       
    32     def __init__(self, filename):
       
    33         """Creates a new VMMConfig instance
       
    34 
       
    35         Keyword arguments:
       
    36         filename -- name of the configuration file
       
    37         """
       
    38         ConfigParser.__init__(self)
       
    39         self.__cfgFileName = filename
       
    40         self.__cfgFile = None
       
    41         self.__VMMsections = ['database', 'maildir', 'domdir', 'bin', 'misc',
       
    42                 'config']
       
    43         self.__changes = False
       
    44         self.__missingSect = []
       
    45         self.__dbopts = [
       
    46                 ['host', 'localhot'],
       
    47                 ['user', 'vmm'],
       
    48                 ['pass', 'your secret password'],
       
    49                 ['name', 'mailsys']
       
    50                 ]
       
    51         self.__mdopts = [
       
    52                 ['base', '/home/mail'],
       
    53                 ['folder', 'Maildir'],
       
    54                 ['mode', 448],
       
    55                 ['diskusage', 'false'],
       
    56                 ['delete', 'false']
       
    57                 ]
       
    58         self.__domdopts = [
       
    59                 ['mode', 504],
       
    60                 ['delete', 'false']
       
    61                 ]
       
    62         self.__binopts = [
       
    63                 ['dovecotpw', '/usr/sbin/dovecotpw'],
       
    64                 ['du', '/usr/bin/du']
       
    65                 ]
       
    66         self.__miscopts = [
       
    67                 ['passwdscheme', 'CRAM-MD5'],
       
    68                 ['gid_mail', 8],
       
    69                 ['forcedel', 'false']
       
    70                 ]
       
    71 
       
    72     def load(self):
       
    73         """Loads the configuration, r/o"""
       
    74         try:
       
    75             self.__cfgFile = file(self.__cfgFileName, 'r')
       
    76         except:
       
    77             raise
       
    78         self.readfp(self.__cfgFile)
       
    79         self.__cfgFile.close()
       
    80 
       
    81     def getsections(self):
       
    82         """Return a list with all configurable sections."""
       
    83         return self.__VMMsections[:-1]
       
    84 
       
    85     def configure(self, sections):
       
    86         """Interactive method for configuring all options in the given section
       
    87 
       
    88         Keyword arguments:
       
    89         sections -- list of strings
       
    90         """
       
    91         if not isinstance(sections, list):
       
    92             raise TypeError("Argument 'sections' is not a list.")
       
    93         # if [config] done = false (default at 1st run),
       
    94         # then set changes true
       
    95         try:
       
    96             if not self.getboolean('config', 'done'):
       
    97                 self.__changes = True
       
    98         except ValueError:
       
    99             self.set('config', 'done', 'False')
       
   100             self.__changes = True
       
   101         for s in sections:
       
   102             if s == 'config':
       
   103                 pass
       
   104             else:
       
   105                 print '* Config section: %s' % s
       
   106             for opt, val in self.items(s):
       
   107                 newval = raw_input('Enter new value for %s [%s]: ' %(opt, val))
       
   108                 if newval and newval != val:
       
   109                     self.set(s, opt, newval)
       
   110                     self.__changes = True
       
   111             print
       
   112         if self.__changes:
       
   113             self.__saveChanges()
       
   114 
       
   115     def __saveChanges(self):
       
   116         """Writes changes to the configuration file."""
       
   117         self.set('config', 'done', 'true')
       
   118         copy2(self.__cfgFileName, self.__cfgFileName+'.bak')
       
   119         self.__cfgFile = file(self.__cfgFileName, 'w')
       
   120         self.write(self.__cfgFile)
       
   121         self.__cfgFile.close()
       
   122 
       
   123     def __chkSections(self):
       
   124         """Checks if all configuration sections are existing."""
       
   125         retval = False
       
   126         for s in self.__VMMsections:
       
   127             if not self.has_section(s):
       
   128                 self.__missingSect.append(s)
       
   129             else:
       
   130                 retval = self.__chkOptions(s)
       
   131         return retval
       
   132 
       
   133     def __chkOptions(self, section):
       
   134         """Checks if all configuration options in section are existing.
       
   135 
       
   136         Keyword arguments:
       
   137         section -- the section to be checked
       
   138         """
       
   139         retval = True
       
   140         VMMConfig.missingOptCtr += 1
       
   141         self.__missingOpt.append([])
       
   142         if section == 'database':
       
   143             opts = self.__dbopts
       
   144         elif section == 'maildir':
       
   145             opts = self.__mdopts
       
   146         elif section == 'bin':
       
   147             opts = self.__binopts
       
   148         elif section == 'misc':
       
   149             opts = self.__miscopts
       
   150         for o, v in opts:
       
   151             if not self.has_option(section, o):
       
   152                 self.__missingOpt[VMMConfig.missingOptCtr].append(o)
       
   153                 retval = False
       
   154         return retval