#!/usr/bin/perl
#############################################################################
#
# webfeed
# $Id: webfeed,v 1.5 2000-06-14 11:48:47-06 vdanen Exp $
#
# requires qmail or sendmail (sendmail requires Mail::Sendmail module)
#
# send email to a specified remote email address (web feedback)
#
#############################################################################

use CGI;

$VERSION = "1.5";
$debug   = 0;

# log writting sub-routine
sub log {
  $text = shift;
  open(TIME, "date '+%a %b %d %T %Y'|");
  $time = <TIME>;
  chop($time);
  print LOGFILE "[$time] $text\n";
  close(TIME);
}  


$query = new CGI($ENV{'QUERY_STRING'});

$c        = $query->param('conf');
$conf     = "/etc/webfeed/" . $c;

# read from configuration file
unless (open(CONF, $conf)) {
  die ("Can't open configuration file $conf.\n");
}

while ($line = <CONF>) {
  chop($line);
  if ($line ne "") {
    if ($line =~ /^\#/) {
      #this is a comment so ignore it
    } else {
      @inline = split(/=/,$line);
      if ($inline[0] eq "MAILER") {
        $MAILER = $inline[1];			# set $MAILER (MTA)
      } elsif ($inline[0] eq "SENDTO") {
        $sendto = $inline[1];			# set $sendto (recipient email)
      } elsif ($inline[0] eq "LOCATION") {
        $location = $inline[1];			# set $location (good url)
      } elsif ($inline[0] eq "LOCERROR") {
        $locerror = $inline[1];			# set $locerror (bad url)
      } elsif ($inline[0] eq "SUBJECT") {
        $subject = $inline[1];			# set $subject (for email)
      } elsif ($inline[0] eq "KEEPEMAIL") {
        $keepemail = $inline[1];		# set $keepemail
      } elsif ($inline[0] eq "KEEPFILE") {
        $keepfile = $inline[1];			# set $keepfile
      } elsif ($inline[0] eq "REQUIRE") {
        $require = $inline[1];			# set $require
      } elsif ($inline[0] eq "DEBUG") {
        $debug = $inline[1];			# set $debug
        if ($debug == 1) {
          $debug = 1;  # 1 enables debug logging
        } else {
          $debug = 0;  # anything else disables it
        }
      } elsif ($inline[0] eq "LOG") {
        $logfile = $inline[1];			# set $logfile
        unless (open(LOGFILE, ">>$logfile")) {
          die ("Can't open logfile $logfile.\n");
        }
        if ($debug == 1 && $logfile) {
          &log("webfeed $VERSION started: $conf");
        }
      }
    }
  }
}
close(CONF);

# test that we have all defined keywords
if ($MAILER && $sendto && $location && $locerror && $subject && $require) {
  # we're ready to rock!
} else {
  if ($logfile) {
    &log("Configuration error: Missing one or more keywords.  Aborting!");
    if ($debug == 1 && $logfile) {
      &log("[debug] Dumping keywords (non-required start with !):");
      &log("[debug] MAILER:     $MAILER");
      &log("[debug] SENDTO:     $sendto");
      &log("[debug] LOCATION:   $location");
      &log("[debug] LOCERROR:   $locerror");
      &log("[debug] SUBJECT:    $subject");
      &log("[debug] !KEEPEMAIL: $keepemail");
      &log("[debug] !KEEPFILE:  $keepfile");
      &log("[debug] REQUIRE:    $require");
      &log("[debug] !DEBUG:     $debug");
      &log("[debug] !LOG:       $logfile");
      &log("webfeed $VERSION finished");
    }
  }
  print "Configuration error: Missing one or more keywords.  Aborting!\n";
  exit(1);
}

if ($MAILER eq "sendmail") {
  use Mail::Sendmail;
}

if ($query->param('feedback')) {
  $feedback = $query->param('feedback');
}

# $email is a required field, must be either an email address or "tmpfile"
if ($query->param('email')) {
  $email = $query->param('email');
  if ($keepemail == 1) {
    if ($email ne "tmpfile") {
      if ($debug == 1 && $logfile) {
        &log("[debug] Request writing email to tmpfile");
      }
      # write email address to temporary file
      unless (open(TMPFILE, ">$keepfile")) {
        if ($logfile) {
          &log("Cannot open $keepfile for writing!");
          &log("webfeed $VERSION finished");
        }
        die ("Cannot open $keepfile for writing!\n");
      }
      print TMPFILE $email;
      close(TMPFILE);
    } elsif ($email eq "tmpfile") {
      if ($debug == 1 && $logfile) {
        &log("[debug] Request reading email from tmpfile");
      }
      # read email address from temporary file if it exists
      unless (open(TMPFILE, $keepfile)) {
        if ($logfile) {
          &log("Cannot open $keepfile for reading!");
          &log("webfeed $VERSION finished");
        }
        die ("Cannot open $keepfile for reading!\n");
      }
      $email = <TMPFILE>;
      close(TMPFILE);
    }
  }
}

# get names of all variables in form
@varname = $query->param;

# get required variables
#if ($require =~ /,/) {
  @require = split(/,/,$require);
#}

# test if required variables are filled
$test   = 0;
$test2  = 0;
$count  = 0; 
$count2 = 0;

while ($count <= (@require -1)) {
  while ($count2 <= (@varname -1)) {
    if ($require[$count] eq $varname[$count2]) {
      if ($debug == 1 && $logfile) {
        &log("[debug] $require[$count] equals $varname[$count2]");
      }
      if ($query->param($varname[$count2]) eq "") {
        if ($debug == 1 && $logfile) {
          &log("[debug] $varname[$count2] is empty: fail");
        }
        $test = 1;
      }
      $test2++;
    }
    $count2++;
  }
  $count2 = 0;
  $count++;
}

if ($debug == 1 && $logfile) {
  $req = @require;
  &log("[debug] Required fields: $req");
  &log("[debug] Required fields filled out: $test2");
}

if (@require != $test2) {
  if ($debug == 1 && $logfile) {
    &log("[debug] Requirements not met: fail");
  }
  $test = 1;
}

# test to see if required fields are filled out, if not redirect to
# $locerror location

if ($test == 0) {
  if ($debug == 1 && $logfile) {
    &log("[debug] Success: Redirect to $location");
  }
  print "Location: http://$location\n\n";
} else {
  if ($debug == 1 && $logfile) {
    &log("[debug] Fail: Redirect to $locerror");
  }
  print "Location: http://$locerror\n\n";
  if ($debug == 1 && $logfile) {
    &log("webfeed $VERSION finished");
  }
  exit(0);
}

# get username from email address
@email = split(/\@/,$email);

if ($MAILER eq "qmail") {
  local *M;

  open M, "|MAILUSER=$email[0] MAILHOST=$email /var/qmail/bin/qmail-inject -f$email $sendto " or die "Can't fork for qmail: $!\n";select M;

  print "From: $email\nTo: $sendto\n";
  print "Subject: $subject\n\n";
  $count = 0;
  while ($count <= (@varname -1)) {
    $value = $query->param($varname[$count]);
    if ($value ne "Submit" && $varname[$count] ne "email" && 
          $varname[$count] ne "conf" && $varname[$count] ne "feedback" && 
          $value ne "") {
      print "$varname[$count] : $value\n";
    }
    $count++;
  }
  if ($feedback) {
    print "\nWeb Feedback text:\n----------------------------------------\n\n";
    print "$feedback\n";
  }

  close M;
} elsif ($MAILER eq "sendmail") {
  $Mail::Sendmail::mailcfg{mime} = 0;
  %mail = (
           smtp       => 'localhost',
           To         => $sendto,
           From       => $email
          );
  $mail{"Subject: "} = "$subject";
  $count = 0;
  while ($count <= (@varname -1)) {
    $value = $query->param($varname[$count]);
    if ($value ne "Submit" && $varname[$count] ne "email" && 
          $varname[$count] ne "conf" && $varname[$count] ne "feedback" &&
          $value ne "") {
      $mail{"Message: "} .= "$varname[$count] : $value\n";
    }
    $count++;
  }
  if ($feedback) {
    $mail{"Message: "} .= "\nWeb Feedback text:\n----------------------------------------\n\n";
    $mail{"Message: "} .= "$feedback\n";
  }
  sendmail(%mail) or die $Mail::Sendmail::error;
} else {
  print "Configuration error.  Invalid value for keyword MAILER.\n\n";
  if ($logfile) { 
    &log("Configuration error.  Invalid value for keyword MAILER.");
    if ($debug == 1 && $logfile) {
      &log("webfeed $VERSION finished");
    }
  }
  exit(1);
}

if ($debug == 1 && $logfile) {
  &log("webfeed $VERSION finished");
}

close(LOGFILE);
exit(0);
