#!/usr/bin/python -u

###
###  This is a utility script we used to do a one-time synchronization
###  of our subscribers' website login passwords with their Mailman
###  mailing list passwords for the emails corresponding with their
###  website login accounts.
###
###  It will not be useful to you in its present form because it
###  refers to some custom Python objects relating to our website
###  login and membership database.  However it's being provided for
###  an example of code you might need to write for a list migration.
###
###  For each list:
###    For each subscriber:
###      If email belongs to a member:
###        If member has a website login:
###          Set passwd for email on list to website login passwd
###        Else (member doesn't have a website login):
###          Set passwd for email on list to some default passwd
###      Else (email doesn't belong to a member):
###        Set passwd for email on list to some default passwd
###
###  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
docroot = os.environ.get('DOCUMENT_ROOT')
sys.path.append(docroot + '/path/to/where/our/python/objects/live')
import MailmanLists, EMail, Members, MemberLogins

###  Set up objects for later use
mml = MailmanLists.MailmanLists()
email = EMail.EMail()
members = Members.Members()
memlogins = MemberLogins.MemberLogins()

###  Other useful/important settings
sitepass = 'YOURSITEPASSHERE' # Mailman site admin password; INSTALL
defaultpw = 'defaultpass'     # default pass for users w/o accouns; INSTALL
debugging = 0      # whether this is a trial run or real thing
emaildone = {}     # key = email, val = 1 (or not present)

def set_password(addr, passwd):
    if (debugging): print "password all %s %s %s" % (sitepass, addr, passwd)
    else: mml.add_or_change_user_password(addr, passwd)

for listname in mml.get_all_list_names():
    if (debugging): print "Processing " + listname
    for addr in mml.get_list_members(listname):
        if (emaildone.has_key(addr)): continue
        memid = members.get_id_by_email(addr)    # '' means not a member
        if (memid == ''):
            if (debugging): print addr + " = non-mem, would set pw to default"
            else: set_password(addr, defaultpw)
        else:
            mlid = memlogins.get_member_login_id_by_member_id(memid)
            if (mlid <> 0):    # if member has website login
                mlpw = memlogins.get_password(mlid)
                if (debugging):
                    print addr + " = mem w/ login, would set pw to " + mlpw
                else: set_password(addr, mlpw)
            else:              # member does NOT have website login
                if (debugging):
                    print addr + " = member w/o login, would set pw to default"
                else: set_password(addr, defaultpw)
        emaildone[addr] = 1    # remember we did this one for next time
