#!/usr/bin/perl
# 
# Author:       James Brister <brister@vix.com> -- berkeley-unix --
# Start Date:   Fri, 25 Apr 1997 14:11:23 +0200
# Project:      INN
# File:         innmail.pl
# RCSId:        $Id: innmail,v 1.2 1997/07/23 14:13:28 brister Exp $
# Description:  A simple replacement for UCB Mail to avoid nasty security
#		problems. 
# 

$0 =~ s!.*/!! ;

require 5.001 ;
require 'getopts.pl' ;

# =()<require "@<_PATH_PERL_SHELLVARS>@" ;>()=
require "/var/news/etc/innshellvars.pl" ;

die "$0: No \$inn::sendmail  variable defined.\n" 
    if ! defined ($inn::sendmail);

$sm = $inn::sendmail ;

die "$0: Sendmail path is not absolute\n" unless ($sm =~ m!^/!) ;

$usage = "usage: $0 -s subject addresses\n\n" .
    "Reads stdin for message body\n" ;

&Getopts ("s:h") || die $usage ;

die $usage if $opt_h ;

if ( !$opt_s ) {
    warn "No subject given. Hope that's ok\n" ;
    $opt_s = "NO SUBJECT" ;
}

# fix up any addresses.
foreach ( @ARGV ) {
    s![^-a-zA-Z0-9+_.@%]!!g ;

    push (@addrs,$_) if ($_ ne "") ;
}

die "$0: No addresses specified\n\n$usage" unless @addrs ;

if ($sm =~ m!%s!) {
    $sm = sprintf $sm,join (' ',@addrs);
} else {
    $sm .= " " . join(' ', @addrs);
}

@smarr = split(/\s+/,$sm);

($t = $inn::sendmail) =~ s!\s.*!!;
die "$0: Sendmail variable definition is changed after subsitution\n" 
    if ($t ne $smarr[0]);

die "$0: Sendmail excutable doesn't appear to exist: $smarr[0]\n"
    if ! -x $smarr[0];

# startup sendmail without using the shell
$pid = open (SENDMAIL,"|-") ;
if ($pid == 0) {
    exec (@smarr) || die "$0: exec of $sm failed: $!\n" ;
} elsif ($pid < 0) {
    die "$0: Fork failed: $!\n" ;
}

print SENDMAIL "To:\t", join (",\n\t",@addrs), "\n" ;
print SENDMAIL "Subject: $opt_s\n" ;
print SENDMAIL "\n" ;
while (<STDIN>) {
    print SENDMAIL $_ ;
}
close (SENDMAIL) ;
exit ;
