#!/usr/local/bin/perl 
#
# Prepare a mail message from xrn for piping through a mailer, notably one
# that handles aliases, unlike the raw sendmail default.
# The script gets the text of the message as stdin, with the format
#	To: addresses 
# 	Subject: some text strings
#	<blank line>
# on the top. It's dumb about assuming that it's given this format. 
# These lines are stripped off to be passed to /usr/ucb/Mail or a derivative of
# your choice. 
# Currently works only for single-line To: and Subject:

# Could pass mailer in as argument.
die "Usage: no arguments; just the script's name, please\n" if $#ARGV != -1;

$to='';
$subject='';
$home=$ENV{'HOME'};

# Abort when we finally hit a nothing line.
# Assume no initial whitespace for now.
while (<STDIN>) {
  local ($head);
  $line = $_;
  chop;
  last if /^[ \t]*$/;			
  $line =~ /^([^:]+):\s*(.+)$/;
  $head = $1;

  $subject = $2	if $head eq 'Subject';
# backslash the quotes because of expansion later
  $subject =~ s#"#\\\"#g;
  $subject =~ s#!#\\\!#g;

   if ($head eq 'To')
	{
	# parse out (User Name). Be cheesy and use this pattern as a separator,
	# so that all stuff of form (a) (a b) (a b c) drops out and leaves
	# behind only addresses. This is easier to do than figure out valid
	# addresses or go through the line word-by-word.
	@addresses = split ( /\s*\([\w\s\.\,\-]+\)\s*|\s+/ , $2);
	foreach $whom (@addresses)
		{
		$to = join (' ',$to,$whom);
		}
	}
}

#print STDERR "\n", $to,"\n", $subject, "\n";
#exit 0;

# use mush, or /usr/ucb/Mail, /usr/ucb/mail or possibly just mail
if ($subject ne '')	
	{
	$MAILER = "|tee $home/dead.letter | mush -s \"$subject\" $to ";
	}
else	# line Subject: is missing or is there but blank; can't pass -s
	{
	$MAILER = "|tee $home/dead.letter | mush $to ";
	}

open(MAILER,$MAILER) || die "Can't pipe to $MAILER: $!";

# push the rest of the message out
print MAILER <STDIN>;

close (MAILER);
exit 0
