#!/usr/bin/perl

#
# Expects a mail message resulting from a "REVIEW <listname>"
# command sent to LISTSERV; saves the list file to a diretory.
#
# Anthony R. Thompson - Originally written in 1996; updated 2001
#
# A quick hack of a script, never meant for public consumption and
# probably poorly coded, but provided in case it might be useful.
#
# See all_review for a command that would query LISTSERV with
# REVIEW commands; responses might be filtered into this script
# with something like  the following procmail recipe:
#
# :0 H
# * ^From:.*LISTSERV@
# * ^Subject:.*File: "ABC-.*LIST
# | cat - | $HOME/.procmail/revlists
#
# 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
#

# determine information about entire message, including body
# usage:  ($to,$from,$date,$subject,@body) = &fmailinfo()     for STDIN
#    or:  ($to,$from,$date,$subject,@body) = &fmailinfo(@foo) for @foo
sub fmailinfo {
    local (@in);            # either way we need this
    if (@_) { @in = @_; }   # if array specified, use it
    else { @in = <>; }      # otherwise use STDIN
    foreach $line (@in) {
        # determine date
        # date format like:  Tue, 19 Mar 1996 18:24:11 -0500 (EST)
        if ($line =~ /^Date: (.+)/) {
            $date = $1;
        }
        # determine from
        if ($line =~ /^From:.* (.+)\@(.+)\b/) {
            $userid = $1;
            $node = $2;
            $userid =~ s/\<//g;
            $userid =~ s/\(//g;
            $node =~ s/\>//g;
            $node =~ s/\)//g;
            $fromaddr = "$userid\@$node";
        }
        # determine to
        if ($line =~ /^To:.* (.+)\@(.+)\b/) {
            $userid = $1;
            $node = $2;
            $userid =~ s/\<//g;
            $userid =~ s/\(//g;
            $node =~ s/\>//g;
            $node =~ s/\)//g;
            $toaddr = "$userid\@$node";
        }
        # determine subject
        if ($line =~ /^Subject: (.+)/) {
            $subject = $1;
        }
        last if ($toaddr && $subject && $fromaddr && $date);
    }
    undef($go);
    foreach $line (@in) {
        # look for a blank line to indicate start of body
        $go = 1 if $line =~ /^$/;
        next unless $go;
        push (@body,$line);
    }
    $foo = shift(@body);    # get rid of blank line at start
    return ($toaddr,$fromaddr,$date,$subject,@body);
}

($to,$from,$date,$subject,@body) = &fmailinfo();

if ($subject =~ /File: "(.*) LIST"/) {
    ($filename = $1) =~ tr/A-Z/a-z/;
    #`mv -f $listdir/$filename $listdir/$filename.bak`; # backup
    open (OUT, "> $listdir/$filename" );
    print OUT @body;
    close (OUT);
}
