#! @PYTHON@
#
# Copyright (C) 1998-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.

"""Clone a member address.

Cloning a member address means that a new member will be added who has all the
same options and passwords as the original member address.  Note that this
operation is fairly trusting of the user who runs it -- it does no
verification to the new address, it does not send out a welcome message, etc.

The existing member's subscription is usually not modified in any way.  If you
want to remove the old address, use the -r flag.  If you also want to change
any list admin addresses, use the -a flag.

Usage:
    clone_member [options] fromoldaddr tonewaddr

Where:

    --listname=listname
    -l listname
        Check and modify only the named mailing lists.  If -l is not given,
        then all mailing lists are scanned from the address.  Multiple -l
        options can be supplied.

    --remove
    -r
        Remove the old address from the mailing list after it's been cloned.

    --admin
    -a
        Scan the list admin addresses for the old address, and clone or change
        them too.

    --quiet
    -q
        Do the modifications quietly.

    --nomodify
    -n
        Print what would be done, but don't actually do it.  Inhibits the
        --quiet flag.

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

 fromoldaddr (`from old address') is the old address of the user.  tonewaddr
 (`to new address') is the new address of the user.

"""

import sys
import getopt

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



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 dolist(mlist, options):
    SPACE = ' '
    if not options.quiet:
        print C_('processing mailing list:'), mlist.internal_name()

    # scan the list owners.  TBD: mlist.owner keys should be lowercase?
    oldowners = mlist.owner[:]
    oldowners.sort()
    if options.admintoo:
        if not options.quiet:
            print C_('    scanning list owners:'), SPACE.join(oldowners)
        newowners = {}
        foundp = 0
        for owner in mlist.owner:
            if options.lfromaddr == owner.lower():
                foundp = 1
                if options.remove:
                    continue
            newowners[owner] = 1
        if foundp:
            newowners[options.toaddr] = 1 
        newowners = newowners.keys()
        newowners.sort()
        if options.modify:
            mlist.owner = newowners
        if not options.quiet:
            if newowners <> oldowners:
                print
                print C_('    new list owners:'), SPACE.join(newowners)
            else:
                print C_('(no change)')

    # see if the fromaddr is a digest member or regular member
    if options.lfromaddr in mlist.getDigestMemberKeys():
        digest = 1
    elif options.lfromaddr in mlist.getRegularMemberKeys():
        digest = 0
    else:
        if not options.quiet:
            print C_('    address not found:'), options.fromaddr
        return
    # Check for banned to address.
    pattern = mlist.GetBannedPattern(options.toaddr)
    if pattern:
        if not options.quiet:
            print '   ', C_('Banned address (matched %(pattern)s)')
        return

    # Now change the membership address
    try:
        if options.modify:
            mlist.changeMemberAddress(options.fromaddr, options.toaddr,
                                      not options.remove)
        if not options.quiet:
            print C_('    clone address added:'), options.toaddr
    except Errors.MMAlreadyAMember:
        if not options.quiet:
            print C_('    clone address is already a member:'), options.toaddr

    if options.remove:
        print C_('    original address removed:'), options.fromaddr



def main():
    # default options
    class Options:
        listnames = None
        remove = 0
        admintoo = 0
        quiet = 0
        modify = 1

    # scan sysargs
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'arl:qnh',
            ['admin', 'remove', 'listname=', 'quiet', 'nomodify', 'help'])
    except getopt.error, msg:
        usage(1, msg)

    options = Options()
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-q', '--quiet'):
            options.quiet = 1
        elif opt in ('-n', '--nomodify'):
            options.modify = 0
        elif opt in ('-a', '--admin'):
            options.admintoo = 1
        elif opt in ('-r', '--remove'):
            options.remove = 1
        elif opt in ('-l', '--listname'):
            if options.listnames is None:
                options.listnames = []
            options.listnames.append(arg.lower())

    # further options and argument processing
    if not options.modify:
        options.quiet = 0

    if len(args) <> 2:
        usage(1)
    fromaddr = args[0]
    toaddr = args[1]
        
    # validate and normalize the target address
    try:
        Utils.ValidateEmail(toaddr)
    except Errors.EmailAddressError:
        usage(1, C_('Not a valid email address: %(toaddr)s'))
    lfromaddr = fromaddr.lower()
    options.toaddr = toaddr
    options.fromaddr = fromaddr
    options.lfromaddr = lfromaddr

    if options.listnames is None:
        options.listnames = Utils.list_names()

    for listname in options.listnames:
        try:
            mlist = MailList.MailList(listname)
        except Errors.MMListError, e:
            print C_('Error opening list "%(listname)s", skipping.\n%(e)s')
            continue
        try:
            dolist(mlist, options)
        finally:
            mlist.Save()
            mlist.Unlock()


if __name__ == '__main__':
    main()
