#! /usr/sbin/perl
#
## Handles articles to moderate.
##     Copyright (C) 1995 Christophe Wolfhugel
##                        <wolf@pasteur.fr>
##
## Usage:   "| moderate" within your favorite mailer.

## Configure

$approved = "Christophe.Wolfhugel@grasp.insa-lyon.fr";

$queue = '/local/etc/tulp/queue';

$tmp = "/tmp/moder1.$$";

die "$program: can't find $queue\n"  unless -x $queue;

# in case inews dumps core or something crazy
$SIG{'PIPE'} = "plumber";
sub plumber { die "$program: \"$queue\" died prematurely!\n"; }

## Reads the message from standard input

$* = 1;
$/ = "";

## First skip the initial message header : we don't care
<STDIN>;

$hdr = <STDIN>;
$hdr =~ s/^>From\s+/From /i;
$hdr =~ s/^X-Listserv-To:\s+(\S+)\s*\n//;
$ml = $1;
if (!$ml) {
   print STDERR "No mailing-list name found. Rejecting message.\n";
   exit(1);
}
chop($hdr);
$hdr .= "Approved: $approved\n\n";
@msg = (<STDIN>);
close(STDIN);

open(MSG, "> $tmp") || die "$program: can't open $tmp\n";
print MSG $hdr;
print MSG @msg;
close(MSG);

do {
   system "vi $tmp < /dev/tty";
   do {
      print "Send, abort or edit? ";
      open(TTY, "< /dev/tty") || die "$program: can't open /dev/tty\n";
      $_ = <TTY>;
      close TTY;
   } until ($_ eq "s\n" || $_ eq "a\n" || $_ eq "e\n");
} while ($_  eq "e\n");

if ($_ eq "s\n") {
   system("$queue $ml < $tmp");
}
unlink("$tmp");
exit 0;
