#! /usr/bin/env python
#
# Copyright (C) 1998,1999,2000 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.

"""Create a new, unpopulated mailing list.

  newlist <list-name> <list-admin's-address> <admin-password> <immediate>

You can specify as many of the arguments as you want on the command line. 
The optional <immediate> argument, if present, means to send out the notice 
immediately.  Otherwise, the script hangs pending input, to give time for
the person creating the list to customize it before sending the admin an
email notice about the existence of the new list.

Note that list-names are forced to lowercase.
"""

import sys
import os
import string
import time

import paths
from Mailman import mm_cfg
from Mailman import MailList
from Mailman import Utils
from Mailman import Errors
from Mailman import Message
from Mailman.Handlers import HandlerAPI
from Mailman.Crypt import crypt
from Mailman.pythonlib import getpass


ALIASTEMPLATE = '''
Entry for aliases file:

## %(listname)s mailing list
## created: %(date)s %(user)s
%(list)s "|%(wrapper)s post %(listname)s"
%(admin)s "|%(wrapper)s mailowner %(listname)s"
%(request)s "|%(wrapper)s mailcmd %(listname)s"
%(owner2)s %(listname)s-admin
'''



def getusername():
    username = os.environ.get('USER') or os.environ.get('LOGNAME')
    if not username:
        import pwd
        username = pwd.getpwuid(os.getuid())[0]
    if not username:
        username = '<unknown>'
    return username



def usage(code, msg=''):
    print __doc__
    if msg:
        print msg
    sys.exit(code)



def main(argv):
    if len(argv) > 1:
	listname = argv[1]
    else:
	listname = raw_input("Enter the name of the list: ")
    listname = string.lower(listname)

    if '@' in listname:
        usage(1, 'List name must not include "@": ' + listname)

    if Utils.list_exists(listname):
        usage(1, 'List already exists: ' + listname)

    if len(argv) > 2:
	owner_mail = argv[2]
    else:
	owner_mail = raw_input(
	    "Enter the email of the person running the list: ")
    if len(argv) > 3:
	list_pw = argv[3]
    else:
        list_pw = getpass.getpass("Initial %s password: " % listname)

    mlist = MailList.MailList()
    try:
        pw = crypt(list_pw , Utils.GetRandomSeed())
        # guarantee that all newly created files have the proper permission.
        # proper group ownership should be assured by the autoconf script
        # enforcing that all directories have the group sticky bit set
        oldmask = os.umask(002)
        try:
            try:
                mlist.Create(listname, owner_mail, pw)
            finally:
                os.umask(oldmask)
        except Errors.MMBadEmailError:
            usage(1, 'Bad owner email address: ' + owner_mail)
        except Errors.MMListAlreadyExistsError:
            usage(1, 'List already exists: ' + listname)

        print ALIASTEMPLATE % {
            'listname': listname,
            'list'    : "%-24s" % (listname + ":"),
            'wrapper' : '%s/wrapper' % mm_cfg.WRAPPER_DIR,
            'admin'   : "%-24s" % ("%s-admin:" % listname),
            'request' : "%-24s" % ("%s-request:" % listname),
            'owner2'  : "%-24s" % ("%s-owner:" % listname),
            'date'    : time.strftime('%d-%b-%Y', time.localtime(time.time())),
            'user'    : getusername(),
            }

        if len(argv) < 5:
            print ("Hit enter to continue with %s owner notification..."
                   % listname),
            sys.stdin.readline()
        # send the notice to the list owner
        text = Utils.maketext(
            'newlist.txt',
            {'listname'    : listname,
             'password'    : list_pw, 
             'admin_url'   : mlist.GetScriptURL('admin', absolute=1), 
             'listinfo_url': mlist.GetScriptURL('listinfo', absolute=1),
             'requestaddr' : "%s-request@%s" % (listname, mlist.host_name),
             'hostname'    : mlist.host_name,
             })
        msg = Message.UserNotification(owner_mail,
                                       'mailman-owner@' + mlist.host_name,
                                       'Your new mailing list: ' + listname,
                                       text)
        HandlerAPI.DeliverToUser(mlist, msg)
    finally:
        mlist.Unlock()


if __name__ == '__main__':
    main(sys.argv)
