/* Author: Ram Samudrala (me@ram.org)
 * Version: O1.0
 * Detail: <http://www.ram.org/computing/mailform/mailform.html>
 * February 18, 1998.
 *
 * See the URL above for more information.
 *
 * Caveats: don't have a item name in the form that starts with uppercase 'X'
 */

#include "cgi_common.h"
#include "cgi_display.h"
#include "cgi_error_handlers.h"
#include "cgi_defines.h"

int content_type_displayed = FALSE;
static int mailform(int number_of_entries, entry entries[], char from_address[], char to_address[], char subject[]);

#define RESTRICT_TO_LOCAL_DOMAIN "ram.org"

/******************************************************************/

int main() 
{
  char from_address[STRING_LENGTH], to_address[STRING_LENGTH], subject[STRING_LENGTH];
  entry entries[MAX_ENTRIES];
  int i, number_of_entries;
  
  check_method("POST", "main");
  check_content_type("main");
  display_content_type("mailform results");
  number_of_entries = read_entries(entries);

  from_address[0] = '\0';
  to_address[0] = '\0';
  subject[0] = '\0';
  
  for(i = 0; i <= number_of_entries; i++)
    {
      if (strcmp(entries[i].name, "From_address") == 0)
	strcpy(from_address, entries[i].val);
      if (strcmp(entries[i].name, "To_address") == 0)
	strcpy(to_address, entries[i].val);
      if (strcmp(entries[i].name, "Subject") == 0)
	strcpy(subject, entries[i].val);
    }
  
  if ((from_address[0] == '\0') || (strchr(from_address, '@') == NULL))
    display_error("main(): invalid From address!");

  if ((to_address[0] == '\0') || (strchr(to_address, '@') == NULL))
    display_error("main(): invalid To address!");

#ifdef RESTRICT_TO_LOCAL_DOMAIN
  if (strstr(to_address, RESTRICT_TO_LOCAL_DOMAIN) == NULL)
    display_error("main(): invalid domain in To address!");
#endif

  if (subject[0] == '\0')
    display_error("main(): invalid Subject!");

  mailform(number_of_entries, entries, from_address, to_address, subject);

  printf("<h1> Mail sent! </h1>\n");
  printf("<hr>\n");
  return TRUE;
}

/******************************************************************/

int mailform(int number_of_entries, entry entries[], char from_address[], char to_address[], char subject[])
{
  char tmp_filename[STRING_LENGTH], command[STRING_LENGTH];
  FILE *output_fp;
  int i;
  
  sprintf(tmp_filename, "%s.%d", TMP_FILENAME_STRING, (int) getpid());
  open_file(&output_fp, tmp_filename, "w", "mailform");
  
  fprintf(output_fp, "From: %s\n", from_address);
  fprintf(output_fp, "To: %s\n", to_address);
  fprintf(output_fp, "Subject: %s\n\n", subject);
  
  for(i = 0; i <= number_of_entries; i++)
    {
      if ((strcmp(entries[i].name, "From_address") != 0) &&
	  (strcmp(entries[i].name, "To_address") != 0) &&
	  (strcmp(entries[i].name, "Subject") != 0))
	fprintf(output_fp, "%s\n", entries[i].val);
    }
  close_file(&output_fp, tmp_filename, "mailform");
  sprintf(command, "%s %s < %s", SENDMAIL_LOCATION, to_address, tmp_filename);
  system(command);
 
  remove(tmp_filename);

  return TRUE;
}

/******************************************************************/
