#!/usr/local/bin/perl -w

###########################################################################
### $Id: //depot/personal/ryan/main/src/wham/bin/sendit#4 $
###########################################################################
### spamsend
### this script is used to send mail and make sure that the "to" and "cc"
### addrs end up in the WHITE list
###########################################################################
my( $BINDIR, $LIBDIR, $CGIDIR);

use FindBin '$RealBin';                             # locate libraries
use vars qw($BINDIR $LIBDIR $CGIDIR $CONFDIR);

BEGIN {
$BINDIR="$RealBin/../bin";
$LIBDIR="$RealBin/../lib";
$CGIDIR="$RealBin/../cgi-bin";
$CONFDIR="$RealBin/../etc";
}

use lib "$LIBDIR";
use strict;
use Getopt::Long;
use Mail::Field::AddrList;
use wham;

my( %argv, %config, $line, $cmd, $header, @addrs, $addr, $a, $filter, $match, @WHITE);

### Get the arguments
if( !(GetOptions( \%argv, "c|config=s", "s|sendmail=s", "l|log=s", "q|quiet", "v|verbose", "h|help",)) || defined($argv{h})) {
        print STDERR "Usage: spamsend [options]\n";
	print STDERR "Options\n";
	print STDERR "  -c, --config <file>        location of config file.  default .whamrc\n";
	print STDERR "  -s, --sendmail <sendmail>  path to sendmail.  defaults to 'sendmail -oem -t -oi'\n";
	print STDERR "  -l, --log <file>           log action to specified file\n";
	print STDERR "  -q, --quiet		   don't log\n";
	print STDERR "  -v, --verbose		   more messages\n";
        exit 0;
}

### Start the log!
my $log = defined( $argv{l}) ? $argv{l} : ".senditlog";
open( LOG, ">>$log") || die "ERROR: could not open '$log'\n" if( !defined( $argv{q}));

### Where is the config file
%config = &LoadConfig($argv{c});

### Load in the white lists
@WHITE = &ReadList( $config{white});

### Start sendmail
$cmd = defined( $argv{s}) ? $argv{s} : "sendmail -oem -t -oi";
unless( open( SENDMAIL, "|$cmd"))
{
	&Log("ERROR: could not execute '$cmd'\n");
	die "ERROR: could not execute '$cmd'\n";
}

### Ok now we read in each line of the mail and send it to 
### sendmail.  IF we're in the header, we need to look out 
### for mail recipients (To|CC|Bcc) and extract them
$header = 1;
while( defined( $line = <STDIN>))
{
	print SENDMAIL $line;
	next if( $header == 0);
#	print $line;
	if( $line =~ /^(To|CC|Bcc): (.*)/i) {
		push( @addrs, $2);
		&Log( "$1: $2\n");
	} elsif( $line =~ /^(Subject): (.*)/) {
		&Log( "$1: $2\n");
	} elsif( $line =~ /^\s*$/) {
		$header = 0;
#		print "EOH: $line\n";
	}
}

### Now that we handed all the data to sendmail let's make sure
### that it did infact send the mail happily (or at least queue
### it up
close( SENDMAIL);
if( $? != 0) {
	&Log( "ERROR: sendmail exited with : $?\n");
	die "ERROR: sendmail exited with : $?\n";
}

### Let's go ahead and save all the addresses
foreach $addr (@addrs)
{
	&Log( "checking $addr\n");

	### We need to extract the actual address out of the
	### address sting.  there might be multiple addrs,
	### and who knows how their formatted.  use this
	### lib to make the world happy
	my( $to) = Mail::Field->new('To', $addr);
	foreach $a ($to->addresses())
	{
		&Log( "addr is $a\n");

		$match = 0;
		foreach $filter (@WHITE)
		{
			if( $a =~/$filter/i)
			{
				&Log( "$a matches $filter\n");
				$match = 1;
				last;
			}
		}
		if( $match == 0) {
			&Log( "Need to add $a\n");
			AddToList( "white", sprintf( "added via sendit"), $a);
#			print "$a\n";
		}
	}
}
&Log( "-----------------\n");
close( LOG) if( !defined( $argv{q}));

exit 0;

###########################################################################
### &Log( $message);
###########################################################################
### Log a line to the log file unless we're not suposed to :>
###########################################################################
sub Log
{
	my( @data) = @_;

	if( defined( $argv{v})) {
		print &LogDate(), " ";
		print @data;
	}

	return if( defined( $argv{q}));
	print LOG &LogDate(), " ";
	print LOG @data;
}

