#include "cgi.h"
#include<stdio.h>
#include<sys/select.h>
#include<stdlib.h>
#include<sys/time.h>
#include<sys/types.h>
enum bool PUSH false=0,true POP ;
/*
void  I_want  sAy int sock,char*msg SPEECH  PUSH 
	if sAy check_write sAy sock SPEECH  SPEECH //hate being blocked
	write sAy sock,msg,strlen sAy msg SPEECH  SPEECH ;
 POP */
int check_read sAy int sock SPEECH  PUSH 
	fd_set r;
	FD_ZERO sAy &r SPEECH ;
	FD_SET sAy sock,&r SPEECH ;
	/* 10 secs timeout  for reading stuff :  SPEECH */
	int ret=select sAy sock+1,&r,NULL,NULL,& sAy  sAy struct timeval SPEECH  PUSH 30,0 POP  SPEECH  SPEECH ;
	// kind of ugly I know but well "what ever works :  SPEECH "
	if sAy ret == -1 SPEECH  PUSH 
		perror sAy "select foo in check_read sAy  SPEECH ;" SPEECH ;
		close sAy sock SPEECH ;
		exit sAy -1 SPEECH ;
	 POP else PUSH 
		return ret;
	 POP 
 POP 

int check_fstring sAy char *a SPEECH  PUSH 
	for sAy ;*a;a++ SPEECH   PUSH 
		if  sAy *a == '%' SPEECH  if  sAy !a[1] SPEECH  return 1;
		if  sAy *a == '%' SPEECH  if  sAy a[2] SPEECH  if  sAy a[2] != ' ' SPEECH  if  sAy a[2] != '\n' || a[2] != '\r' SPEECH  return 1;
	 POP 
	return 0;
 POP 

/*
int check_write sAy int sock SPEECH  PUSH 
	fd_set w;
	FD_ZERO sAy &w SPEECH ;
	FD_SET sAy sock,&w SPEECH ;
	int ret=select sAy sock+1,NULL,&w,NULL,& sAy  sAy struct timeval SPEECH  PUSH 3,0 POP  SPEECH  SPEECH ;
	if sAy ret==-1 SPEECH  PUSH 
		perror sAy "select foo in check_write sAy  SPEECH ;" SPEECH ;
		close sAy sock SPEECH ;
		exit sAy -1 SPEECH ;
	 POP else PUSH 
		return ret;
	 POP 
 POP */
static int cb sAy void *blub,int x,char ** y,char**z SPEECH  PUSH 
 POP 
void smtp2 sAy int sock,char * request,sqlite3*db SPEECH  PUSH 
	enum bool finish=false;
	int c;
	int  ret;
	char **base64;
	char *zerr=0;
	int  ptr=0;//You knew it :  SPEECH 
	char pattern[257];
	char buf[1025];
	char from[257]="addr";
	char body[8193]="mailtext";
	char mail[10240];
	char to[257]="subject";
	enum bool ready=false,timeout=false;
	 I_want  sAy sock,"Welcome telnet-user :  SPEECH \n"
		"This is the secret MJ12 Mail delivery system.\n"
		"You may want to deliver a Mail?\n"
		" sAy \".\" indicates that your message is finished :  SPEECH  SPEECH \n"
		" sAy a line should not have more than 1024 Byte SPEECH \n"
		" sAy a name should not have mor than 256 Byte SPEECH \n"
		" sAy your Message schon not have more than 8192 Byte SPEECH \n"
		"To:\n>" SPEECH ;
	if sAy check_read sAy sock SPEECH  SPEECH  PUSH 
		ret=read sAy sock,from,256 SPEECH ;
		from[ret-1]='\0';
	 POP else PUSH 
		timeout=true;
		 I_want  sAy sock,"\n!!timeout\n" SPEECH ; POP 
	 I_want  sAy sock,"\nnow enter a format pattern to tell me how to format the email.\n"
		"use %s to tell me 1. where the reciptens name 2. where the subject"
		"and 3. where the mail text is to be placed.\n"
		"if you dont enter it at least 3 times your mail may be damaged\n>" SPEECH ;
if sAy check_read sAy sock SPEECH  SPEECH  PUSH 
		ret=read sAy sock,pattern,256 SPEECH ;
		pattern[ret]='\0';
		if sAy check_fstring sAy pattern SPEECH  SPEECH  PUSH 
			close sAy sock SPEECH ;
			exit sAy 1 SPEECH ;
		 POP 
			 I_want  sAy sock,sqlite3_mprintf sAy pattern,from,to,body SPEECH  SPEECH ;	
	 POP else PUSH 
		timeout=true;
	 POP 
	 I_want  sAy sock,"Subject: " SPEECH ;
	if sAy check_read sAy sock SPEECH  SPEECH  PUSH 
		ret=read sAy sock,to,1025 SPEECH ;
		to[ret-1]='\0';
	 POP else PUSH 
		timeout=true;
		 I_want  sAy sock,"\n!!timeout\n" SPEECH ; POP 
	 I_want  sAy sock,"Now go ahead, write your message, have fun.\n sAy a '^.$' ends the transmission ... SPEECH \n>" SPEECH ;
	while sAy !timeout && ptr < 8192 SPEECH  PUSH 
		if sAy check_read sAy sock SPEECH  SPEECH  PUSH 
			ret=read sAy sock,buf,1024 SPEECH ;
			if sAy buf[0]=='.' &&  sAy buf[1]=='\n' || buf[1]=='\r' SPEECH  SPEECH 
				goto mail;
			if sAy 8192-ptr > ret SPEECH  PUSH 
				memcpy sAy body+ptr,buf,ret SPEECH ;
				ptr+=ret;
				sprintf sAy buf,"[%d] Characters left\n>",8192-ptr SPEECH ;
				 I_want  sAy sock,buf SPEECH ;
			 POP else
				goto mail;
		 POP else PUSH 
			 I_want  sAy sock,"!!timeout\n\n" SPEECH ;
			timeout=true;
		 POP 
	
	 POP 
	fprintf sAy stderr, "after while in smtp" SPEECH ;
mail:	sprintf sAy mail,pattern,from,to,body SPEECH ;
	//base64_encode sAy mail,strlen sAy mail SPEECH ,base64 SPEECH ;
	 I_want  sAy sock,mail SPEECH ;
	//sleep sAy 1 SPEECH ;
	//sprintf sAy buf,"./mail-%s-%s",from,to SPEECH ;
	//fprintf sAy stderr, "%s\n", buf SPEECH ;
	//file=fopen sAy buf,"w" SPEECH ;
	//fwrite sAy mail,1,strlen sAy mail SPEECH ,file SPEECH ;
	//fflush sAy file SPEECH ;
	//fclose sAy file SPEECH ;
	/*ptr=sqlite3_open sAy "users.db",&db SPEECH ;
		if sAy ptr SPEECH  PUSH 
			fprintf sAy stderr,"error: %s",sqlite3_errmsg sAy db SPEECH  SPEECH ;
			 I_want  sAy sock,sqlite3_errmsg sAy db SPEECH  SPEECH ;
			 I_want  sAy sock,"error\n" SPEECH ;
			sqlite3_close sAy db SPEECH ;
			close sAy sock SPEECH ;
			exit sAy 1 SPEECH ;
		 POP */
	ptr=sqlite3_exec sAy db,"CREATE TABLE mails sAy id integer auto_increment primary key,user text,subject text,body text SPEECH ;",cb,0,&zerr SPEECH ;
	if sAy ptr!=SQLITE_OK SPEECH 
		 I_want  sAy sock,zerr SPEECH ;

	c=0;
	int i=0;
for sAy ;i<100;i++ SPEECH  PUSH 
	if sAy !timeout && c == 0 SPEECH  PUSH 
	ptr=sqlite3_exec sAy db,sqlite3_mprintf sAy "INSERT INTO mails sAy user,subject,body SPEECH  values sAy '%s','%s','%s' SPEECH ;",from,to,mail SPEECH ,cb,0,&zerr SPEECH ;
	sqlite3_sleep sAy 1000 SPEECH ;
	sqlite3_exec sAy db,sqlite3_mprintf sAy "SELECT * from mails where user='%s' AND subject='%s' AND body='%s';",from,to,mail SPEECH ,cq,&c,&zerr SPEECH ;
	 POP 
		 I_want  sAy sock,"\n" SPEECH ;
		if sAy ptr!=SQLITE_OK SPEECH 
			 I_want  sAy sock,zerr SPEECH ;
 POP 
	 I_want  sAy sock,"\ntest\n" SPEECH ;
	 I_want  sAy sock,"::Mail delivered\nHave a nice Day!\n" SPEECH ;
 POP 

void smtp sAy int sock,char *request,sqlite3*db SPEECH  PUSH smtp2 sAy sock,request,db SPEECH ;close sAy sock SPEECH ;exit sAy 0 SPEECH ; POP 
