VirtualMailManager/VirtualMailManager.py
changeset 128 cf8116625866
parent 119 014335f38962
child 132 fa22bd13b4d1
equal deleted inserted replaced
127:97a9f6dd954b 128:cf8116625866
    35 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
    35 RE_LOCALPART = """[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""
    36 RE_MBOX_NAMES = """^[\x20-\x25\x27-\x7E]*$"""
    36 RE_MBOX_NAMES = """^[\x20-\x25\x27-\x7E]*$"""
    37 locale.setlocale(locale.LC_ALL, '')
    37 locale.setlocale(locale.LC_ALL, '')
    38 ENCODING = locale.nl_langinfo(locale.CODESET)
    38 ENCODING = locale.nl_langinfo(locale.CODESET)
    39 
    39 
    40 class VirtualMailManager:
    40 class VirtualMailManager(object):
    41     """The main class for vmm"""
    41     """The main class for vmm"""
       
    42     __slots__ = ('__Cfg', '__cfgFileName', '__cfgSections', '__dbh', '__scheme',
       
    43             '__warnings', '_postconf')
    42     def __init__(self):
    44     def __init__(self):
    43         """Creates a new VirtualMailManager instance.
    45         """Creates a new VirtualMailManager instance.
    44         Throws a VMMNotRootException if your uid is greater 0.
    46         Throws a VMMNotRootException if your uid is greater 0.
    45         """
    47         """
    46         self.__cfgFileName = ''
    48         self.__cfgFileName = ''
    47         self.__permWarnMsg = _(u"fix permissions for »%(cfgFileName)s«\n\
       
    48 `chmod 0600 %(cfgFileName)s` would be great.") % {'cfgFileName':
       
    49             self.__cfgFileName}
       
    50         self.__warnings = []
    49         self.__warnings = []
    51         self.__Cfg = None
    50         self.__Cfg = None
    52         self.__dbh = None
    51         self.__dbh = None
    53 
    52 
    54         if os.geteuid():
    53         if os.geteuid():
    80         self.__findCfgFile()
    79         self.__findCfgFile()
    81         fstat = os.stat(self.__cfgFileName)
    80         fstat = os.stat(self.__cfgFileName)
    82         fmode = int(oct(fstat.st_mode & 0777))
    81         fmode = int(oct(fstat.st_mode & 0777))
    83         if fmode % 100 and fstat.st_uid != fstat.st_gid \
    82         if fmode % 100 and fstat.st_uid != fstat.st_gid \
    84         or fmode % 10 and fstat.st_uid == fstat.st_gid:
    83         or fmode % 10 and fstat.st_uid == fstat.st_gid:
    85             raise VMMPermException(self.__permWarnMsg, ERR.CONF_ERROR)
    84             raise VMMPermException(_(
       
    85                 u'fix permissions (%(perms)s) for »%(file)s«\n\
       
    86 `chmod 0600 %(file)s` would be great.') % {'file':
       
    87                 self.__cfgFileName, 'perms': fmode}, ERR.CONF_WRONGPERM)
    86         else:
    88         else:
    87             return True
    89             return True
    88 
    90 
    89     def __chkenv(self):
    91     def __chkenv(self):
    90         """"""
    92         """"""
   124             except PgSQL.libpq.DatabaseError, e:
   126             except PgSQL.libpq.DatabaseError, e:
   125                 raise VMMException(str(e), ERR.DATABASE_ERROR)
   127                 raise VMMException(str(e), ERR.DATABASE_ERROR)
   126 
   128 
   127     def idn2ascii(domainname):
   129     def idn2ascii(domainname):
   128         """Converts an idn domainname in punycode.
   130         """Converts an idn domainname in punycode.
   129         
   131 
   130         Keyword arguments:
   132         Keyword arguments:
   131         domainname -- the domainname to convert (str)
   133         domainname -- the domainname to convert (str)
   132         """
   134         """
   133         tmp = []
   135         tmp = []
   134         for label in domainname.split('.'):
   136         for label in domainname.split('.'):
   138         return '.'.join(tmp)
   140         return '.'.join(tmp)
   139     idn2ascii = staticmethod(idn2ascii)
   141     idn2ascii = staticmethod(idn2ascii)
   140 
   142 
   141     def ace2idna(domainname):
   143     def ace2idna(domainname):
   142         """Convertis a domainname from ACE according to IDNA
   144         """Convertis a domainname from ACE according to IDNA
   143         
   145 
   144         Keyword arguments:
   146         Keyword arguments:
   145         domainname -- the domainname to convert (str)
   147         domainname -- the domainname to convert (str)
   146         """
   148         """
   147         tmp = []
   149         tmp = []
   148         for label in domainname.split('.'):
   150         for label in domainname.split('.'):
   152         return '.'.join(tmp)
   154         return '.'.join(tmp)
   153     ace2idna = staticmethod(ace2idna)
   155     ace2idna = staticmethod(ace2idna)
   154 
   156 
   155     def chkDomainname(domainname):
   157     def chkDomainname(domainname):
   156         """Validates the domain name of an e-mail address.
   158         """Validates the domain name of an e-mail address.
   157         
   159 
   158         Keyword arguments:
   160         Keyword arguments:
   159         domainname -- the domain name that should be validated
   161         domainname -- the domain name that should be validated
   160         """
   162         """
   161         if not re.match(RE_ASCII_CHARS, domainname):
   163         if not re.match(RE_ASCII_CHARS, domainname):
   162             domainname = VirtualMailManager.idn2ascii(domainname)
   164             domainname = VirtualMailManager.idn2ascii(domainname)
   245         return Domain(self.__dbh, domainname,
   247         return Domain(self.__dbh, domainname,
   246                 self.__Cfg.get('domdir', 'base'), transport)
   248                 self.__Cfg.get('domdir', 'base'), transport)
   247 
   249 
   248     def __getDiskUsage(self, directory):
   250     def __getDiskUsage(self, directory):
   249         """Estimate file space usage for the given directory.
   251         """Estimate file space usage for the given directory.
   250         
   252 
   251         Keyword arguments:
   253         Keyword arguments:
   252         directory -- the directory to summarize recursively disk usage for
   254         directory -- the directory to summarize recursively disk usage for
   253         """
   255         """
   254         if self.__isdir(directory):
   256         if self.__isdir(directory):
   255             return Popen([self.__Cfg.get('bin', 'du'), "-hs", directory],
   257             return Popen([self.__Cfg.get('bin', 'du'), "-hs", directory],
   610     def aliasAdd(self, aliasaddress, targetaddress):
   612     def aliasAdd(self, aliasaddress, targetaddress):
   611         alias = self.__getAlias(aliasaddress, targetaddress)
   613         alias = self.__getAlias(aliasaddress, targetaddress)
   612         alias.save(long(self._postconf.read('virtual_alias_expansion_limit')))
   614         alias.save(long(self._postconf.read('virtual_alias_expansion_limit')))
   613         gid = self.__getDomain(alias._dest._domainname).getID()
   615         gid = self.__getDomain(alias._dest._domainname).getID()
   614         if gid > 0 and not VirtualMailManager.accountExists(self.__dbh,
   616         if gid > 0 and not VirtualMailManager.accountExists(self.__dbh,
   615         alias._dest) and not VirtualMailManager.aliasExists(self.__dbh, 
   617         alias._dest) and not VirtualMailManager.aliasExists(self.__dbh,
   616         alias._dest):
   618         alias._dest):
   617             self.__warnings.append(
   619             self.__warnings.append(
   618                 _(u"The destination account/alias »%s« doesn't exists yet.")%\
   620                 _(u"The destination account/alias »%s« doesn't exists yet.")%\
   619                         alias._dest)
   621                         alias._dest)
   620 
   622