* 'VirtualMailManager/Exceptions.py'
- Fixed AttributeError in class VMMException on older Python installations.
(VMMException instance has no attribute 'message')
* 'vmm'
- let show_version() print more details
#!/usr/bin/env python# -*- coding: UTF-8 -*-# Copyright 2007-2008 VEB IT# See COPYING for distribution information.# $Id$"""This is the vmm main script."""fromVirtualMailManager.constants.VERSIONimportVERSION__author__='Pascal Volk <p.volk@veb-it.de>'__version__=VERSION__revision__='rev '+'$Rev$'.split()[1]__date__='$Date$'.split()[1]importlocaleimportosimportsysimportgettextfromtimeimportstrftime,strptimefromVirtualMailManager.VirtualMailManagerimportVirtualMailManagerimportVirtualMailManager.ExceptionsasVMMEimportVirtualMailManager.constants.EXITasEXITimportVirtualMailManager.constants.ERRORasERRdefw_err(code,*args):forarginargs:sys.stderr.write(arg.encode(ENCODING,'replace'))sys.stderr.write('\n')sys.exit(code)defw_std(*args):forarginargs:sys.stdout.write(arg.encode(ENCODING,'replace'))sys.stdout.write('\n')defusage(excode=0,errMsg=None):u_head=_("""\Usage: %s SUBCOMMAND OBJECT ARGS* short long subcommand object args (* = optional)\n""")\%__prog__u_body="""\ da domainadd domain.tld transport* di domaininfo domain.tld detailed* dt domaintransport domain.tld transport force* dd domaindelete domain.tld delalias*|deluser*|delall* ada aliasdomainadd aliasdomain.tld domain.tld adi aliasdomaininfo aliasdomain.tld add aliasdomaindelete aliasdomain.tld ua useradd user@domain.tld password* ui userinfo user@domain.tld du* un username user@domain.tld 'Users Name' up userpassword user@domain.tld password* ut usertransport user@domain.tld transport u0 userdisable user@domain.tld smtp*|pop3*|imap*|managesieve*|all* u1 userenable user@domain.tld smtp*|pop3*|imap*|managesieve*|all* ud userdelete user@domain.tld aa aliasadd alias@domain.tld user@domain.tld ai aliasinfo alias@domain.tld ad aliasdelete alias@domain.tld user@domain.tld* gu getuser userid ld listdomains pattern* cf configure section* h help v version"""ifexcode>0:iferrMsgisNone:w_err(excode,u_head,u_body)else:w_err(excode,u_head,u_body,'%s: %s\n'%(_('Error'),errMsg))else:w_std(u_head,u_body)sys.exit(excode)defget_vmm():try:vmm=VirtualMailManager()returnvmmexcept(VMME.VMMException,VMME.VMMNotRootException,VMME.VMMPermException,VMME.VMMConfigException),e:w_err(e.code(),"%s: %s\n"%(_('Error'),e.msg()))def_getOrder():order=()ifargv[1]in['di','domaininfo']:order=(('domainname',0),('gid',1),('transport',0),('domaindir',0),('aliasdomains',0),('accounts',0),('aliases',0))elifargv[1]in['ui','userinfo']:ifargc==4orvmm.cfgGetBoolean('maildir','diskusage'):order=(('address',0),('name',0),('uid',1),('gid',1),('transport',0),('maildir',0),('disk usage',0),('smtp',1),('pop3',1),('imap',1),('managesieve',1))else:order=(('address',0),('name',0),('uid',1),('gid',1),('transport',0),('maildir',0),('smtp',1),('pop3',1),('imap',1),('managesieve',1))elifargv[1]in['gu','getuser']:order=(('uid',1),('gid',1),('address',0))returnorderdef_printInfo(info,title):msg='%s%s'%(title,_('information'))w_std('%s\n%s'%(msg,'-'*len(msg)))fork,uin_getOrder():ifu:w_std('\t%s: %s'%(k.upper().ljust(15,'.'),info[k]))else:w_std('\t%s: %s'%(k.title().ljust(15,'.'),info[k]))printdef_printList(alist,title):msg='%s%s'%(_('Available'),title)w_std('%s\n%s'%(msg,'-'*len(msg)))iflen(alist)>0:iftitle!=_('alias domains'):forvalinalist:w_std('\t%s'%val)else:fordominalist:ifnotdom.startswith('xn--'):w_std('\t%s'%dom)else:w_std('\t%s (%s)'%(dom,vmm.ace2idna(dom)))else:w_std(_('\tNone'))printdef_printAliases(alias,targets):msg=_('Alias information')w_std('%s\n%s'%(msg,'-'*len(msg)))w_std(_('\tMail for %s will be redirected to:')%alias)iflen(targets)>0:fortargetintargets:w_std('\t * %s'%target)else:w_std(_('\tNone'))printdef_formatDom(domain,main=True):ifdomain.startswith('xn--'):domain='%s (%s)'%(domain,vmm.ace2idna(domain))ifmain:return'\t[+] %s'%domainelse:return'\t[-] %s'%domaindef_printDomList(dids,domains):ifargc<3:msg=_('Available domains')else:msg=_('Matching domains')w_std('%s\n%s'%(msg,'-'*len(msg)))ifnotlen(domains):w_std(_('\tNone'))else:foridindids:ifdomains[id][0]isnotNone:w_std(_formatDom(domains[id][0]))iflen(domains[id])>1:foraliasindomains[id][1:]:w_std(_formatDom(alias,main=False))printdef_printAliasDomInfo(info):msg=_('Alias domain information')forkin['alias','domain']:ifinfo[k].startswith('xn--'):info[k]="%s (%s)"%(info[k],vmm.ace2idna(info[k]))w_std('%s\n%s'%(msg,'-'*len(msg)))w_std(_('\tThe alias domain %(alias)s belongs to:\n\t * %(domain)s')%info)printdefconfigure():ifneed_setuporlen(argv)<3:vmm.configure()else:vmm.configure(argv[2])defdomain_add():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing domain name.'))elifargc<4:vmm.domainAdd(argv[2].lower())else:vmm.domainAdd(argv[2].lower(),argv[3])defdomain_delete():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing domain name.'))elifargc<4:vmm.domainDelete(argv[2].lower())else:vmm.domainDelete(argv[2].lower(),argv[3])defdomain_info():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing domain name.'))elifargc<4:_printInfo(vmm.domainInfo(argv[2].lower()),_('Domain'))else:infos=vmm.domainInfo(argv[2].lower(),argv[3])_printInfo(infos[0],_('Domain'))_printList(infos[1],_('alias domains'))_printList(infos[2],_('accounts'))_printList(infos[3],_('aliases'))defdomain_transport():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing domain name and new transport.'))ifargc<4:usage(EXIT.MISSING_ARGS,_(u'Missing new transport.'))elifargc<5:vmm.domainTransport(argv[2].lower(),argv[3])else:vmm.domainTransport(argv[2].lower(),argv[3],argv[4])defalias_domain_add():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing alias domain name and target domain name.'))elifargc<4:usage(EXIT.MISSING_ARGS,_(u'Missing target domain name.'))else:vmm.aliasDomainAdd(argv[2].lower(),argv[3].lower())defalias_domain_info():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing alias domain name.'))else:_printAliasDomInfo(vmm.aliasDomainInfo(argv[2].lower()))defalias_domain_delete():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing alias domain name.'))else:vmm.aliasDomainDelete(argv[2].lower())defuser_add():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))elifargc<4:password=Noneelse:password=argv[3]vmm.userAdd(argv[2].lower(),password)defuser_delete():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))else:vmm.userDelete(argv[2].lower())defuser_info():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))elifargc<4:_printInfo(vmm.userInfo(argv[2].lower()),'Account')else:_printInfo(vmm.userInfo(argv[2].lower(),True),'Account')defuser_name():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address and users name.'))ifargc<4:usage(EXIT.MISSING_ARGS,_('Missing users name.'))else:vmm.userName(argv[2].lower(),argv[3])defuser_transport():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address and transport.'))ifargc<4:usage(EXIT.MISSING_ARGS,_(u'Missing transport.'))else:vmm.userTransport(argv[2].lower(),argv[3])defuser_enable():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))elifargc<4:vmm.userEnable(argv[2].lower())else:vmm.userEnable(argv[2].lower(),argv[3].lower())defuser_disable():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))elifargc<4:vmm.userDisable(argv[2].lower())else:vmm.userDisable(argv[2].lower(),argv[3].lower())defuser_password():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing e-mail address.'))elifargc<4:password=Noneelse:password=argv[3]vmm.userPassword(argv[2].lower(),password)defalias_add():ifargc<4:usage(EXIT.MISSING_ARGS,_(u'Missing alias address and destination.'))else:vmm.aliasAdd(argv[2].lower(),argv[3])defalias_info():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing alias address'))else:_printAliases(argv[2],vmm.aliasInfo(argv[2].lower()))defalias_delete():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing alias address'))elifargc<4:vmm.aliasDelete(argv[2].lower())else:vmm.aliasDelete(argv[2].lower(),argv[3].lower())defuser_byID():ifargc<3:usage(EXIT.MISSING_ARGS,_(u'Missing userid'))else:_printInfo(vmm.userByID(argv[2]),u'Account')defdomain_list():ifargc<3:order,doms=vmm.domainList()else:order,doms=vmm.domainList(argv[2].lower())_printDomList(order,doms)defshow_warnings():ifvmm.hasWarnings():w_std(_(u'Warnings:'))forwarninginvmm.getWarnings():w_std(" * %s"%warning)defshow_version():w_std("%s, %s%s (%s%s%s)\nPython %s%s%s\n"%(__prog__,_('version'),__version__,__revision__,_('from'),strftime(locale.nl_langinfo(locale.D_FMT),strptime(__date__,'%Y-%m-%d')),sys.version.split()[0],_('on'),sys.platform.title()))#def main():if__name__=='__main__':__prog__=os.path.basename(sys.argv[0])locale.setlocale(locale.LC_ALL,'')ENCODING=locale.nl_langinfo(locale.CODESET)gettext.install(__prog__,'/usr/local/share/locale',unicode=1)argv=[unicode(arg,ENCODING)forarginsys.argv]argc=len(sys.argv)ifargc<2:usage(EXIT.MISSING_ARGS)vmm=get_vmm()try:need_setup=notvmm.setupIsDone()ifargv[1]in['cf','configure']orneed_setup:configure()elifargv[1]in['da','domainadd']:domain_add()elifargv[1]in['di','domaininfo']:domain_info()elifargv[1]in['dt','domaintransport']:domain_transport()elifargv[1]in['dd','domaindelete']:domain_delete()elifargv[1]in['ada','aliasdomainadd']:alias_domain_add()elifargv[1]in['adi','aliasdomaininfo']:alias_domain_info()elifargv[1]in['add','aliasdomaindelete']:alias_domain_delete()elifargv[1]in['ua','useradd']:user_add()elifargv[1]in['ui','userinfo']:user_info()elifargv[1]in['un','username']:user_name()elifargv[1]in['up','userpassword']:user_password()elifargv[1]in['ut','usertransport']:user_transport()elifargv[1]in['u0','userdisable']:user_disable()elifargv[1]in['u1','userenable']:user_enable()elifargv[1]in['ud','userdelete']:user_delete()elifargv[1]in['aa','aliasadd']:alias_add()elifargv[1]in['ai','aliasinfo']:alias_info()elifargv[1]in['ad','aliasdelete']:alias_delete()elifargv[1]in['gu','getuser']:user_byID()elifargv[1]in['ld','listdomains']:domain_list()elifargv[1]in['h','help']:usage()elifargv[1]in['v','version']:show_version()else:usage(EXIT.UNKNOWN_COMMAND,u"%s: »%s«"%(_('Unknown subcommand'),argv[1]))show_warnings()except(EOFError,KeyboardInterrupt):w_err(EXIT.USER_INTERRUPT,'\n%s!\n'%_(u'Ouch'))except(VMME.VMMConfigException,VMME.VMMException),e:w_err(e.code(),"%s: %s"%(_(u'Error'),e.msg()))