#! /usr/local/bin/perl

########################################################################
# qsf.pl Version 4.1 January 22, 1997
# Copyright (c) 1996-1997 Matthew J. Walker
# Written for the UCLA Virtual Office Hours Project
# Code can be freely distributed for educational use, as long as header
# remains intact.  See included Copyright and Licensing information.
# For info send email to voh@chem.ucla.edu
########################################################################
# Requires the existence of several files:
# voh.conf, qa.html's.
# You MUST edit the mail command to point to a valid mail program!!!!
########################################################################
require 'voh-lib.pl';

&SetupVOH;
&GetConf;
&CheckEmpty;

($in{type} eq "confidential") && (&Confidential) ;
($conf[1] eq "no") && (&Failure(22)) ;


$questions_file_name = "$questions_path/$time";

$html = <<"EOF";
<b>Date Submitted:</b>  $date<br>
<b>Student Name:</b>  $in{name} <br>
<b>Student Email:</b>  $in{email} <br>
<b>Submitted from:</b>  $posted_from <p>
<p>
$in{question}
</p>
<hr>
EOF
open(FILE, ">$questions_file_name") || &Failure(13) ;
print FILE ($html) ;
close FILE ;
&Success(Public) ;

# Confidential   Sends mail to professor
# Config file voh.conf for email address.  A "no" in the confidential field
# specifies the function as disabled.

sub Confidential {
   local($output);
   ($conf[2] eq "no") && (&Failure(23)) ;
#----------------------------------------------------------
# Edit below as necessary.  This will be the body of the 
# email sent to the instructor by the confidential post.
#----------------------------------------------------------
   $output = <<"EOO";
Lecture: $in{lecture}
Posted From:  $posted_from
Name:  $in{name}

Question: 

$in{question}

###########################################################
Note:  
Please verify the From: email address.  If there is nothing
or something obviously invalid in that field, replying to
this message will be fruitless.

VOH Project
###########################################################

EOO

#----------------------------------------------------------
# Mail routine:
# Edit as necessary!
#----------------------------------------------------------
   if($system_type eq "UNIX") {
   open(MAIL, "|/usr/bin/mailx -r $in{email} -s \"VOH Confidential Submission\" $conf[3]") || (&Failure(14));
   print MAIL $output;
   close(MAIL) ;
   } elsif ($system_type eq "Mac") {
   require 'MacSMTP.pl';
   unless (MacSMTP::smtp_connect($SMTP_server)) {
      &Failure("$MacSMTP::smtp_error");
   }
   MacSMTP::smtp_reset();
   MacSMTP::smtp_hello($VOH_server);
   MacSMTP::smtp_mail($in{email});
   MacSMTP::smtp_recipient($conf[3]);
   MacSMTP::smtp_data($in{email}, "Confidential Submission from VOH");
   MacSMTP::smtp_send_string($output);
   MacSMTP::smtp_data_done();
   MacSMTP::smtp_close();
   } elsif ($system_type eq "NT") {
      $smtp_message = &NTSMTP();
      $smtp_message =~ s/\d+\s+(.*)\@.*/$1/;
   } else {
   &Failure(14);
   }
   &Success(Confidential);
}

# Success
# Creates HTML text to send after query.
# Uses file specified by $success for majority of text, and inserts
# submission specific info with a substitution of XXXX.

sub Success {
   local ($type,$html) = @_ ;
   $html = <<"EOS";
$HTTP_header
<! follows W3 html 2.0 standard>
<html>
<head>
<title>$HTML_title // Success!</Title>
<body $HTML_bgcolor  $HTML_background  >
</head>
<center>
<h1>$type Submission Successful!</h1>
</center>
<ul>
<li>
<a href="$cgi_url/qtba.pl?lecture=$in{lecture}&department=$in{department}">View Unanswered Questions</a>
<li><a href="$link_path/qa.html">View Answered Questions</a>
<li><a href="$link_path/qsf.html">Submit Another Question</a>
</ul>
$HTML_footer
EOS
   if ($system_type eq "Mac") {
      MacPerl::Reply($html);
   } else {
      print($html);
   }
exit ;
}

sub NTSMTP {
# Inspired by Robin Chatterjee

use Socket;

my($port,$iaddr,$paddr,$proto,$a);
$port = 25;

# Setup stuff
# Error messages need rewritten....
$iaddr = inet_aton($SMTP_server) || (&Failure("SMTP Server Address Faulty"));
$paddr = sockaddr_in($port, $iaddr);
$proto = getprotobyname('tcp');
socket(S, PF_INET, SOCK_STREAM, $proto) || (&Failure("SMTP Socket:  $!"));
connect(S, $paddr) || (&Failure("SMTP Connect: $!"));

# Mail away
select(S); $| = 1; select(STDOUT);
$a=<S>;
print S "HELO $SMTP_server\n";
$a=<S>;  unless($a =~ /^250/) { &Failure("$a"); }
print S "MAIL FROM:<$in{email}>\n";
$a=<S>;
print S "RCPT TO:<$conf[3]>\n";
$a=<S>;
print S "DATA\n";
$a=<S>;
print S "$output";
print S "\n.\n";
$a=<S>;
print S "QUIT";
close S;
return $a;
}

