#!/usr/bin/python

###
###  Expects a mail message resulting from a "who listname listpass"
###  command which was sent to Mailman; saves the contents of the
###  message (which has all the subscribers to that list) to a
###  listname.list file in a directory.
###
###  This would be used in a setup where you're unable to run a cron
###  job on the list server to periodically get the subscribers to
###  each list as a file and then scp them automatically to another
###  server (i.e., the process in the review_and_copy script).
###
###  In that case, you'd use cron to periodically send an email
###  query to the Mailman admin command handler with the who
###  command for the list (one list per message, to make sure that
###  each response gets saved to its own file), and then you'd use
###  something like procmail to filter the responses into this
###  script for saving into a file.
###
###  An example of such a procmail recipe might be:
###
###  :0
###  * ^From:.*mailman-admin-command@
###  * ^Subject:.*Re: who
###  | cat - | $HOME/.procmail/revmmlists
###  
###  Anthony R. Thompson - Originally written in 1996; updated 2001,
###  converted to Python for Listserv -> Mailman migration in 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 email, re, sys

# This has to be hard-coded because procmail doesn't have our environment
savedir = '/path/to/place/to/save/outputfiles'       # INSTALL

from cStringIO import StringIO # needed later for faster string append
  # See http://www.skymind.com/~ocrow/python_string/ for more info
def list_to_string(list):
    file_str = StringIO()
    for item in list: file_str.write(str(item))
    return file_str.getvalue()

msg = email.message_from_string(list_to_string(sys.stdin.readlines()))
subject = msg.get('Subject')
body = msg.get_payload()

# Easy hack to get list name from subject - kill before, kill after :)
listname = re.sub('^Re: who ', '', subject)
listname = re.sub(' \(mailman_admin_cmd_handler\).*', '', listname)
listname = listname.lower()

# Overwrite existing file with new results of who command
listfile = open(savedir + '/' + listname + '.list', 'w')
listfile.write(body)
listfile.close()
