VirtualMailManager/cli/subcommands.py
branchv0.6.x
changeset 403 a4f5d4cd886d
parent 401 00a8c12a3da3
child 406 58e23bd7c97f
equal deleted inserted replaced
402:8984b1f4e6e3 403:a4f5d4cd886d
    15 from time import strftime, strptime
    15 from time import strftime, strptime
    16 
    16 
    17 from VirtualMailManager import ENCODING
    17 from VirtualMailManager import ENCODING
    18 from VirtualMailManager.account import SERVICES
    18 from VirtualMailManager.account import SERVICES
    19 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
    19 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
    20 from VirtualMailManager.common import human_size, version_str
    20 from VirtualMailManager.common import human_size, size_in_bytes, version_str
    21 from VirtualMailManager.constants import __copyright__, __date__, \
    21 from VirtualMailManager.constants import __copyright__, __date__, \
    22      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    22      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    23      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
    23      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
    24 from VirtualMailManager.errors import VMMError
    24 from VirtualMailManager.errors import VMMError
    25 
    25 
    26 __all__ = (
    26 __all__ = (
    27     'Command', 'RunContext', 'cmd_map', 'usage', 'alias_add', 'alias_delete',
    27     'Command', 'RunContext', 'cmd_map', 'usage', 'alias_add', 'alias_delete',
    28     'alias_info', 'aliasdomain_add', 'aliasdomain_delete', 'aliasdomain_info',
    28     'alias_info', 'aliasdomain_add', 'aliasdomain_delete', 'aliasdomain_info',
    29     'aliasdomain_switch', 'config_get', 'config_set', 'configure',
    29     'aliasdomain_switch', 'config_get', 'config_set', 'configure',
    30     'domain_add', 'domain_delete',  'domain_info', 'domain_transport',
    30     'domain_add', 'domain_delete',  'domain_info', 'domain_quota',
    31     'get_user', 'help_', 'list_domains', 'relocated_add', 'relocated_delete',
    31     'domain_transport', 'get_user', 'help_', 'list_domains', 'relocated_add',
    32     'relocated_info', 'user_add', 'user_delete', 'user_disable', 'user_enable',
    32     'relocated_delete', 'relocated_info', 'user_add', 'user_delete',
    33     'user_info', 'user_name', 'user_password', 'user_transport', 'version',
    33     'user_disable', 'user_enable', 'user_info', 'user_name', 'user_password',
       
    34     'user_quota', 'user_transport', 'version',
    34 )
    35 )
    35 
    36 
    36 _ = lambda msg: msg
    37 _ = lambda msg: msg
    37 txt_wrpr = TextWrapper(width=get_winsize()[1] - 1)
    38 txt_wrpr = TextWrapper(width=get_winsize()[1] - 1)
    38 
    39 
   277                 _print_list(info[2], _(u'accounts'))
   278                 _print_list(info[2], _(u'accounts'))
   278                 _print_list(info[3], _(u'aliases'))
   279                 _print_list(info[3], _(u'aliases'))
   279                 _print_list(info[4], _(u'relocated users'))
   280                 _print_list(info[4], _(u'relocated users'))
   280 
   281 
   281 
   282 
       
   283 def domain_quota(ctx):
       
   284     """update the quota limit of the specified domain"""
       
   285     if ctx.argc < 3:
       
   286         usage(EX_MISSING_ARGS, _(u'Missing domain name and storage value.'),
       
   287               ctx.scmd)
       
   288     if ctx.argc < 4:
       
   289         usage(EX_MISSING_ARGS, _(u'Missing storage value.'), ctx.scmd)
       
   290     messages = 0
       
   291     force = None
       
   292     try:
       
   293         bytes_ = size_in_bytes(ctx.args[3])
       
   294     except (ValueError, TypeError):
       
   295         usage(INVALID_ARGUMENT, _(u"Invalid storage value: '%s'") %
       
   296               ctx.args[3], ctx.scmd)
       
   297     if ctx.argc < 5:
       
   298         pass
       
   299     elif ctx.argc < 6:
       
   300         try:
       
   301             messages = int(ctx.args[4])
       
   302         except ValueError:
       
   303             if ctx.args[4].lower() != 'force':
       
   304                 usage(INVALID_ARGUMENT,
       
   305                       _(u"Neither a valid number of messages nor the keyword "
       
   306                         u"'force': '%s'") % ctx.args[4], ctx.scmd)
       
   307             force = 'force'
       
   308     else:
       
   309         try:
       
   310             messages = int(ctx.args[4])
       
   311         except ValueError:
       
   312             usage(INVALID_ARGUMENT,
       
   313                   _(u"Not a valid number of messages: '%s'") % ctx.args[4],
       
   314                   ctx.scmd)
       
   315         if ctx.args[5].lower() != 'force':
       
   316             usage(INVALID_ARGUMENT, _(u"Invalid argument: '%s'") % ctx.args[5],
       
   317                   ctx.scmd)
       
   318         force = 'force'
       
   319     ctx.hdlr.domain_quotalimit(ctx.args[2].lower(), bytes_, messages, force)
       
   320 
       
   321 
   282 def domain_transport(ctx):
   322 def domain_transport(ctx):
   283     """update the transport of the specified domain"""
   323     """update the transport of the specified domain"""
   284     if ctx.argc < 3:
   324     if ctx.argc < 3:
   285         usage(EX_MISSING_ARGS, _(u'Missing domain name and new transport.'),
   325         usage(EX_MISSING_ARGS, _(u'Missing domain name and new transport.'),
   286               ctx.scmd)
   326               ctx.scmd)
   302         usage(EX_MISSING_ARGS, _(u'Missing userid.'), ctx.scmd)
   342         usage(EX_MISSING_ARGS, _(u'Missing userid.'), ctx.scmd)
   303     _print_info(ctx, ctx.hdlr.user_by_uid(ctx.args[2]), _(u'Account'))
   343     _print_info(ctx, ctx.hdlr.user_by_uid(ctx.args[2]), _(u'Account'))
   304 
   344 
   305 
   345 
   306 def help_(ctx):
   346 def help_(ctx):
   307     """print help messgaes."""
   347     """print help messages."""
   308     if ctx.argc > 2:
   348     if ctx.argc > 2:
   309         hlptpc = ctx.args[2].lower()
   349         hlptpc = ctx.args[2].lower()
   310         if hlptpc in cmd_map:
   350         if hlptpc in cmd_map:
   311             topic = hlptpc
   351             topic = hlptpc
   312         else:
   352         else:
   502     elif ctx.argc < 4:
   542     elif ctx.argc < 4:
   503         password = None
   543         password = None
   504     else:
   544     else:
   505         password = ctx.args[3]
   545         password = ctx.args[3]
   506     ctx.hdlr.user_password(ctx.args[2].lower(), password)
   546     ctx.hdlr.user_password(ctx.args[2].lower(), password)
       
   547 
       
   548 
       
   549 def user_quota(ctx):
       
   550     """update the quota limit for the given address"""
       
   551     if ctx.argc < 3:
       
   552         usage(EX_MISSING_ARGS, _(u'Missing e-mail address and storage value.'),
       
   553               ctx.scmd)
       
   554     elif ctx.argc < 4:
       
   555         usage(EX_MISSING_ARGS, _(u'Missing storage value.'), ctx.scmd)
       
   556     try:
       
   557         bytes_ = size_in_bytes(ctx.args[3])
       
   558     except (ValueError, TypeError):
       
   559         usage(INVALID_ARGUMENT, _(u"Invalid storage value: '%s'") %
       
   560               ctx.args[3], ctx.scmd)
       
   561     if ctx.argc < 5:
       
   562         messages = 0
       
   563     else:
       
   564         try:
       
   565             messages = int(ctx.args[4])
       
   566         except ValueError:
       
   567             usage(INVALID_ARGUMENT,
       
   568                   _(u"Not a valid number of messages: '%s'") % ctx.args[4],
       
   569                   ctx.scmd)
       
   570     ctx.hdlr.user_quotalimit(ctx.args[2].lower(), bytes_, messages)
   507 
   571 
   508 
   572 
   509 def user_transport(ctx):
   573 def user_transport(ctx):
   510     """update the transport of the given address"""
   574     """update the transport of the given address"""
   511     if ctx.argc < 3:
   575     if ctx.argc < 3:
   577     'username': cmd('username', 'un', user_name, 'address name',
   641     'username': cmd('username', 'un', user_name, 'address name',
   578                     _(u'set or update the real name for an address')),
   642                     _(u'set or update the real name for an address')),
   579     'userpassword': cmd('userpassword', 'up', user_password,
   643     'userpassword': cmd('userpassword', 'up', user_password,
   580                         'address [password]',
   644                         'address [password]',
   581                         _(u'update the password for the given address')),
   645                         _(u'update the password for the given address')),
       
   646     'userquota': cmd('userquota', 'uq', user_quota,
       
   647                      'address storage [messages]',
       
   648                      _(u'update the quota limit for the given address')),
   582     'usertransport': cmd('usertransport', 'ut', user_transport,
   649     'usertransport': cmd('usertransport', 'ut', user_transport,
   583                          'address transport',
   650                          'address transport',
   584                          _(u'update the transport of the given address')),
   651                          _(u'update the transport of the given address')),
   585     # Alias commands
   652     # Alias commands
   586     'aliasadd': cmd('aliasadd', 'aa', alias_add, 'address destination ...',
   653     'aliasadd': cmd('aliasadd', 'aa', alias_add, 'address destination ...',
   607                      _(u'create a new domain')),
   674                      _(u'create a new domain')),
   608     'domaindelete': cmd('domaindelete', 'dd', domain_delete, 'fqdn [force]',
   675     'domaindelete': cmd('domaindelete', 'dd', domain_delete, 'fqdn [force]',
   609                       _(u'delete the given domain and all its alias domains')),
   676                       _(u'delete the given domain and all its alias domains')),
   610     'domaininfo': cmd('domaininfo', 'di', domain_info, 'fqdn [details]',
   677     'domaininfo': cmd('domaininfo', 'di', domain_info, 'fqdn [details]',
   611                       _(u'display information about the given domain')),
   678                       _(u'display information about the given domain')),
       
   679     'domainquota': cmd('domainquota', 'dq', domain_quota,
       
   680                        'fqdn storage [messages] [force]',
       
   681                        _(u'update the quota limit of the specified domain')),
   612     'domaintransport': cmd('domaintransport', 'dt', domain_transport,
   682     'domaintransport': cmd('domaintransport', 'dt', domain_transport,
   613                            'fqdn transport [force]',
   683                            'fqdn transport [force]',
   614                            _(u'update the transport of the specified domain')),
   684                            _(u'update the transport of the specified domain')),
   615     'listdomains': cmd('listdomains', 'ld', list_domains, '[pattern]',
   685     'listdomains': cmd('listdomains', 'ld', list_domains, '[pattern]',
   616                        _(u'list all domains / search domains by pattern')),
   686                        _(u'list all domains / search domains by pattern')),