/* Copyright Julian H. Stacey, Munich 2002 & 2003 */
#include <stdio.h>
/* http://httpd.apache.org/docs/2.2/howto/cgi.html */

/* #include <gcic.h>	*/ 
#ifdef BOOK
#include <stdlib.h>
#include <string.h>
#define MAX_ENTRIES 10000
typedef struct 
	{
	char *name ;
	char *val ;
	} entry ;
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);
#endif
#include <time.h>
#include <string.h>	/* strcpy() */
#include <unistd.h>	/* mktemp() */
#include <stdlib.h>	/* syste() */

extern char *ctime(const time_t *);
extern  time_t    time(time_t *) ;

FILE *mail ;

	int
jputchar(int c)
	{
	fputc(c,mail);
	return (putchar(c));
	}

	int
main(int argc, char **argv)
	{
	time_t    log_time;
	char julian[50] ;
	char mailname[50];
	char command[50];
#ifdef BOOK
	entry entries[MAX_ENTRIES];
	int num_entries, i ;
	int cl;

	/* Parse params from stdin, & build a table of entries */
	for (num_entries=0; !feof(stdin); num_entries++)
		{
		entries[num_entries].val = fmakeword(stdin, '&', &cl);
		plustospace(entries[num_entries].val);
		unescape_url(entries[num_entries].val);
		entries[num_entries].name =
			 makeword(entries[num_entries].val, "=");
		}
#else
	int ch, ch1, ch2;
#endif
	/* Emit HTML header */
	printf("Content-type: text/html\n\n");
	printf("<html>\n");
	printf("<head>\n");
	printf("<META NAME=\"generator\" CONTENT=\"http://www.berklix.com/~jhs/src/bsd/jhs/bin/public/ski_form/ski_form.c\">\n");
	printf("<LINK REL=\"shortcut icon\" HREF=\"http://www.berklix.net/gifs/berklix.ico\">\n");
	printf("<title>\n");
	printf("Output from Berklix.Org Ski Form\n");
	printf("</title>\n");
	printf("</head>\n");
	printf("<body>\n");

	sprintf(julian,"%s@%s","jhs","berklix.com");
		/* address is split in 2 to defeat spammers' web harvesters,
			as src/ is  on web */

 printf("An email booking has been sent to \"Julian H. Stacey\" &lt;%s&gt;.\n",julian);
 printf("The content is shown below for your information,\n");
 printf("& so if you want you can copy it with your mouse.\n");
 printf("<I><BR>Please check Julian manually emails you a confirmation copy on his\n");
 printf("receipt of your data, Allow a day or so for that.</I>\n");
 printf("<P>\n");
 printf("<B>Phone <a href=http://www.berklix.com/~jhs/phone/> Julian </A> \n");
 printf("to arrange a mutualy convenient time to deliver your cash deposit,</B>\n");
 printf("see <a href=http://www.berklix.com/~jhs/ski/event/mayrhofen/>Information Sheet</A> for details).");
 printf("<P>\n");
 printf("Push your browser's \"Back\" button, & you will return to the form,\n");
 printf("Push the same \"Back\" again, & you will leave the form.\n");
 printf("<P><HR><P>\n");

	printf("<ul><li>\n");
#ifdef BOOK
	for ( i=0; i < num_entries; i++ )
		printf("<li> %s = %s\n", entries[i].name,
			entries[i].val);
#else
	strcpy(mailname,"/tmp/ski_form.XXXXXX");
	mktemp(mailname);
	if ((mail = fopen(mailname, "w")) == (FILE *)NULL )
		printf("%s%s\n",
			"Cannot open file to mail, ",
			"you must copy with mouse & mail yourself.");
	while (( ch = getchar()) != EOF )
		{
		if (((char)ch) == '&') 
			{ printf("\n<LI>"); fprintf(mail,"\n"); }
		/* This messy conversion below of scrambled data
		    is probably un-necessary, now I've discovered 
		    P. 232 of O Reilly HTML Definitive Guide.
		    Instead I should try emitting
			"MIME-Version: 1.0"
			"Content-Type: text/plain;"
			"Content-Transfer-Encoding: \
				 application/x-www-form-urlencoded"
		*/
		else if (((char)ch) == '+') jputchar((int)' ');
		else if (((char)ch) == ',') jputchar((int)'Z');
		/* else if (((char)ch) == ' ') jputchar((int)'\n'); */
		else if (((char)ch) == '%')
			{	/* hexadecimal */
			if ((ch1 = getchar())== EOF) 
				{ printf("Err. with %%%c\n",
					(char)ch1) ; 
				fprintf(mail,"Err. with %%%c\n",
					(char)ch1) ; break ; }
			ch1 -= '0' ; ch1 *= 16 ;
			if ((ch2 = getchar())== EOF) 
				{ printf("Err. with %%%c%c\n",
					(char)ch1,(char)ch2) ;
				fprintf(mail,"Err. with %%%c%c\n",
					(char)ch1,(char)ch2) ; break ; }
			ch2 -= '0' ;
			ch1 += ch2 ;
			/*  Errors:
				Input	Wrong Output
				^	e
				+	2D (or 2)
				=	,
			A lot of these screw up !@#$%^&*()_-+={[}]:;"'|\<,>.?/
			*/
			jputchar(ch1);
			}
		else jputchar(ch);
		}
#endif
	printf("<HR></ul></body></html>\n");
	(void) time(&log_time) ;
	fprintf(mail,"\nCreated %s\n",ctime(&log_time));
	fclose(mail);
	sprintf(command,
		"/bin/sh /usr/local/www/cgi-bin/ski_form.sh %s",
		mailname);
	system(command);
	unlink(mailname);
	}
