#!/usr/bin/python

###
###  A program which takes all addresses in a file and unsubscribes
###  them from 1) a specific list, or 2) all lists.  It is expected
###  that the input file will have one address per line.  If you want
###  a log of addresses unsubscribed, redirect STDOUT to a file.
###
###  Anthony R. Thompson - November 2003
###  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, string, re, time
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 = 0

###
###  Check usage, get args from command line
###
program = sys.argv.pop(0)
if (len(sys.argv) < 2):
    print "Usage: %s addrfile.txt listname|all" % (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 <> 'all') and (listname not in all_lists)):
    print listname + ' is not a valid list name.'
    sys.exit(1)

###
###  Do basic error checking here
###
to_unsub = []
for line in file.contents_as_list(addrfile):
    if (re.search('\@', line)):
	email = re.sub('\s+', '', line)
	to_unsub.append(email)

###
###  If debugging, handle here and exit
###
if (debug == 1):
    if (listname == 'all'): listdesc = 'all lists'
    else: listdesc = listname
    for email in to_unsub:
	print "Would be unsubbing " + email + " from " + listdesc
    sys.exit(0)

###  
###  Now do the unsubscription
###  
if (listname == 'all'):
    for email in to_unsub:
	mml.delete_member_from_all_lists(email)
	print email + ' removed from all lists'
	time.sleep(emailpause)
else:
    for email in to_unsub:
	mml.delete_member_from_list(listname, email)
	print email + ' removed from ' + listname
	time.sleep(emailpause)

print 'Done.'
