#!/usr/bin/perl
#
#NAME
#  Send - send email to recipients
#
#SYNOPSIS
#  Send [-options] [file]... [list mlist...] [to recipient...]
#
#DESCRIPTION
# This  program  sends  its stdin or a list of files.  If the keyword
# "to" is present, then a list of recipients will be used; otherwise,
# the  file(s) will be examined for "To:" lines.  Also, if the "list"
# keyword  is  present,  the  fields  after  it  are  looked  up   in
# $HOME/mail/list,  and  if  found,  they are taken as mailing lists.
# Each line is taken as a recipient, and each file is mailed to  each
# recipient.
#
# Here is one piece of information we need to know.   This must be a
# program that accepts a list of recipients on its command line, and
# reads the messa/local/bin/perl from stdin.  Likely values are 'smail', 'rmail',
# and 'sendmail'.
#
	$mailer = 'sendmail';
#
#AUTHOR
# Copyright (c) 1993, 1995, 1998 by John Chambers <jc@trillian.mit.edu>
#
# You are welcome to use this program as you wish,  as  long  as  you
# give  me credit, and take credit for your chan/local/bin/perls.  If you make any
# significant chan/local/bin/perls, you might send me a copy, and I'll incorporate
# them into this public version.

$| = 1;
($P = $0) =~ s".*/"";
$P = $ENV{"V_$P"} || 0;
$filefl = 1;	# Treat names as files initially.
$rcptfl = 0;	# Don't treat names as recipients.
$listfl = 0;	# Don't treat names as mailing lists.

for $a (@ARGV) {
	if ($a =~ s/^[-+]//) {
		if ($a =~ /[DdVv]/) {
			$opts[$#opts+1] = '-v';
			next;
		}
		if ($a =~ /[Tt]/) {
			$opts[$#opts+1] = '-t';
			next;
		}
		next;
	}
	if ($a eq 'to') {
		++$rcptfl;		# Treat names as recipients.
		$listfl = 0;	# Don't accept mailing lists.
		$filefl = 0;	# Don't accept file names.
		next;
	}
	if ($a eq 'list') {
		++$listfl;		# Treat names as mailing lists.
		$rcptfl = 0;	# Don't accept recipient names.
		$filefl = 0;	# Don't accept file names.
		next;
	}
	if ($a eq 'file') {
		++$filefl;		# Treat names as files.
		$listfl = 0;	# Don't accept mailing lists.
		$rcptfl = 0;	# Don't accept recipient names.
		next;
	}
	if ($listfl) {	# Have we seen a "list" keyword?
		$lists[$#lists+1] = $a;
		next;
	} elsif ($rcptfl) {	# Have we seen a "to" keyword?
		$rcpts[$#rcpts+1] = $a;
		next;
	} else {
		$files[$#files+1] = $a;
		next;
	}
}
if (!@opts) {
	$opts[$#opts+1] = '-v';	# Verbose by default.
}
if (!@rcpts) {
	$opts[$#opts+1] = '-t';	# Look for To: lines in files.
}
if (@lists) {
	for $l (@lists) {
		if ( -f "/usr/mail/list/$l" ) {
			print STDERR "No list \"$l\"\n";
			next;
		}
		if (open(L,"</usr/mail/list/$l")) {
			print STDERR "Can't read \"/usr/mail/list/$l\"\n";
			next;
		}
		for $r (<L>) {
			$r =~ s/\s+$//;				# Trim away white space.
			next if !$r;				# Ignore blank lines.
			next if ($r =~ /^\s*#/);	# Ignore comments.
			print "Rcpt: \"$r\"\n" if $P;
			for $f (@files) {
				printf("Mail $f to $r.\n");
				system "(echo To: '$r';echo X-List: $l;cat $f)|$mailer -t";
			}
		}
	}
} elsif (@files) {
	for $f (@files) {
		print "Reading mail from $f ...\n" if $P;
		system "$mailer @opts @rcpts <$f";
	}
} else {
	print "Reading mail from stdin ...\n" if $P;
	system "$mailer @opts @rcpts";	# No files; use stdin.
}
