VirtualMailManager/cli/subcommands.py
branchv0.6.x
changeset 445 b8c94e06cd46
parent 429 9842650569c2
child 446 18b75e6a39d5
equal deleted inserted replaced
444:95275b61ff8a 445:b8c94e06cd46
    13 
    13 
    14 from textwrap import TextWrapper
    14 from textwrap import TextWrapper
    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
       
    19 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
    18 from VirtualMailManager.cli import get_winsize, prog, w_err, w_std
    20 from VirtualMailManager.common import human_size, size_in_bytes, version_str
    19 from VirtualMailManager.common import human_size, size_in_bytes, version_str
    21 from VirtualMailManager.constants import __copyright__, __date__, \
    20 from VirtualMailManager.constants import __copyright__, __date__, \
    22      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    21      __version__, ACCOUNT_EXISTS, ALIAS_EXISTS, ALIASDOMAIN_ISDOMAIN, \
    23      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
    22      DOMAIN_ALIAS_EXISTS, INVALID_ARGUMENT, EX_MISSING_ARGS, RELOCATED_EXISTS
    24 from VirtualMailManager.errors import VMMError
    23 from VirtualMailManager.errors import VMMError
       
    24 from VirtualMailManager.serviceset import SERVICES
    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_quota',
    30     'domain_add', 'domain_delete',  'domain_info', 'domain_quota',
    31     'domain_transport', 'get_user', 'help_', 'list_domains', 'relocated_add',
    31     'domain_services', 'domain_transport', 'get_user', 'help_', 'list_domains',
    32     'relocated_delete', 'relocated_info', 'user_add', 'user_delete',
    32     'relocated_add', 'relocated_delete', 'relocated_info', 'user_add',
    33     'user_disable', 'user_enable', 'user_info', 'user_name', 'user_password',
    33     'user_delete', 'user_info', 'user_name', 'user_password', 'user_quota',
    34     'user_quota', 'user_transport', 'version',
    34     'user_services', 'user_transport', 'version',
    35 )
    35 )
    36 
    36 
    37 _ = lambda msg: msg
    37 _ = lambda msg: msg
    38 txt_wrpr = TextWrapper(width=get_winsize()[1] - 1)
    38 txt_wrpr = TextWrapper(width=get_winsize()[1] - 1)
    39 cmd_map = {}
    39 cmd_map = {}
   322                   ctx.scmd)
   322                   ctx.scmd)
   323         force = 'force'
   323         force = 'force'
   324     ctx.hdlr.domain_quotalimit(ctx.args[2].lower(), bytes_, messages, force)
   324     ctx.hdlr.domain_quotalimit(ctx.args[2].lower(), bytes_, messages, force)
   325 
   325 
   326 
   326 
       
   327 def domain_services(ctx):
       
   328     """allow all named service and block the uncredited."""
       
   329     if ctx.argc < 3:
       
   330         usage(EX_MISSING_ARGS, _(u'Missing domain name.'), ctx.scmd)
       
   331     services = []
       
   332     force = False
       
   333     if ctx.argc is 4:
       
   334         arg = ctx.args[3].lower()
       
   335         if arg in SERVICES:
       
   336             services.append(arg)
       
   337         elif arg == 'force':
       
   338             force = True
       
   339         else:
       
   340             usage(INVALID_ARGUMENT, _(u"Invalid argument: '%s'") % arg,
       
   341                   ctx.scmd)
       
   342     else:
       
   343         services.extend([service.lower() for service in ctx.args[3:-1]])
       
   344         arg = ctx.args[-1].lower()
       
   345         if arg == 'force':
       
   346             force = True
       
   347         else:
       
   348             services.append(arg)
       
   349         unknown = [service for service in services if service not in SERVICES]
       
   350         if unknown:
       
   351             usage(INVALID_ARGUMENT, _(u'Invalid service arguments: %s') %
       
   352                   ' '.join(unknown), ctx.scmd)
       
   353     ctx.hdlr.domain_services(ctx.args[2].lower(), (None, 'force')[force],
       
   354                              *services)
       
   355 
       
   356 
   327 def domain_transport(ctx):
   357 def domain_transport(ctx):
   328     """update the transport of the specified domain"""
   358     """update the transport of the specified domain"""
   329     if ctx.argc < 3:
   359     if ctx.argc < 3:
   330         usage(EX_MISSING_ARGS, _(u'Missing domain name and new transport.'),
   360         usage(EX_MISSING_ARGS, _(u'Missing domain name and new transport.'),
   331               ctx.scmd)
   361               ctx.scmd)
   454     elif ctx.args[3].lower() == 'force':
   484     elif ctx.args[3].lower() == 'force':
   455         ctx.hdlr.user_delete(ctx.args[2].lower(), True)
   485         ctx.hdlr.user_delete(ctx.args[2].lower(), True)
   456     else:
   486     else:
   457         usage(INVALID_ARGUMENT, _(u"Invalid argument: '%s'") % ctx.args[3],
   487         usage(INVALID_ARGUMENT, _(u"Invalid argument: '%s'") % ctx.args[3],
   458               ctx.scmd)
   488               ctx.scmd)
   459 
       
   460 
       
   461 def user_disable(ctx):
       
   462     """deactivate all/the given service(s) for a user"""
       
   463     if ctx.argc < 3:
       
   464         usage(EX_MISSING_ARGS, _(u'Missing e-mail address.'), ctx.scmd)
       
   465     elif ctx.argc < 4:
       
   466         ctx.hdlr.user_disable(ctx.args[2].lower())
       
   467     else:
       
   468         services = [service.lower() for service in ctx.args[3:]]
       
   469         unknown = [service for service in services if service not in SERVICES]
       
   470         if unknown:
       
   471             usage(INVALID_ARGUMENT, _(u"Invalid service arguments: %s") %
       
   472                   ' '.join(unknown), ctx.scmd)
       
   473         ctx.hdlr.user_disable(ctx.args[2].lower(), services)
       
   474 
       
   475 
       
   476 def user_enable(ctx):
       
   477     """activate all or the given service(s) for a user"""
       
   478     if ctx.argc < 3:
       
   479         usage(EX_MISSING_ARGS, _(u'Missing e-mail address.'), ctx.scmd)
       
   480     elif ctx.argc < 4:
       
   481         ctx.hdlr.user_enable(ctx.args[2].lower())
       
   482     else:
       
   483         services = [service.lower() for service in ctx.args[3:]]
       
   484         unknown = [service for service in services if service not in SERVICES]
       
   485         if unknown:
       
   486             usage(INVALID_ARGUMENT, _(u"Invalid service arguments: %s") %
       
   487                   ' '.join(unknown), ctx.scmd)
       
   488         ctx.hdlr.user_enable(ctx.args[2].lower(), services)
       
   489 
   489 
   490 
   490 
   491 def user_info(ctx):
   491 def user_info(ctx):
   492     """display information about the given address"""
   492     """display information about the given address"""
   493     if ctx.argc < 3:
   493     if ctx.argc < 3:
   571         except ValueError:
   571         except ValueError:
   572             usage(INVALID_ARGUMENT,
   572             usage(INVALID_ARGUMENT,
   573                   _(u"Not a valid number of messages: '%s'") % ctx.args[4],
   573                   _(u"Not a valid number of messages: '%s'") % ctx.args[4],
   574                   ctx.scmd)
   574                   ctx.scmd)
   575     ctx.hdlr.user_quotalimit(ctx.args[2].lower(), bytes_, messages)
   575     ctx.hdlr.user_quotalimit(ctx.args[2].lower(), bytes_, messages)
       
   576 
       
   577 
       
   578 def user_services(ctx):
       
   579     """allow all named service and block the uncredited."""
       
   580     if ctx.argc < 3:
       
   581         usage(EX_MISSING_ARGS, _(u'Missing e-mail address.'), ctx.scmd)
       
   582     services = []
       
   583     if ctx.argc >= 4:
       
   584         services.extend([service.lower() for service in ctx.args[3:]])
       
   585         unknown = [service for service in services if service not in SERVICES]
       
   586         if unknown:
       
   587             usage(INVALID_ARGUMENT, _(u'Invalid service arguments: %s') %
       
   588                   ' '.join(unknown), ctx.scmd)
       
   589     ctx.hdlr.user_services(ctx.args[2].lower(), *services)
   576 
   590 
   577 
   591 
   578 def user_transport(ctx):
   592 def user_transport(ctx):
   579     """update the transport of the given address"""
   593     """update the transport of the given address"""
   580     if ctx.argc < 3:
   594     if ctx.argc < 3:
   638     'useradd': cmd('useradd', 'ua', user_add, _(u'address [password]'),
   652     'useradd': cmd('useradd', 'ua', user_add, _(u'address [password]'),
   639                    _(u'create a new e-mail user with the given address')),
   653                    _(u'create a new e-mail user with the given address')),
   640     'userdelete': cmd('userdelete', 'ud', user_delete,
   654     'userdelete': cmd('userdelete', 'ud', user_delete,
   641                       _(u'address') + ' [force]',
   655                       _(u'address') + ' [force]',
   642                       _(u'delete the specified user')),
   656                       _(u'delete the specified user')),
   643     'userdisable': cmd('userdisable', 'u0', user_disable,
       
   644                        _(u'address [service ...]'),
       
   645                        _(u'deactivate all/the given service(s) for a user')),
       
   646     'userenable': cmd('userenable', 'u1', user_enable,
       
   647                       _(u'address [service ...]'),
       
   648                       _(u'activate all or the given service(s) for a user')),
       
   649     'userinfo': cmd('userinfo', 'ui', user_info, _(u'address [details]'),
   657     'userinfo': cmd('userinfo', 'ui', user_info, _(u'address [details]'),
   650                     _(u'display information about the given address')),
   658                     _(u'display information about the given address')),
   651     'username': cmd('username', 'un', user_name, _(u'address name'),
   659     'username': cmd('username', 'un', user_name, _(u'address name'),
   652                     _(u'set or update the real name for an address')),
   660                     _(u'set or update the real name for an address')),
   653     'userpassword': cmd('userpassword', 'up', user_password,
   661     'userpassword': cmd('userpassword', 'up', user_password,
   654                         _(u'address [password]'),
   662                         _(u'address [password]'),
   655                         _(u'update the password for the given address')),
   663                         _(u'update the password for the given address')),
   656     'userquota': cmd('userquota', 'uq', user_quota,
   664     'userquota': cmd('userquota', 'uq', user_quota,
   657                      _(u'address storage [messages]'),
   665                      _(u'address storage [messages]'),
   658                      _(u'update the quota limit for the given address')),
   666                      _(u'update the quota limit for the given address')),
       
   667     'userservices': cmd('userservices', 'us', user_services,
       
   668                         _(u'address [service ...]'),
       
   669                         _(u'enables the specified services and disables all '
       
   670                           u'not specified services')),
   659     'usertransport': cmd('usertransport', 'ut', user_transport,
   671     'usertransport': cmd('usertransport', 'ut', user_transport,
   660                          _(u'address transport'),
   672                          _(u'address transport'),
   661                          _(u'update the transport of the given address')),
   673                          _(u'update the transport of the given address')),
   662     # Alias commands
   674     # Alias commands
   663     'aliasadd': cmd('aliasadd', 'aa', alias_add, _(u'address destination ...'),
   675     'aliasadd': cmd('aliasadd', 'aa', alias_add, _(u'address destination ...'),
   691     'domaininfo': cmd('domaininfo', 'di', domain_info, _(u'fqdn [details]'),
   703     'domaininfo': cmd('domaininfo', 'di', domain_info, _(u'fqdn [details]'),
   692                       _(u'display information about the given domain')),
   704                       _(u'display information about the given domain')),
   693     'domainquota': cmd('domainquota', 'dq', domain_quota,
   705     'domainquota': cmd('domainquota', 'dq', domain_quota,
   694                        _(u'fqdn storage [messages]') + ' [force]',
   706                        _(u'fqdn storage [messages]') + ' [force]',
   695                        _(u'update the quota limit of the specified domain')),
   707                        _(u'update the quota limit of the specified domain')),
       
   708     'domainservices': cmd('domainservices', 'ds', domain_services,
       
   709                           _(u'fqdn [service ...]') + ' [force]',
       
   710                         _(u'enables the specified services and disables all '
       
   711                           u'not specified services of the given domain')),
   696     'domaintransport': cmd('domaintransport', 'dt', domain_transport,
   712     'domaintransport': cmd('domaintransport', 'dt', domain_transport,
   697                            _(u'fqdn transport') + ' [force]',
   713                            _(u'fqdn transport') + ' [force]',
   698                            _(u'update the transport of the specified domain')),
   714                            _(u'update the transport of the specified domain')),
   699     'listdomains': cmd('listdomains', 'ld', list_domains, _(u'[pattern]'),
   715     'listdomains': cmd('listdomains', 'ld', list_domains, _(u'[pattern]'),
   700                        _(u'list all domains / search domains by pattern')),
   716                        _(u'list all domains / search domains by pattern')),