#!/usr/bin/perl
# mailbot2.pl Re-Written by Steven R. Johnson (stevej@mistic.net) 01/02/97
# some portions of this code was taken from mailbot.pl
# mailbot.pl -- a simple mailbot program
# By Chris Candreva <chris@westnet.com>
# Stardate 0709.95
#
# This file is released into public domain.  You can modify it all you
# you want.  But please send me a copy of what you've added.  I dont claim
# to be a great perl programmer,  Actualy I believe im a beginner.
# So I wont be able to support you on this.
#
# Basicly the way this works is you put it in your /etc/aliases file
# Make an entry like this
# mailbot         :"|/bin/mailbot2.pl /responders/"
# of course makeing mailbot = the aliases you want and /responders/
# is the directory where your responce files are.
# Mailbot2 will look in /responders for a file defined by the subject
# field.  If it does not find that filename it defaults to menu.. So its
# a good idea to have /responders/menu there.  Anyways have fun..
# and drop me a note if you like this code.. And if you dont then dont!
#
#

unless ($ARGV[0]) {
	open (OUT,"|/bin/mail -s 'Error!' postmaster");
	print OUT "There is a misconfigured mailbot. Please fix.\n\n\n";
	close OUT;
	exit 1;
}

$destdir = $ARGV[0] ;

while(<STDIN>) {
	if (s/^From: //) {
		s/[\012\015]|^ *//g;  #Remove all CR and LF
		$to = $_; } elsif (s/^To: //) {
		s/[\012\015]|^ *//g;
		$from = $_; } elsif (s/^Subject: //) {
		s/[\012\015]|^ *//g;
                s/../pp/;
		$subject = $_;  }
	}


#### Open the mailfile.. and see if it is there...
#### if it is not then we are screwed.  we need to come up with an
#### error handeling procedure
####

if ($subject eq "") {
	$subject="menu"; }

unless (-e "$destdir/$subject") { $subject="menu" }	

                open (INFILE, "$destdir/$subject" );
	open (OUT, "|/usr/lib/sendmail -oi -t");
        print OUT "From: $from\n";
        print OUT "To: $to\n";
        print OUT "Subject: $subject\n\n";        

		$_="l";
                while ($_) { 
		$_= <INFILE>;
	print OUT $_; 
			}
                close <INFILE>;
                close <OUT>;
