#!/usr/bin/python

###
###  Simple script to unsubscribe someone from one, multiple, or all
###  of our mailing lists; this is basically a rewrite in Python of a
###  script we had for years, which was written in Perl against
###  Listserv subscriber files on our old web host.
###
###  Usage: unsub user@host listname|all listname ...
###
###  As a bit of a hack to enable command-line laziness, if any list
###  name ends with .list, that will be chopped off; this is because
###  this command could be invoked from /members/office/db/lists/data
###  and list names could be tab-completed w/ those files in there.
###
###  Note that due to the up-to-one-hour lag between list subscriber
###  files getting copied from our list server to our web host, we're
###  not bothering to check whether someone is subscribed to any (or
###  the specified) lists first, since it's a moving target - the list
###  server may have more recent info than we have here anyway.
###
###  Don't need to worry about pausing between emails, as with the
###  sub script, because we'll only end up sending one email to the
###  list server anyway - it will either have one "unsubscribe all"
###  command, or it will be one message with multiple unsubscribe
###  commands, one for each list.
###
###  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
import MailmanLists

###  Various useful settings
debug = 0

###  Check usage
program = sys.argv.pop(0)
if (len(sys.argv) < 2):
    print "Usage: %s user@host listname|all listname ..." % (program)
    sys.exit(1)

###  VERY loose validation of email address; does it have an @?
address = sys.argv.pop(0)    # now sys.argv only has listnames    
if (not re.search('@', address)):
    print "%s appears to be an invalid email address; exiting" % (address)
    sys.exit(1)

###  If first listname is 'all' then our work here is done
mml = MailmanLists.MailmanLists()
if (sys.argv[0] == 'all'):
    if (debug):
        print "Would unsubscribe %s from all lists" % (address)
    else:
        mml.delete_member_from_all_lists(address)
        print "Unsubscribed %s from all lists" % (address)

###  First listname wasn't 'all', so sys.argv has lists to process
else:

    def sublist(mylist): return re.sub('\.list$', '', mylist)
    liststodo = map(sublist, sys.argv)

    ###  Make sure all list names are valid before doing anything
    for listname in liststodo:
        if (not mml.valid_list_name(listname)):
            print "%s is not a valid list name - no changes made" % (listname)
            sys.exit(1)

    ###  Now we can really do the unsubcscription (if not debugging)
    if (debug):
        for listname in liststodo:
            print "Would unsubscribe %s from %s" % (address, listname)
    else:
        mml.delete_member_from_lists(liststodo, address)
        for listname in liststodo:
            print "Unsubscribed %s from %s" % (address, listname)
