#! /usr/bin/python
#
# Copyright (C) 2004 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Change a list's maximum number of members.

Usage: change_maxmembers [options]

Options:

    --all / -a
        Change the member limit for all lists.

    --domain=domain
    -d domain
        Change the member limit for all lists in the virtual domain `domain'.  It
        is okay to give multiple -d options.

    --listname=listname
    -l listname
        Change/Print (see below) the member limit only for the named list.  It is 
	okay to give multiple -l options.

    --max_members=max_members
    -m max_members
        Use the supplied `max_members` as the maximum number of members 
	than can be subscribed on the list(s).
	
    --show_limit	
    -s
        Print the maximum number of members for the named list(s).
	 
    --help / -h
        Print this help message and exit.

The options -m and -s are mutually exclusive.	
"""

import sys
import getopt

import paths
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman import i18n

_ = i18n._

SPACE = ' '



def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, _(__doc__)
    if msg:
        print >> fd, msg
    sys.exit(code)



_listcache = {}

def openlist(listname):
    missing = []
    mlist = _listcache.get(listname, missing)
    if mlist is missing:
        try:
            mlist = MailList.MailList(listname, lock=0)
        except Errors.MMListError, e:
            usage(1, _('No such list "%(listname)s"\n%(e)s'))
        _listcache[listname] = mlist
    return mlist



def main():
    # Parse options
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'ad:l:m:sh',
            ['all', 'domain=', 'listname=', 'max_members=', 'show_limit', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    # defaults
    listnames = {}
    domains = {}
    max_members = mm_cfg.DEFAULT_MAX_MEMBERS
    show_limit = 0
    
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-a', '--all'):
            for name in Utils.list_names():
                listnames[name] = 1
        elif opt in ('-d', '--domain'):
            domains[arg] = 1
        elif opt in ('-l', '--listname'):
            listnames[arg] = 1
        elif opt in ('-m', '--max_members'):
	    try:
                max_members = int(arg)
	    except ValueError:
	        usage(1)
	elif opt in ('-s', '--show_limit'):
	    show_limit = 1

    if max_members is not mm_cfg.DEFAULT_MAX_MEMBERS and show_limit is not 0:
        usage(1)
    
    if args:
        strargs = SPACE.join(args)
        usage(1, _('Bad arguments: %(strargs)s'))
    
    if domains:
        for name in Utils.list_names():
            mlist = openlist(name)
            if domains.has_key(mlist.host_name):
                listnames[name] = 1

    if not listnames:
        print >> sys.stderr, _('Nothing to do.')
        sys.exit(0)

    for listname in listnames.keys():
        mlist = openlist(listname)
        mlist.Lock()
        try:
	    if show_limit is not 0:
	        max_members = mlist.max_members
	        print _('%(listname)s limit of members: %(max_members)d')
	    else:
                mlist.max_members = max_members
	        mlist.Save()
                print _('New %(listname)s limit of members: %(max_members)d')
        finally:
            mlist.Unlock()



if __name__ == '__main__':
    main()
