#! /usr/bin/python
#
# Copyright (C) 2001,2002 by the Free Software Foundation, Inc.
# Portions Copyright (C) 2004 by Florian Weimer.
#
# 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 the passwords of list members.

This script resets all passwords of list members.  This is helpful if
the passwords have been compromised.

Usage: change_pw [options]

Options:

    --all / -a
        Change the password for all lists.

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

    --listname=listname
    -l listname
        Change the password only for the named list.  It is okay to give
        multiple -l options.

    --help / -h
        Print this help message and exit.
"""

import sys
import sha
import getopt

import paths
from Mailman import mm_cfg
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman import Message
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:h',
            ['all', 'domain=', 'listname=', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    # defaults
    listnames = {}
    domains = {}
    password = None
    quiet = 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

    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)

    # Set the member passwords.
    for listname in listnames.keys():
        mlist = openlist(listname)
        mlist.Lock()
        try:
            for address in mlist.getMembers():
                randompw = Utils.MakeRandomPassword()
                mlist.setMemberPassword(address, randompw)

            mlist.Save()
        finally:
            mlist.Unlock()


if __name__ == '__main__':
    main()
