#!/usr/bin/python

###
###  A program which takes all addresses in a file and subscribes them
###  to a specific list.  It is expected that the input file will have
###  one address per line and that the format will either be:
###
###  email@place.com Firstname Lastname      (or just Name)
###    or
###  Firstname Lastname <email@place.com>
###
###  (Note that the presence of a "<" on the line is what the program
###  uses to determine which of those two formats the line is in.)
###
###  For a log of addresses subscribed, redirect STDOUT to a file.
###
###  Anthony R. Thompson - June 2010
###  Contact: put @ between art and sigilservices.com
###
###  Copyright (C) 1998-2010 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. http://www.fsf.org/licensing/licenses/gpl.html
###

import os, sys, re, time, email
docroot = os.environ.get('DOCUMENT_ROOT')
sys.path.append(docroot + '/members/office/db/objs')
import MailmanLists, File

###  Throttle to avoid getting email errors
emailpause = 8

###  Whether or not we're just testing this
debug = 1

###
###  Check usage, get args from command line
###
program = sys.argv.pop(0)
if (len(sys.argv) < 2):
    print "Usage: %s addrfile.txt listname" % (program)
    sys.exit(1)
addrfile = sys.argv.pop(0)
listname = sys.argv.pop(0)
listname = re.sub('\.list$', '', listname)

###
###  Instantiate useful objects
###
mml = MailmanLists.MailmanLists()
file = File.File()
all_lists = mml.get_all_list_names()

###
###  Check for common errors
###
if not os.path.isfile(addrfile):
    print addrfile + ' is not a valid file path.'
    sys.exit(1)
if (listname not in all_lists):
    print listname + ' is not a valid list name.'
    sys.exit(1)

###
###  Do the work here
###
for line in file.contents_as_list(addrfile):
    line = re.sub('^\s*', '', line)
    line = re.sub('\s*$', '', line)
    if (re.search('\@', line)):
        if (re.search('<', line)):
            subname, subemail = email.Utils.parseaddr(line)
        else:
            subemail, subname = re.split('\s+', line, 1)
            if (not re.search('\@', subemail)):
                print "Invalid input file format"
                sys.exit(1)
        if (debug):
            print "Would be subbing %s (%s) to %s" % \
                (subemail, subname, listname)
        else:
            mml.add_member_to_list(listname, subemail, subname)

print 'Done.'
