#!/usr/bin/python

###
###  Simple script to subscribe someone to one or more lists; this is
###  basically a Python rewrite of a Perl script we had for years that
###  was written against Listserv subscriber files on our old host.
###
###  Usage: sub user@host 'subscriber name' listname 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 /path/to/list/data/files/
###  and list names could be tab-completed w/ those files in there.
###
###  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
emailpause = 5      # to throttle, for our web host's max emails/hour

###  Check usage
program = sys.argv.pop(0)
if (len(sys.argv) < 3):
    print "Usage: %s user@host 'their name' 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)

###  Check to make sure they gave us a name for the subscriber
subname = sys.argv.pop(0)
if (re.search('^abc-', subname)):     # all our lists begin with abc-
    print "Usage: %s user@host 'their name' listname ..." % (program)
    sys.exit(1)

###  Make sure all given list names are valid before doing anything
mml = MailmanLists.MailmanLists()
for listname in sys.argv:
    listname = re.sub('\.list$', '', listname)
    if (not mml.valid_list_name(listname)):
        print "%s is not a valid list name - no changes made" % (listname)
        sys.exit(1)

###  Now actually do the subscription
for listname in sys.argv:
    listname = re.sub('\.list$', '', listname)
    if (debug):
        print "Would add %s to %s as %s" % (address, listname, subname)
    else:
        mml.add_member_to_list(listname, address, subname)
        print "Subscribed %s to %s as %s" % (address, listname, subname)
        time.sleep(emailpause)  # because our web host throttles our msgs
