/*****************************************************************

  (c) Rob Hartill  1994


  This program reads comments POSTed by HTTP clients (browsers).
   and mails them to the maintainer.
  
*****************************************************************/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "config.h"

void decode_escape_sequences();
void plus_to_space();

void main()
  {
  char *comments;
  int  content_length;
  FILE *comment_file;

  printf("Content-type: text/html\n\n");               /* keeps mosaic happy */
  printf("<h2>Thank you for your comments</h2><br>");

  content_length = atoi(getenv("CONTENT_LENGTH"));
  comments = (char *) malloc(content_length+1);

  if (comments == NULL)
     {  
     printf("Unable to reserve enough memory to read the comments<br>\n");
     return;
     }

  /* Read the comments.
  */
  fgets(comments, content_length+1, stdin);

  /* Add a '&' field delimiter for good measure. */

  comment_file = fopen(COMMENT_TEMP_FILE,"w");
  if (comment_file == NULL)
     {
     printf("Error opening comment logfile. Please report this.<p>\n");
     }
  else
     {
     decode_escape_sequences(comments);
     plus_to_space(comments);
     fprintf(comment_file,"%s",comments);
     fclose(comment_file);

     system(MAIL_COMMAND);
     }      

  remove(comment_file);

  }


/**************************************************************
 decode_escape_sequences takes a string as input, and translates 
   HTTP escape sequences, format %hh, into plain ASCII
***************************************************************/
void decode_escape_sequences(request)
  char *request;
  {
  int indx,indx2;
  long hex_char;
  char hex_num[3];

  indx = indx2 = 0;
  while ( request[indx] != '\0' )
     {
     if ( request[indx] == '%')
        {
        if ((request[indx+1] >='0' && request[indx+1] <= '9') ||
           (request[indx+1] >='A' && request[indx+1] <= 'F'))
           { 
           if ((request[indx+2] >='0' && request[indx+2] <= '9') ||
              (request[indx+2] >='A' && request[indx+2] <= 'F'))
              {
              hex_num[0] = request[indx+1];
              hex_num[1] = request[indx+2];
              hex_num[2] = '\0';
              hex_char = strtol(hex_num,(char **)NULL,16);

              request[indx2++] = (char) hex_char;
                 
              indx += 3;
              }
             else request[indx2++] = request[indx++];
           }
          else request[indx2++] = request[indx++];
        }
        else request[indx2++] = request[indx++];
     }
  request[indx2] = '\0';
  }


/**************************************************************
 plus_to_space takes a string as input, and translates 
   '+' into ' '. Note theat '+' in HTTP requests is a field
 separator.
***************************************************************/
void plus_to_space(string)
char *string;
  {
  int indx;

  indx = 0;
  while( string[indx] != '\0' )
     {
     if (string[indx] == '+') string[indx] = ' ';
     indx++;
     }
  }

