#!/usr/bin/perl
#
# print header stuff
#
print "Content-type: text/html", "\n\n";
print "<html>";
print "<body background=\"nevada.gif\">";
#
#define stuff to send email with
#
require TripodMail;
$mail = new TripodMail;
#
#define stuff to grab data entered on form
#
require TripodCGI;
use CGI;
$cgi = new CGI;
#
#get users email address, subject of letter and letters text
#
#
# $formname name of disk file with model on how to send email
# this file must exist or we cant send the email
#
$formname=$cgi->param('formname');
$replyto=$cgi->param('replyto');
$frommail=$cgi->param('frommail');
$subject=$cgi->param('subject');
$text=$cgi->param('text');
$title=$cgi->param('title');
$file_prefix=$cgi->param('file_prefix');
$thefile=$cgi->param('thefile');
$debugflag=$cgi->param('debugflag');
if ($debugflag eq 'y' ) {
   print "debug_flag='$debugflag'<br>";
   print "formname='$formname'<br>";
   print "replyto='$replyto'<br>";
   print "frommail='$frommail'<br>";
   print "subject='$subject'<br>";
   print "text='$text'<br>";
   print "title='$title'<br>";
   print "the_file='$the_file'<br>";
   print "thefile='$thefile'<br>";
}
$errors=0;
if ( !  -e $formname ) {
   print "ERROR mail form file '$formname' does not exist<p>";
   print "ERROR to fix either<p>";
   print "<ol>";
   print "<li>Some idiot deleted the file $formname so someone must recreate the file";
   print "<li>The program has a bug and you must get the programmer to fix it.";
   print "</ol>";
   $errors++;
}
if ( !  -e $thefile ) {
   print "ERROR mail form file '$thefile' does not exist<p>";
   print "ERROR thats odd because the script that calls this program does a ls $filename<p>";
   print "ERROR your probably calling this program out of sequence<p>";
   $errors++;
}
#
#if the variable $text is null or blank
# its the 1st time the program has been run
# and we must display the form, or the 
# user has clicked on the submit button with
# out entering any text. againg we must display the
# form
if ($text =~ /^ *$/ ) {
   print "ERROR no text entered. \$text is blank<p>";
   $errors++;
}
if ($debugflag eq 'y' ) {
   print "validate user inputs<p>";
}
#if $text is not blank or null they entered
#some text they want to send as an email
#dont let them send the email if
#       $subject is blank
#       $from    is blank
#



$frommail=~s/ //g;
$replyto=~s/ //g;
#if ($frommail =~ /^snail\@aztec.asu.edu$/ ) {
if ($frommail =~ /snail\@aztec[0-9]*.asu.edu/i ) {
  $errors++;
  for ($i=0;$i<5000;$i++) {
    print "Dont't FUCK with the snail<p>";
  }
  print "</html>";
  exit;
}
if ($frommail =~ /^ *$/ ) {
  $errors++;
  print "You must enter a From: address<p>";
}
else {
  if ($frommail !~ /[^@]@[^@]/ ) {
    $errors++;
    print "From: address must contain an @ surronded by userid and server<p>";
    print "'$frommail' is invalid<p>";
  }
}
#
#if a Reply-To: address was entered valiate it
#
if ($replyto !~ /^ *$/i ) {
   if ($replyto !~ /[^@]@[^@]/ ) {
     $errors++;
     print "Reply-TO: address must contain an @ surronded by userid and server<p>";
     print "'$replyto' is invalid<p>";
   }
   if ($replyto =~ /snail\@aztec[0-9]*.asu.edu/i ) {
     $errors++;
     for ($i=0;$i<5000;$i++) {
       print "Dont't FUCK with the snail<p>";
     }
     print "</html>";
     exit;
   }
}


if ($subject =~ /^ *$/ ) {
  $errors++;
  print "You must enter a subject<p>";
}
if ($errors > 0 ) {
  print "you had errors!!!<p>";
  print "</html>";
  exit;
}
 open(BCC,$thefile) or print "ERROR cant open '$thefile'<p>";
 #
 # read in all the names to bcc
 #
 @bcc=<BCC>;
 close(BCC);
 #
 #for each name make a line that says
 #    BCC: name
 #
 if ( $replyto =~ /^ *$/ ) {
    $bcc="";
 }
 else {
    $bcc="Reply-To: $replyto\n";
 }
 $i=0;
 foreach $x (@bcc) {
   $i++;
   $bcc=$bcc."BCC: $x";
 }
 #
 # print the names in debug mode
 #
 if ($debugflag eq 'y' ) {
    print "bcc list=<p><pre>";
    print "$bcc";
    print "</pre>";
 }
 #
 #build the stinking hash which is used by
 #the tripod software to send the email
 #
 %hash = (
          'frommail', $frommail ,
          'bcc', $bcc ,
          'subject', $subject ,
          'text', $text 
         );
 #
 #use one stinking back slash for the \%hash
 #
 if ($debugflag eq 'y' ) {
    print "before sendmail<p>"; 
 }
 #
 #send the stinking email
 #
 $rc=$mail->sendMail($formname,\%hash);
 if ($rc == 1 ) {
    print "Mail has been sent to $i email addresses in the file $thefile<p>";
    print "The addresses are:<p>";
    foreach $x (@bcc) {
      print "$x<br>\n";
    }
 }
 else {
    print "<h3>Somethings Fucked Up</h3>";
    print "<h3>Try Again</h3>";
    print "<p>";
    print "Error sending Mail to addresss in file $thefile<p>";
    print "Error rc=$rc <p>";
 }
 print "</html>";
 exit;