#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include "safestring.h"
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>

#include <arpa/nameser.h>
#include <resolv.h>

//#include "general_types.h"
#include "clog.h"
//#include "mx.h"
#include "smtp.h"
#include "timer.h"
//#include "socket.h"

int SMTP2( const char * _server, const char * _from, char * _to, char * _body, const char * _domain, const char * _user, const char * _pass, const char * _name)
{
	// SAF 1/24/03: first copy all data to local structures to avoid race conditions, which is currently causing crashes !
	// Actually, it wasn't the race condition, it was the fact that '_to' was NULL due to a bug in profile.cpp: "email" was not set

	logfmt( FLOG_MAX, "ESMTP( %s, %s, %s )", _server, _from, _to );

	if ( !_server || !_from || !_to || !_body )
	{
		logfmt( FLOG_MAX, "Input parameters server, from, to & body are requied" );
		return 0;
	}
	if ( !_to[0])
	{
		logfmt( FLOG_MAX, "Empty recipient list" );
		return 0;
	}

	int BUF_SIZE = 512+strlen(_server)+strlen(_from)+strlen(_to)+strlen(_domain)+strlen(_user)+strlen(_pass)+strlen(_name)+2*strlen(_body);

	char *subject = NULL, *body = NULL;
	char *name = new char[BUF_SIZE+1];

	// Prepare body & subject
  logfmt( FLOG_MAX, "Prepare body" );
	int lines = 0;
	int len = strlen( _body );
	for ( int i=0; i<len; i++ )
	{
	    int c = _body[i];
	    // Replace ' with "
	    if (c == 39)
	    {
		_body[i] = '"';
	    }
	    // count LFs
	    if (c == 10)
	    {
		lines++;
	    }
	    // remove CRs (replace with whitespaces)
	    if (c == 13)
	    {
		_body[i] = 32;
	    }
	}
	// Locate & extract subject
  logfmt( FLOG_MAX, "Locate & extract subject" );
	char *subj = strstr ( _body, "Subject: " );
	if (subj != NULL)
	{
		int k = 0;
	  int n = strlen(subj);
	  for (int i=0; i<n; i++)
	  {
			int c = subj[i];
			if (subject != NULL)
			{
				name[k++] = c;
			}
			if (subject == NULL && c == 10)
			{
		    if (i > 9)
		    {
					subject = new char[i+1];
					memset( subject, 0, i );
					int j = 9;
					for (; j<i; j++)
					{
						//logfmt( FLOG_MAX, "c=%ld", subj[j] );
			    	subject[j-9] = subj[j];
						//subj[j] = 32;
					}
					subject[j] = '\0';
					logfmt( FLOG_MAX, "Found subject: %s", subject);
		    }
			}
	  }
		if (subject != NULL)
		{
			name[k++] = '\0';
			body = new char[k];
			//logfmt( FLOG_MAX, "New body(%ld): %s (%ld)", k, name, strlen(_body) );
			snprintf( body, k, "%s", name);
		}
	}
	if (subject == NULL)
	{
		subject = new char[15];
		strcpy(subject,"Wrenchboard Support");
		logfmt( FLOG_MAX, "Subject is not found - defaulting to: %s", subject );
	}

	// Google
/*
	snprintf( name, BUF_SIZE, EMAIL_PREFIX"/../src/modules/mailsend -smtp %s -f %s -t %s -d %s -name '%s' +cc +bcc -v -starttls -auth-plain -user %s -pass %s -sub '%s' -M '%s'",
	    _server, _from, _to, _domain, _name, _user, _pass, subject, body != NULL ? body : _body );
*//*
	// Virtualmail
	snprintf( name, BUF_SIZE, EMAIL_PREFIX"/../src/modules/mailsend -smtp %s -f %s -t %s -d %s -name '%s' +cc +bcc -v -port 25 -auth-plain -user %s -pass %s -sub '%s' -M '%s'",
	    _server, _from, _to, _domain, _name, _user, _pass, subject, body != NULL ? body : _body );
*/
	// GoDaddy
	snprintf( name, BUF_SIZE, EMAIL_PREFIX"/../src/modules/mailsend -smtp %s -f %s -t %s -d %s -name '%s' +cc +bcc -v -port 80 -auth-plain -user %s -pass %s -sub '%s' -M '%s'",
	    _server, _from, _to, _domain, _name, _user, _pass, subject, body != NULL ? body : _body );

	logfmt( FLOG_MAX, "Calling MAILSEND: %s", name);

	FILE * p = popen( name, "r" );

	int c = 0;
	do
	{
	  c = fread( name, 1, BUF_SIZE, p );
	  if (c > 0)
	  {
			logfmt( FLOG_MAX, "%s", name );
	  }
	  else
	  {
			logfmt( FLOG_MAX, "\n\nEND OF PIPE OUTPUT\n\n" );
	  }
	}
	while (c > 0);

  pclose( p );

  if (name != NULL)
	{
		delete name;
	}
	if (subject != NULL)
	{
		delete subject;
	}
	if (body != NULL)
	{
		delete body;
	}
	logfmt( FLOG_MAX, "/ESMTP()" );

	return 1;

}

