#! @PYTHON@
#
# Copyright (C) 2002-2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.

"""List the owners of a mailing list, or all mailing lists.

Usage: %(PROGRAM)s [options] [listname ...]
Options:

    -w / --with-listnames
        Group the owners by list names and include the list names in the
        output.  Otherwise, the owners will be sorted and uniquified based on
        the email address.

    -m / --moderators
        Include the list moderators in the output.

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

    listname
        Print the owners of the specified lists.  More than one can appear
        after the options.  If there are no listnames provided, the owners of
        all the lists will be displayed.
"""

import sys
import getopt

import paths
from Mailman import Utils
from Mailman.MailList import MailList
from Mailman.i18n import C_

PROGRAM = sys.argv[0]

try:
    True, False
except NameError:
    True = 1
    False = 0


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



def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'wmh',
                                   ['with-listnames', 'moderators', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    withnames = moderators = False
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-m', '--moderators'):
            moderators = True
        elif opt in ('-w', '--with-listnames'):
            withnames = True

    listnames = [x.lower() for x in args] or Utils.list_names()
    bylist = {}

    for listname in listnames:
        mlist = MailList(listname, lock=0)
        addrs = mlist.owner[:]
        if moderators:
            addrs.extend(mlist.moderator)
        bylist[listname] = addrs

    if withnames:
        for listname in listnames:
            unique = {}
            for addr in bylist[listname]:
                unique[addr] = 1
            keys = unique.keys()
            keys.sort()
            print listname
            for k in keys:
                print '\t', k
    else:
        unique = {}
        for listname in listnames:
            for addr in bylist[listname]:
                unique[addr] = 1
        keys = unique.keys()
        keys.sort()
        for k in keys:
            print k



if __name__ == '__main__':
    main()
