/* This program has been modified from its original form to mail "recipient"
   comments from the a comments page

     THESE VARIABLE ARE REQUIRED IN THE HTML FORM

     Variable Name     Variable Value
     "recipient"       recipient of mail
     "name"            name of person submitting comments
     "email"           email address of person submitting comments
     "subject"         subject of comments
     "comments"        comments

   Modified by Russell Berrett
   Last updated 2/17/96

   Modifications Copyright 1995 SurfUtah.Com.
   Permission granted to copy, modify, and otherwise use the modifications
   to this code in whatever manner you see fit, with no warranty expressed or
   implied.  Please retain this notice; this is the only restriction.  If you
   make any changes to the original code and/or modifications put forth by 
   SurfUtah.Com, plase credit yourself so that SurfUtah.Com is not asked to 
   maintain versions of this modified code that were not developed by 
   SurfUtah.Com.

   SURFUTAH.COM GIVES NO WARRANTY, EXPRESSED OR IMPLIED, FOR THE SOFTWARE 
   AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT LIMITATION, WARRANTY 
   OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE.
*/
   
/* 
        Email handler. This is based on the post_query.c code provided with 
        the NCSA httpd 1.1 package, released to the public domain
        (see their policy, which follows).

        Original portions Copyright 1994 Cold Spring Harbor Labs. 
        Permission granted to copy, modify and otherwise use
        this code in whatever manner you see fit, with no warranty
        expressed or implied. Plesae retain this notice; this is
        the only restriction. We also ask that you credit
        yourself for any changes so we are not asked to maintain
        versions of the code that are no longer recognizable to us. 

        Original portions by Thomas Boutell. 

        Portions developed at the National Center for Supercomputing
        Applications at the University of Illinois at Urbana-Champaign.

        THE UNIVERSITY OF ILLINOIS GIVES NO WARRANTY, EXPRESSED OR IMPLIED,
        FOR THE SOFTWARE AND/OR DOCUMENTATION PROVIDED, INCLUDING, WITHOUT
        LIMITATION, WARRANTY OF MERCHANTABILITY AND WARRANTY OF FITNESS FOR A
        PARTICULAR PURPOSE. */

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define MAX_ENTRIES 1000

extern char *getenv(char *e);

typedef struct {
    char *name;
    char *val;
} entry;

/* Removes trailing white space, etc */
void wsremove(char *s);

char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

/*-------------------------------------------------*/
void PrintHeader(char *title)
{
  printf("<html>\n<head>\n");
  printf("<title>%s</title>\n</head>\n", title);
  printf("<body>\n");
  printf("<h2>%s</h2>\n<hr>\n", title);

} /* PrintHeader */

/*-------------------------------------------------*/
void PrintFooter(void) 
{
  printf("<p>\n<hr>\n");
  printf("</body>\n</html>\n");

} /* PrintFooter */

/*-------------------------------------------------*/

main(int argc, char *argv[]) {
  entry entries[MAX_ENTRIES];
  register int x, m = 0;
  int index, ok = 0;
  int cl, len; 
  int recipientid = -1;
  int nameid = -1;
  int emailid = -1;
  int subjectid = -1;
  int commentsid = -1;
  FILE *out;
  char buf[81];

  printf("Content-type: text/html%c%c",10,10);
  if (strcmp(getenv("REQUEST_METHOD"),"POST")) {
    printf("This script should be referenced with a METHOD of POST.\n");
    printf("If you don't understand this, see this ");
    printf("<A HREF=\"http://www.ncsa.uiuc.edu/SDG/Software/Mosaic/Docs/fill-out-forms/overview.html\">forms overview</A>.%c",10);
    exit(1);
  }
  if (strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
    printf("This script can only be used to decode form results. \n");
    exit(1);
  }
  cl = atoi(getenv("CONTENT_LENGTH"));

  for (x=0; cl && (!feof(stdin)); x++) {
    m=x;
    entries[x].val = fmakeword(stdin,'&',&cl);
    plustospace(entries[x].val);
    unescape_url(entries[x].val);
    entries[x].name = makeword(entries[x].val,'=');

      /* set the values of each variable in the array */

    if ((!strcmp(entries[x].name, "name")) &&
             (strcmp(entries[x].val, ""))) {
      nameid = x;
    } 
    else if ((!strcmp(entries[x].name, "email")) &&
             (strcmp(entries[x].val, ""))) {
      emailid = x;
    } 
    else if ((!strcmp(entries[x].name, "subject")) &&
             (strcmp(entries[x].val, ""))) {
      subjectid = x;
    } 
    else if ((!strcmp(entries[x].name, "comments")) &&
             (strcmp(entries[x].val, ""))) {
      commentsid = x;
    } 
    else if ((!strcmp(entries[x].name, "recipient")) &&
             (strcmp(entries[x].val, ""))) {
      recipientid = x;
    } 
  }

    /* check to see if all the infomation was entered properly, 
       if not... reject the request */

  if ((nameid == -1) || (commentsid == -1) || (emailid == -1) ||
      (subjectid == -1)) {
    PrintHeader("Comments Rejected");
    printf("<p><font size=\"+1\">\n");
    printf("Your request to send comments to %s has been \n",
           entries[recipientid].val);
    printf("rejected due to insufficient information.\n<p>\n");
    printf("To properly send your comments,\n");
    printf("please fill out: \n");
    printf("<ul>\n");
    if (nameid == -1)
      printf("<li>Your name\n");
    if (emailid == -1)
      printf("<li>Your email address\n");
    if (subjectid == -1)
      printf("<li>The subject of your message\n");
    if (commentsid == -1)
      printf("<li>Your comments\n");
    printf("</ul></font>\n");
    PrintFooter();
    return(0);
  }

    /* one quirky thing with communicating with sendmail (and other
       mail execuatables) is that a single line composed of only a
       "." will close the pipe.... so we will prefix all lines with
       a space to insure that we get all of the information */

    /* now open up a pipe to communicate to the unix mail executable */

  sprintf(buf, "/bin/sendmail %s", entries[recipientid].val);
  out = popen(buf, "w");
  fprintf(out, "To: %s\n", entries[recipientid].val);
  fprintf(out, "Subject: %s\n", entries[subjectid].val);
  fprintf(out, "From: %s\n\n", entries[emailid].val);
  fprintf(out, "Reply-To: %s\n\n", entries[emailid].val);
  fprintf(out, "*------------------------------------------------------*\n");
  fprintf(out, "| ---------------------------------------------------- |\n");
  fprintf(out, "|     This message sent from the web comments page     |\n");
  fprintf(out, "| ---------------------------------------------------- |\n");
  fprintf(out, "*------------------------------------------------------*\n");
  fprintf(out, "\n");
  fprintf(out, "Name :  %s\n\n", entries[nameid].val);
  fprintf(out, "Comments :\n\n ");
  len = strlen(entries[commentsid].val);
  for (index = 0; index < len; index++) {
    if (entries[commentsid].val[index] == '\n')
      fprintf(out, "\n ");
    else
      fprintf(out, "%c", entries[commentsid].val[index]);
  }
  fprintf(out, "\n");
  for (index = 0; index < x; index++) {
    if ((index != recipientid) && (index != nameid) && (index != emailid) &&
        (index != subjectid) && (index != commentsid)) {
      fprintf(out, "%s: %s\n", entries[index].name, entries[index].val);
    }
  }
  pclose(out);

    /* echo back the input */

  PrintHeader("Request Accepted");
  printf("<p><font size=\"+1\">\n");
  printf("Your Name :  %s<br>\n", entries[nameid].val); 
  printf("Your Email Address : %s<br>\n", entries[emailid].val); 
  printf("Subject :\n  %s<br>\n", entries[subjectid].val); 
  printf("Comments :\n<ul>\n  <dd>%s<br>\n</ul>\n", entries[commentsid].val); 
  printf("<p>\n");
  for (index = 0; index < x; index++) {
    if ((index != recipientid) && (index != nameid) && (index != emailid) &&
        (index != subjectid) && (index != commentsid)) {
      printf("%s : %s<br>\n", entries[index].name, entries[index].val);
    }
  }
  printf("</font>\n");
  PrintFooter();

} /* main */

/* Removes trailing white space, etc */
void wsremove(char *s) {
  while(1) {
    int l = strlen(s);
    if (!l) {
      break;
    }
    if (isspace(s[l-1])) {
      s[l-1] = '\0';
    } 
    else {
      break;
    }
  }
} 
