vmm
changeset 0 bb0aa2102206
child 1 d08cda9d7c1a
equal deleted inserted replaced
-1:000000000000 0:bb0aa2102206
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright 2007-2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """This is the vmm main script."""
       
     8 
       
     9 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    10 __version__ = 'rev '+'$Rev$'.split()[1]
       
    11 __date__ = '$Date$'.split()[1]
       
    12 
       
    13 import os
       
    14 import sys
       
    15 from getpass import getpass
       
    16 
       
    17 #sys.path.insert(0, '/usr/local/lib/VirtualMailManager')
       
    18 
       
    19 from VirtualMailManager.VirtualMailManager import VirtualMailManager
       
    20 from VirtualMailManager.Config import VMMConfig
       
    21 import VirtualMailManager.Exceptions as VMME
       
    22 import VirtualMailManager.constants.EXIT as EXIT
       
    23 
       
    24 def usage(excode=0, errMsg=None):
       
    25     sys.stderr.write("""\
       
    26 Usage: vmm OPTION OBJECT ARGS*
       
    27   short long
       
    28   option                object           args (* = optional)
       
    29 
       
    30   da    domainadd       domain.tld       transport*
       
    31   di    domaininfo      domain.tld       detailed*
       
    32   dt    domaintransport domain.tld       transport
       
    33   dd    domaindelete    domain.tld       delalias*|deluser*|delall*
       
    34   ua    useradd         user@domain.tld  password*
       
    35   ui    userinfo        user@domain.tld  du*
       
    36   un    username        user@domain.tld  'Users Name'
       
    37   up    userpassword    user@domain.tld  password*
       
    38   u0    userdisable     user@domain.tld
       
    39   u1    userenable      user@domain.tld
       
    40   ud    userdelete      user@domain.tld
       
    41   aa    aliasadd        alias@domain.tld user@domain.tld
       
    42   ai    aliasinfo       alias@domain.tld
       
    43   ad    aliasdelete     alias@domain.tld
       
    44   cf    configure                        section*
       
    45   h     help
       
    46   v     version
       
    47 
       
    48 """)
       
    49     if not errMsg is None:
       
    50         sys.stderr.write('Error: %s\n' % errMsg)
       
    51     sys.exit(excode)
       
    52 
       
    53 def getVMM():
       
    54     try:
       
    55         vmm = VirtualMailManager()
       
    56         return vmm
       
    57     except VMME.MANotRootException, e:
       
    58         sys.stderr.write(str(e))
       
    59         sys.exit(EXIT.CONF_NOPERM)
       
    60     except IOError, e:
       
    61         sys.stderr.write(str(e))
       
    62         sys.exit(EXIT.CONF_NOFILE)
       
    63     except VMME.MAPermException, e:
       
    64         sys.stderr.write(str(e))
       
    65         sys.exit(EXIT.CONF_WRONGPERM)
       
    66 
       
    67 def configure():
       
    68     try:
       
    69         if len(sys.argv) < 3:
       
    70             vmm.configure()
       
    71         else:
       
    72             vmm.configure(sys.argv[2])
       
    73     except (EOFError, KeyboardInterrupt):
       
    74         sys.stderr.write('\nOuch!\n')
       
    75         sys.exit(EXIT.USER_INTERRUPT)
       
    76     except VMME.VMMConfigException, e:
       
    77         sys.stderr.write(str(e))
       
    78         sys.exit(EXIT.CONF_ERROR)
       
    79     sys.exit(0)
       
    80 
       
    81 def _readpass():
       
    82     clear0 = ''
       
    83     clear1 = '1'
       
    84     while clear0 != clear1:
       
    85         while len(clear0) < 1:
       
    86             clear0 = getpass(prompt='Enter new password: ')
       
    87             if len(clear0) < 1:
       
    88                 sys.stderr.write('Sorry, empty passwords are not permitted\n')
       
    89         clear1 = getpass(prompt='Retype new password: ')
       
    90         if clear0 != clear1:
       
    91             clear0 = ''
       
    92             sys.stderr.write('Sorry, passwords do not match\n')
       
    93     return clear0
       
    94 
       
    95 def _printInfo(info, title):
       
    96     msg = title+' information'
       
    97     print '%s\n%s' % (msg, '-'*len(msg))
       
    98     for k,v in info.items():
       
    99         print '\t%s: %s' % (k.title().ljust(15, '.'), v)
       
   100     print
       
   101 
       
   102 def _printUsers(users, title):
       
   103     msg = 'Available '+title
       
   104     print '%s\n%s' % (msg, '-'*len(msg))
       
   105     if len(users) > 0:
       
   106         for user in users:
       
   107             print '\t%s' % user
       
   108     else:
       
   109         print '\tNone'
       
   110     print
       
   111 
       
   112 def _printAliases(alias, targets):
       
   113     msg = 'Alias information'
       
   114     print '%s\n%s' % (msg, '-'*len(msg))
       
   115     print '\tMail for %s goes to:' % alias
       
   116     if len(targets) > 0:
       
   117         for target in targets:
       
   118             print '\t     -> %s' % target
       
   119     else:
       
   120         print '\tNone'
       
   121     print
       
   122 
       
   123 def domain_add():
       
   124     global argc
       
   125     if argc < 3:
       
   126         usage(EXIT.MISSING_ARGS, 'Missing domain name.')
       
   127     elif argc < 4:
       
   128         vmm.domain_add(sys.argv[2].lower())
       
   129     else:
       
   130         vmm.domain_add(sys.argv[2].lower(), sys.argv[3])
       
   131 
       
   132 def domain_delete():
       
   133     global argc
       
   134     if argc < 3:
       
   135         usage(EXIT.MISSING_ARGS, 'Missing domain name.')
       
   136     elif argc < 4:
       
   137         vmm.domain_delete(sys.argv[2].lower())
       
   138     else:
       
   139         vmm.domain_delete(sys.argv[2].lower(), sys.argv[3])
       
   140 
       
   141 def domain_info():
       
   142     global argc
       
   143     if argc < 3:
       
   144         usage(EXIT.MISSING_ARGS, 'Missing domain name.')
       
   145     elif argc < 4:
       
   146         _printInfo(vmm.domain_info(sys.argv[2].lower()), 'Domain')
       
   147     else:
       
   148         infos = vmm.domain_info(sys.argv[2].lower(), sys.argv[3])
       
   149         _printInfo(infos[0], 'Domain')
       
   150         _printUsers(infos[1], 'accounts')
       
   151         _printUsers(infos[2], 'aliases')
       
   152 
       
   153 def domain_transport():
       
   154     global argc
       
   155     if argc < 3:
       
   156         usage(EXIT.MISSING_ARGS, 'Missing domain name and new transport.')
       
   157     if argc < 4:
       
   158         usage(EXIT.MISSING_ARGS, 'Missing new transport.')
       
   159     else:
       
   160         vmm.domain_transport(sys.argv[2].lower(), sys.argv[3])
       
   161 
       
   162 def user_add():
       
   163     global argc
       
   164     if argc < 3:
       
   165         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   166     elif argc < 4:
       
   167         password = _readpass()
       
   168     else:
       
   169         password = sys.argv[3]
       
   170     vmm.user_add(sys.argv[2].lower(), password)
       
   171 
       
   172 def user_delete():
       
   173     global argc
       
   174     if argc < 3:
       
   175         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   176     else:
       
   177         vmm.user_delete(sys.argv[2].lower())
       
   178 
       
   179 def user_info():
       
   180     global argc
       
   181     if argc < 3:
       
   182         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   183     elif argc < 4:
       
   184         _printInfo(vmm.user_info(sys.argv[2].lower()), 'Account')
       
   185     else:
       
   186         _printInfo(vmm.user_info(sys.argv[2].lower(), True), 'Account')
       
   187 
       
   188 def user_name():
       
   189     global argc
       
   190     if argc < 4:
       
   191         usage(EXIT.MISSING_ARGS, 'Missing email address an users name.')
       
   192     else:
       
   193         vmm.user_name(sys.argv[2].lower(), sys.argv[3])
       
   194 
       
   195 def user_enable():
       
   196     global argc
       
   197     if argc < 3:
       
   198         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   199     else:
       
   200         vmm.user_enable(sys.argv[2].lower())
       
   201 
       
   202 def user_disable():
       
   203     global argc
       
   204     if argc < 3:
       
   205         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   206     else:
       
   207         vmm.user_disable(sys.argv[2].lower())
       
   208 
       
   209 def user_password():
       
   210     global argc
       
   211     if argc < 3:
       
   212         usage(EXIT.MISSING_ARGS, 'Missing email address.')
       
   213     elif argc < 4:
       
   214         password = _readpass()
       
   215     else:
       
   216         password = sys.argv[3]
       
   217     vmm.user_password(sys.argv[2].lower(), password)
       
   218 
       
   219 def alias_add():
       
   220     global argc
       
   221     if argc < 4:
       
   222         usage(EXIT.MISSING_ARGS, 'Missing alias address and destination.')
       
   223     else:
       
   224         vmm.alias_add(sys.argv[2].lower(), sys.argv[3])
       
   225 
       
   226 def alias_info():
       
   227     global argc
       
   228     if argc < 3:
       
   229         usage(EXIT.MISSING_ARGS, 'Missing alias address')
       
   230     else:
       
   231         _printAliases(sys.argv[2], vmm.alias_info(sys.argv[2].lower()))
       
   232 
       
   233 def alias_delete():
       
   234     global argc
       
   235     if argc < 3:
       
   236         usage(EXIT.MISSING_ARGS, 'Missing alias address')
       
   237     else:
       
   238         vmm.alias_delete(sys.argv[2].lower())
       
   239 
       
   240 def showWarnings():
       
   241     if vmm.hasWarnings():
       
   242         print '\nWarnings:'
       
   243         for w in vmm.getWarnings():
       
   244             print " * ",w
       
   245 
       
   246 #def main():
       
   247 if __name__ == '__main__':
       
   248     argc = len(sys.argv)
       
   249     if argc < 2:
       
   250         usage(EXIT.MISSING_ARGS) # -> exit
       
   251     vmm = getVMM()
       
   252     try:
       
   253         if sys.argv[1] in ['cf',  'configure'] or not vmm.setupIsDone():
       
   254             configure()
       
   255     except VMME.VMMConfigException, e:
       
   256         sys.stderr.write(str(e))
       
   257         sys.exit(EXIT.CONF_ERROR)
       
   258     except VMME.VMMException, e:
       
   259         sys.stderr.write("\aERROR: %s\n" % e[0][0])
       
   260         sys.exit(e[0][1])
       
   261     try:
       
   262         if sys.argv[1] in ['da', 'domainadd']:
       
   263             domain_add()
       
   264         elif sys.argv[1] in ['di', 'domaininfo']:
       
   265             domain_info()
       
   266         elif sys.argv[1] in ['dt', 'domaintransport']:
       
   267             domain_transport()
       
   268         elif sys.argv[1] in ['dd', 'domaindelete']:
       
   269             domain_delete()
       
   270         elif sys.argv[1] in ['ua', 'useradd']:
       
   271             user_add()
       
   272         elif sys.argv[1] in ['ui', 'userinfo']:
       
   273             user_info()
       
   274         elif sys.argv[1] in ['un', 'username']:
       
   275             user_name()
       
   276         elif sys.argv[1] in ['up', 'userpassword']:
       
   277             user_password()
       
   278         elif sys.argv[1] in ['u0', 'userdisable']:
       
   279             user_disable()
       
   280         elif sys.argv[1] in ['u1', 'userenable']:
       
   281             user_enable()
       
   282         elif sys.argv[1] in ['ud', 'userdelete']:
       
   283             user_delete()
       
   284         elif sys.argv[1] in ['aa', 'aliasadd']:
       
   285             alias_add()
       
   286         elif sys.argv[1] in ['ai', 'aliasinfo']:
       
   287             alias_info()
       
   288         elif sys.argv[1] in ['ad', 'aliasdelete']:
       
   289             alias_delete()
       
   290         elif sys.argv[1] in ['h', 'help']:
       
   291             usage()
       
   292         elif sys.argv[1] in ['v', 'version']:
       
   293             print "%s: %s (Date: %s)\n" % (os.path.basename(sys.argv[0]),
       
   294                     __version__, __date__)
       
   295         else:
       
   296             sys.stderr.write('Unknown option: "%s"\n' % sys.argv[1])
       
   297             usage(EXIT.UNKNOWN_OPTION)
       
   298         showWarnings()
       
   299     except (EOFError, KeyboardInterrupt):
       
   300         sys.stderr.write('\nOuch!\n')
       
   301         sys.exit(EXIT.USER_INTERRUPT)
       
   302     except VMME.VMMException, e:
       
   303         sys.stderr.write("\aERROR: %s\n" % e[0][0])
       
   304         sys.exit(e[0][1])